-
Notifications
You must be signed in to change notification settings - Fork 30
Description
import pygame
import random
import sys
Initialize pygame
pygame.init()
Game constants
WIDTH = 400
HEIGHT = 600
GRAVITY = 0.25
FLAP_STRENGTH = -7
PIPE_SPEED = 3
PIPE_GAP = 150
PIPE_FREQUENCY = 1500 # milliseconds
Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
SKY_BLUE = (135, 206, 235)
Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Flappy Bird')
clock = pygame.time.Clock()
font = pygame.font.SysFont('Arial', 30)
class Bird:
def init(self):
self.x = 100
self.y = HEIGHT // 2
self.velocity = 0
self.width = 30
self.height = 30
def flap(self):
self.velocity = FLAP_STRENGTH
def update(self):
# Apply gravity
self.velocity += GRAVITY
self.y += self.velocity
# Keep bird from going above the screen
if self.y < 0:
self.y = 0
self.velocity = 0
def draw(self):
pygame.draw.rect(screen, (255, 255, 0), (self.x, self.y, self.width, self.height))
# Draw eye
pygame.draw.circle(screen, BLACK, (self.x + 22, self.y + 10), 4)
# Draw beak
pygame.draw.polygon(screen, (255, 165, 0), [(self.x + 30, self.y + 15),
(self.x + 40, self.y + 15),
(self.x + 30, self.y + 20)])
def get_mask(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
class Pipe:
def init(self):
self.x = WIDTH
self.height = random.randint(150, 400)
self.top_pipe = pygame.Rect(self.x, 0, 60, self.height - PIPE_GAP // 2)
self.bottom_pipe = pygame.Rect(self.x, self.height + PIPE_GAP // 2, 60, HEIGHT - self.height - PIPE_GAP // 2)
self.passed = False
def update(self):
self.x -= PIPE_SPEED
self.top_pipe.x = self.x
self.bottom_pipe.x = self.x
def draw(self):
pygame.draw.rect(screen, GREEN, self.top_pipe)
pygame.draw.rect(screen, GREEN, self.bottom_pipe)
def collide(self, bird):
bird_rect = bird.get_mask()
return bird_rect.colliderect(self.top_pipe) or bird_rect.colliderect(self.bottom_pipe)
def game():
bird = Bird()
pipes = []
score = 0
last_pipe = pygame.time.get_ticks()
game_active = True
while True:
clock.tick(60)
# Event handling
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.flap()
if event.key == pygame.K_SPACE and not game_active:
return # Restart game
# Background
screen.fill(SKY_BLUE)
if game_active:
# Bird update
bird.update()
bird.draw()
# Pipe generation
time_now = pygame.time.get_ticks()
if time_now - last_pipe > PIPE_FREQUENCY:
pipes.append(Pipe())
last_pipe = time_now
# Pipe update and drawing
for pipe in pipes:
pipe.update()
pipe.draw()
# Check for collisions
if pipe.collide(bird):
game_active = False
# Score counting
if pipe.x + 60 < bird.x and not pipe.passed:
pipe.passed = True
score += 1
# Remove off-screen pipes
if pipe.x < -60:
pipes.remove(pipe)
# Check if bird hits the ground
if bird.y + bird.height > HEIGHT:
game_active = False
else:
# Game over screen
game_over_text = font.render(f'Game Over! Score: {score}', True, BLACK)
restart_text = font.render('Press SPACE to restart', True, BLACK)
screen.blit(game_over_text, (WIDTH//2 - game_over_text.get_width()//2, HEIGHT//2 - 50))
screen.blit(restart_text, (WIDTH//2 - restart_text.get_width()//2, HEIGHT//2 + 50))
# Display score
if game_active:
score_text = font.render(f'Score: {score}', True, BLACK)
screen.blit(score_text, (10, 10))
pygame.display.update()
Main game loop
while True:
game()