Hotstepper

Untitled

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