Advertisement
Isaacmm

Untitled

Jun 9th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. import sys
  2. import pygame
  3. import random
  4. from pygame.locals import *
  5. from itertools import cycle
  6.  
  7. FPS = 30
  8. SCREEN_WIDTH = 288
  9. SCREEN_HEIGHT = 512
  10. BASEY = SCREEN_HEIGHT * 0.8
  11. PIPEGAPSIZE = 150
  12.  
  13. IMAGES = {}
  14.  
  15. PLAYERS_LIST = (
  16. # red bird
  17. (
  18. 'assets/sprites/redbird-upflap.png',
  19. 'assets/sprites/redbird-midflap.png',
  20. 'assets/sprites/redbird-downflap.png'
  21. ),
  22. # blue bird
  23. (
  24. 'assets/sprites/bluebird-upflap.png',
  25. 'assets/sprites/bluebird-midflap.png',
  26. 'assets/sprites/bluebird-downflap.png'
  27. ),
  28. # yellow bird
  29. (
  30. 'assets/sprites/yellowbird-upflap.png',
  31. 'assets/sprites/yellowbird-midflap.png',
  32. 'assets/sprites/yellowbird-downflap.png'
  33. ),
  34. )
  35.  
  36. BACKGROUNDS_LIST = (
  37. 'assets/sprites/background-day.png',
  38. 'assets/sprites/background-night.png'
  39. )
  40.  
  41. PIPES_LIST = (
  42. 'assets/sprites/pipe-green.png',
  43. 'assets/sprites/pipe-red.png'
  44. )
  45.  
  46. def main():
  47. global SCREEN, FPSCLOCK
  48. pygame.init()
  49. FPSCLOCK = pygame.time.Clock()
  50. SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  51. pygame.display.set_caption("Flappy Bird")
  52.  
  53. IMAGES['message'] = pygame.image.load('assets/sprites/message.png').convert_alpha()
  54. IMAGES['base'] = pygame.image.load('assets/sprites/base.png').convert_alpha()
  55.  
  56. while True:
  57. randBg = random.choice(BACKGROUNDS_LIST)
  58. IMAGES['background'] = pygame.image.load(randBg).convert()
  59.  
  60. randPlayer = random.choice(PLAYERS_LIST)
  61. IMAGES['player'] = (
  62. pygame.image.load(randPlayer[0]).convert_alpha(),
  63. pygame.image.load(randPlayer[1]).convert_alpha(),
  64. pygame.image.load(randPlayer[2]).convert_alpha(),
  65. )
  66.  
  67. randPipe = random.choice(PIPES_LIST)
  68. IMAGES['pipe'] = (
  69. pygame.transform.flip(pygame.image.load(randPipe).convert_alpha(), False, True),
  70. pygame.image.load(randPipe).convert_alpha()
  71. )
  72.  
  73. movementInfo = showWelcomeAnimation()
  74. mainGame(movementInfo)
  75.  
  76.  
  77. def showWelcomeAnimation():
  78. pass
  79. playerIndex = 0
  80. playerIndexGen = cycle([0, 1, 2, 1])
  81. loopIter = 0
  82. baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
  83. playerShmVals = {'val': 0, 'dir': 1}
  84.  
  85. messagex = int((SCREEN_WIDTH - IMAGES['message'].get_width()) / 2)
  86. messagey = int(SCREEN_HEIGHT * 0.12)
  87. basex = 0
  88.  
  89. playerx = int(SCREEN_WIDTH * 0.2)
  90. playery = int((SCREEN_HEIGHT - IMAGES['player'][0].get_height()) / 2)
  91.  
  92. while True:
  93. for event in pygame.event.get():
  94. if event.type == QUIT:
  95. pygame.quit()
  96. sys.exit()
  97. if event.type == KEYDOWN and event.key == K_SPACE:
  98. return {
  99. 'playery': playery + playerShmVals['val'],
  100. 'basex': basex,
  101. 'playerIndexGen': playerIndexGen,
  102. }
  103.  
  104. # adjust playery, playerIndex, basex
  105. if (loopIter + 1) % 5 == 0:
  106. playerIndex = next(playerIndexGen)
  107. loopIter = (loopIter + 1) % 30
  108. basex = -((-basex + 4) % baseShift)
  109. playerShm(playerShmVals)
  110.  
  111. # draw sprites
  112. SCREEN.blit(IMAGES['background'], (0, 0))
  113. SCREEN.blit(IMAGES['message'], (messagex, messagey))
  114. SCREEN.blit(IMAGES['base'], (basex, BASEY))
  115. # SCREEN.blit(IMAGES['player'][0], (playerx, playery))
  116. SCREEN.blit(IMAGES['player'][playerIndex], (playerx, playery + playerShmVals['val']))
  117. pygame.display.update()
  118. FPSCLOCK.tick(FPS)
  119.  
  120. def mainGame(movementInfo):
  121. playerIndex = 0
  122. loopIter = 0
  123. playerIndexGen = movementInfo['playerIndexGen']
  124.  
  125. playerx = int(SCREEN_WIDTH * 0.2)
  126. playery = movementInfo['playery']
  127.  
  128. basex = movementInfo['basex']
  129. baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
  130.  
  131. # get 2 pipes
  132. newPipe1 = getRandomPipe()
  133. newPipe2 = getRandomPipe()
  134.  
  135. upperPipes = [
  136. {'x': SCREEN_WIDTH + 200, 'y': newPipe1[0]['y']},
  137. {'x': SCREEN_WIDTH + 200 + (SCREEN_WIDTH / 2), 'y': newPipe2[0]['y']}
  138. ]
  139.  
  140. lowerPipes = [
  141. {'x': SCREEN_WIDTH + 200, 'y': newPipe1[1]['y']},
  142. {'x': SCREEN_WIDTH + 200 + (SCREEN_WIDTH / 2), 'y': newPipe2[1]['y']},
  143. ]
  144.  
  145. dt = FPSCLOCK.tick(FPS)/1000
  146. pipeVelX = -128 * dt
  147.  
  148. playerVelY = -9
  149. playerMaxVelY = 10
  150. playerMinVel = -8
  151. playerAccY = 1
  152. playerRot = 45
  153. playerVelRot = 3
  154. playerRotThr = 20
  155. playerFlapAcc = -9
  156. playerFlapped = False
  157.  
  158. while True:
  159. for event in pygame.event.get():
  160. if event.type == QUIT:
  161. pygame.quit()
  162. sys.exit()
  163. if event.type == KEYDOWN and event.key == K_SPACE:
  164. if playery > -2 * IMAGES['player'][0].get_height():
  165. playerVelY = playerFlapAcc
  166. playerFlapped = True
  167.  
  168. playerSurface = pygame.transform.rotate(IMAGES['player'][playerIndex], 0)
  169. crashTest = checkCrash(playerSurface, playerx, playery, IMAGES['pipe'][0], (upperPipes + lowerPipes))
  170. if crashTest is True:
  171. sys.exit()
  172.  
  173. # playerIndex change
  174. if (loopIter + 1) % 3 == 0:
  175. playerIndex = next(playerIndexGen)
  176. loopIter = (loopIter + 1) % 30
  177.  
  178. # player movement
  179. if playerVelY < playerMaxVelY and not playerFlapped:
  180. playerVelY += playerAccY
  181. if playerFlapped:
  182. playerFlapped = False
  183.  
  184. playerHeight = IMAGES['player'][playerIndex].get_height()
  185. playery += min(playerVelY, BASEY - playery - playerHeight)
  186.  
  187. # moves pipes to left
  188. for uPipe, lPipe in zip(upperPipes, lowerPipes):
  189. uPipe["x"] += pipeVelX
  190. lPipe["x"] += pipeVelX
  191.  
  192. if 3 > len(upperPipes) > 0 and 0 < upperPipes[0]["x"] < 5:
  193. newPipe = getRandomPipe()
  194. upperPipes.append(newPipe[0])
  195. lowerPipes.append(newPipe[1])
  196.  
  197. if len(upperPipes) > 0 and upperPipes[0]["x"] < -IMAGES['pipe'][0].get_width():
  198. upperPipes.pop(0)
  199. lowerPipes.pop(0)
  200.  
  201. # draw sprites
  202. SCREEN.blit(IMAGES['background'], (0, 0))
  203.  
  204. for uPipe, lPipe in zip(upperPipes, lowerPipes):
  205. SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))
  206. SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))
  207.  
  208. SCREEN.blit(playerSurface, (playerx, playery))
  209. SCREEN.blit(IMAGES['base'], (basex, BASEY))
  210.  
  211. pygame.display.update()
  212. FPSCLOCK.tick(FPS)
  213.  
  214. def playerShm(playerShm):
  215. if abs(playerShm['val']) == 8:
  216. playerShm['dir'] *= -1
  217.  
  218. if playerShm['dir'] == 1:
  219. playerShm['val'] += 1
  220. else:
  221. playerShm['val'] -= 1
  222.  
  223. def getRandomPipe():
  224. gapY = random.randrange(0, int(BASEY * 0.6 - PIPEGAPSIZE))
  225. gapY += int(BASEY * 0.2)
  226. pipeHeight = IMAGES['pipe'][0].get_height()
  227. pipeX = SCREEN_WIDTH + 10
  228.  
  229. return [
  230. {'x': pipeX, 'y': gapY - pipeHeight}, # upper pipe
  231. {'x': pipeX, 'y': gapY + PIPEGAPSIZE} # lower pipe
  232. ]
  233.  
  234. def checkCrash(player, playerx, playery, pipeimage, pipes):
  235. playerRect = {"x": playerx, "y": playery, "w": player.get_width(), "h": player.get_height()}
  236. for pipe in pipes:
  237. pipe.update({"w": pipeimage.get_width(), "h": pipeimage.get_height()})
  238.  
  239. for pipe in pipes:
  240. if (
  241. playerRect["x"] + playerRect["w"] >= pipe["x"] and
  242. pipe["x"] + pipe["w"] >= playerRect["x"] and
  243. playerRect["y"] + playerRect["h"] >= pipe["y"] and
  244. pipe["y"] + pipe["h"] >= playerRect["y"]
  245. ):
  246. return True # collision has occured
  247. return False # no collision
  248.  
  249.  
  250. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement