Hotstepper

Untitled

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