code flappy bird in python, no external assets.
- User
code flappy bird in python, no external assets.
Assistant
Here is a simple implementation of a Flappy Bird game in Python using the
pygamelibrary, without relying on external assets. The game includes basic functionality such as gravity, jumping, pipes, and collision detection.import pygame import sys import random # Initialize pygame pygame.init() # Screen dimensions and colors WIDTH, HEIGHT = 400, 600 WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) RED = (255, 0, 0) # Game settings FPS = 60 GRAVITY = 0.5 JUMP_STRENGTH = -10 PIPE_SPEED = 3 PIPE_GAP = 150 # Initialize screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Flappy Bird") clock = pygame.time.Clock() # Bird class class Bird: def __init__(self): self.x = 50 self.y = HEIGHT // 2 self.radius = 15 self.velocity = 0 def draw(self): pygame.draw.circle(screen, BLUE, (self.x, int(self.y)), self.radius) def update(self): self.velocity += GRAVITY self.y += self.velocity def jump(self): self.velocity = JUMP_STRENGTH # Pipe class class Pipe: def __init__(self, x): self.x = x self.top_height = random.randint(100, HEIGHT - PIPE_GAP - 100) self.bottom_height = HEIGHT - self.top_height - PIPE_GAP self.width = 50 def draw(self): pygame.draw.rect(screen, GREEN, (self.x, 0, self.width, self.top_height)) pygame.draw.rect(screen, GREEN, (self.x, HEIGHT - self.bottom_height, self.width, self.bottom_height)) def update(self): self.x -= PIPE_SPEED def is_off_screen(self): return self.x + self.width < 0 def collides_with(self, bird): if bird.y - bird.radius < self.top_height or bird.y + bird.radius > HEIGHT - self.bottom_height: if bird.x + bird.radius > self.x and bird.x - bird.radius < self.x + self.width: return True return False # Main game loop def main(): bird = Bird() pipes = [Pipe(WIDTH + 200)] score = 0 running = True while running: screen.fill(WHITE) bird.draw() bird.update() # Draw and update pipes for pipe in pipes: pipe.draw() pipe.update() if pipe.collides_with(bird): running = False if pipe.x + pipe.width < bird.x and not hasattr(pipe, 'scored'): score += 1 pipe.scored = True # Add new pipes if pipes[-1].x < WIDTH - 200: pipes.append(Pipe(WIDTH)) # Remove off-screen pipes pipes = [pipe for pipe in pipes if not pipe.is_off_screen()] # Display score font = pygame.font.SysFont(None, 36) score_text = font.render(f"Score: {score}", True, BLACK) screen.blit(score_text, (10, 10)) # Check for collisions with ground or ceiling if bird.y - bird.radius < 0 or bird.y + bird.radius > HEIGHT: running = False # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: bird.jump() pygame.display.flip() clock.tick(FPS) # Game over screen screen.fill(RED) game_over_text = font.render("Game Over!", True, BLACK) screen.blit(game_over_text, (WIDTH // 2 - 80, HEIGHT // 2 - 20)) pygame.display.flip() pygame.time.wait(2000) if __name__ == "__main__": main()Features:
- Bird Movement: The bird is represented as a circle that falls due to gravity and jumps when the spacebar is pressed.
- Pipes: Pipes are generated with random gaps and move from right to left. They are removed once off-screen.
- Collision Detection: The game ends if the bird collides with a pipe or the screen boundaries.
- Score: The score increases when the bird successfully passes through a pipe.
This implementation uses basic shapes and avoids external assets, making it simple and self-contained.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.