Hotstepper

Untitled

Jul 3rd, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.82 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, level):
  11. super().__init__()
  12.  
  13. self.level = level
  14. self.coordinates = 0
  15. self.landingCords = 0
  16.  
  17. self.theGround = Ground_lvl1(0, 625)
  18.  
  19. if self.level == 1:
  20.  
  21. self.theGround = Ground_lvl1(0, 725)
  22. self.theGround2 = Ground_lvl2(0, 725)
  23.  
  24. self.clock = pygame.time.Clock()
  25. self.CameraX = 0
  26. self.CameraY = 0
  27.  
  28. self.font = pygame.font.SysFont('comicsans', 30, True, True)
  29. self.score = 0
  30.  
  31. self.noBorderCross = True
  32. self.noCameraCross = True
  33. self.bullets = []
  34.  
  35. self.running = True
  36.  
  37. self.hero = thePlayer(45, 625, 40, 40)
  38.  
  39. self.enemy = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900), enemy(1000, 625, 40, 40, 1500),
  40. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400), enemy(2300, 625, 40, 40, 2900),
  41. enemy(2700, 625, 40, 40, 3400), enemy(3000, 625, 40, 40, 3400), enemy(3000, 625, 40, 40, 3400),
  42. enemy(3100, 625, 40, 40, 3400)]
  43.  
  44. self.enemy_sky = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900), enemy(1000, 625, 40, 40, 1500),
  45. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400)]
  46.  
  47. self.enemy_forest = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900),
  48. enemy(1000, 625, 40, 40, 1500),
  49. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400)]
  50.  
  51. self.platform = [Platform(200, 550, 460), Platform(600, 549, 459), Platform(990, 450, 360),
  52. Platform(1500, 548, 458), Platform(1900, 449, 359), Platform(2500, 550, 461)]
  53.  
  54. self.platform_sky = [Platform_sky(200, 550, 460), Platform_sky(600, 549, 459), Platform_sky(990, 450, 360),
  55. Platform_sky(1500, 548, 458)]
  56.  
  57. self.platform_forest = [Platform_sky(200, 550, 460), Platform_sky(600, 549, 459), Platform_sky(990, 450, 360),
  58. Platform_sky(1500, 548, 458)]
  59.  
  60. self.tunnel = Tunnel(4390, 525)
  61.  
  62. self.tunnel_sky = Tunnel(3000, 525)
  63.  
  64. self.facing = 0
  65. self.landed = True
  66. self.enterGame = pygame.image.load('startorclose.png')
  67.  
  68. # Coins for Platform
  69. self.coin = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  70. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  71. Coins(1690, 525), Coins(2550, 525), Coins(2585, 525), Coins(2620, 525), Coins(2655, 525),
  72. Coins(2690, 525)]
  73.  
  74. # Coins for Platform_sky
  75. self.coin_sky = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  76. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  77. Coins(1690, 525), Coins(2550, 490), Coins(2585, 500), Coins(2620, 510), Coins(2655, 500),
  78. Coins(2690, 490)]
  79.  
  80. self.coin_forest = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  81. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  82. Coins(1690, 525), Coins(2550, 490), Coins(2585, 500), Coins(2620, 510), Coins(2655, 500),
  83. Coins(2690, 490)]
  84.  
  85. self.showMenu = pygame.image.load('MainMenu.png')
  86. self.stopScreen = False
  87. self.gameOver = False
  88.  
  89. elif self.level == 2:
  90.  
  91. self.theGround2 = Ground_lvl2(0, 725)
  92.  
  93. self.clock = pygame.time.Clock()
  94. self.CameraX = 0
  95. self.CameraY = 0
  96.  
  97. self.font = pygame.font.SysFont('comicsans', 30, True, True)
  98. self.score = 0
  99.  
  100. self.noBorderCross = True
  101. self.noCameraCross = True
  102. self.bullets = []
  103.  
  104. self.running = True
  105.  
  106. self.hero = thePlayer(45, 650, 40, 40)
  107.  
  108. self.enemy_sky = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900), enemy(1000, 625, 40, 40, 1500),
  109. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400)]
  110.  
  111. self.enemy_forest = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900),
  112. enemy(1000, 625, 40, 40, 1500),
  113. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400)]
  114.  
  115. self.platform_sky = [Platform_sky(200, 550, 460), Platform_sky(600, 549, 459), Platform_sky(990, 450, 360),
  116. Platform_sky(1500, 548, 458)]
  117.  
  118. self.platform_forest = [Platform_sky(200, 550, 460), Platform_sky(600, 549, 459), Platform_sky(990, 450, 360),
  119. Platform_sky(1500, 548, 458)]
  120.  
  121. self.tunnel_sky = Tunnel(3000, 525)
  122.  
  123. self.facing = 0
  124. self.landed = True
  125. self.enterGame = pygame.image.load('startorclose.png')
  126.  
  127. # Coins for Platform_sky
  128. self.coin_sky = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  129. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  130. Coins(1690, 525), Coins(2550, 490), Coins(2585, 500), Coins(2620, 510), Coins(2655, 500),
  131. Coins(2690, 490)]
  132.  
  133. self.coin_forest = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  134. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  135. Coins(1690, 525), Coins(2550, 490), Coins(2585, 500), Coins(2620, 510), Coins(2655, 500),
  136. Coins(2690, 490)]
  137.  
  138. self.showMenu = pygame.image.load('MainMenu.png')
  139. self.stopScreen = False
  140. self.gameOver = False
  141.  
  142. elif self.level == 3:
  143.  
  144. self.clock = pygame.time.Clock()
  145. self.CameraX = 0
  146. self.CameraY = 0
  147.  
  148. self.font = pygame.font.SysFont('comicsans', 30, True, True)
  149. self.score = 0
  150.  
  151. self.noBorderCross = True
  152. self.noCameraCross = True
  153. self.bullets = []
  154.  
  155. self.running = True
  156.  
  157. self.hero = thePlayer(45, 650, 40, 40)
  158.  
  159. self.enemy_sky = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900),
  160. enemy(1000, 625, 40, 40, 1500),
  161. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400)]
  162.  
  163. self.enemy_forest = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900),
  164. enemy(1000, 625, 40, 40, 1500),
  165. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400)]
  166.  
  167. self.platform_sky = [Platform_sky(200, 550, 460), Platform_sky(600, 549, 459), Platform_sky(990, 450, 360),
  168. Platform_sky(1500, 548, 458)]
  169.  
  170. self.platform_forest = [Platform_sky(200, 550, 460), Platform_sky(600, 549, 459), Platform_sky(990, 450, 360),
  171. Platform_sky(1500, 548, 458)]
  172.  
  173. self.tunnel_sky = Tunnel(3000, 525)
  174.  
  175. self.facing = 0
  176. self.landed = True
  177. self.enterGame = pygame.image.load('startorclose.png')
  178.  
  179. # Coins for Platform_sky
  180. self.coin_sky = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  181. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  182. Coins(1690, 525), Coins(2550, 490), Coins(2585, 500), Coins(2620, 510), Coins(2655, 500),
  183. Coins(2690, 490)]
  184.  
  185. self.coin_forest = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  186. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  187. Coins(1690, 525), Coins(2550, 490), Coins(2585, 500), Coins(2620, 510), Coins(2655, 500),
  188. Coins(2690, 490)]
  189.  
  190. self.showMenu = pygame.image.load('MainMenu.png')
  191. self.stopScreen = False
  192. self.gameOver = False
  193.  
  194. # ----------------------------------------------------------------------------------------------------------------------
  195.  
  196. def events(self):
  197. for event in pygame.event.get():
  198. if event.type == pygame.QUIT:
  199. self.running = False
  200. pygame.quit()
  201.  
  202. # ----------------------------------------------------------------------------------------------------------------------
  203.  
  204. def hit_or_not(self):
  205.  
  206. if self.level == 1:
  207. # Checking Collision of Enemy and Hero
  208. self.enemyCollide(self.hero, self.enemy)
  209.  
  210. # Checking Collision of Platform and Hero
  211. self.obstacleHit_or_not(self.hero, self.platform)
  212.  
  213. self.groundHit_or_not(self.hero, self.theGround, 625)
  214.  
  215. # Checking Collision of Coins and Hero
  216. self.coinsHit_or_not(self.hero, self.coin)
  217.  
  218. self.tunnelHit_or_not(self.hero, self.tunnel)
  219.  
  220. # Statements for colliding bullets with enemies
  221. self.bulletCollision(self.enemy)
  222.  
  223. elif self.level == 2:
  224. self.enemyCollide(self.hero, self.enemy_sky)
  225.  
  226. # Checking Collision of Platform_sky and Hero
  227. self.obstacleHit_or_not(self.hero, self.platform_sky)
  228.  
  229. self.groundHit_or_not(self.hero, self.theGround2, 625)
  230.  
  231. # Checking Collision of Coin_Sky and Hero
  232. self.coinsHit_or_not(self.hero, self.coin_sky)
  233.  
  234. self.tunnelHit_or_not_2(self.hero, self.tunnel_sky)
  235.  
  236. # Statements for colliding bullets with enemies on lvl 2
  237. self.bulletCollision(self.enemy_sky)
  238.  
  239. elif self.level == 3:
  240. self.enemyCollide(self.hero, self.enemy_forest)
  241.  
  242. # Checking Collision of Platform_sky and Hero
  243. self.obstacleHit_or_not(self.hero, self.platform_forest)
  244.  
  245. self.groundHit_or_not(self.hero, self.theGround2, 625)
  246.  
  247. # Checking Collision of Coin_Sky and Hero
  248. self.coinsHit_or_not(self.hero, self.coin_forest)
  249.  
  250. # Statements for colliding bullets with enemies on lvl 2
  251. self.bulletCollision(self.enemy_forest)
  252. # ----------------------------------------------------------------------------------------------------------------------
  253.  
  254. def movements(self):
  255. keys = pygame.key.get_pressed()
  256.  
  257. if keys[pygame.K_r]:
  258. if self.hero.left:
  259. self.facing = -1
  260. else:
  261. self.facing = 1
  262.  
  263. if len(self.bullets) < 1:
  264. self.bullets.append(projectile(round(self.hero.x + self.hero.width), round(self.hero.y + self.hero.height), 6, (0, 0, 0), self.facing))
  265.  
  266. if keys[pygame.K_LEFT] and self.hero.x > self.hero.velocity:
  267. self.hero.standing = False
  268.  
  269. self.hero.x -= self.hero.velocity
  270.  
  271. self.hero.left = True
  272. self.hero.right = False
  273.  
  274. self.noBorderCross = True
  275.  
  276. # ----------------------------------------------------------------------------------------------------------------------
  277.  
  278. self.hero.rect.x = self.hero.x
  279. self.hero.rect.y = self.hero.y
  280.  
  281. # ----------------------------------------------------------------------------------------------------------------------
  282.  
  283. if self.level == 1:
  284. self.updatePlatformRect(self.platform)
  285. elif self.level == 2:
  286. self.updatePlatformRect(self.platform_sky)
  287.  
  288. # ----------------------------------------------------------------------------------------------------------------------
  289.  
  290. elif keys[pygame.K_RIGHT] and self.hero.x < BG_WIDTH - self.hero.velocity - self.hero.width:
  291. self.hero.standing = False
  292.  
  293. if self.noBorderCross:
  294. self.hero.x += self.hero.velocity
  295.  
  296. if self.noCameraCross:
  297.  
  298. if self.level == 1:
  299. # Moving Coins
  300. self.updateCoinPos(self.coin)
  301.  
  302. # Moving Platforms
  303. self.updatePlatformPos(self.platform)
  304.  
  305. self.updateEnemyPos(self.enemy)
  306.  
  307. self.updateTunnelPos(self.tunnel)
  308.  
  309. self.updateGroundPos(self.theGround)
  310.  
  311. # For moving enemies dead bodies
  312. for e in self.enemy:
  313. if e.enemyDead:
  314. e.x += self.hero.velocity
  315.  
  316. self.updateCoinPos(self.coin_sky)
  317.  
  318. if self.hero.x >= startScrollingPos:
  319. self.noBorderCross = False
  320.  
  321. self.coordinates = 3700
  322.  
  323. # Updating rect of Platform
  324. self.updatePlatformRect(self.platform)
  325.  
  326. # Updating rect of Tunnel
  327. self.updateTunnelRect(self.tunnel)
  328.  
  329. # Updating rect of Coin
  330. self.updateCoinRect(self.coin)
  331.  
  332. self.updateGroundRect(self.theGround)
  333. # ----------------------------------------------------------------------------------------------------------------------
  334. if self.level == 2:
  335. # Moving Coins
  336. self.updateCoinPos(self.coin_sky)
  337.  
  338. # Moving Platform_sky
  339. self.updatePlatformPos(self.platform_sky)
  340.  
  341. self.updateEnemyPos(self.enemy_sky)
  342.  
  343. self.updateTunnelPos(self.tunnel_sky)
  344.  
  345. self.updateGroundPos(self.theGround2)
  346.  
  347. # For moving enemies dead bodies in lvl 2
  348. for e in self.enemy_sky:
  349. if e.enemyDead:
  350. e.x += self.hero.velocity
  351.  
  352. if self.hero.x >= startScrollingPos_sky:
  353. self.noBorderCross = False
  354.  
  355. self.coordinates = 2200
  356.  
  357. # Updating rect of Platform_sky
  358. self.updatePlatformRect(self.platform_sky)
  359.  
  360. self.updateTunnelRect(self.tunnel_sky)
  361.  
  362. self.updateCoinRect(self.coin_sky)
  363.  
  364. self.updateGroundRect(self.theGround2)
  365.  
  366. # ----------------------------------------------------------------------------------------------------------------------
  367.  
  368. if self.level == 3:
  369. # Moving Coins
  370. self.updateCoinPos(self.coin_forest)
  371.  
  372. # Moving Platform_sky
  373. self.updatePlatformPos(self.platform_forest)
  374.  
  375. self.updateEnemyPos(self.enemy_forest)
  376.  
  377. # For moving enemies dead bodies in lvl 2
  378. for e in self.enemy_forest:
  379. if e.enemyDead:
  380. e.x += self.hero.velocity
  381.  
  382. if self.hero.x >= startScrollingPos_forest:
  383. self.noBorderCross = False
  384.  
  385. self.coordinates = 2200
  386.  
  387. # Updating rect of Platform_sky
  388. self.updatePlatformRect(self.platform_forest)
  389.  
  390. self.updateCoinRect(self.coin_forest)
  391.  
  392. # ----------------------------------------------------------------------------------------------------------------------
  393.  
  394. self.CameraX += self.hero.velocity
  395.  
  396. # ----------------------------------------------------------------------------------------------------------------------
  397.  
  398. # 6500 is Camera Position Limit - If increased Camera will behave abnormally
  399. if self.CameraX >= self.coordinates:
  400. self.noCameraCross = False
  401. if self.hero.x < 890 - self.hero.velocity - self.hero.width:
  402. self.hero.x += self.hero.velocity
  403. #self.level = 2
  404.  
  405. self.hero.left = False
  406. self.hero.right = True
  407.  
  408. # Updating rect of Hero
  409. self.hero.rect.x = self.hero.x
  410. self.hero.rect.y = self.hero.y
  411.  
  412. # ----------------------------------------------------------------------------------------------------------------------
  413.  
  414. elif keys[pygame.K_r]:
  415. self.hero.shoot = True
  416.  
  417. else:
  418. self.hero.standing = True
  419. self.hero.shoot = False
  420. self.hero.walkCount = 0
  421.  
  422. if not self.hero.isJump:
  423. if keys[pygame.K_SPACE]:
  424. self.hero.isJump = True
  425. self.hero.walkCount = 0
  426. else:
  427. if self.hero.jumpCount >= -10:
  428. self.hero.y -= (self.hero.jumpCount * abs(self.hero.jumpCount)) * 0.9
  429. self.hero.jumpCount -= 1
  430. self.hero.rect.x = self.hero.x
  431. self.hero.rect.y = self.hero.y
  432. else:
  433. self.hero.jumpCount = 10
  434. self.hero.isJump = False
  435. self.hero.rect.x = self.hero.x
  436. self.hero.rect.y = self.hero.y
  437.  
  438. # Method to draw Sprites
  439. # ----------------------------------------------------------------------------------------------------------------------
  440.  
  441. def redrawGameWindow(self):
  442. # This statement continuously keep drawing background image according to the camera value
  443. screen.blit(bg, (0 - self.CameraX, 0 - self.CameraY))
  444.  
  445. text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
  446. screen.blit(text, (390, 10))
  447.  
  448. # Drawing hero and enemies on Screen
  449. self.hero.draw(screen)
  450.  
  451. self.tunnel.draw(screen)
  452.  
  453. self.theGround.draw(screen)
  454.  
  455. # Drawing Enemies
  456. for e in self.enemy:
  457. e.draw(screen)
  458.  
  459. # Drawing Platforms(Tiles)
  460. for p in self.platform:
  461. p.draw(screen)
  462.  
  463. # Drawing Coins for Platform
  464. for c in self.coin:
  465. c.draw(screen)
  466.  
  467. for bullet in self.bullets:
  468. bullet.draw(screen)
  469.  
  470. pygame.display.flip()
  471.  
  472. def redrawGameWindow2(self):
  473. # This statement continuously keep drawing background image according to the camera value
  474. screen.blit(bg_2, (0 - self.CameraX, 0 - self.CameraY))
  475.  
  476. text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
  477. screen.blit(text, (390, 10))
  478.  
  479. # Drawing hero and enemies on Screen
  480. self.hero.draw(screen)
  481.  
  482. self.tunnel_sky.draw(screen)
  483.  
  484. self.theGround2.draw(screen)
  485.  
  486. # Drawing Enemies
  487. for e in self.enemy_sky:
  488. e.draw(screen)
  489.  
  490. # Drawing Platform_sky(Tiles)
  491. for p in self.platform_sky:
  492. p.draw(screen)
  493.  
  494. # Drawing Coins for Platform
  495. for c in self.coin_sky:
  496. c.draw(screen)
  497.  
  498. for bullet in self.bullets:
  499. bullet.draw(screen)
  500.  
  501. pygame.display.flip()
  502.  
  503. def redrawGameWindow3(self):
  504.  
  505. # This statement continuously keep drawing background image according to the camera value
  506. screen.blit(bg_3, (0 - self.CameraX, 0 - self.CameraY))
  507.  
  508. text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
  509. screen.blit(text, (390, 10))
  510.  
  511. # Drawing hero and enemies on Screen
  512. self.hero.draw(screen)
  513.  
  514. # Drawing Enemies
  515. for e in self.enemy_forest:
  516. e.draw(screen)
  517.  
  518. # Drawing Platform_sky(Tiles)
  519. for p in self.platform_forest:
  520. p.draw(screen)
  521.  
  522. # Drawing Coins for Platform
  523. for c in self.coin_forest:
  524. c.draw(screen)
  525.  
  526. for bullet in self.bullets:
  527. bullet.draw(screen)
  528.  
  529. pygame.display.flip()
  530.  
  531. # Method to update Rect of Platform and Coins
  532. # ----------------------------------------------------------------------------------------------------------------------
  533.  
  534. def updatePlatformRect(self, platform):
  535. for p in platform:
  536. p.rect.x = p.x
  537. p.rect.y = p.y
  538.  
  539. def updateCoinRect(self, coin):
  540. for c in coin:
  541. c.rect.x = c.x
  542. c.rect.y = c.y
  543.  
  544. def updateTunnelRect(self, tunnel):
  545. tunnel.rect.x = tunnel.x
  546. tunnel.rect.y = tunnel.y
  547.  
  548. def updateGroundRect(self, ground):
  549. ground.rect.x = ground.x
  550. ground.rect.y = ground.y
  551.  
  552. #Methods to update position
  553. # ----------------------------------------------------------------------------------------------------------------------
  554.  
  555. def updatePlatformPos(self, platform):
  556. for p in platform:
  557. p.x += -self.hero.velocity
  558.  
  559. def updateCoinPos(self, coin):
  560. for c in coin:
  561. c.x += -self.hero.velocity
  562.  
  563. def updateEnemyPos(self, enemy):
  564. for e in enemy:
  565. e.path[0] -= self.hero.velocity
  566. e.path[1] -= self.hero.velocity
  567.  
  568. def updateTunnelPos(self, tunnel):
  569. tunnel.x += -self.hero.velocity
  570.  
  571. def updateGroundPos(self, ground):
  572. ground.x += -self.hero.velocity
  573.  
  574. # ----------------------------------------------------------------------------------------------------------------------
  575.  
  576. # Methods for Checking Collision
  577. # ----------------------------------------------------------------------------------------------------------------------
  578.  
  579. def enemyCollide(self, hero, enemy):
  580.  
  581. for e in enemy:
  582. # This statement will start reacting when player touches enemy
  583. if hero.hitbox[1] < e.hitbox[1] + e.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > e.hitbox[1]:
  584. if hero.hitbox[0] + hero.hitbox[2] > e.hitbox[0] and hero.hitbox[0] < e.hitbox[0] + e.hitbox[2]:
  585. readyGame(True, True)
  586.  
  587. def groundHit_or_not(self, hero, ground, pos):
  588. hit = pygame.sprite.collide_rect(hero, ground)
  589.  
  590. if hit:
  591. hero.y = pos
  592.  
  593. # This function works for Tunnel Only
  594. def tunnelHit_or_not(self, hero, tunnel):
  595. hit = pygame.sprite.collide_rect(hero, tunnel)
  596.  
  597. if hit:
  598. self.level = 2
  599.  
  600. # This function works for Tunnel Only
  601. def tunnelHit_or_not_2(self, hero, tunnel):
  602. hit = pygame.sprite.collide_rect(hero, tunnel)
  603.  
  604. if hit:
  605. self.level = 3
  606.  
  607. # This function works for Blocks only
  608. def obstacleHit_or_not(self, hero, platform):
  609.  
  610. for p in platform:
  611. hit = pygame.sprite.collide_rect(hero, p)
  612.  
  613. if hit:
  614. #print("ITS HITTING")
  615. hero.y = p.pos
  616. print("hero.y - ", hero.y)
  617.  
  618. elif hero.y == p.pos:
  619. #print("HITTING INSIDE OF 450")
  620.  
  621. if self.level == 1:
  622. self.landingCords = 625
  623. elif self.level == 2:
  624. self.landingCords = 650
  625. elif self.level == 3:
  626. self.landingCords = 625
  627.  
  628. while True:
  629. hero.y += 5
  630. if hero.y >= self.landingCords:
  631. break
  632.  
  633. # This function works for Coins Only
  634. def coinsHit_or_not(self, hero, thecoin):
  635.  
  636. for c in thecoin:
  637. if not c.coinTouched:
  638. hit = pygame.sprite.collide_rect(hero, c)
  639. if hit:
  640. c.hit()
  641. self.score += 2
  642.  
  643. # This is the function used for detecting bullets that will hit enemy
  644. def bulletCollision(self, enemy):
  645.  
  646. for e in enemy:
  647. for bullet in self.bullets:
  648. if bullet.y - bullet.radius < e.hitbox[1] + e.hitbox[3] and bullet.y + bullet.radius > e.hitbox[1]:
  649. if bullet.x + bullet.radius > e.hitbox[0] and bullet.x - bullet.radius < e.hitbox[0] + e.hitbox[2]:
  650. e.hit()
  651. self.score += 1
  652. self.bullets.pop(self.bullets.index(bullet))
  653.  
  654. # This won't let bullets travel more than screen size i.e 928
  655. if bullet.x < 928 and bullet.x > 0:
  656. bullet.x += bullet.vel
  657. else:
  658. try:
  659. self.bullets.pop(self.bullets.index(bullet))
  660. except ValueError:
  661. print("OUT OF BOUND")
  662.  
  663. # ----------------------------------------------------------------------------------------------------------------------
  664.  
  665. gameOver = False
  666. onTouch = True
  667. run = True
  668.  
  669.  
  670. def runGame(theGame):
  671.  
  672. theGame.clock.tick(FPS)
  673.  
  674. # This function consists code for Events
  675. theGame.events()
  676. # This function consists code from enemy hit events
  677. theGame.hit_or_not()
  678. # This function consists code for player movements
  679. theGame.movements()
  680.  
  681. if theGame.level == 1:
  682.  
  683. # This function consists code for drawing the sprites over the screen
  684. theGame.redrawGameWindow()
  685.  
  686. elif theGame.level == 2:
  687. theGame.redrawGameWindow2()
  688.  
  689. elif theGame.level == 3:
  690. theGame.redrawGameWindow3()
  691.  
  692. def readyGame(run, gameOver):
  693. game = Game(1)
  694. game2 = Game(2)
  695. game3 = Game(3)
  696.  
  697. while run:
  698. if game.level == 1:
  699. runGame(game)
  700.  
  701. keys = pygame.key.get_pressed()
  702.  
  703. if keys[pygame.K_x]:
  704. game = Game(1)
  705. elif keys[pygame.K_ESCAPE]:
  706. run = False
  707. pygame.event.pump()
  708.  
  709. if gameOver:
  710. runGame(game)
  711.  
  712. elif game.level == 2:
  713. runGame(game2)
  714.  
  715. keys = pygame.key.get_pressed()
  716.  
  717. if keys[pygame.K_x]:
  718. game2 = Game(2)
  719. elif keys[pygame.K_ESCAPE]:
  720. run = False
  721. pygame.event.pump()
  722.  
  723. if gameOver:
  724. runGame(game)
  725.  
  726. elif game.level == 3:
  727. runGame(game3)
  728.  
  729. keys = pygame.key.get_pressed()
  730.  
  731. if keys[pygame.K_x]:
  732. game3 = Game(3)
  733. elif keys[pygame.K_ESCAPE]:
  734. run = False
  735. pygame.event.pump()
  736.  
  737. if gameOver:
  738. runGame(game)
  739.  
  740.  
  741. readyGame(True, False)
  742.  
  743. pygame.quit()
Add Comment
Please, Sign In to add comment