Advertisement
Isaacmm

Untitled

Jun 2nd, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 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.  
  12. IMAGES = {}
  13.  
  14. PLAYERS_LIST = (
  15. # red bird
  16. (
  17. 'assets/sprites/redbird-upflap.png',
  18. 'assets/sprites/redbird-midflap.png',
  19. 'assets/sprites/redbird-downflap.png'
  20. ),
  21. # blue bird
  22. (
  23. 'assets/sprites/bluebird-upflap.png',
  24. 'assets/sprites/bluebird-midflap.png',
  25. 'assets/sprites/bluebird-downflap.png'
  26. ),
  27. # yellow bird
  28. (
  29. 'assets/sprites/yellowbird-upflap.png',
  30. 'assets/sprites/yellowbird-midflap.png',
  31. 'assets/sprites/yellowbird-downflap.png'
  32. ),
  33. )
  34.  
  35. BACKGROUNDS_LIST = (
  36. 'assets/sprites/background-day.png',
  37. 'assets/sprites/background-night.png'
  38. )
  39.  
  40. PIPES_LIST = (
  41. 'assets/sprites/pipe-green.png',
  42. 'assets/sprites/pipe-red.png'
  43. )
  44.  
  45. def main():
  46. global SCREEN, FPSCLOCK
  47. pygame.init()
  48. FPSCLOCK = pygame.time.Clock()
  49. SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  50. pygame.display.set_caption("Flappy Bird")
  51.  
  52. IMAGES['message'] = pygame.image.load('assets/sprites/message.png').convert_alpha()
  53. IMAGES['base'] = pygame.image.load('assets/sprites/base.png').convert_alpha()
  54.  
  55. while True:
  56. randBg = random.choice(BACKGROUNDS_LIST)
  57. IMAGES['background'] = pygame.image.load(randBg).convert()
  58.  
  59. randPlayer = random.choice(PLAYERS_LIST)
  60. IMAGES['player'] = (
  61. pygame.image.load(randPlayer[0]).convert_alpha(),
  62. pygame.image.load(randPlayer[1]).convert_alpha(),
  63. pygame.image.load(randPlayer[2]).convert_alpha(),
  64. )
  65.  
  66. movementInfo = showWelcomeAnimation()
  67. mainGame(movementInfo)
  68.  
  69.  
  70. def showWelcomeAnimation():
  71. pass
  72. playerIndex = 0
  73. playerIndexGen = cycle([0, 1, 2, 1])
  74. loopIter = 0
  75. baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
  76. playerShmVals = {'val': 0, 'dir': 1}
  77.  
  78. messagex = int((SCREEN_WIDTH - IMAGES['message'].get_width()) / 2)
  79. messagey = int(SCREEN_HEIGHT * 0.12)
  80. basex = 0
  81.  
  82. playerx = int(SCREEN_WIDTH * 0.2)
  83. playery = int((SCREEN_HEIGHT - IMAGES['player'][0].get_height()) / 2)
  84.  
  85. while True:
  86. for event in pygame.event.get():
  87. if event.type == QUIT:
  88. pygame.quit()
  89. sys.exit()
  90. if event.type == KEYDOWN and event.key == K_SPACE:
  91. return {
  92. 'playery': playery + playerShmVals['val'],
  93. 'basex': basex,
  94. 'playerIndexGen': playerIndexGen,
  95. }
  96.  
  97. # adjust playery, playerIndex, basex
  98. if (loopIter + 1) % 5 == 0:
  99. playerIndex = next(playerIndexGen)
  100. loopIter = (loopIter + 1) % 30
  101. basex = -((-basex + 4) % baseShift)
  102. playerShm(playerShmVals)
  103.  
  104. # draw sprites
  105. SCREEN.blit(IMAGES['background'], (0, 0))
  106. SCREEN.blit(IMAGES['message'], (messagex, messagey))
  107. SCREEN.blit(IMAGES['base'], (basex, BASEY))
  108. # SCREEN.blit(IMAGES['player'][0], (playerx, playery))
  109. SCREEN.blit(IMAGES['player'][playerIndex], (playerx, playery + playerShmVals['val']))
  110. pygame.display.update()
  111. FPSCLOCK.tick(FPS)
  112.  
  113. def mainGame(movementInfo):
  114. playerIndex = 0
  115. loopIter = 0
  116. playerIndexGen = movementInfo['playerIndexGen']
  117.  
  118. playerx = int(SCREEN_WIDTH * 0.2)
  119. playery = movementInfo['playery']
  120.  
  121. basex = movementInfo['basex']
  122. baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
  123.  
  124. playerVelY = -9
  125. playerMaxVelY = 10
  126. playerMinVel = -8
  127. playerAccY = 1
  128. playerRot = 45
  129. playerVelRot = 3
  130. playerRotThr = 20
  131. playerFlapAcc = -9
  132. playerFlapped = False
  133.  
  134. while True:
  135. for event in pygame.event.get():
  136. if event.type == QUIT:
  137. pygame.quit()
  138. sys.exit()
  139. if event.type == KEYDOWN and event.key == K_SPACE:
  140. if playery > -2 * IMAGES['player'][0].get_height():
  141. playerVelY = playerFlapAcc
  142. playerFlapped = True
  143.  
  144. # playerIndex change
  145. if (loopIter + 1) % 3 == 0:
  146. playerIndex = next(playerIndexGen)
  147. loopIter = (loopIter + 1) % 30
  148.  
  149. # player movement
  150. if playerVelY < playerMaxVelY and not playerFlapped:
  151. playerVelY += playerAccY
  152. if playerFlapped:
  153. playerFlapped = False
  154.  
  155. playerHeight = IMAGES['player'][playerIndex].get_height()
  156. playery += min(playerVelY, BASEY - playery - playerHeight)
  157.  
  158. # draw sprites
  159. SCREEN.blit(IMAGES['background'], (0, 0))
  160.  
  161. playerSurface = pygame.transform.rotate(IMAGES['player'][playerIndex], 0)
  162. SCREEN.blit(playerSurface, (playerx, playery))
  163. SCREEN.blit(IMAGES['base'], (basex, BASEY))
  164.  
  165. pygame.display.update()
  166. FPSCLOCK.tick(FPS)
  167.  
  168.  
  169. def playerShm(playerShm):
  170. if abs(playerShm['val']) == 8:
  171. playerShm['dir'] *= -1
  172.  
  173. if playerShm['dir'] == 1:
  174. playerShm['val'] += 1
  175. else:
  176. playerShm['val'] -= 1
  177.  
  178. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement