Hotstepper

Untitled

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