Here’s a minimal, yet functional “Angry Birds–style” game in Python using Pygame. It includes:
python
import pygame
import sys
import math
# --- Constants ---
WIDTH, HEIGHT = 800, 600
FPS = 60
# Colors
SKY_BLUE = (135, 206, 235)
GROUND_COLOR = (100, 50, 0)
BIRD_COLOR = (255, 0, 0)
PIG_COLOR = ( 0, 255, 0)
TEXT_COLOR = ( 0, 0, 0)
# Physics
GRAVITY = 0.5
# Bird
BIRD_RADIUS = 15
BIRD_START_POS = pygame.math.Vector2(150, HEIGHT - 100)
MAX_DRAG_DISTANCE = 100
# Pig
PIG_RADIUS = 20
PIG_POSITIONS = [
(600, HEIGHT - 100 - PIG_RADIUS),
(650, HEIGHT - 100 - PIG_RADIUS),
(700, HEIGHT - 100 - PIG_RADIUS),
]
# --- Game Classes ---
class Bird:
def __init__(self):
self.reset()
def reset(self):
self.pos = BIRD_START_POS.copy()
self.vel = pygame.math.Vector2(0, 0)
self.launched = False
def update(self):
if self.launched:
# apply gravity
self.vel.y += GRAVITY
self.pos += self.vel
def draw(self, screen):
pygame.draw.circle(screen, BIRD_COLOR, (int(self.pos.x), int(self.pos.y)), BIRD_RADIUS)
class Pig:
def __init__(self, pos):
self.pos = pygame.math.Vector2(pos)
self.alive = True
def draw(self, screen):
if self.alive:
pygame.draw.circle(screen, PIG_COLOR, (int(self.pos.x), int(self.pos.y)), PIG_RADIUS)
# --- Game Functions ---
def draw_text(screen, text, size, pos):
font = pygame.font.SysFont("Arial", size)
surf = font.render(text, True, TEXT_COLOR)
rect = surf.get_rect(center=pos)
screen.blit(surf, rect)
def main_menu(screen, clock):
"""Show the main menu. Return True if we should start the game."""
while True:
screen.fill(SKY_BLUE)
draw_text(screen, "Angry Birds Python", 60, (WIDTH//2, HEIGHT//3))
draw_text(screen, "S: Start | Q: Quit", 40, (WIDTH//2, HEIGHT//2))
pygame.display.flip()
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
sys.exit()
if ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_s:
return True
if ev.key == pygame.K_q:
pygame.quit()
sys.exit()
clock.tick(FPS)
def game_loop(screen, clock):
bird = Bird()
pigs = [Pig(pos) for pos in PIG_POSITIONS]
score = 0
dragging = False
game_paused = False
while True:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Mouse down: begin aiming
if ev.type == pygame.MOUSEBUTTONDOWN and not bird.launched and not game_paused:
if pygame.math.Vector2(ev.pos).distance_to(bird.pos) < BIRD_RADIUS*2:
dragging = True
# Mouse up: launch bird
if ev.type == pygame.MOUSEBUTTONUP and dragging:
dragging = False
bird.launched = True
mouse = pygame.math.Vector2(ev.pos)
dir_vec = bird.pos - mouse
if dir_vec.length() > MAX_DRAG_DISTANCE:
dir_vec.scale_to_length(MAX_DRAG_DISTANCE)
bird.vel = dir_vec * 0.2
# Keys
if ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_p: # Pause / resume
game_paused = not game_paused
if ev.key == pygame.K_r: # Restart
return "restart"
if ev.key == pygame.K_m: # Main menu
return "menu"
if not game_paused:
bird.update()
# Collision: bird with pigs
for pig in pigs:
if pig.alive and bird.pos.distance_to(pig.pos) < BIRD_RADIUS + PIG_RADIUS:
pig.alive = False
score += 10
# Draw
screen.fill(SKY_BLUE)
# Ground
pygame.draw.rect(screen, GROUND_COLOR, (0, HEIGHT-100, WIDTH, 100))
# Pigs & Bird
for pig in pigs:
pig.draw(screen)
bird.draw(screen)
# Draw aiming line
if dragging:
mouse = pygame.mouse.get_pos()
pygame.draw.line(screen, TEXT_COLOR, bird.pos, mouse, 2)
# HUD
draw_text(screen, f"Score: {score}", 30, (80, 30))
if game_paused:
draw_text(screen, "PAUSED", 60, (WIDTH//2, HEIGHT//2))
# Win / lose conditions
alive_pigs = sum(1 for p in pigs if p.alive)
if alive_pigs == 0:
draw_text(screen, "YOU WIN! Press R to restart or M for menu", 30, (WIDTH//2, HEIGHT//2 + 50))
elif (bird.pos.y > HEIGHT or bird.pos.x > WIDTH or bird.pos.x < 0) and bird.launched:
# bird fell off-screen
draw_text(screen, "GAME OVER! Press R to restart or M for menu", 30, (WIDTH//2, HEIGHT//2 + 50))
pygame.display.flip()
clock.tick(FPS)
# --- Main Program ---
def run():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Angry Birds Py")
clock = pygame.time.Clock()
while True:
start = main_menu(screen, clock)
if start:
result = game_loop(screen, clock)
if result == "menu":
continue
# if "restart", will drop back into game_loop
else:
break
if __name__ == "__main__":
run()
Enjoy tweaking it—add blocks, mute/unmute sounds, more levels, better art… have fun! 😊