write me an angry birds-like game in python using pygame. do not use images
- User
write me an angry birds-like game in python using pygame. do not use images
- Assistant
write me an angry birds-like game in python using pygame. do not use images
Creating an Angry Birds-like game using Pygame without images involves utilizing Pygame's drawing capabilities to represent game elements such as birds, pigs, blocks, and the slingshot. Below is a simplified version of such a game. This implementation includes basic mechanics like launching a bird with a slingshot, simple physics for projectile motion, collision detection, and destructible structures.
Python 3.x installed on your system.
Pygame library installed. You can install it using pip:
pip install pygame
Below is the complete Python code for the game. It's well-commented to help you understand each part.
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("Simple Angry Birds Clone")
# Colors
WHITE = (255, 255, 255)
BROWN = (139, 69, 19)
GREEN = (34, 139, 34)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GRAY = (128, 128, 128)
BLACK = (0, 0, 0)
# Clock for controlling frame rate
clock = pygame.time.Clock()
FPS = 60
# Gravity
GRAVITY = 0.5
# Define game objects using classes
class Bird:
def __init__(self, x, y):
self.radius = 15
self.color = RED
self.x = x
self.y = y
self.vel_x = 0
self.vel_y = 0
self.launched = False
self.alive = True
def update(self):
if self.launched and self.alive:
self.vel_y += GRAVITY
self.x += self.vel_x
self.y += self.vel_y
# Check for collision with ground
if self.y + self.radius >= HEIGHT - 50:
self.y = HEIGHT - 50 - self.radius
self.vel_y *= -0.5 # Simple bounce with damping
# Check for going off-screen
if self.x > WIDTH or self.y > HEIGHT or self.x < 0 or self.y < 0:
self.alive = False
def draw(self, screen):
if self.alive:
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
class Pig:
def __init__(self, x, y):
self.radius = 15
self.color = GREEN
self.x = x
self.y = y
self.alive = True
def draw(self, screen):
if self.alive:
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
class Block:
def __init__(self, x, y, width, height):
self.color = BROWN
self.rect = pygame.Rect(x, y, width, height)
self.health = 3 # Number of hits it can take
def draw(self, screen):
pygame.draw.rect(screen, self.color, self.rect)
# Draw health bar
pygame.draw.rect(screen, GREEN, (self.rect.x, self.rect.y - 10, self.rect.width * (self.health / 3), 5))
class Slingshot:
def __init__(self, x, y):
self.x = x
self.y = y
self.band_color = BLACK
self.band_width = 5
self.max_pull = 100
def draw(self, screen, bird):
if not bird.launched:
# Draw the band
pygame.draw.line(screen, self.band_color, (self.x, self.y), (bird.x, bird.y), self.band_width)
pygame.draw.circle(screen, self.band_color, (self.x, self.y), 10)
class Game:
def __init__(self):
self.bird = Bird(150, HEIGHT - 50)
self.slingshot = Slingshot(150, HEIGHT - 50)
self.pigs = [Pig(650, HEIGHT - 65)]
self.blocks = [
Block(500, HEIGHT - 65, 50, 50),
Block(550, HEIGHT - 65, 50, 50),
Block(525, HEIGHT - 115, 50, 50)
]
self.dragging = False
self.font = pygame.font.SysFont(None, 36)
self.score = 0
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mx, my = pygame.mouse.get_pos()
distance = math.hypot(mx - self.bird.x, my - self.bird.y)
if distance <= self.bird.radius and not self.bird.launched:
self.dragging = True
elif event.type == pygame.MOUSEBUTTONUP:
if self.dragging:
self.dragging = False
# Calculate velocity
mx, my = pygame.mouse.get_pos()
dx = self.slingshot.x - mx
dy = self.slingshot.y - my
distance = math.hypot(dx, dy)
angle = math.atan2(dy, dx)
power = min(distance, self.slingshot.max_pull)
self.bird.vel_x = math.cos(angle) * power / 15
self.bird.vel_y = math.sin(angle) * power / 15
self.bird.launched = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
# Reset the game
self.__init__()
def update(self):
self.bird.update()
self.check_collisions()
def check_collisions(self):
# Check collision between bird and pigs
for pig in self.pigs:
if pig.alive:
distance = math.hypot(self.bird.x - pig.x, self.bird.y - pig.y)
if distance <= self.bird.radius + pig.radius:
pig.alive = False
self.score += 100
self.bird.alive = False
# Check collision between bird and blocks
for block in self.blocks:
if block.health > 0 and self.bird.alive:
if block.rect.collidepoint(self.bird.x, self.bird.y):
block.health -= 1
self.bird.vel_x *= -0.5
self.bird.vel_y *= -0.5
if block.health <= 0:
self.score += 50
def draw(self, screen):
screen.fill(WHITE)
# Draw ground
pygame.draw.rect(screen, GRAY, (0, HEIGHT - 50, WIDTH, 50))
# Draw slingshot
self.slingshot.draw(screen, self.bird)
# Draw bird
self.bird.draw(screen)
# Draw pigs
for pig in self.pigs:
pig.draw(screen)
# Draw blocks
for block in self.blocks:
block.draw(screen)
# Draw score
score_text = self.font.render(f"Score: {self.score}", True, BLACK)
screen.blit(score_text, (10, 10))
# Instructions
instr_text = self.font.render("Drag and release the bird. Press 'R' to reset.", True, BLACK)
screen.blit(instr_text, (10, 40))
pygame.display.flip()
def run(self):
while True:
clock.tick(FPS)
self.handle_events()
if self.dragging:
mx, my = pygame.mouse.get_pos()
dx = mx - self.slingshot.x
dy = my - self.slingshot.y
distance = math.hypot(dx, dy)
if distance > self.slingshot.max_pull:
angle = math.atan2(dy, dx)
mx = self.slingshot.x + math.cos(angle) * self.slingshot.max_pull
my = self.slingshot.y + math.sin(angle) * self.slingshot.max_pull
self.bird.x = mx
self.bird.y = my
self.update()
self.draw(screen)
# Run the game
if __name__ == "__main__":
game = Game()
game.run()
Classes:
Bird: Represents the projectile. Handles its position, velocity, and rendering.Pig: Represents the target. Handles rendering and status (alive or not).Block: Represents the destructible structures. Each block has health and can be damaged.Slingshot: Handles drawing the slingshot bands and the aiming mechanism.Game: Manages the game state, including initializing objects, handling events, updating game logic, collision detection, and rendering.Game Mechanics:
User Interface:
Save the Code: Save the above code in a Python file, e.g., angry_birds_clone.py.
Run the Game:
python angry_birds_clone.py
Controls:
This is a basic implementation. Here are some ideas to enhance the game:
pymunk for more realistic interactions.This should give you a solid foundation to build upon and create a more complex and engaging Angry Birds-like game using Pygame.
Not yet included in a published release.