Hotstepper

Untitled

Jun 29th, 2019
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 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):
  11. super().__init__()
  12. self.clock = pygame.time.Clock()
  13. self.CameraX = 0
  14. self.CameraY = 0
  15.  
  16. self.font = pygame.font.SysFont('comicsans', 30, True, True)
  17. self.score = 0
  18.  
  19. self.noBorderCross = True
  20. self.noCameraCross = True
  21. self.bullets = []
  22.  
  23. self.running = True
  24.  
  25. self.hero = thePlayer(45, 625, 40, 40)
  26.  
  27. self.enemy = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900), enemy(1000, 625, 40, 40, 1500),
  28. enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400), enemy(2300, 625, 40, 40, 2900),
  29. enemy(2700, 625, 40, 40, 3400), enemy(3200, 625, 40, 40, 3900), enemy(3900, 625, 40, 40, 4200),
  30. enemy(3500, 625, 40, 40, 4290)]
  31.  
  32. self.platform = [Platform(200, 550, 460), Platform(600, 549, 459), Platform(990, 450, 360),
  33. Platform(1500, 548, 458), Platform(1900, 449, 359), Platform(2500, 550, 457)]
  34.  
  35. self.facing = 0
  36. self.landed = True
  37. self.enterGame = pygame.image.load('startorclose.png')
  38.  
  39. # Coins for Platform
  40. self.coin = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
  41. Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
  42. Coins(1690, 525), Coins(2550, 525), Coins(2585, 525), Coins(2620, 525), Coins(2655, 525),
  43. Coins(2690, 525)]
  44.  
  45. self.showMenu = pygame.image.load('MainMenu.png')
  46. self.stopScreen = False
  47. self.gameOver = False
  48.  
  49. # ----------------------------------------------------------------------------------------------------------------------
  50.  
  51. def events(self):
  52. for event in pygame.event.get():
  53. if event.type == pygame.QUIT:
  54. self.running = False
  55. pygame.quit()
  56.  
  57. # ----------------------------------------------------------------------------------------------------------------------
  58.  
  59. def hit_or_not(self):
  60. # Checking Collision of Enemy and Hero
  61. self.enemyCollide(self.hero, self.enemy)
  62.  
  63. # ----------------------------------------------------------------------------------------------------------------------
  64.  
  65. # Checking Collision of Platform and Hero
  66. self.obstacleHit_or_not(self.hero, self.platform)
  67.  
  68. # ----------------------------------------------------------------------------------------------------------------------
  69.  
  70. # Checking Collision of Coins and Hero
  71. self.coinsHit_or_not(self.hero, self.coin)
  72.  
  73. # ----------------------------------------------------------------------------------------------------------------------
  74.  
  75. # Statements for colliding bullets with enemies
  76. self.bulletCollision(self.enemy)
  77.  
  78. # ----------------------------------------------------------------------------------------------------------------------
  79.  
  80. def movements(self):
  81. keys = pygame.key.get_pressed()
  82.  
  83. if keys[pygame.K_r]:
  84. if self.hero.left:
  85. self.facing = -1
  86. else:
  87. self.facing = 1
  88.  
  89. if len(self.bullets) < 1:
  90. self.bullets.append(projectile(round(self.hero.x + self.hero.width), round(self.hero.y + self.hero.height), 6, (0, 0, 0), self.facing))
  91.  
  92. if keys[pygame.K_LEFT] and self.hero.x > self.hero.velocity:
  93. self.hero.standing = False
  94.  
  95. self.hero.x -= self.hero.velocity
  96.  
  97. self.hero.left = True
  98. self.hero.right = False
  99.  
  100. self.noBorderCross = True
  101.  
  102. # ----------------------------------------------------------------------------------------------------------------------
  103.  
  104. self.hero.rect.x = self.hero.x
  105. self.hero.rect.y = self.hero.y
  106.  
  107. # ----------------------------------------------------------------------------------------------------------------------
  108.  
  109. self.updatePlatformRect(self.platform)
  110.  
  111. # ----------------------------------------------------------------------------------------------------------------------
  112.  
  113. elif keys[pygame.K_RIGHT] and self.hero.x < BG_WIDTH - self.hero.velocity - self.hero.width:
  114. self.hero.standing = False
  115.  
  116. if self.noBorderCross:
  117. self.hero.x += self.hero.velocity
  118.  
  119. if self.noCameraCross:
  120.  
  121. # Moving Coins
  122. self.updateCoinPos(self.coin)
  123.  
  124. # ----------------------------------------------------------------------------------------------------------------------
  125.  
  126. # Moving Platforms
  127. self.updatePlatformPos(self.platform)
  128.  
  129. # ----------------------------------------------------------------------------------------------------------------------
  130.  
  131. # Moving Camera
  132. self.CameraX += self.hero.velocity
  133.  
  134. # ----------------------------------------------------------------------------------------------------------------------
  135.  
  136. self.updateEnemyPos(self.enemy)
  137.  
  138. # ----------------------------------------------------------------------------------------------------------------------
  139.  
  140. # For moving enemies dead bodies
  141. for e in self.enemy:
  142. if e.enemyDead:
  143. e.x += self.hero.velocity
  144.  
  145. if self.hero.x >= startScrollingPos:
  146. self.noBorderCross = False
  147.  
  148. # 6500 is Camera Position Limit - If increased Camera will behave abnormally
  149. if self.CameraX >= 3700:
  150. self.noCameraCross = False
  151. if self.hero.x < 890 - self.hero.velocity - self.hero.width:
  152. self.hero.x += self.hero.velocity
  153.  
  154. self.hero.left = False
  155. self.hero.right = True
  156.  
  157. # Updating rect of Hero
  158. self.hero.rect.x = self.hero.x
  159. self.hero.rect.y = self.hero.y
  160.  
  161. # ----------------------------------------------------------------------------------------------------------------------
  162.  
  163. # Updating rect of Platform
  164. self.updatePlatformRect(self.platform)
  165.  
  166. # ----------------------------------------------------------------------------------------------------------------------
  167.  
  168. # Updating rect of Coin
  169. self.updateCoinRect(self.coin)
  170.  
  171. # ----------------------------------------------------------------------------------------------------------------------
  172.  
  173. elif keys[pygame.K_r]:
  174. self.hero.shoot = True
  175.  
  176. else:
  177. self.hero.standing = True
  178. self.hero.shoot = False
  179. self.hero.walkCount = 0
  180.  
  181. if not self.hero.isJump:
  182. if keys[pygame.K_SPACE]:
  183. self.hero.isJump = True
  184. self.hero.walkCount = 0
  185. else:
  186. if self.hero.jumpCount >= -10:
  187. self.hero.y -= (self.hero.jumpCount * abs(self.hero.jumpCount)) * 0.5
  188. self.hero.jumpCount -= 1
  189. self.hero.rect.x = self.hero.x
  190. self.hero.rect.y = self.hero.y
  191. else:
  192. self.hero.jumpCount = 10
  193. self.hero.isJump = False
  194. self.hero.rect.x = self.hero.x
  195. self.hero.rect.y = self.hero.y
  196.  
  197. # Method to draw Sprites
  198. # ----------------------------------------------------------------------------------------------------------------------
  199.  
  200. def redrawGameWindow(self):
  201. # This statement continuously keep drawing background image according to the camera value
  202. screen.blit(bg, (0 - self.CameraX, 0 - self.CameraY))
  203.  
  204. text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
  205. screen.blit(text, (390, 10))
  206.  
  207. # Drawing hero and enemies on Screen
  208. self.hero.draw(screen)
  209.  
  210.  
  211. # Drawing Enemies
  212. for e in self.enemy:
  213. e.draw(screen)
  214.  
  215. # Drawing Platforms(Tiles)
  216. for p in self.platform:
  217. p.draw(screen)
  218.  
  219. # Drawing Coins for Platform
  220. for c in self.coin:
  221. c.draw(screen)
  222.  
  223. for bullet in self.bullets:
  224. bullet.draw(screen)
  225.  
  226. pygame.display.flip()
  227.  
  228. # Method to update Rect of Platform and Coins
  229. # ----------------------------------------------------------------------------------------------------------------------
  230.  
  231. def updatePlatformRect(self, platform):
  232. for p in platform:
  233. p.rect.x = p.x
  234. p.rect.y = p.y
  235.  
  236. def updateCoinRect(self, coin):
  237. for c in coin:
  238. c.rect.x = c.x
  239. c.rect.y = c.y
  240.  
  241. #Methods to update position
  242. # ----------------------------------------------------------------------------------------------------------------------
  243.  
  244. def updatePlatformPos(self, platform):
  245. for p in platform:
  246. p.x += -self.hero.velocity
  247.  
  248. def updateCoinPos(self, coin):
  249. for c in coin:
  250. c.x += -self.hero.velocity
  251.  
  252. def updateEnemyPos(self, enemy):
  253. for e in enemy:
  254. e.path[0] -= self.hero.velocity
  255. e.path[1] -= self.hero.velocity
  256.  
  257. # ----------------------------------------------------------------------------------------------------------------------
  258.  
  259. # Methods for Checking Collision
  260. # ----------------------------------------------------------------------------------------------------------------------
  261.  
  262. def enemyCollide(self, hero, enemy):
  263.  
  264. for e in enemy:
  265. # This statement will start reacting when player touches enemy
  266. if hero.hitbox[1] < e.hitbox[1] + e.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > e.hitbox[1]:
  267. if hero.hitbox[0] + hero.hitbox[2] > e.hitbox[0] and hero.hitbox[0] < e.hitbox[0] + e.hitbox[2]:
  268. readyGame(True, True)
  269.  
  270. # This function works for Blocks only
  271. def obstacleHit_or_not(self, hero, platform):
  272.  
  273. for p in platform:
  274. hit = pygame.sprite.collide_rect(hero, p)
  275.  
  276. if hit:
  277. #print("ITS HITTING")
  278. hero.y = p.pos
  279. print("hero.y - ", hero.y)
  280.  
  281. elif hero.y == p.pos:
  282. #print("HITTING INSIDE OF 450")
  283.  
  284. while True:
  285. hero.y += 5
  286. if hero.y >= 625:
  287. break
  288.  
  289. # This function works for Coins Only
  290. def coinsHit_or_not(self, hero, thecoin):
  291.  
  292. for c in thecoin:
  293. if not c.coinTouched:
  294. hit = pygame.sprite.collide_rect(hero, c)
  295. if hit:
  296. c.hit()
  297. self.score += 2
  298.  
  299. # This is the function used for detecting bullets that will hit enemy
  300. def bulletCollision(self, enemy):
  301.  
  302. for e in enemy:
  303. for bullet in self.bullets:
  304. if bullet.y - bullet.radius < e.hitbox[1] + e.hitbox[3] and bullet.y + bullet.radius > e.hitbox[1]:
  305. if bullet.x + bullet.radius > e.hitbox[0] and bullet.x - bullet.radius < e.hitbox[0] + e.hitbox[2]:
  306. e.hit()
  307. self.score += 1
  308. self.bullets.pop(self.bullets.index(bullet))
  309.  
  310. # This won't let bullets travel more than screen size i.e 928
  311. if bullet.x < 928 and bullet.x > 0:
  312. bullet.x += bullet.vel
  313. else:
  314. try:
  315. self.bullets.pop(self.bullets.index(bullet))
  316. except ValueError:
  317. print("OUT OF BOUND")
  318.  
  319. # ----------------------------------------------------------------------------------------------------------------------
  320.  
  321. gameOver = False
  322. onTouch = True
  323. run = True
  324.  
  325.  
  326. def runGame(theGame):
  327.  
  328. theGame.clock.tick(FPS)
  329.  
  330. # This function consists code for Events
  331. theGame.events()
  332. # This function consists code from enemy hit events
  333. theGame.hit_or_not()
  334. # This function consists code for player movements
  335. theGame.movements()
  336. # This function consists code for drawing the sprites over the screen
  337. theGame.redrawGameWindow()
  338.  
  339.  
  340. def readyGame(run, gameOver):
  341. game = Game()
  342.  
  343. while run:
  344. runGame(game)
  345.  
  346. keys = pygame.key.get_pressed()
  347.  
  348. if keys[pygame.K_x]:
  349. game = Game()
  350. elif keys[pygame.K_ESCAPE]:
  351. run = False
  352. pygame.event.pump()
  353.  
  354. if gameOver:
  355. runGame(game)
  356.  
  357.  
  358. readyGame(True, False)
  359.  
  360. pygame.quit()
Add Comment
Please, Sign In to add comment