Creating a simple Flappy Bird game in Pygame without using sprites involves drawing shapes directly onto the screen. Below is a basic implementation of the game:
Install Pygame if you haven't already:
bash
pip install pygame
Create a Python file (e.g., flappy_bird.py) and add the following code:
python
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Bird properties
BIRD_SIZE = 20
BIRD_COLOR = BLACK
GRAVITY = 0.5
JUMP_STRENGTH = -8
# Pipe properties
PIPE_WIDTH = 50
PIPE_GAP = 150
PIPE_COLOR = GREEN
PIPE_SPEED = 3
# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
# Bird class
class Bird:
def __init__(self):
self.x = SCREEN_WIDTH // 4
self.y = SCREEN_HEIGHT // 2
self.velocity = 0
def jump(self):
self.velocity = JUMP_STRENGTH
def update(self):
self.velocity += GRAVITY
self.y += self.velocity
def draw(self):
pygame.draw.rect(screen, BIRD_COLOR, (self.x, self.y, BIRD_SIZE, BIRD_SIZE))
# Pipe class
class Pipe:
def __init__(self):
self.x = SCREEN_WIDTH
self.height = random.randint(100, 400)
def update(self):
self.x -= PIPE_SPEED
def draw(self):
pygame.draw.rect(screen, PIPE_COLOR, (self.x, 0, PIPE_WIDTH, self.height))
pygame.draw.rect(screen, PIPE_COLOR, (self.x, self.height + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT - self.height - PIPE_GAP))
def off_screen(self):
return self.x + PIPE_WIDTH < 0
def collides_with(self, bird):
return (
bird.x < self.x + PIPE_WIDTH and
bird.x + BIRD_SIZE > self.x and
(bird.y < self.height or bird.y + BIRD_SIZE > self.height + PIPE_GAP)
)
# Main game loop
def main():
bird = Bird()
pipes = [Pipe()]
score = 0
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.jump()
bird.update()
# Update and remove off-screen pipes
for pipe in pipes:
pipe.update()
if pipe.off_screen():
pipes.remove(pipe)
score += 1
if pipe.collides_with(bird):
game_over = True
# Add new pipes
if pipes[-1].x < SCREEN_WIDTH - 200:
pipes.append(Pipe())
# Clear screen
screen.fill(WHITE)
# Draw bird and pipes
bird.draw()
for pipe in pipes:
pipe.draw()
# Update display
pygame.display.flip()
# Cap the frame rate
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
main()
This code sets up a basic Flappy Bird game where the bird is represented by a black rectangle and the pipes are represented by green rectangles. The bird can be controlled by pressing the spacebar to jump. The game ends if the bird collides with a pipe or goes off the screen.
To run the game, simply execute the Python file:
bash
python flappy_bird.py
This is a very basic implementation and can be expanded with additional features such as scoring, sound effects, and more sophisticated collision detection.