Okonar

flappy

Oct 14th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. import pygame
  2. from sys import exit
  3. import random
  4.  
  5. pygame.init()
  6. clock = pygame.time.Clock()
  7.  
  8. # Window
  9. win_height = 720
  10. win_width = 551
  11. window = pygame.display.set_mode((win_width, win_height))
  12.  
  13. # Images
  14. bird_images = [pygame.image.load("bird_down.png"),
  15. pygame.image.load("bird_mid.png"),
  16. pygame.image.load("bird_up.png")]
  17. skyline_image = pygame.image.load("background.png")
  18. ground_image = pygame.image.load("ground.png")
  19. top_pipe_image = pygame.image.load("pipe_top.png")
  20. bottom_pipe_image = pygame.image.load("pipe_bottom.png")
  21. game_over_image = pygame.image.load("game_over.png")
  22. start_image = pygame.image.load("start.png")
  23.  
  24. # Game
  25. scroll_speed = 1
  26. bird_start_position = (100, 250)
  27. score = 0
  28. font = pygame.font.SysFont('Segoe', 26)
  29. game_stopped = True
  30.  
  31.  
  32. class Bird(pygame.sprite.Sprite):
  33. def __init__(self):
  34. pygame.sprite.Sprite.__init__(self)
  35. self.image = bird_images[0]
  36. self.rect = self.image.get_rect()
  37. self.rect.center = bird_start_position
  38. self.image_index = 0
  39. self.vel = 0
  40. self.flap = False
  41. self.alive = True
  42.  
  43. def update(self, user_input):
  44. # Animate Bird
  45. if self.alive:
  46. self.image_index += 1
  47. if self.image_index >= 30:
  48. self.image_index = 0
  49. self.image = bird_images[self.image_index // 10]
  50.  
  51. # Gravity and Flap
  52. self.vel += 0.5
  53. if self.vel > 7:
  54. self.vel = 7
  55. if self.rect.y < 500:
  56. self.rect.y += int(self.vel)
  57. if self.vel == 0:
  58. self.flap = False
  59.  
  60. # Rotate Bird
  61. self.image = pygame.transform.rotate(self.image, self.vel * -7)
  62.  
  63. # User Input
  64. if user_input[pygame.K_SPACE] and not self.flap and self.rect.y > 0 and self.alive:
  65. self.flap = True
  66. self.vel = -7
  67.  
  68.  
  69. class Pipe(pygame.sprite.Sprite):
  70. def __init__(self, x, y, image, pipe_type):
  71. pygame.sprite.Sprite.__init__(self)
  72. self.image = image
  73. self.rect = self.image.get_rect()
  74. self.rect.x, self.rect.y = x, y
  75. self.enter, self.exit, self.passed = False, False, False
  76. self.pipe_type = pipe_type
  77.  
  78. def update(self):
  79. # Move Pipe
  80. self.rect.x -= scroll_speed
  81. if self.rect.x <= -win_width:
  82. self.kill()
  83.  
  84. # Score
  85. global score
  86. if self.pipe_type == 'bottom':
  87. if bird_start_position[0] > self.rect.topleft[0] and not self.passed:
  88. self.enter = True
  89. if bird_start_position[0] > self.rect.topright[0] and not self.passed:
  90. self.exit = True
  91. if self.enter and self.exit and not self.passed:
  92. self.passed = True
  93. score += 1
  94.  
  95.  
  96. class Ground(pygame.sprite.Sprite):
  97. def __init__(self, x, y):
  98. pygame.sprite.Sprite.__init__(self)
  99. self.image = ground_image
  100. self.rect = self.image.get_rect()
  101. self.rect.x, self.rect.y = x, y
  102.  
  103. def update(self):
  104. # Move Ground
  105. self.rect.x -= scroll_speed
  106. if self.rect.x <= -win_width:
  107. self.kill()
  108.  
  109.  
  110. def quit_game():
  111. # Exit Game
  112. for event in pygame.event.get():
  113. if event.type == pygame.QUIT:
  114. pygame.quit()
  115. exit()
  116.  
  117.  
  118. # Game Main Method
  119. def main():
  120. global score
  121.  
  122. # Instantiate Bird
  123. bird = pygame.sprite.GroupSingle()
  124. bird.add(Bird())
  125.  
  126. # Setup Pipes
  127. pipe_timer = 0
  128. pipes = pygame.sprite.Group()
  129.  
  130. # Instantiate Initial Ground
  131. x_pos_ground, y_pos_ground = 0, 520
  132. ground = pygame.sprite.Group()
  133. ground.add(Ground(x_pos_ground, y_pos_ground))
  134.  
  135. run = True
  136. while run:
  137. # Quit
  138. quit_game()
  139.  
  140. # Reset Frame
  141. window.fill((0, 0, 0))
  142.  
  143. # User Input
  144. user_input = pygame.key.get_pressed()
  145.  
  146. # Draw Background
  147. window.blit(skyline_image, (0, 0))
  148.  
  149. # Spawn Ground
  150. if len(ground) <= 2:
  151. ground.add(Ground(win_width, y_pos_ground))
  152.  
  153. # Draw - Pipes, Ground and Bird
  154. pipes.draw(window)
  155. ground.draw(window)
  156. bird.draw(window)
  157.  
  158. # Show Score
  159. score_text = font.render('Score: ' + str(score), True, pygame.Color(255, 255, 255))
  160. window.blit(score_text, (20, 20))
  161.  
  162. # Update - Pipes, Ground and Bird
  163. if bird.sprite.alive:
  164. pipes.update()
  165. ground.update()
  166. bird.update(user_input)
  167.  
  168. # Collision Detection
  169. collision_pipes = pygame.sprite.spritecollide(bird.sprites()[0], pipes, False)
  170. collision_ground = pygame.sprite.spritecollide(bird.sprites()[0], ground, False)
  171. if collision_pipes or collision_ground:
  172. bird.sprite.alive = False
  173. if collision_ground:
  174. window.blit(game_over_image, (win_width // 2 - game_over_image.get_width() // 2,
  175. win_height // 2 - game_over_image.get_height() // 2))
  176. if user_input[pygame.K_r]:
  177. score = 0
  178. break
  179.  
  180. # Spawn Pipes
  181. if pipe_timer <= 0 and bird.sprite.alive:
  182. x_top, x_bottom = 550, 550
  183. y_top = random.randint(-600, -480)
  184. y_bottom = y_top + random.randint(90, 130) + bottom_pipe_image.get_height()
  185. pipes.add(Pipe(x_top, y_top, top_pipe_image, 'top'))
  186. pipes.add(Pipe(x_bottom, y_bottom, bottom_pipe_image, 'bottom'))
  187. pipe_timer = random.randint(180, 250)
  188. pipe_timer -= 1
  189.  
  190. clock.tick(60)
  191. pygame.display.update()
  192.  
  193.  
  194. # Menu
  195. def menu():
  196. global game_stopped
  197.  
  198. while game_stopped:
  199. quit_game()
  200.  
  201. # Draw Menu
  202. window.fill((0, 0, 0))
  203. window.blit(skyline_image, (0, 0))
  204. window.blit(ground_image, Ground(0, 520))
  205. window.blit(bird_images[0], (100, 250))
  206. window.blit(start_image, (win_width // 2 - start_image.get_width() // 2,
  207. win_height // 2 - start_image.get_height() // 2))
  208.  
  209. # User Input
  210. user_input = pygame.key.get_pressed()
  211. if user_input[pygame.K_SPACE]:
  212. main()
  213.  
  214. pygame.display.update()
  215.  
  216.  
  217. menu()
Advertisement
Add Comment
Please, Sign In to add comment