Advertisement
Guest User

Untitled

a guest
Mar 5th, 2025
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | Gaming | 0 0
  1. import pygame
  2. import random
  3.  
  4. # Initialize Pygame
  5. pygame.init()
  6.  
  7. # Game constants
  8. SCREEN_WIDTH = 288
  9. SCREEN_HEIGHT = 512
  10. FPS = 30
  11. BACKGROUND_COLOR = (135, 206, 250)  # Light blue
  12. GROUND_COLOR = (0, 128, 0)         # Green
  13. BIRD_COLOR = (255, 255, 0)         # Yellow
  14. GROUND_HEIGHT = 50
  15.  
  16. class Bird:
  17.     def __init__(self):
  18.         self.x = int(SCREEN_WIDTH * 0.2)  # Start position
  19.         self.y = SCREEN_HEIGHT // 2
  20.         self.velocity = 0
  21.         self.gravity = 0.3
  22.         self.radius = 15
  23.  
  24.     def jump(self):
  25.         self.velocity = -7  # Upward velocity
  26.  
  27.     def update(self):
  28.         self.velocity += self.gravity
  29.         self.y += self.velocity
  30.  
  31.     def draw(self, screen):
  32.         pygame.draw.circle(screen, BIRD_COLOR, (self.x, int(self.y)), self.radius)
  33.  
  34.     def get_rect(self):
  35.         return pygame.Rect(
  36.             self.x - self.radius,
  37.             self.y - self.radius,
  38.             self.radius * 2,
  39.             self.radius * 2
  40.         )
  41.  
  42. class Pipe:
  43.     def __init__(self, x):
  44.         self.x = x
  45.         self.top_height = random.randint(50, 350)
  46.         self.gap = 150  # Gap between pipes
  47.         self.bottom_y = self.top_height + self.gap
  48.         self.passed = False
  49.  
  50.     def move(self, speed):
  51.         self.x -= speed
  52.  
  53.     def draw(self, screen):
  54.         # Draw top pipe
  55.         pygame.draw.rect(screen, GROUND_COLOR, (self.x, 0, 50, self.top_height))
  56.         # Draw bottom pipe
  57.         pygame.draw.rect(screen, GROUND_COLOR,
  58.                         (self.x, self.bottom_y, 50, SCREEN_HEIGHT - self.bottom_y))
  59.  
  60.     def get_top_rect(self):
  61.         return pygame.Rect(self.x, 0, 50, self.top_height)
  62.  
  63.     def get_bottom_rect(self):
  64.         return pygame.Rect(self.x, self.bottom_y, 50, SCREEN_HEIGHT - self.bottom_y)
  65.  
  66. def main():
  67.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  68.     pygame.display.set_caption("Flappy Bird Clone")
  69.     clock = pygame.time.Clock()
  70.  
  71.     bird = Bird()
  72.     pipes = []
  73.     pipe_speed = 3
  74.     pipe_add_interval = 70  # Add new pipe every x frames
  75.     pipe_count = 0
  76.     score = 0
  77.     font = pygame.font.Font(None, 36)
  78.     ground_y = SCREEN_HEIGHT - GROUND_HEIGHT
  79.  
  80.     running = True
  81.     game_over = False
  82.  
  83.     while running:
  84.         screen.fill(BACKGROUND_COLOR)
  85.         pygame.draw.rect(screen, GROUND_COLOR,
  86.                         (0, ground_y, SCREEN_WIDTH, GROUND_HEIGHT))
  87.  
  88.         for event in pygame.event.get():
  89.             if event.type == pygame.QUIT:
  90.                 running = False
  91.             if not game_over and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  92.                 bird.jump()
  93.  
  94.         if not game_over:
  95.             bird.update()
  96.             pipe_count += 1
  97.  
  98.             if pipe_count >= pipe_add_interval:
  99.                 pipes.append(Pipe(SCREEN_WIDTH))
  100.                 pipe_count = 0
  101.  
  102.             # Move and remove pipes
  103.             pipes_to_remove = []
  104.             for pipe in pipes:
  105.                 pipe.move(pipe_speed)
  106.                 if pipe.x + 50 < 0:
  107.                     pipes_to_remove.append(pipe)
  108.  
  109.             for pipe in pipes_to_remove:
  110.                 pipes.remove(pipe)
  111.  
  112.             # Check collisions
  113.             for pipe in pipes:
  114.                 if bird.get_rect().colliderect(pipe.get_top_rect()) or \
  115.                    bird.get_rect().colliderect(pipe.get_bottom_rect()):
  116.                     game_over = True
  117.                 if not pipe.passed and bird.x > pipe.x + 50:
  118.                     pipe.passed = True
  119.                     score += 1
  120.  
  121.             if (bird.y + bird.radius > ground_y) or (bird.y - bird.radius < 0):
  122.                 game_over = True
  123.  
  124.             # Draw game elements
  125.             for pipe in pipes:
  126.                 pipe.draw(screen)
  127.             bird.draw(screen)
  128.  
  129.             # Draw score
  130.             text = font.render(f"Score: {score}", True, (0,0,0))
  131.             screen.blit(text, (10, 10))
  132.  
  133.         else:
  134.             # Game over screen
  135.             text = font.render("Game Over! Press R to Restart", True, (0,0,0))
  136.             screen.blit(text, (40, SCREEN_HEIGHT//2))
  137.             keys = pygame.key.get_pressed()
  138.             if keys[pygame.K_r]:
  139.                 bird = Bird()
  140.                 pipes = []
  141.                 score = 0
  142.                 game_over = False
  143.  
  144.         pygame.display.flip()
  145.         clock.tick(FPS)
  146.  
  147.     pygame.quit()
  148.  
  149. if __name__ == "__main__":
  150.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement