Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()#We always need to initalize our pygame IN EVERY PROJECT/FILE
  4.  
  5. win = pygame.display.set_mode((500, 480))# Here win is representing "window" for our screen which we have set at 500 by 480
  6.  
  7. pygame.display.set_caption("First Game")#We are giving our Window/Screen a name
  8.  
  9. walkRight = [pygame.image.load('image/gokuR0.png'), pygame.image.load('image/gokuR1.png'), pygame.image.load('image/gokuR2.png')]
  10. walkLeft = [pygame.image.load('image/gokuL0.png'), pygame.image.load('image/gokuL1.png'), pygame.image.load('image/gokuL2.png')]
  11. bg = pygame.image.load('image/bg.jpg')
  12. char = pygame.image.load('image/goku sprite - standing.png')
  13.  
  14. clock = pygame.time.Clock()
  15.  
  16. class player():
  17.     def __init__(self, x, y, width, height):
  18.         self.x = x
  19.         self.y = y
  20.         self.width = width
  21.         self.height = height
  22.         self.vel = 5
  23.         self.isJump = False
  24.         self.left = False
  25.         self.right = False
  26.         self.walkCount = 0
  27.         self.jumpCount = 10
  28.         self.standing = False
  29.  
  30.     def draw(self, win):
  31.         if self.walkCount + 1 >= 8:
  32.             self.walkCount = 0
  33.  
  34.         if not(self.standing):
  35.             if self.left:
  36.                 win.blit(walkLeft[self.walkCount // 100], (self.x, self.y))
  37.                 self.walkCount += 1
  38.             elif self.right:
  39.                 win.blit(walkRight[self.walkCount // 100], (self.x, self.y))
  40.                 self.walkCount += 1
  41.         else:
  42.             if self.right:
  43.                 win.blit(walkRight[0], (self.x, self.y))
  44.             else:
  45.                 win.blit(walkLeft[0], (self.x, self.y))
  46.  
  47.  
  48. class projectile():
  49.     def __init__(self, x, y, radius, color, facing):
  50.         self.x = x
  51.         self.y = y
  52.         self.radius = radius
  53.         self.color = color
  54.         self.facing = facing
  55.         self.vel = 8 * facing
  56.  
  57.     def draw(self, win):
  58.         pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
  59.  
  60. class enemy():
  61.     walkRight = [pygame.image.load("image/R1E.png"), pygame.image.load("image/R2E.png"), pygame.image.load("image/R3E.png"), pygame.image.load("image/R4E.png"), pygame.image.load("image/R5E.png"), pygame.image.load("image/R6E.png"), pygame.image.load("image/R7E.png"), pygame.image.load("image/R8E.png"), pygame.image.load("image/R9E.png"), pygame.image.load("image/R10E.png"), pygame.image.load("image/R11E.png")]
  62.     walkLeft = [pygame.image.load("image/L1E.png"), pygame.image.load("image/L2E.png"), pygame.image.load("image/L3E.png"), pygame.image.load("image/L4E.png"), pygame.image.load("image/L5E.png"), pygame.image.load("image/L6E.png"), pygame.image.load("image/L7E.png"), pygame.image.load("image/L8E.png"), pygame.image.load("image/L9E.png"), pygame.image.load("image/L10E.png"), pygame.image.load("image/L11E.png")]
  63.     def __init__(self, x, y, width, height, end):
  64.         self.x = x
  65.         self.y = y
  66.         self.width = width
  67.         self.height = height
  68.         self.end = end
  69.         self.path = [self.x, self.end]
  70.         self.walkcount = 0
  71.         self.vel = 3
  72.  
  73.     def draw(self,win):
  74.         self.move()
  75.         if self.walkcount + 1 >= 33:
  76.             self.walkcount = 0
  77.  
  78.         if self.vel > 0:
  79.             win.blit(self.walkRight[self.walkcount // 3], (self.x, self.y))
  80.             self.walkcount += 1
  81.         else:
  82.             win.blit(self.walkLeft[self.walkcount // 3], (self.x, self.y))
  83.             self.walkcount += 1
  84.  
  85.  
  86.  
  87.     def move(self):
  88.         if self.vel > 0:
  89.             if self.x + self.vel < self.path[1]:
  90.                 self.x += self.vel
  91.             else:
  92.                 self.vel = self.vel * -1
  93.                 self.walkcount = 0
  94.  
  95.         else:
  96.             if self.x - self.vel > self.path[0]:
  97.                 self.x += self.vel
  98.             else:
  99.                 self.vel = self.vel * -1
  100.                 self.walkcount = 0
  101.  
  102.  
  103.  
  104. def redrawGameWindow():
  105.     win.blit(bg, (0, 0))
  106.     goblin.draw(win)
  107.     man.draw(win)
  108.     for bullet in bullets:
  109.         bullet.draw(win)
  110.  
  111.     pygame.display.update()
  112.  
  113.  
  114. # mainloop
  115. man = player(200, 400, 85, 85)
  116. goblin = enemy(100, 400, 64, 64, 450)
  117. bullets = []
  118. run = True
  119. while run:
  120.     clock.tick(27)
  121.  
  122.     for event in pygame.event.get():
  123.         if event.type == pygame.QUIT:
  124.             run = False
  125.  
  126.     for bullet in bullets:
  127.         if bullet.x < 500 and bullet.x > 0:
  128.             bullet.x += bullet.vel
  129.         else:
  130.             bullets.pop(bullets.index(bullet))
  131.  
  132.     keys = pygame.key.get_pressed()
  133.  
  134.     if keys[pygame.K_SPACE]:
  135.         if man.left:
  136.             facing = -1
  137.         else:
  138.             facing = 1
  139.  
  140.         if len(bullets) < 5:
  141.             bullets.append(
  142.                 projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))
  143.  
  144.     if keys[pygame.K_LEFT] and man.x > man.vel:
  145.         man.x -= man.vel
  146.         man.left = True
  147.         man.right = False
  148.         man.standing = False
  149.     elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
  150.         man.x += man.vel
  151.         man.right = True
  152.         man.left = False
  153.         man.standing = False
  154.     else:
  155.         man.standing = True
  156.         man.walkCount = 0
  157.  
  158.     if not (man.isJump):
  159.         if keys[pygame.K_UP]:
  160.             man.isJump = True
  161.             man.right = False
  162.             man.left = False
  163.             man.walkCount = 0
  164.     else:
  165.         if man.jumpCount >= -10:
  166.             neg = 1
  167.             if man.jumpCount < 0:
  168.                 neg = -1
  169.             man.y -= (man.jumpCount ** 2) * 0.5 * neg
  170.             man.jumpCount -= 1
  171.         else:
  172.             man.isJump = False
  173.             man.jumpCount = 10
  174.  
  175.     redrawGameWindow()
  176.  
  177. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement