import pygame
import random
import sys
pygame.init()
screen_width = 600
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Flappy Bird")
light_colors = [(135, 206, 250), (144, 238, 144), (255, 255, 224)]
dark_colors = [(0, 0, 0), (0, 0, 139), (64, 64, 64)]
pipe_colors = [(0, 128, 0), (210, 180, 140), (105, 105, 105)]
land_colors = [(139, 69, 19), (255, 255, 0)]
class Bird:
def __init__(self):
self.shape = random.choice(['square', 'circle', 'triangle'])
self.color = random.choice(dark_colors)
self.x = screen_width // 4
self.y = screen_height // 2
self.size = 50
self.rect = pygame.Rect(self.x, self.y, self.size, self.size)
self.vel = 0
self.gravity = 0.4
self.jump_strength = -7
def jump(self):
self.vel += self.jump_strength
def update(self):
self.vel += self.gravity
self.y += self.vel
self.rect.topleft = (self.x, self.y)
def draw(self, screen):
if self.shape == 'square':
pygame.draw.rect(screen, self.color, self.rect)
elif self.shape == 'circle':
center = (self.x + self.size // 2, self.y + self.size // 2)
radius = self.size // 2
pygame.draw.circle(screen, self.color, center, radius)
elif self.shape == 'triangle':
points = [
(self.x + self.size // 2, self.y),
(self.x, self.y + self.size),
(self.x + self.size, self.y + self.size)
]
pygame.draw.polygon(screen, self.color, points)
def main():
bird = Bird()
pipes = []
score = 0
best_score = 0
background_color = random.choice(light_colors)
land_color = random.choice(land_colors)
land_height = 100
land_rect = pygame.Rect(0, screen_height - land_height, screen_width, land_height)
clock = pygame.time.Clock()
FPS = 60
font = pygame.font.Font(None, 36)
game_over = False
last_pipe_time = pygame.time.get_ticks()
pipe_speed = 4
pipe_width = 50
min_pipe_height = 50
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if not game_over:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.jump()
else:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_SPACE:
game_over = False
bird = Bird()
pipes.clear()
score = 0
land_color = random.choice(land_colors)
background_color = random.choice(light_colors)
land_rect = pygame.Rect(0, screen_height - land_height, screen_width, land_height)
last_pipe_time = pygame.time.get_ticks()
if not game_over:
bird.update()
if bird.rect.bottom >= land_rect.top or bird.rect.top <= 0:
game_over = True
current_time = pygame.time.get_ticks()
if current_time - last_pipe_time > random.randint(1200, 2400):
pipe_gap = random.randint(150, 300)
top_pipe_max = (screen_height - land_height - min_pipe_height) - pipe_gap
top_pipe_height = random.randint(min_pipe_height, top_pipe_max)
pipe_color = random.choice(pipe_colors)
pipe_x = screen_width
top_rect = pygame.Rect(pipe_x, 0, pipe_width, top_pipe_height)
bottom_rect = pygame.Rect(
pipe_x,
top_pipe_height + pipe_gap,
pipe_width,
(screen_height - land_height) - (top_pipe_height + pipe_gap)
)
pipes.append({'top_rect': top_rect, 'bottom_rect': bottom_rect, 'color': pipe_color, 'passed': False})
last_pipe_time = current_time
for pipe in pipes.copy():
pipe['top_rect'].x -= pipe_speed
pipe['bottom_rect'].x -= pipe_speed
if bird.rect.colliderect(pipe['top_rect']) or bird.rect.colliderect(pipe['bottom_rect']):
game_over = True
if pipe['top_rect'].x + pipe['top_rect'].width < bird.x:
if not pipe['passed']:
score += 1
pipe['passed'] = True
if pipe['top_rect'].x < -pipe['top_rect'].width:
pipes.remove(pipe)
screen.fill(background_color)
for pipe in pipes:
pygame.draw.rect(screen, pipe['color'], pipe['top_rect'])
pygame.draw.rect(screen, pipe['color'], pipe['bottom_rect'])
bird.draw(screen)
pygame.draw.rect(screen, land_color, land_rect)
score_text = font.render(f"Score: {score}", True, (0, 0, 0))
screen.blit(score_text, (screen_width - score_text.get_width() - 10, 10))
pygame.display.update()
clock.tick(FPS)
else:
screen.fill(background_color)
game_over_text = font.render("Game Over!", True, (255, 0, 0))
screen.blit(game_over_text, (screen_width//2 - 80, screen_height//2 - 50))
current_score = font.render(f"Current Score: {score}", True, (0, 0, 0))
screen.blit(current_score, (screen_width//2 - 100, screen_height//2))
if score > best_score:
best_score = score
best_score_text = font.render(f"Best Score: {best_score}", True, (0, 0, 255))
screen.blit(best_score_text, (screen_width//2 - 100, screen_height//2 + 50))
restart_text = font.render("Press SPACE to restart, Q/ESC to quit", True, (255, 255, 255))
screen.blit(restart_text, (screen_width//2 - 200, screen_height//2 + 100))
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
main()