Hotstepper

Untitled

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