Hotstepper

Untitled

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