Advertisement
Python_Hunter

Flappy Bird source code

Nov 14th, 2021
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. import pygame, sys, random
  2.  
  3.  
  4. def draw_floor():
  5. screen.blit(floor_surface, (floor_x_pos, 400))
  6. screen.blit(floor_surface, (floor_x_pos + 288, 400))
  7.  
  8.  
  9. def create_pipe():
  10. random_pipe_pos = random.choice(pipe_height)
  11. bottom_pipe = pipe_surface.get_rect(midtop=(350, random_pipe_pos))
  12. top_pipe = pipe_surface.get_rect(midbottom=(350, random_pipe_pos - 120))
  13. return bottom_pipe, top_pipe
  14.  
  15.  
  16. def move_pipes(pipes):
  17. for pipe in pipes:
  18. pipe.centerx -= 2.5
  19. return pipes
  20.  
  21.  
  22. def draw_pipes(pipes):
  23. for pipe in pipes:
  24. if pipe.bottom >= 512:
  25. screen.blit(pipe_surface, pipe)
  26.  
  27.  
  28. def check_collision(pipes):
  29. for pipe in pipes:
  30. if bird_rect.colliderect(pipe):
  31. death_sound.play()
  32. return False
  33. else:
  34. # 2 boolean values for flip function: 1st is if x flip and 2nd is if y flip (boolean)
  35. flip_pipe = pygame.transform.flip(pipe_surface, False, True)
  36. screen.blit(flip_pipe, pipe)
  37.  
  38. if bird_rect.top <= -50 or bird_rect.bottom >= 400:
  39. return False
  40. return True
  41.  
  42.  
  43. def rotate_bird(bird):
  44. new_bird = pygame.transform.rotozoom(bird, -bird_movement * 7, 1)
  45. return new_bird
  46.  
  47.  
  48. def bird_animation():
  49. new_bird = bird_frames[bird_index]
  50. new_bird_rect = new_bird.get_rect(center=(50, bird_rect.centery))
  51. return new_bird, new_bird_rect
  52.  
  53.  
  54. def score_display(game_state):
  55. if game_state == 'main_game':
  56. score_surface = game_font.render(str(int(score)), True, (0, 0, 255))
  57. score_rect = score_surface.get_rect(center=(144, 50))
  58. screen.blit(score_surface, score_rect)
  59.  
  60. if game_state == 'game over':
  61. score_surface = game_font.render(f'Score: {int(score)}', True, (0, 0, 255))
  62. score_rect = score_surface.get_rect(center=(144, 50))
  63. screen.blit(score_surface, score_rect)
  64.  
  65. high_score_surface = game_font.render(f'High Score: {int(high_score)}', True, (0, 0, 255))
  66. high_score_rect = high_score_surface.get_rect(center=(144, 350))
  67. screen.blit(high_score_surface, high_score_rect)
  68.  
  69.  
  70. def update_score(score, high_score):
  71. if score > high_score:
  72. high_score = score
  73. return high_score
  74.  
  75.  
  76. pygame.mixer.pre_init(frequency=44100, size=16, channels=1, buffer=1024)
  77. pygame.init()
  78. screen = pygame.display.set_mode((288, 512)) # half of this (x,y) is 288,512; double is 576,1024
  79. game_font = pygame.font.Font('freesansbold.ttf', 30)
  80. # Important game variables
  81. # we will add gravity to bird movement every frame
  82. # and this bird movement will move the bird rectangle down
  83. # if bird rectangle goes down, then we put bird_surface on different position
  84.  
  85.  
  86. # game variables
  87. gravity = 0.125
  88. bird_movement = 0
  89. game_active = True
  90. score = 0
  91. high_score = 0
  92.  
  93. bg_surface = pygame.image.load('Images/background-day.png').convert()
  94. # bg_surface = pygame.transform.scale2x(bg_surface) #(if screen is 576,1024)
  95. floor_surface = pygame.image.load('Images/base.png')
  96. # floor_surface = pygame.transform.scale2x(floor_surface)
  97. FPS = pygame.time.Clock()
  98. floor_x_pos = 0
  99.  
  100. # animation
  101. bird_downflap = pygame.image.load('Images/yellowbird-downflap.png').convert_alpha()
  102. bird_midflap = pygame.image.load('Images/yellowbird-midflap.png').convert_alpha()
  103. bird_upflap = pygame.image.load('Images/yellowbird-upflap.png').convert_alpha()
  104. bird_frames = [bird_downflap, bird_midflap, bird_upflap]
  105. bird_index = 0
  106. bird_surface = bird_frames[bird_index]
  107. bird_rect = bird_surface.get_rect(center=(50, 256))
  108. # to create a new USEREVENT, add +1,+2 etc
  109. BIRDFLAP = pygame.USEREVENT + 1
  110. pygame.time.set_timer(BIRDFLAP, 200)
  111.  
  112. # bird_surface = pygame.image.load('Images/yellowbird-midflap.png').convert_alpha()
  113. # using rect (rectangle) function center = x,y is coordinate of center of rectangle of bird
  114. # bird_rect = bird_surface.get_rect(center=(50, 256))
  115.  
  116. pipe_surface = pygame.image.load('Images/pipe-red.png').convert()
  117. pipe_list = []
  118.  
  119. # how to use timer: 1200 is always in milliseconds. 1 sec = 1000 milliseconds
  120. SPAWNPIPE = pygame.USEREVENT
  121. pygame.time.set_timer(SPAWNPIPE, 1200)
  122. pipe_height = [200, 250, 300, 350]
  123.  
  124. game_over_surface = pygame.image.load('Images/message.png').convert_alpha()
  125. game_over_rect = game_over_surface.get_rect(center=(144, 256))
  126.  
  127. # sounds
  128. flap_sound = pygame.mixer.Sound('Sounds/sfx_wing.wav')
  129. death_sound = pygame.mixer.Sound('Sounds/sfx_hit.wav')
  130. score_sound = pygame.mixer.Sound('Sounds/sfx_point.wav')
  131. score_sound_countdown = 100
  132.  
  133. while True:
  134. for event in pygame.event.get():
  135. if event.type == pygame.QUIT:
  136. pygame.quit()
  137. sys.exit()
  138.  
  139. if event.type == pygame.KEYDOWN:
  140. if event.key == pygame.K_SPACE and game_active:
  141. bird_movement = 0
  142. bird_movement -= 4.1
  143. flap_sound.play()
  144.  
  145. if event.key == pygame.K_SPACE and game_active == False:
  146. game_active = True
  147. pipe_list.clear()
  148. bird_rect.center = (50, 170)
  149. bird_movement = 0
  150. score = 0
  151.  
  152. # for spawning pipes
  153. if event.type == SPAWNPIPE:
  154. # extend function adds to a tuple
  155. pipe_list.extend(create_pipe())
  156.  
  157. # for bird animation
  158. if event.type == BIRDFLAP:
  159. if bird_index < 2:
  160. bird_index += 1
  161. else:
  162. bird_index = 0
  163.  
  164. bird_surface, bird_rect = bird_animation()
  165.  
  166. screen.blit(bg_surface, (0, 0))
  167.  
  168. if game_active:
  169. # bird
  170. bird_movement += gravity
  171. bird_rect.centery += bird_movement
  172.  
  173. rotated_bird = rotate_bird(bird_surface)
  174.  
  175. screen.blit(rotated_bird, bird_rect)
  176. game_active = check_collision(pipe_list)
  177.  
  178. # pipe movement
  179. pipe_list = move_pipes(pipe_list)
  180. draw_pipes(pipe_list)
  181.  
  182. # show score
  183. score += 0.01
  184. score_display('main_game')
  185. #score_sound_countdown -= 1
  186. #if score_sound_countdown <= 0:
  187. # score_sound.play()
  188. # score_sound_countdown = 100
  189.  
  190. else:
  191. screen.blit(game_over_surface, game_over_rect)
  192. high_score = update_score(score, high_score)
  193. score_display('game_over')
  194.  
  195. # floor
  196. floor_x_pos -= 1
  197.  
  198. draw_floor()
  199.  
  200. if floor_x_pos <= -288:
  201. floor_x_pos = 0
  202.  
  203. pygame.display.update()
  204.  
  205. FPS.tick(120)
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement