Below is an example of a simple “Angry Birds”–inspired game built using Python and Pygame. In this example we simulate a bird that can be clicked and dragged (imitating a slingshot). When released the bird is “launched” using a simple physics update (gravity is applied) and if it hits a simple target, you score points. The game also includes a main menu, pause functionality, and a restart feature.
You can run this code (make sure Pygame is installed with pip install pygame) and then use the following controls:
-
Main Menu:
Press S to start the game.
-
Gameplay:
Click and drag the bird (red circle) to set your launch direction. Release the mouse button to launch.
Press P to pause/resume.
Press R to restart the level (which resets the bird and score).
-
Exit:
Close the window or press the close button.
Copy the code into a file (e.g., angry_birds.py) and run it.
import pygame
import sys
import math
# Initialize Pygame
pygame.init()
# Screen dimensions and setup
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Simple Angry Birds")
clock = pygame.time.Clock()
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
GRAY = (150, 150, 150)
# Game States
STATE_MENU = "menu"
STATE_PLAYING = "playing"
STATE_PAUSED = "paused"
# Gravity constant to simulate falling effect
GRAVITY = 0.5
# --- Bird Class ---
class Bird:
def __init__(self, x, y):
self.origin = (x, y) # Starting position (anchor of the slingshot)
self.x = x
self.y = y
self.radius = 15
self.color = RED
self.vel_x = 0
self.vel_y = 0
self.launched = False
self.dragging = False
def draw(self, surface):
pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.radius)
def update(self):
if self.launched:
self.vel_y += GRAVITY
self.x += self.vel_x
self.y += self.vel_y
def reset(self):
self.x, self.y = self.origin
self.vel_x, self.vel_y = 0, 0
self.launched = False
self.dragging = False
# --- Target Class ---
class Target:
def __init__(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height)
self.color = GREEN
self.hit = False
def draw(self, surface):
if not self.hit:
pygame.draw.rect(surface, self.color, self.rect)
# --- Utility function to draw text ---
def draw_text(surface, text, size, color, pos):
font = pygame.font.Font(None, size)
rendered_text = font.render(text, True, color)
surface.blit(rendered_text, pos)
# --- Main Game Loop ---
def main():
game_state = STATE_MENU
score = 0
# Create game objects: the bird and a target
bird = Bird(150, HEIGHT - 100)
target = Target(WIDTH - 150, HEIGHT - 150, 50, 50)
running = True
while running:
clock.tick(FPS)
if game_state == STATE_MENU:
# ----- MENU STATE -----
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s: # Start the game
game_state = STATE_PLAYING
score = 0
bird.reset()
target.hit = False
# Draw menu screen
screen.fill(WHITE)
draw_text(screen, "Simple Angry Birds", 64, BLACK, (WIDTH // 2 - 200, HEIGHT // 2 - 100))
draw_text(screen, "Press S to Start", 36, BLACK, (WIDTH // 2 - 100, HEIGHT // 2))
pygame.display.flip()
elif game_state == STATE_PLAYING:
# ----- PLAYING STATE -----
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Pause or restart using keys
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
game_state = STATE_PAUSED
if event.key == pygame.K_r:
score = 0
bird.reset()
target.hit = False
# Handle mouse events for dragging & launching the bird
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
dist = math.hypot(mouse_x - bird.x, mouse_y - bird.y)
if dist < bird.radius:
bird.dragging = True
if event.type == pygame.MOUSEBUTTONUP:
if bird.dragging:
bird.dragging = False
bird.launched = True
# Compute velocity based on how far the bird is pulled from its origin:
dx = bird.origin[0] - bird.x
dy = bird.origin[1] - bird.y
bird.vel_x = dx * 0.2
bird.vel_y = dy * 0.2
if event.type == pygame.MOUSEMOTION:
if bird.dragging:
mouse_x, mouse_y = pygame.mouse.get_pos()
# Limit the drag distance to prevent excessive velocity:
max_drag = 100
dx = mouse_x - bird.origin[0]
dy = mouse_y - bird.origin[1]
distance = math.hypot(dx, dy)
if distance > max_drag:
scale = max_drag / distance
dx *= scale
dy *= scale
bird.x = bird.origin[0] + dx
bird.y = bird.origin[1] + dy
# Update the bird's movement if launched
if bird.launched:
bird.update()
# Check for collision between the bird (circle approximated as a rectangle) and the target
if not target.hit:
bird_rect = pygame.Rect(bird.x - bird.radius, bird.y - bird.radius, bird.radius * 2, bird.radius * 2)
if bird_rect.colliderect(target.rect):
target.hit = True
score += 100
# If the bird goes out of the screen boundaries, reset it
if bird.x > WIDTH or bird.x < 0 or bird.y > HEIGHT:
bird.reset()
# Drawing the game objects
screen.fill(WHITE)
# Draw ground line
pygame.draw.line(screen, BLACK, (0, HEIGHT - 50), (WIDTH, HEIGHT - 50), 4)
# Draw slingshot line between the origin and the bird (only visible when dragging)
pygame.draw.line(screen, GRAY, bird.origin, (bird.x, bird.y), 5)
target.draw(screen)
bird.draw(screen)
draw_text(screen, f"Score: {score}", 30, BLACK, (10, 10))
draw_text(screen, "Press P to Pause, R to Restart", 24, BLACK, (10, 40))
pygame.display.flip()
elif game_state == STATE_PAUSED:
# ----- PAUSED STATE -----
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
# Press P again to resume or R to restart
if event.key == pygame.K_p:
game_state = STATE_PLAYING
if event.key == pygame.K_r:
game_state = STATE_PLAYING
score = 0
bird.reset()
target.hit = False
# Display the pause overlay
screen.fill(WHITE)
draw_text(screen, "Paused", 64, BLACK, (WIDTH // 2 - 100, HEIGHT // 2 - 50))
draw_text(screen, "Press P to Resume or R to Restart", 30, BLACK, (WIDTH // 2 - 200, HEIGHT // 2 + 20))
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
Explanation
-
Menu State:
The game starts in a menu state where the player is prompted to press S to start.
-
Playing State:
- The bird is drawn at its starting position.
- When the player clicks the bird and drags it, a line is drawn (to simulate the slingshot).
- Releasing the mouse button launches the bird with a velocity calculated from the drag distance.
- Gravity is applied on each update causing the bird to follow a parabolic trajectory.
- If the bird collides with the target (a simple rectangle), the score increases.
-
Paused State:
Press P to pause the game. While paused, the player can resume (press P) or restart (press R).
This code is a very basic example meant for educational purposes. You can expand it further with more targets, levels, and more refined collision detection or animations. Enjoy experimenting with it!