Hotstepper

Game Class

Jun 13th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.64 KB | None | 0 0
  1. class Game(pygame.sprite.Sprite):
  2.  
  3. def __init__(self):
  4. super().__init__()
  5. self.clock = pygame.time.Clock()
  6. self.CameraX = 0
  7. self.CameraY = 0
  8.  
  9. self.font = pygame.font.SysFont('comicsans', 30, True, True)
  10. self.score = 0
  11.  
  12. self.noBorderCross = True
  13. self.noCameraCross = True
  14. self.bullets = []
  15.  
  16. self.running = True
  17.  
  18. self.hero = thePlayer(45, 625, 40, 40)
  19. self.enemy = enemy(400, 625, 40, 40, 850)
  20. self.enemy2 = enemy(1450, 625, 40, 40, 1900)
  21.  
  22. self.platform = Platform(200, 550)
  23.  
  24. self.facing = 0
  25. self.landed = True
  26. self.enterGame = pygame.image.load('startorclose.png')
  27.  
  28. self.coin = Coins(250, 525)
  29. self.coin1 = Coins(285, 525)
  30. self.coin2 = Coins(320, 525)
  31. self.coin3 = Coins(355, 525)
  32. self.coin4 = Coins(390, 525)
  33.  
  34. self.showMenu = pygame.image.load('MainMenu.png')
  35. self.stopScreen = False
  36. self.gameOver = False
  37.  
  38. def events(self):
  39. for event in pygame.event.get():
  40. if event.type == pygame.QUIT:
  41. self.running = False
  42.  
  43. def hit_or_not(self):
  44. '''
  45. # This statement will start reacting when player touches enemy
  46. if self.hero.hitbox[1] < self.enemy.hitbox[1] + self.enemy.hitbox[3] and self.hero.hitbox[1] + self.hero.hitbox[3] > self.enemy.hitbox[1]:
  47. if self.hero.hitbox[0] + self.hero.hitbox[2] > self.enemy.hitbox[0] and self.hero.hitbox[0] < self.enemy.hitbox[0] + self.enemy.hitbox[2]:
  48. # hero.x = bricks_block.x
  49. # hero.y = bricks_block.y
  50. print("GOT HIT")
  51. '''
  52.  
  53. if self.enemyHit_or_not(self.hero, self.enemy):
  54. self.gameOver = True
  55.  
  56. # This statement will start reacting when player touches enemy
  57. if self.hero.hitbox[1] < self.enemy2.hitbox[1] + self.enemy2.hitbox[3] and self.hero.hitbox[1] + self.hero.hitbox[3] > self.enemy2.hitbox[1]:
  58. if self.hero.hitbox[0] + self.hero.hitbox[2] > self.enemy2.hitbox[0] and self.hero.hitbox[0] < self.enemy2.hitbox[0] + self.enemy2.hitbox[2]:
  59. print("GOT HIT!")
  60. # self.hero.hit(win)
  61.  
  62. self.obstacleHit_or_not(self.hero, self.platform, 460)
  63. self.coinsHit_or_not(self.hero, self.coin)
  64. self.coinsHit_or_not(self.hero, self.coin1)
  65. self.coinsHit_or_not(self.hero, self.coin2)
  66. self.coinsHit_or_not(self.hero, self.coin3)
  67. self.coinsHit_or_not(self.hero, self.coin4)
  68.  
  69. # This function works for Blocks only
  70. def obstacleHit_or_not(self, hero, platform, position):
  71. hit = pygame.sprite.collide_rect(hero, platform)
  72.  
  73. if hit:
  74. #print("ITS HITTING")
  75. hero.y = position
  76.  
  77. elif self.hero.y == position:
  78. #print("HITTING INSIDE OF 450")
  79. while True:
  80. hero.y += 5
  81. if hero.y >= 625:
  82. break
  83.  
  84. # This function works for Coins Only
  85. def coinsHit_or_not(self, hero, thecoin):
  86. if not thecoin.coinTouched:
  87. hit = pygame.sprite.collide_rect(hero, thecoin)
  88. if hit:
  89. thecoin.hit()
  90. self.score += 2
  91.  
  92. def enemyHit_or_not(self, hero, enemy):
  93. hit = pygame.sprite.collide_rect(hero, enemy)
  94.  
  95. '''
  96. if hit:
  97. screen.blit(self.showMenu, (0, 0))
  98. pygame.display.flip()
  99. print("Do something")
  100. elif not hit:
  101. print("Do some more work")
  102. '''
  103. return hit
  104.  
  105. # This loop is used for detecting bullets that will hit enemy
  106. # One statement for enemy and other one for enemy2
  107. def bulletHit_or_not(self):
  108. for bullet in self.bullets:
  109. if bullet.y - bullet.radius < self.enemy.hitbox[1] + self.enemy.hitbox[3] and bullet.y + bullet.radius > self.enemy.hitbox[1]:
  110. if bullet.x + bullet.radius > self.enemy.hitbox[0] and bullet.x - bullet.radius < self.enemy.hitbox[0] + self.enemy.hitbox[2]:
  111. self.enemy.hit()
  112. self.score += 1
  113. self.bullets.pop(self.bullets.index(bullet))
  114.  
  115. if bullet.y - bullet.radius < self.enemy2.hitbox[1] + self.enemy2.hitbox[3] and bullet.y + bullet.radius > self.enemy2.hitbox[1]:
  116. if bullet.x + bullet.radius > self.enemy2.hitbox[0] and bullet.x - bullet.radius < self.enemy2.hitbox[0] + self.enemy2.hitbox[2]:
  117. self.enemy2.hit()
  118. self.score += 1
  119. self.bullets.pop(self.bullets.index(bullet))
  120.  
  121. # This won't let bullets travel more than screen size i.e 928
  122. if bullet.x < 928 and bullet.x > 0:
  123. bullet.x += bullet.vel
  124. else:
  125. self.bullets.pop(self.bullets.index(bullet))
  126.  
  127. def movements(self):
  128. keys = pygame.key.get_pressed()
  129.  
  130. if keys[pygame.K_r]:
  131. if self.hero.left:
  132. self.facing = -1
  133. else:
  134. self.facing = 1
  135.  
  136. if len(self.bullets) < 1:
  137. self.bullets.append(projectile(round(self.hero.x + self.hero.width), round(self.hero.y + self.hero.height), 6, (0, 0, 0), self.facing))
  138.  
  139. if keys[pygame.K_LEFT] and self.hero.x > self.hero.velocity:
  140. self.hero.standing = False
  141.  
  142. self.hero.x -= self.hero.velocity
  143.  
  144. self.hero.left = True
  145. self.hero.right = False
  146.  
  147. self.noBorderCross = True
  148.  
  149. self.hero.rect.x = self.hero.x
  150. self.hero.rect.y = self.hero.y
  151.  
  152. self.platform.rect.x = self.platform.x
  153. self.platform.rect.y = self.platform.y
  154.  
  155. elif keys[pygame.K_RIGHT] and self.hero.x < BG_WIDTH - self.hero.velocity - self.hero.width:
  156. self.hero.standing = False
  157.  
  158. if self.noBorderCross:
  159. self.hero.x += self.hero.velocity
  160.  
  161. if self.noCameraCross:
  162. # Moving Coins
  163. self.coin.x += -self.hero.velocity
  164. self.coin1.x += -self.hero.velocity
  165. self.coin2.x += -self.hero.velocity
  166. self.coin3.x += -self.hero.velocity
  167. self.coin4.x += -self.hero.velocity
  168.  
  169. # Moving Platforms
  170. self.platform.x += -self.hero.velocity
  171.  
  172. # Moving Camera
  173. self.CameraX += self.hero.velocity
  174.  
  175. self.enemy.path[0] -= self.hero.velocity
  176. self.enemy.path[1] -= self.hero.velocity
  177.  
  178. self.enemy2.path[0] -= self.hero.velocity
  179. self.enemy2.path[1] -= self.hero.velocity
  180.  
  181. if self.enemy.enemyDead:
  182. self.enemy.x += self.hero.velocity
  183. if self.enemy2.enemyDead:
  184. self.enemy2.x += self.hero.velocity
  185.  
  186. if self.hero.x >= startScrollingPos:
  187. self.noBorderCross = False
  188.  
  189. # 6500 is Camera Position Limit - If increased Camera will behave abnormally
  190. if self.CameraX >= 6500:
  191. self.noCameraCross = False
  192. if self.hero.x < 890 - self.hero.velocity - self.hero.width:
  193. self.hero.x += self.hero.velocity
  194.  
  195. self.hero.left = False
  196. self.hero.right = True
  197.  
  198. # Updating rect of Hero
  199. self.hero.rect.x = self.hero.x
  200. self.hero.rect.y = self.hero.y
  201.  
  202. # Updating rect of Platform
  203. self.platform.rect.x = self.platform.x
  204. self.platform.rect.y = self.platform.y
  205.  
  206. # Updating rect of Coin
  207. self.coin.rect.x = self.coin.x
  208. self.coin.rect.y = self.coin.y
  209.  
  210. self.coin1.rect.x = self.coin1.x
  211. self.coin1.rect.y = self.coin1.y
  212.  
  213. self.coin2.rect.x = self.coin2.x
  214. self.coin2.rect.y = self.coin2.y
  215.  
  216. self.coin3.rect.x = self.coin3.x
  217. self.coin3.rect.y = self.coin3.y
  218.  
  219. self.coin4.rect.x = self.coin4.x
  220. self.coin4.rect.y = self.coin4.y
  221.  
  222. elif keys[pygame.K_r]:
  223. self.hero.shoot = True
  224.  
  225. else:
  226. self.hero.standing = True
  227. self.hero.shoot = False
  228. self.hero.walkCount = 0
  229.  
  230. if not self.hero.isJump:
  231. if keys[pygame.K_SPACE]:
  232. self.hero.isJump = True
  233. self.hero.walkCount = 0
  234. else:
  235. if self.hero.jumpCount >= -10:
  236. self.hero.y -= (self.hero.jumpCount * abs(self.hero.jumpCount)) * 0.7
  237. self.hero.jumpCount -= 1
  238. self.hero.rect.x = self.hero.x
  239. self.hero.rect.y = self.hero.y
  240. else:
  241. self.hero.jumpCount = 10
  242. self.hero.isJump = False
  243. self.hero.rect.x = self.hero.x
  244. self.hero.rect.y = self.hero.y
  245.  
  246. def redrawGameWindow(self):
  247. # This statement continuously keep drawing background image according to the camera value
  248. screen.blit(bg, (0 - self.CameraX, 0 - self.CameraY))
  249.  
  250. text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
  251. screen.blit(text, (390, 10))
  252.  
  253. # Drawing hero and enemies on Screen
  254. self.hero.draw(screen)
  255. self.enemy.draw(screen)
  256. self.enemy2.draw(screen)
  257. self.platform.draw(screen)
  258. self.coin.draw(screen)
  259. self.coin1.draw(screen)
  260. self.coin2.draw(screen)
  261. self.coin3.draw(screen)
  262. self.coin4.draw(screen)
  263.  
  264. for bullet in self.bullets:
  265. bullet.draw(screen)
  266.  
  267. pygame.display.flip()
  268.  
  269.  
  270. # ----------------------------------------------------------------------------------------------------------------------
  271. run = True
  272.  
  273. def runGame(theGame):
  274.  
  275. while run:
  276. theGame.clock.tick(FPS)
  277.  
  278. # This function consists code for Events
  279. theGame.events()
  280. # This function consists code from enemy hit events
  281. theGame.hit_or_not()
  282. # This function consists code for bullet hit events
  283. theGame.bulletHit_or_not()
  284. # This function consists code for player movements
  285. theGame.movements()
  286. # This function consists code for drawing the sprites over the screen
  287. theGame.redrawGameWindow()
  288.  
  289.  
  290. def startGame(run):
  291. game = Game()
  292.  
  293. while run:
  294. runGame(game)
  295.  
  296. keys = pygame.key.get_pressed()
  297.  
  298. if keys[pygame.K_ESCAPE]:
  299. run = False
  300.  
  301. elif keys[pygame.K_c]:
  302. game = Game()
  303.  
  304.  
  305. startGame(True)
  306.  
  307. pygame.quit()
Add Comment
Please, Sign In to add comment