gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
write me code for flappy bird in python
- User
write me code for flappy bird in python
- Assistant
Here is a simple implementation of the Flappy Bird game in Python using the Pygame library:
pythonimport pygame import random # Initialize pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 600, 600 SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Flappy Bird") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Bird properties bird_x = 50 bird_y = HEIGHT // 2 bird_width = 20 bird_height = 20 bird_vel = 0 bird_acc = 0.5 # Pipe properties pipe_width = 50 pipe_gap = 200 pipe_vel = 2 pipes = [] # Score score = 0 font = pygame.font.Font(None, 36) def draw_bird(x, y): pygame.draw.rect(SCREEN, WHITE, (x, y, bird_width, bird_height)) def draw_pipe(x, gap): top_pipe = pygame.Rect(x, 0, pipe_width, gap) bottom_pipe = pygame.Rect(x, gap + pipe_gap, pipe_width, HEIGHT - gap - pipe_gap) pygame.draw.rect(SCREEN, WHITE, top_pipe) pygame.draw.rect(SCREEN, WHITE, bottom_pipe) def check_collision(x, y): if y < 0 or y + bird_height > HEIGHT: return True for pipe_x, gap in pipes: if x > pipe_x and x < pipe_x + pipe_width: if y < gap or y + bird_height > gap + pipe_gap: return True return False clock = pygame.time.Clock() running = True while running: SCREEN.fill(BLACK) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bird_vel = -10 bird_vel += bird_acc bird_y += bird_vel if bird_y < 0: bird_y = 0 if bird_y + bird_height > HEIGHT: bird_y = HEIGHT - bird_height for pipe in pipes: pipe[0] -= pipe_vel if pipes and pipes[0][0] < -pipe_width: pipes.pop(0) if len(pipes) < 5 and random.random() < 0.02: gap = random.randint(100, HEIGHT - 100 - pipe_gap) pipes.append([WIDTH, gap]) if check_collision(bird_x, bird_y): print("Game Over! Score:", score) running = False for pipe_x, gap in pipes: draw_pipe(pipe_x, gap) draw_bird(bird_x, bird_y) if pipes and pipes[0][0] + pipe_width < bird_x: pipes.pop(0) score += 1 text = font.render("Score: " + str(score), True, WHITE) SCREEN.blit(text, (10, 10)) pygame.display.update() clock.tick(30) pygame.quit()This code creates a simple version of the Flappy Bird game using Pygame. The player controls the bird using the spacebar to make it jump and avoid colliding with the pipes. The score increases each time a pipe is passed. The game ends when the bird collides with the pipes or goes out of bounds.