Here's a simplified version of an Angry Birds-like game using Pygame. This code includes a start menu, basic physics, scoring, and controls. Note that this is a simplified version and you can expand upon it to add more features, levels, and polish.
import pygame
import math
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Angry Birds Clone")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Game states
MENU = 0
PLAYING = 1
game_state = MENU
# Physics constants
GRAVITY = 0.5
FRICTION = 0.99
# Bird class
class Bird:
def __init__(self, x, y):
self.x = x
self.y = y
self.radius = 15
self.vx = 0
self.vy = 0
self.dragging = False
self.start_x = x
self.start_y = y
def draw(self, surface):
pygame.draw.circle(surface, RED, (int(self.x), int(self.y)), self.radius)
if self.dragging:
pygame.draw.line(surface, BLACK, (self.start_x, self.start_y), pygame.mouse.get_pos(), 2)
def update(self):
if not self.dragging:
self.vy += GRAVITY
self.x += self.vx
self.y += self.vy
self.vx *= FRICTION
self.vy *= FRICTION
# Keep bird within screen bounds
if self.y > HEIGHT - self.radius:
self.y = HEIGHT - self.radius
self.vy = 0
if self.x < self.radius or self.x > WIDTH - self.radius:
self.vx = -self.vx
def launch(self, mouse_x, mouse_y):
dx = self.start_x - mouse_x
dy = self.start_y - mouse_y
distance = math.sqrt(dx**2 + dy**2)
if distance > 0:
self.vx = dx / distance * 10
self.vy = dy / distance * 10
# Target class
class Target:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 40
self.height = 40
self.destroyed = False
def draw(self, surface):
if not self.destroyed:
pygame.draw.rect(surface, GREEN, (self.x, self.y, self.width, self.height))
def check_collision(self, bird):
if not self.destroyed:
bird_rect = pygame.Rect(bird.x - bird.radius, bird.y - bird.radius, bird.radius * 2, bird.radius * 2)
target_rect = pygame.Rect(self.x, self.y, self.width, self.height)
return bird_rect.colliderect(target_rect)
return False
# Game variables
bird = Bird(100, HEIGHT - 100)
targets = [Target(600, HEIGHT - 100), Target(650, HEIGHT - 100), Target(625, HEIGHT - 140)]
score = 0
font = pygame.font.Font(None, 36)
# Menu button
start_button = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 - 50, 200, 100)
def draw_menu():
screen.fill(WHITE)
pygame.draw.rect(screen, BLUE, start_button)
start_text = font.render("Start Game", True, WHITE)
screen.blit(start_text, (start_button.x + 20, start_button.y + 30))
pygame.display.flip()
def draw_game():
screen.fill(WHITE)
# Draw ground
pygame.draw.rect(screen, BLACK, (0, HEIGHT - 50, WIDTH, 50))
# Draw bird
bird.draw(screen)
# Draw targets
for target in targets:
target.draw(screen)
# Draw score
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (10, 10))
pygame.display.flip()
def main():
global game_state, score
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if game_state == MENU:
if event.type == pygame.MOUSEBUTTONDOWN:
if start_button.collidepoint(event.pos):
game_state = PLAYING
# Reset game variables
global bird, targets
bird = Bird(100, HEIGHT - 100)
targets = [Target(600, HEIGHT - 100), Target(650, HEIGHT - 100), Target(625, HEIGHT - 140)]
score = 0
elif game_state == PLAYING:
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = event.pos
distance = math.sqrt((bird.x - mouse_x)**2 + (bird.y - mouse_y)**2)
if distance <= bird.radius:
bird.dragging = True
elif event.type == pygame.MOUSEBUTTONUP and bird.dragging:
bird.dragging = False
mouse_x, mouse_y = event.pos
bird.launch(mouse_x, mouse_y)
if game_state == MENU:
draw_menu()
elif game_state == PLAYING:
# Update game objects
bird.update()
# Check collisions
for target in targets:
if target.check_collision(bird) and not target.destroyed:
target.destroyed = True
score += 100
# Check if all targets are destroyed
if all(target.destroyed for target in targets):
game_state = MENU
draw_game()
clock.tick(60)
if __name__ == "__main__":
main()
Explanation of the Code:
-
Initialization:
- Pygame is initialized, and a window of 800x600 pixels is created.
- Colors, game states, and physics constants are defined.
-
Bird Class:
- Represents the bird (projectile) with properties like position, velocity, and dragging state.
- The
launch method calculates the velocity based on the mouse drag distance and direction.
- The
update method applies gravity and friction to simulate physics.
-
Target Class:
- Represents the targets (blocks) that the bird needs to hit.
- Includes collision detection with the bird.
-
Game States:
MENU: Displays a start button to begin the game.
PLAYING: The main game loop where the player interacts with the bird and targets.
-
Main Loop:
- Handles events like mouse clicks for dragging and launching the bird.
- Updates game objects and checks for collisions.
- Draws the game elements on the screen.
- Returns to the menu when all targets are destroyed.
-
Controls:
- Click and drag the bird to aim.
- Release the mouse to launch the bird.
- In the menu, click the "Start Game" button to begin.
-
Scoring:
- The player earns 100 points for each target destroyed.
Features to Add for Enhancement:
- Multiple levels with increasing difficulty.
- Different types of birds with special abilities.
- Sound effects and background music.
- Better graphics and animations.
- A slingshot visual effect.
- A high score system.
- Obstacles and more complex physics (e.g., bouncing, breaking).
This code provides a basic framework that you can build upon to create a more complete Angry Birds-like game. Remember to test and tweak the physics constants to achieve the desired gameplay feel.