import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Set up the display
screen_width = 288
screen_height = 512
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Flappy Bird (No Sprites)")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Game variables
gravity = 0.25
bird_y = screen_height // 2
bird_speed = 0
pipe_width = 50
pipe_height = random.randint(100, 300)
pipe_x = screen_width
pipe_speed = -4
score = 0
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird_speed = -10
# Update bird position
bird_speed += gravity
bird_y += bird_speed
# Update pipe position
pipe_x += pipe_speed
if pipe_x + pipe_width < 0:
pipe_x = screen_width
pipe_height = random.randint(100, 300)
score += 1
# Check for collisions
if bird_y <= 0 or bird_y >= screen_height - 50 or bird_x + 25 >= pipe_x and bird_x <= pipe_x + pipe_width or bird_y <= pipe_y + pipe_height or bird_y >= pipe_y + pipe_height + 100:
running = False
# Draw background
screen.fill(BLACK)
# Draw bird
pygame.draw.circle(screen, WHITE, (bird_x, bird_y), 25)
# Draw pipes
pygame.draw.rect(screen, GREEN, (pipe_x, 0, pipe_width, pipe_height))
pygame.draw.rect(screen, GREEN, (pipe_x, screen_height - pipe_height - 100, pipe_width, 100))
# Draw score
font = pygame.font.Font(None, 36)
text = font.render(f"Score: {score}", True, WHITE)
screen.blit(text, (10, 10))
pygame.display.flip()
pygame.time.Clock().tick(60)
# Quit Pygame
pygame.quit()
sys.exit()
This code creates a simple Flappy Bird game where the player controls a white circle (the bird) that falls under gravity and can jump by pressing the spacebar. The game generates green rectangles (pipes) that move from right to left, and the player must avoid colliding with them. The score is displayed at the top-left corner of the screen.