Untitled

 avatar
unknown
plain_text
4 months ago
3.0 kB
1
Indexable
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)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)

# Clock for controlling the frame rate
clock = pygame.time.Clock()

# Physics constants
GRAVITY = 0.5

# Bird class
class Bird:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = 15
        self.color = RED
        self.velocity_x = 0
        self.velocity_y = 0
        self.launched = False

    def draw(self):
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)

    def update(self):
        if self.launched:
            self.x += self.velocity_x
            self.y += self.velocity_y
            self.velocity_y += GRAVITY

            # Bounce off ground
            if self.y > HEIGHT - self.radius:
                self.y = HEIGHT - self.radius
                self.velocity_y *= -0.7  # Lose some energy
                self.velocity_x *= 0.7  # Friction

# Target class
class Target:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = GREEN

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))

    def is_hit(self, bird):
        return (
            bird.x + bird.radius > self.x and
            bird.x - bird.radius < self.x + self.width and
            bird.y + bird.radius > self.y and
            bird.y - bird.radius < self.y + self.height
        )

# Initialize objects
bird = Bird(150, HEIGHT - 100)
targets = [Target(600, HEIGHT - 50, 50, 50)]

# Game loop
dragging = False
while True:
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if not bird.launched:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                if math.hypot(mouse_x - bird.x, mouse_y - bird.y) < bird.radius:
                    dragging = True
        elif event.type == pygame.MOUSEBUTTONUP:
            if dragging:
                dragging = False
                bird.launched = True
                bird.velocity_x = (bird.x - pygame.mouse.get_pos()[0]) / 10
                bird.velocity_y = (bird.y - pygame.mouse.get_pos()[1]) / 10
        elif event.type == pygame.MOUSEMOTION and dragging:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            bird.x, bird.y = mouse_x, mouse_y

    # Update and draw bird
    bird.update()
    bird.draw()

    # Draw targets and check for collisions
    for target in targets:
        target.draw()
        if target.is_hit(bird):
            targets.remove(target)

    pygame.display.flip()
    clock.tick(60)
Editor is loading...
Leave a Comment