Hotstepper

Game class

Jun 28th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.04 KB | None | 0 0
  1. import pygame
  2. from the_sprites import *
  3. from settings import *
  4.  
  5. pygame.init()
  6.  
  7.  
  8. class Game(pygame.sprite.Sprite):
  9.  
  10. def __init__(self):
  11. super().__init__()
  12. self.clock = pygame.time.Clock()
  13. self.CameraX = 0
  14. self.CameraY = 0
  15.  
  16. self.font = pygame.font.SysFont('comicsans', 30, True, True)
  17. self.score = 0
  18.  
  19. self.noBorderCross = True
  20. self.noCameraCross = True
  21. self.bullets = []
  22.  
  23. self.running = True
  24.  
  25. self.hero = thePlayer(45, 625, 40, 40)
  26. self.enemy = enemy(400, 625, 40, 40, 850)
  27. self.enemy2 = enemy(1450, 625, 40, 40, 1900)
  28. self.enemy3 = enemy(1000, 625, 40, 40, 1500)
  29. self.enemy4 = enemy(800, 625, 40, 40, 1300)
  30. self.enemy5 = enemy(1900, 625, 40, 40, 2400)
  31. self.enemy6 = enemy(2300, 625, 40, 40, 2900)
  32. self.enemy7 = enemy(2700, 625, 40, 40, 3400)
  33. self.enemy8 = enemy(3200, 625, 40, 40, 3900)
  34. self.enemy9 = enemy(3900, 625, 40, 40, 4200)
  35. self.enemy10 = enemy(3500, 625, 40, 40, 4290)
  36.  
  37. self.platform = Platform(200, 550)
  38. self.platform1 = Platform(600, 549)
  39. self.platform2 = Platform(990, 450)
  40. self.platform3 = Platform(1500, 548)
  41. self.platform4 = Platform(1900, 449)
  42. self.platform5 = Platform(2500, 550)
  43.  
  44. self.facing = 0
  45. self.landed = True
  46. self.enterGame = pygame.image.load('startorclose.png')
  47.  
  48. # Coins for Platform
  49. self.coin = Coins(250, 525)
  50. self.coin1 = Coins(285, 525)
  51. self.coin2 = Coins(320, 525)
  52. self.coin3 = Coins(355, 525)
  53. self.coin4 = Coins(390, 525)
  54.  
  55. # Coins for Platform3
  56. self.coin5 = Coins(1550, 525)
  57. self.coin6 = Coins(1585, 525)
  58. self.coin7 = Coins(1620, 525)
  59. self.coin8 = Coins(1655, 525)
  60. self.coin9 = Coins(1690, 525)
  61.  
  62. # Coins for Platform 5
  63. self.coin10 = Coins(2550, 525)
  64. self.coin11 = Coins(2585, 525)
  65. self.coin12 = Coins(2620, 525)
  66. self.coin13 = Coins(2655, 525)
  67. self.coin14 = Coins(2690, 525)
  68.  
  69. self.showMenu = pygame.image.load('MainMenu.png')
  70. self.stopScreen = False
  71. self.gameOver = False
  72.  
  73. # ----------------------------------------------------------------------------------------------------------------------
  74.  
  75. def events(self):
  76. for event in pygame.event.get():
  77. if event.type == pygame.QUIT:
  78. self.running = False
  79. pygame.quit()
  80.  
  81. # ----------------------------------------------------------------------------------------------------------------------
  82.  
  83. def hit_or_not(self):
  84. # Checking Collision of Enemy and Hero
  85. self.enemyCollide(self.hero, self.enemy)
  86. self.enemyCollide(self.hero, self.enemy2)
  87. self.enemyCollide(self.hero, self.enemy3)
  88. self.enemyCollide(self.hero, self.enemy4)
  89. self.enemyCollide(self.hero, self.enemy5)
  90. self.enemyCollide(self.hero, self.enemy6)
  91. self.enemyCollide(self.hero, self.enemy7)
  92. self.enemyCollide(self.hero, self.enemy8)
  93. self.enemyCollide(self.hero, self.enemy9)
  94. self.enemyCollide(self.hero, self.enemy10)
  95.  
  96. # ----------------------------------------------------------------------------------------------------------------------
  97.  
  98. # Checking Collision of Platform and Hero
  99. self.obstacleHit_or_not(self.hero, self.platform, 460)
  100. self.obstacleHit_or_not(self.hero, self.platform1, 459)
  101. self.obstacleHit_or_not(self.hero, self.platform2, 360)
  102. self.obstacleHit_or_not(self.hero, self.platform3, 458)
  103. self.obstacleHit_or_not(self.hero, self.platform4, 359)
  104. self.obstacleHit_or_not(self.hero, self.platform5, 457)
  105.  
  106. # ----------------------------------------------------------------------------------------------------------------------
  107.  
  108. # Checking Collision of Coins and Hero
  109. self.coinsHit_or_not(self.hero, self.coin)
  110. self.coinsHit_or_not(self.hero, self.coin1)
  111. self.coinsHit_or_not(self.hero, self.coin2)
  112. self.coinsHit_or_not(self.hero, self.coin3)
  113. self.coinsHit_or_not(self.hero, self.coin4)
  114.  
  115. self.coinsHit_or_not(self.hero, self.coin5)
  116. self.coinsHit_or_not(self.hero, self.coin6)
  117. self.coinsHit_or_not(self.hero, self.coin7)
  118. self.coinsHit_or_not(self.hero, self.coin8)
  119. self.coinsHit_or_not(self.hero, self.coin9)
  120.  
  121. self.coinsHit_or_not(self.hero, self.coin10)
  122. self.coinsHit_or_not(self.hero, self.coin11)
  123. self.coinsHit_or_not(self.hero, self.coin12)
  124. self.coinsHit_or_not(self.hero, self.coin13)
  125. self.coinsHit_or_not(self.hero, self.coin14)
  126.  
  127. # ----------------------------------------------------------------------------------------------------------------------
  128.  
  129. # Statements for colliding bullets with enemies
  130. self.bulletCollision(self.enemy)
  131. self.bulletCollision(self.enemy2)
  132. self.bulletCollision(self.enemy3)
  133. self.bulletCollision(self.enemy4)
  134. self.bulletCollision(self.enemy5)
  135. self.bulletCollision(self.enemy6)
  136. self.bulletCollision(self.enemy7)
  137. self.bulletCollision(self.enemy8)
  138. self.bulletCollision(self.enemy9)
  139. self.bulletCollision(self.enemy10)
  140.  
  141. # ----------------------------------------------------------------------------------------------------------------------
  142.  
  143. def movements(self):
  144. keys = pygame.key.get_pressed()
  145.  
  146. if keys[pygame.K_r]:
  147. if self.hero.left:
  148. self.facing = -1
  149. else:
  150. self.facing = 1
  151.  
  152. if len(self.bullets) < 1:
  153. self.bullets.append(projectile(round(self.hero.x + self.hero.width), round(self.hero.y + self.hero.height), 6, (0, 0, 0), self.facing))
  154.  
  155. if keys[pygame.K_LEFT] and self.hero.x > self.hero.velocity:
  156. self.hero.standing = False
  157.  
  158. self.hero.x -= self.hero.velocity
  159.  
  160. self.hero.left = True
  161. self.hero.right = False
  162.  
  163. self.noBorderCross = True
  164.  
  165. # ----------------------------------------------------------------------------------------------------------------------
  166.  
  167. self.hero.rect.x = self.hero.x
  168. self.hero.rect.y = self.hero.y
  169.  
  170. # ----------------------------------------------------------------------------------------------------------------------
  171.  
  172. self.updatePlatformRect(self.platform)
  173. self.updatePlatformRect(self.platform1)
  174. self.updatePlatformRect(self.platform2)
  175. self.updatePlatformRect(self.platform3)
  176. self.updatePlatformRect(self.platform4)
  177. self.updatePlatformRect(self.platform5)
  178.  
  179. # ----------------------------------------------------------------------------------------------------------------------
  180.  
  181. elif keys[pygame.K_RIGHT] and self.hero.x < BG_WIDTH - self.hero.velocity - self.hero.width:
  182. self.hero.standing = False
  183.  
  184. if self.noBorderCross:
  185. self.hero.x += self.hero.velocity
  186.  
  187. if self.noCameraCross:
  188.  
  189. # Moving Coins
  190. self.updateCoinPos(self.coin)
  191. self.updateCoinPos(self.coin1)
  192. self.updateCoinPos(self.coin2)
  193. self.updateCoinPos(self.coin3)
  194. self.updateCoinPos(self.coin4)
  195.  
  196. self.updateCoinPos(self.coin5)
  197. self.updateCoinPos(self.coin6)
  198. self.updateCoinPos(self.coin7)
  199. self.updateCoinPos(self.coin8)
  200. self.updateCoinPos(self.coin9)
  201.  
  202. self.updateCoinPos(self.coin10)
  203. self.updateCoinPos(self.coin11)
  204. self.updateCoinPos(self.coin12)
  205. self.updateCoinPos(self.coin13)
  206. self.updateCoinPos(self.coin14)
  207.  
  208. # ----------------------------------------------------------------------------------------------------------------------
  209.  
  210. # Moving Platforms
  211. self.updatePlatformPos(self.platform)
  212. self.updatePlatformPos(self.platform1)
  213. self.updatePlatformPos(self.platform2)
  214. self.updatePlatformPos(self.platform3)
  215. self.updatePlatformPos(self.platform4)
  216. self.updatePlatformPos(self.platform5)
  217.  
  218. # ----------------------------------------------------------------------------------------------------------------------
  219.  
  220. # Moving Camera
  221. self.CameraX += self.hero.velocity
  222.  
  223. # ----------------------------------------------------------------------------------------------------------------------
  224.  
  225. self.updateEnemyPos(self.enemy)
  226. self.updateEnemyPos(self.enemy2)
  227. self.updateEnemyPos(self.enemy3)
  228. self.updateEnemyPos(self.enemy4)
  229. self.updateEnemyPos(self.enemy5)
  230. self.updateEnemyPos(self.enemy6)
  231. self.updateEnemyPos(self.enemy7)
  232. self.updateEnemyPos(self.enemy8)
  233. self.updateEnemyPos(self.enemy9)
  234. self.updateEnemyPos(self.enemy10)
  235.  
  236. # ----------------------------------------------------------------------------------------------------------------------
  237.  
  238. if self.enemy.enemyDead:
  239. self.enemy.x += self.hero.velocity
  240. elif self.enemy2.enemyDead:
  241. self.enemy2.x += self.hero.velocity
  242. elif self.enemy3.enemyDead:
  243. self.enemy3.x += self.hero.velocity
  244. elif self.enemy4.enemyDead:
  245. self.enemy4.x += self.hero.velocity
  246. elif self.enemy5.enemyDead:
  247. self.enemy5.x += self.hero.velocity
  248. elif self.enemy6.enemyDead:
  249. self.enemy6.x += self.hero.velocity
  250. elif self.enemy7.enemyDead:
  251. self.enemy7.x += self.hero.velocity
  252. elif self.enemy8.enemyDead:
  253. self.enemy8.x += self.hero.velocity
  254. elif self.enemy9.enemyDead:
  255. self.enemy9.x += self.hero.velocity
  256. elif self.enemy10.enemyDead:
  257. self.enemy10.x += self.hero.velocity
  258.  
  259. if self.hero.x >= startScrollingPos:
  260. self.noBorderCross = False
  261.  
  262. # 6500 is Camera Position Limit - If increased Camera will behave abnormally
  263. if self.CameraX >= 3700:
  264. self.noCameraCross = False
  265. if self.hero.x < 890 - self.hero.velocity - self.hero.width:
  266. self.hero.x += self.hero.velocity
  267.  
  268. self.hero.left = False
  269. self.hero.right = True
  270.  
  271. # Updating rect of Hero
  272. self.hero.rect.x = self.hero.x
  273. self.hero.rect.y = self.hero.y
  274.  
  275. # ----------------------------------------------------------------------------------------------------------------------
  276.  
  277. # Updating rect of Platform
  278. self.updatePlatformRect(self.platform)
  279. self.updatePlatformRect(self.platform1)
  280. self.updatePlatformRect(self.platform2)
  281. self.updatePlatformRect(self.platform3)
  282. self.updatePlatformRect(self.platform4)
  283. self.updatePlatformRect(self.platform5)
  284.  
  285. # ----------------------------------------------------------------------------------------------------------------------
  286.  
  287. # Updating rect of Coin
  288. self.updateCoinRect(self.coin)
  289. self.updateCoinRect(self.coin1)
  290. self.updateCoinRect(self.coin2)
  291. self.updateCoinRect(self.coin3)
  292. self.updateCoinRect(self.coin4)
  293.  
  294. self.updateCoinRect(self.coin5)
  295. self.updateCoinRect(self.coin6)
  296. self.updateCoinRect(self.coin7)
  297. self.updateCoinRect(self.coin8)
  298. self.updateCoinRect(self.coin9)
  299.  
  300. self.updateCoinRect(self.coin10)
  301. self.updateCoinRect(self.coin11)
  302. self.updateCoinRect(self.coin12)
  303. self.updateCoinRect(self.coin13)
  304. self.updateCoinRect(self.coin14)
  305.  
  306. # ----------------------------------------------------------------------------------------------------------------------
  307.  
  308. elif keys[pygame.K_r]:
  309. self.hero.shoot = True
  310.  
  311. else:
  312. self.hero.standing = True
  313. self.hero.shoot = False
  314. self.hero.walkCount = 0
  315.  
  316. if not self.hero.isJump:
  317. if keys[pygame.K_SPACE]:
  318. self.hero.isJump = True
  319. self.hero.walkCount = 0
  320. else:
  321. if self.hero.jumpCount >= -10:
  322. self.hero.y -= (self.hero.jumpCount * abs(self.hero.jumpCount)) * 0.5
  323. self.hero.jumpCount -= 1
  324. self.hero.rect.x = self.hero.x
  325. self.hero.rect.y = self.hero.y
  326. else:
  327. self.hero.jumpCount = 10
  328. self.hero.isJump = False
  329. self.hero.rect.x = self.hero.x
  330. self.hero.rect.y = self.hero.y
  331.  
  332. # Method to draw Sprites
  333. # ----------------------------------------------------------------------------------------------------------------------
  334. def redrawGameWindow(self):
  335. # This statement continuously keep drawing background image according to the camera value
  336. screen.blit(bg, (0 - self.CameraX, 0 - self.CameraY))
  337.  
  338. text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
  339. screen.blit(text, (390, 10))
  340.  
  341. # Drawing hero and enemies on Screen
  342. self.hero.draw(screen)
  343.  
  344.  
  345. # Drawing Enemies
  346. self.enemy.draw(screen)
  347. self.enemy2.draw(screen)
  348. self.enemy3.draw(screen)
  349. self.enemy4.draw(screen)
  350. self.enemy5.draw(screen)
  351. self.enemy6.draw(screen)
  352. self.enemy7.draw(screen)
  353. self.enemy8.draw(screen)
  354. self.enemy9.draw(screen)
  355. self.enemy10.draw(screen)
  356.  
  357. # Drawing Platforms(Tiles)
  358. self.platform.draw(screen)
  359. self.platform1.draw(screen)
  360. self.platform2.draw(screen)
  361. self.platform3.draw(screen)
  362. self.platform4.draw(screen)
  363. self.platform5.draw(screen)
  364.  
  365. # Drawing Coins for Platform
  366. self.coin.draw(screen)
  367. self.coin1.draw(screen)
  368. self.coin2.draw(screen)
  369. self.coin3.draw(screen)
  370. self.coin4.draw(screen)
  371.  
  372. # Drawing Coins for Platform3
  373. self.coin5.draw(screen)
  374. self.coin6.draw(screen)
  375. self.coin7.draw(screen)
  376. self.coin8.draw(screen)
  377. self.coin9.draw(screen)
  378.  
  379. # Drawing Coins for Platform5
  380. self.coin10.draw(screen)
  381. self.coin11.draw(screen)
  382. self.coin12.draw(screen)
  383. self.coin13.draw(screen)
  384. self.coin14.draw(screen)
  385.  
  386. for bullet in self.bullets:
  387. bullet.draw(screen)
  388.  
  389. pygame.display.flip()
  390.  
  391. # Method to update Rect of Platform and Coins
  392. # ----------------------------------------------------------------------------------------------------------------------
  393.  
  394. def updatePlatformRect(self, platform):
  395. platform.rect.x = platform.x
  396. platform.rect.y = platform.y
  397.  
  398. def updateCoinRect(self, coin):
  399. coin.rect.x = coin.x
  400. coin.rect.y = coin.y
  401.  
  402. #Methods to update position
  403. # ----------------------------------------------------------------------------------------------------------------------
  404.  
  405. def updatePlatformPos(self, platform):
  406. platform.x += -self.hero.velocity
  407.  
  408. def updateCoinPos(self, coin):
  409. coin.x += -self.hero.velocity
  410.  
  411. def updateEnemyPos(self, enemy):
  412. enemy.path[0] -= self.hero.velocity
  413. enemy.path[1] -= self.hero.velocity
  414.  
  415. # ----------------------------------------------------------------------------------------------------------------------
  416.  
  417. # Methods for Checking Collision
  418. # ----------------------------------------------------------------------------------------------------------------------
  419.  
  420. def enemyCollide(self, hero, enemy):
  421. # This statement will start reacting when player touches enemy
  422. if hero.hitbox[1] < enemy.hitbox[1] + enemy.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > enemy.hitbox[1]:
  423. if hero.hitbox[0] + hero.hitbox[2] > enemy.hitbox[0] and hero.hitbox[0] < enemy.hitbox[0] + enemy.hitbox[2]:
  424. readyGame(True, True)
  425.  
  426. # This function works for Blocks only
  427. def obstacleHit_or_not(self, hero, platform, position):
  428. hit = pygame.sprite.collide_rect(hero, platform)
  429.  
  430. if hit:
  431. #print("ITS HITTING")
  432. hero.y = position
  433. print("hero.y - ", hero.y)
  434.  
  435. elif hero.y == position:
  436. #print("HITTING INSIDE OF 450")
  437.  
  438. while True:
  439. hero.y += 5
  440. if hero.y >= 625:
  441. break
  442.  
  443. # This function works for Coins Only
  444. def coinsHit_or_not(self, hero, thecoin):
  445. if not thecoin.coinTouched:
  446. hit = pygame.sprite.collide_rect(hero, thecoin)
  447. if hit:
  448. thecoin.hit()
  449. self.score += 2
  450.  
  451. # This is the function used for detecting bullets that will hit enemy
  452. def bulletCollision(self, enemy):
  453. for bullet in self.bullets:
  454. if bullet.y - bullet.radius < enemy.hitbox[1] + enemy.hitbox[3] and bullet.y + bullet.radius > enemy.hitbox[1]:
  455. if bullet.x + bullet.radius > enemy.hitbox[0] and bullet.x - bullet.radius < enemy.hitbox[0] + enemy.hitbox[2]:
  456. enemy.hit()
  457. self.score += 1
  458. self.bullets.pop(self.bullets.index(bullet))
  459.  
  460. # This won't let bullets travel more than screen size i.e 928
  461. if bullet.x < 928 and bullet.x > 0:
  462. bullet.x += bullet.vel
  463. else:
  464. try:
  465. self.bullets.pop(self.bullets.index(bullet))
  466. except ValueError:
  467. print("OUT OF BOUND")
  468.  
  469. # ----------------------------------------------------------------------------------------------------------------------
  470.  
  471. gameOver = False
  472. onTouch = True
  473. run = True
  474.  
  475.  
  476. def runGame(theGame):
  477.  
  478. theGame.clock.tick(FPS)
  479.  
  480. # This function consists code for Events
  481. theGame.events()
  482. # This function consists code from enemy hit events
  483. theGame.hit_or_not()
  484. # This function consists code for player movements
  485. theGame.movements()
  486. # This function consists code for drawing the sprites over the screen
  487. theGame.redrawGameWindow()
  488.  
  489.  
  490. def readyGame(run, gameOver):
  491. game = Game()
  492.  
  493. while run:
  494. runGame(game)
  495.  
  496. keys = pygame.key.get_pressed()
  497.  
  498. if keys[pygame.K_x]:
  499. game = Game()
  500. elif keys[pygame.K_ESCAPE]:
  501. run = False
  502. pygame.event.pump()
  503.  
  504. if gameOver:
  505. runGame(game)
  506.  
  507.  
  508. readyGame(True, False)
  509.  
  510. pygame.quit()
Add Comment
Please, Sign In to add comment