Facebook
From QwQ-32B, 3 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 248
  1. import pygame
  2. import random                                    
  3. import sys                                                                                                              
  4.                                                            
  5. pygame.init()                                                                                                          
  6. screen_width = 600                                                                                                      
  7. screen_height = 800                                    
  8. screen = pygame.display.set_mode((screen_width, screen_height))
  9. pygame.display.set_caption("Flappy Bird")                                                                              
  10.                                                            
  11. light_colors = [(135, 206, 250), (144, 238, 144), (255, 255, 224)]
  12. dark_colors = [(0, 0, 0), (0, 0, 139), (64, 64, 64)]
  13. pipe_colors = [(0, 128, 0), (210, 180, 140), (105, 105, 105)]
  14. land_colors = [(139, 69, 19), (255, 255, 0)]                                                                            
  15.                                                            
  16. class Bird:                                                                                                            
  17.     def __init__(self):                      
  18.         self.shape = random.choice(['square', 'circle', 'triangle'])
  19.         self.color = random.choice(dark_colors)
  20.         self.x = screen_width // 4              
  21.         self.y = screen_height // 2                
  22.         self.size = 50                                                                                                  
  23.         self.rect = pygame.Rect(self.x, self.y, self.size, self.size)
  24.         self.vel = 0                                                                                                    
  25.         self.gravity = 0.4                
  26.         self.jump_strength = -7  
  27.                                                            
  28.     def jump(self):                                                                                                    
  29.         self.vel += self.jump_strength
  30.  
  31.     def update(self):                    
  32.         self.vel += self.gravity
  33.         self.y += self.vel                                                                                              
  34.         self.rect.topleft = (self.x, self.y)                                                                            
  35.                                                            
  36.     def draw(self, screen):                                
  37.         if self.shape == 'square':                                                                                      
  38.             pygame.draw.rect(screen, self.color, self.rect)                                                            
  39.         elif self.shape == 'circle':
  40.             center = (self.x + self.size // 2, self.y + self.size // 2)
  41.             radius = self.size // 2
  42.             pygame.draw.circle(screen, self.color, center, radius)
  43.         elif self.shape == 'triangle':                                                                                  
  44.             points = [                                                                                                  
  45.                 (self.x + self.size // 2, self.y),                                                                      
  46.                 (self.x, self.y + self.size),                                                                          
  47.                 (self.x + self.size, self.y + self.size)
  48.             ]                    
  49.             pygame.draw.polygon(screen, self.color, points)
  50.  
  51. def main():                                                                                                            
  52.     bird = Bird()                                                                                                      
  53.     pipes = []                    
  54.     score = 0              
  55.     best_score = 0
  56.     background_color = random.choice(light_colors)
  57.     land_color = random.choice(land_colors)
  58.     land_height = 100
  59.     land_rect = pygame.Rect(0, screen_height - land_height, screen_width, land_height)
  60.     clock = pygame.time.Clock()
  61.     FPS = 60                                                                                                                                                                                                                                    
  62.     font = pygame.font.Font(None, 36)                    
  63.     game_over = False                                                                                                                                                                                                                          
  64.     last_pipe_time = pygame.time.get_ticks()                                                                                                                                                                                                    
  65.     pipe_speed = 4                                                                                                      
  66.     pipe_width = 50                    
  67.     min_pipe_height = 50
  68.  
  69.     while True:
  70.         for event in pygame.event.get():
  71.             if event.type == pygame.QUIT:
  72.                 pygame.quit()
  73.                 sys.exit()
  74.             if not game_over:
  75.                 if event.type == pygame.KEYDOWN:
  76.                     if event.key == pygame.K_SPACE:
  77.                         bird.jump()
  78.             else:
  79.                 if event.type == pygame.KEYDOWN:
  80.                     if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
  81.                         pygame.quit()
  82.                         sys.exit()
  83.                     if event.key == pygame.K_SPACE:
  84.                         game_over = False
  85.                         bird = Bird()
  86.                         pipes.clear()
  87.                         score = 0
  88.                         land_color = random.choice(land_colors)
  89.                         background_color = random.choice(light_colors)
  90.                         land_rect = pygame.Rect(0, screen_height - land_height, screen_width, land_height)
  91.                         last_pipe_time = pygame.time.get_ticks()
  92.  
  93.         if not game_over:
  94.             bird.update()
  95.             if bird.rect.bottom >= land_rect.top or bird.rect.top <= 0:
  96.                 game_over = True
  97.  
  98.             current_time = pygame.time.get_ticks()
  99.             if current_time - last_pipe_time > random.randint(1200, 2400):
  100.                 pipe_gap = random.randint(150, 300)
  101.                 top_pipe_max = (screen_height - land_height - min_pipe_height) - pipe_gap
  102.                 top_pipe_height = random.randint(min_pipe_height, top_pipe_max)
  103.                 pipe_color = random.choice(pipe_colors)
  104.                 pipe_x = screen_width
  105.                 top_rect = pygame.Rect(pipe_x, 0, pipe_width, top_pipe_height)
  106.                 bottom_rect = pygame.Rect(
  107.                     pipe_x,
  108.                     top_pipe_height + pipe_gap,
  109.                     pipe_width,
  110.                     (screen_height - land_height) - (top_pipe_height + pipe_gap)
  111.                 )
  112.                 pipes.append({'top_rect': top_rect, 'bottom_rect': bottom_rect, 'color': pipe_color, 'passed': False})
  113.                 last_pipe_time = current_time
  114.  
  115.             for pipe in pipes.copy():
  116.                 pipe['top_rect'].x -= pipe_speed
  117.                 pipe['bottom_rect'].x -= pipe_speed
  118.                 if bird.rect.colliderect(pipe['top_rect']) or bird.rect.colliderect(pipe['bottom_rect']):
  119.                     game_over = True
  120.                 if pipe['top_rect'].x + pipe['top_rect'].width < bird.x:
  121.                     if not pipe['passed']:
  122.                         score += 1
  123.                         pipe['passed'] = True
  124.                 if pipe['top_rect'].x < -pipe['top_rect'].width:
  125.                     pipes.remove(pipe)
  126.  
  127.             screen.fill(background_color)
  128.             for pipe in pipes:
  129.                 pygame.draw.rect(screen, pipe['color'], pipe['top_rect'])
  130.                 pygame.draw.rect(screen, pipe['color'], pipe['bottom_rect'])
  131.             bird.draw(screen)
  132.             pygame.draw.rect(screen, land_color, land_rect)
  133.             score_text = font.render(f"Score: {score}", True, (0, 0, 0))
  134.             screen.blit(score_text, (screen_width - score_text.get_width() - 10, 10))
  135.             pygame.display.update()
  136.             clock.tick(FPS)
  137.         else:
  138.             screen.fill(background_color)
  139.             game_over_text = font.render("Game Over!", True, (255, 0, 0))
  140.             screen.blit(game_over_text, (screen_width//2 - 80, screen_height//2 - 50))
  141.             current_score = font.render(f"Current Score: {score}", True, (0, 0, 0))
  142.             screen.blit(current_score, (screen_width//2 - 100, screen_height//2))
  143.             if score > best_score:
  144.                 best_score = score
  145.             best_score_text = font.render(f"Best Score: {best_score}", True, (0, 0, 255))
  146.             screen.blit(best_score_text, (screen_width//2 - 100, screen_height//2 + 50))
  147.             restart_text = font.render("Press SPACE to restart, Q/ESC to quit", True, (255, 255, 255))
  148.             screen.blit(restart_text, (screen_width//2 - 200, screen_height//2 + 100))
  149.             pygame.display.update()
  150.             clock.tick(FPS)
  151.  
  152. if __name__ == "__main__":
  153.     main()
  154.