make a flappy bird game in pygame
- User
make a flappy bird game in pygame
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
make a flappy bird game in pygame
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Flappy Bird is a popular mobile game where the player controls a bird that must navigate through obstacles. The game ends when the bird collides with an obstacle or falls to the ground.
import pygame
import sys
# Game Variables
gravity = 0.25
bird_movement = 0
game_active = True
score = 0
high_score = 0
# Pygame Initialization
pygame.init()
screen = pygame.display.set_mode((288, 512))
clock = pygame.time.Clock()
# Colors
bg_color = (135, 206, 235)
bird_color = (255, 255, 255)
pipe_color = (0, 255, 0)
text_color = (255, 255, 255)
# Fonts
font = pygame.font.Font('freesansbold.ttf', 24)
# Bird
bird_x, bird_y = 50, 256
bird_size = 20
# Pipes
pipe_x = 300
pipe_gap = 100
pipe_width = 50
pipe_height = 200
pipe_y = 200
def draw_floor():
"""Draw the floor"""
pygame.draw.rect(screen, (225, 215, 188), (0, 450, 288, 62))
def draw_bird():
"""Draw the bird"""
pygame.draw.rect(screen, bird_color, (bird_x, bird_y, bird_size, bird_size))
def draw_pipes():
"""Draw the pipes"""
global pipe_x, pipe_y, pipe_gap, pipe_width, pipe_height
pygame.draw.rect(screen, pipe_color, (pipe_x, 0, pipe_width, pipe_y))
pygame.draw.rect(screen, pipe_color, (pipe_x, pipe_y + pipe_gap, pipe_width, 512 - (pipe_y + pipe_gap)))
def check_collision():
"""Check for collision with pipes or floor"""
global game_active, pipe_x, pipe_y, pipe_gap, pipe_width, bird_x, bird_y, bird_size
if (pipe_x < bird_x + bird_size and
pipe_x + pipe_width > bird_x and
(bird_y < pipe_y or bird_y + bird_size > pipe_y + pipe_gap)):
return False
if bird_y + bird_size > 450:
return False
return True
def update_score():
"""Update the score"""
global score, pipe_x
if pipe_x == bird_x:
score += 1
def main():
global gravity, bird_movement, game_active, score, high_score, bird_y, pipe_x, pipe_y
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird_movement = 0
bird_movement -= 6
if event.key == pygame.K_SPACE and not game_active:
game_active = True
bird_y = 256
pipe_x = 300
pipe_y = 200
score = 0
screen.fill(bg_color)
if game_active:
# Bird movement
bird_movement += gravity
bird_y += bird_movement
draw_bird()
# Pipe movement
pipe_x -= 2
if pipe_x < -pipe_width:
pipe_x = 300
pipe_y = (200, 250, 300)[score % 3]
draw_pipes()
# Collision detection
game_active = check_collision()
# Score update
update_score()
# Draw floor
draw_floor()
# Display score
score_text = font.render(str(score), True, text_color)
screen.blit(score_text, (144, 50))
else:
# Display game over text
game_over_text = font.render('Game Over', True, text_color)
screen.blit(game_over_text, (100, 256))
# Display score
score_text = font.render('Score: ' + str(score), True, text_color)
screen.blit(score_text, (100, 280))
# Update high score
if score > high_score:
high_score = score
high_score_text = font.render('High Score: ' + str(high_score), True, text_color)
screen.blit(high_score_text, (100, 304))
pygame.display.update()
clock.tick(120)
if __name__ == "__main__":
main()
pip install pygame in your terminal.flappy_bird.py.python flappy_bird.py in your terminal.