Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. thePlayer Class:
  2.  
  3. # Delcaring thePlayer class which contains data about player(hero)
  4. class thePlayer(pygame.sprite.Sprite):
  5. heroWalkRight =[pygame.image.load('HeroWalkRight/PenRunR0.png')....
  6. hero_Death = [pygame.image.load('hero_death/Armature_DEAD_00.png')..
  7. pygame.image.load('hero_death/Armature_DEAD_32.png')]
  8.  
  9. heroShootR = [pygame.image.load('HeroShootR/shot0.png')...pygame.image.load('HeroShootR/shot7.png')]
  10.  
  11. heroShootL = [pygame.image.load('HeroShootL/shot0.png')....pygame.image.load('HeroShootL/shot7.png')]
  12.  
  13. hero_charR = pygame.image.load('HeroStandChar/PStandStillR.png')
  14. hero_charL = pygame.image.load('HeroStandChar/PStandStillL.png')
  15.  
  16. def __init__(self, x, y, width, height):
  17. super().__init__()
  18. self.x = x
  19. self.y = y
  20. self.width = width
  21. self.height = height
  22. self.velocity = 5
  23. self.isJump = False
  24. self.jumpCount = 10
  25. self.left = False
  26. self.right = False
  27. self.shoot = False
  28. self.walkCount = 0
  29. self.shootCount = 0
  30. self.standing = True
  31. self.hitbox = (self.x + 20, self.y, 28, 60)
  32. self.deathCount = 0
  33. self.gameOver = False
  34.  
  35. def draw(self, window):
  36. if self.walkCount + 1 >= 15:
  37. self.walkCount = 0
  38.  
  39. if self.shootCount + 1 >= 7:
  40. self.shootCount = 0
  41.  
  42. if not self.standing:
  43. if self.left:
  44. window.blit(self.heroWalkLeft[self.walkCount], (self.x, self.y))
  45. self.walkCount += 1
  46.  
  47. elif self.right:
  48. window.blit(self.heroWalkRight[self.walkCount], (self.x, self.y))
  49. self.walkCount += 1
  50.  
  51. else:
  52. if self.left:
  53. if self.shoot:
  54. window.blit(self.heroShootL[self.shootCount], (self.x, self.y))
  55. self.shootCount += 1
  56. else:
  57. window.blit(self.hero_charL, (self.x, self.y))
  58. elif self.right:
  59. if self.shoot:
  60. window.blit(self.heroShootR[self.shootCount], (self.x, self.y))
  61. self.shootCount += 1
  62. else:
  63. window.blit(self.hero_charR, (self.x, self.y))
  64.  
  65. self.hitbox = (self.x + 20, self.y, 55, 80)
  66. # pygame.draw.rect(win, (0, 0, 0), self.hitbox, 2)
  67. # pygame.draw.rect(win, (0, 0, 0), self.hitbox, 2)
  68.  
  69. def restart(self):
  70. startGame()
  71.  
  72. def redrawGameWindow(CameraX):
  73. # This statement continuosly keep drawing background image according to
  74. the camera value
  75. win.blit(bg, (0 - CameraX, 0 - CameraY))
  76.  
  77. text = font.render('Score: ' + str(score), 1, (0, 0, 0))
  78. win.blit(text, (390, 10))
  79.  
  80. # Drawing hero and enemies on Screen
  81. hero.draw(win)
  82. enemy2.draw(win)
  83. enemy.draw(win)
  84.  
  85. for bullet in bullets:
  86. bullet.draw(win)
  87.  
  88. pygame.display.flip()
  89.  
  90. pygame.display.set_caption("First Game")
  91. bg = pygame.image.load('LongBGx8.png')
  92. bgWidth, bgHeight = bg.get_rect().size
  93. win = pygame.display.set_mode((928, bgHeight))
  94. startScrollingPos = bgWidth / 28
  95.  
  96. score = 0
  97. clock = pygame.time.Clock()
  98. CameraY = 0
  99.  
  100. font = pygame.font.SysFont('comicsans', 30, True, True)
  101. hero = thePlayer(45, 625, 40, 40)
  102. enemy2 = enemy(1450, 625, 40, 40, 1900)
  103. enemy = enemy(400, 625, 40, 40, 850)
  104.  
  105. alternateX = 0
  106.  
  107. bullets = []
  108. run = True
  109.  
  110. def startGame():`
  111. clock.tick(60)
  112.  
  113. noBorderCross = True
  114. noCameraCross = True
  115. CameraX = 0
  116.  
  117. # This statement will start reacting when player touches enemy
  118. if hero.hitbox[1] < enemy.hitbox[1] + enemy.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > enemy.hitbox[1]:
  119. if hero.hitbox[0] + hero.hitbox[2] > enemy.hitbox[0] and hero.hitbox[0] < enemy.hitbox[0] + enemy.hitbox[2]:
  120. print("GOT HIT!")
  121. hero.restart()
  122.  
  123. # This statement will start reacting when player touches enemy
  124. if hero.hitbox[1] < enemy2.hitbox[1] + enemy2.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > enemy2.hitbox[1]:
  125. if hero.hitbox[0] + hero.hitbox[2] > enemy2.hitbox[0] and hero.hitbox[0] < enemy2.hitbox[0] + enemy2.hitbox[2]:
  126. print("GOT HIT!")
  127. # hero.hit(win)
  128.  
  129. for event in pygame.event.get():
  130. if event.type == pygame.QUIT:
  131. run = False
  132.  
  133. # This loop is used for detecting bullets that will hit enemy
  134. # One statement for enemy and other one for enemy2
  135. for bullet in bullets:
  136. if bullet.y - bullet.radius < enemy.hitbox[1] + enemy.hitbox[3] and bullet.y + bullet.radius > enemy.hitbox[1]:
  137. if bullet.x + bullet.radius > enemy.hitbox[0] and bullet.x - bullet.radius < enemy.hitbox[0] + enemy.hitbox[2]:
  138. enemy.hit()
  139. score += 1
  140. bullets.pop(bullets.index(bullet))
  141.  
  142. if bullet.y - bullet.radius < enemy2.hitbox[1] + enemy2.hitbox[3] and bullet.y + bullet.radius > enemy2.hitbox[1]:
  143. if bullet.x + bullet.radius > enemy2.hitbox[0] and bullet.x - bullet.radius < enemy2.hitbox[0] +
  144. enemy2.hitbox[2]:
  145. enemy2.hit()
  146. score += 1
  147. bullets.pop(bullets.index(bullet))
  148.  
  149. # This won't let bullets travel more than screen size i.e 928
  150. if bullet.x < 928 and bullet.x > 0:
  151. bullet.x += bullet.vel
  152. else:
  153. bullets.pop(bullets.index(bullet))
  154.  
  155. keys = pygame.key.get_pressed()
  156.  
  157. if keys[pygame.K_r]:
  158. if hero.left:
  159. facing = -1
  160. else:
  161. facing = 1
  162.  
  163. if len(bullets) < 1:
  164. bullets.append(projectile(round(hero.x + hero.width), round(hero.y + hero.height), 6, (0, 0, 0), facing))
  165.  
  166. if keys[pygame.K_LEFT] and hero.x > hero.velocity:
  167. hero.standing = False
  168. hero.x -= hero.velocity
  169. hero.left = True
  170. hero.right = False
  171. noBorderCross = True
  172.  
  173. elif keys[pygame.K_RIGHT] and hero.x < bgWidth - hero.velocity - hero.width:
  174. hero.standing = False
  175.  
  176. if noBorderCross:
  177. hero.x += hero.velocity
  178.  
  179. if noCameraCross:
  180. CameraX += hero.velocity
  181. enemy.path[0] -= hero.velocity
  182. enemy.path[1] -= hero.velocity
  183.  
  184. enemy2.path[0] -= hero.velocity
  185. enemy2.path[1] -= hero.velocity
  186.  
  187. if enemy.enemyDead:
  188. enemy.x += enemy.velocity
  189. enemy2.x += enemy2.velocity
  190.  
  191. if hero.x >= startScrollingPos:
  192. noBorderCross = False
  193.  
  194. # 6500 is Camera Position Limit - If increased Camera will behave abnormally
  195. if CameraX >= 6500:
  196. noCameraCross = False
  197. if hero.x < 890 - hero.velocity - hero.width:
  198. hero.x += hero.velocity
  199.  
  200. hero.left = False
  201. hero.right = True
  202.  
  203. elif keys[pygame.K_r]:
  204. hero.shoot = True
  205.  
  206. else:
  207. hero.standing = True
  208. hero.shoot = False
  209. hero.walkCount = 0
  210.  
  211. if not hero.isJump:
  212. if keys[pygame.K_SPACE]:
  213. hero.isJump = True
  214. hero.walkCount = 0
  215. else:
  216. if hero.jumpCount >= -10:
  217. hero.y -= (hero.jumpCount * abs(hero.jumpCount)) * 0.5
  218. hero.jumpCount -= 1
  219. else:
  220. hero.jumpCount = 10
  221. hero.isJump = False
  222.  
  223. redrawGameWindow(CameraX)
  224.  
  225. while run:
  226. startGame()
  227.  
  228. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement