Coding performance example

#1
by rombodawg - opened
Rombo-Org org

I just wanted to post some coding examples made by this model. This was a many shot example but still, it was completely made by this model alone with minimal troubleshooting and error correcting.

Its an angry birds clone in python. And it has working menus and multiple levels.

import pygame
import sys
import math

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Simplified Angry Birds")

# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
BROWN = (139, 69, 19)  # Wood
GRAY = (128, 128, 128)  # Stone
LIGHT_BLUE = (173, 216, 230)  # Glass
PINK = (255, 182, 193)  # Pig nose color

# Slingshot position
SLING_X, SLING_Y = 100, 400

# Gravity
gravity = 9.81

# Initialize the clock
clock = pygame.time.Clock()

class Bird:
    def __init__(self, x, y, radius, color):
        self.pos = [x, y]
        self.radius = radius
        self.color = color
        self.velocity = [0, 0]
        self.rect = pygame.Rect(x - radius, y - radius, 2 * radius, 2 * radius)

    def apply_gravity(self, dt):
        self.velocity[1] += gravity * dt

    def update(self, dt):
        self.apply_gravity(dt)
        self.pos[0] += self.velocity[0]
        self.pos[1] += self.velocity[1]
        self.rect.topleft = (self.pos[0] - self.radius, self.pos[1] - self.radius)

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (int(self.pos[0]), int(self.pos[1])), self.radius)

class RedBird(Bird):
    def __init__(self, x, y):
        super().__init__(x, y, 20, RED)

    def draw(self, screen):
        # Draw the body
        pygame.draw.circle(screen, self.color, (int(self.pos[0]), int(self.pos[1])), self.radius)
        # Draw eyes
        eye_radius = 3
        eye_offset = 7
        pygame.draw.circle(screen, BLACK, (int(self.pos[0] - eye_offset), int(self.pos[1] - eye_offset)), eye_radius)
        pygame.draw.circle(screen, BLACK, (int(self.pos[0] + eye_offset), int(self.pos[1] - eye_offset)), eye_radius)
        # Draw beak
        beak_points = [
            (int(self.pos[0]), int(self.pos[1]) - self.radius),
            (int(self.pos[0] + self.radius / 2), int(self.pos[1])),
            (int(self.pos[0]), int(self.pos[1]) - self.radius * 0.7)
        ]
        pygame.draw.polygon(screen, BLACK, beak_points)

class BlueBird(Bird):
    def __init__(self, x, y):
        super().__init__(x, y, 25, BLUE)
        self.split = False

    def draw(self, screen):
        # Draw the body
        pygame.draw.circle(screen, self.color, (int(self.pos[0]), int(self.pos[1])), self.radius)
        # Draw wings
        wing_length = 15
        pygame.draw.line(screen, BLACK, (int(self.pos[0] - wing_length), int(self.pos[1]) - wing_length // 2),
                         (int(self.pos[0] - wing_length * 2), int(self.pos[1])), 3)
        pygame.draw.line(screen, BLACK, (int(self.pos[0] + wing_length), int(self.pos[1]) - wing_length // 2),
                         (int(self.pos[0] + wing_length * 2), int(self.pos[1])), 3)

    def split_birds(self):
        if not self.split:
            angle = math.pi / 6
            speed = 10
            dx, dy = speed * math.cos(angle), -speed * math.sin(angle)
            bird1 = BlueBird(self.pos[0], self.pos[1])
            bird2 = BlueBird(self.pos[0], self.pos[1])
            bird3 = BlueBird(self.pos[0], self.pos[1])

            bird1.velocity = [dx, dy]
            bird2.velocity = [-dx, dy]
            bird3.velocity = [0, -speed * 2]

            return [bird1, bird2, bird3]
        return []

class Structure:
    def __init__(self, x, y, width, height, color, durability):
        self.rect = pygame.Rect(x, y, width, height)
        self.color = color
        self.durability = durability
        self.hit = False
        self.velocity = [0, 0]

    def draw(self, screen):
        pygame.draw.rect(screen, self.color, self.rect)

    def hit_structure(self, damage):
        self.durability -= damage
        if self.durability <= 0:
            return True
        self.hit = True
        return False

    def apply_gravity(self, dt):
        if self.hit:
            self.velocity[1] += gravity * dt
            self.rect.y += self.velocity[1]

class Pig:
    def __init__(self, x, y, radius, color):
        self.pos = [x, y]
        self.radius = radius
        self.color = color
        self.health = 1

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (int(self.pos[0]), int(self.pos[1])), self.radius)

    def hit(self, damage):
        self.health -= damage
        return self.health <= 0

# Game states
STATE_MENU = 0
STATE_PLAYING = 1
STATE_LEVEL_SELECTION = 2

def start_menu():
    global current_state, running
    screen.fill(WHITE)
    font = pygame.font.Font(None, 72)
    title = font.render("Angry Birds", True, BLACK)
    screen.blit(title, (WIDTH // 2 - title.get_width() // 2, HEIGHT // 4))

    button_font = pygame.font.Font(None, 36)
    start_button = button_font.render("Start Game", True, WHITE)
    level_button = button_font.render("Select Level", True, WHITE)
    quit_button = button_font.render("Quit Game", True, WHITE)

    start_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2, 200, 50)
    level_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + 70, 200, 50)
    quit_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + 140, 200, 50)

    pygame.draw.rect(screen, BLACK, start_rect, border_radius=10)
    pygame.draw.rect(screen, BLACK, level_rect, border_radius=10)
    pygame.draw.rect(screen, BLACK, quit_rect, border_radius=10)
    screen.blit(start_button, (start_rect.x + 60, start_rect.y + 10))
    screen.blit(level_button, (level_rect.x + 60, level_rect.y + 10))
    screen.blit(quit_button, (quit_rect.x + 60, quit_rect.y + 10))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if start_rect.collidepoint(mouse_pos):
                current_state = STATE_PLAYING
                reset_game(0)
            elif level_rect.collidepoint(mouse_pos):
                current_state = STATE_LEVEL_SELECTION
            elif quit_rect.collidepoint(mouse_pos):
                running = False

def level_selection():
    global current_state, current_level
    screen.fill(WHITE)
    font = pygame.font.Font(None, 72)
    title = font.render("Select Level", True, BLACK)
    screen.blit(title, (WIDTH // 2 - title.get_width() // 2, HEIGHT // 4))

    button_font = pygame.font.Font(None, 36)
    for i, level in enumerate(levels):
        level_button = button_font.render(f"Level {i + 1}", True, WHITE)
        level_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + i * 70, 200, 50)
        pygame.draw.rect(screen, BLACK, level_rect, border_radius=10)
        screen.blit(level_button, (level_rect.x + 60, level_rect.y + 10))

    quit_button = button_font.render("Quit Game", True, WHITE)
    quit_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + len(levels) * 70, 200, 50)
    pygame.draw.rect(screen, BLACK, quit_rect, border_radius=10)
    screen.blit(quit_button, (quit_rect.x + 60, quit_rect.y + 10))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            for i, level in enumerate(levels):
                level_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + i * 70, 200, 50)
                if level_rect.collidepoint(mouse_pos):
                    current_level = i
                    reset_game(current_level)
                    current_state = STATE_PLAYING
            if quit_rect.collidepoint(mouse_pos):
                running = False

def reset_game(level_index):
    global birds, structures, pigs, score, remaining_birds, dragging_bird, start_pos, end_pos, ready_bird
    birds = []
    structures = [Structure(s.rect.x, s.rect.y, s.rect.width, s.rect.height, s.color, s.durability) for s in levels[level_index]["structures"]]
    pigs = [Pig(p.pos[0], p.pos[1], p.radius, p.color) for p in levels[level_index]["pigs"]]
    score = 0
    remaining_birds = 5
    dragging_bird = False
    start_pos = None
    end_pos = None
    ready_bird = RedBird(SLING_X, SLING_Y)

def draw_sling():
    pygame.draw.line(screen, BLACK, (SLING_X - 30, SLING_Y), (SLING_X + 30, SLING_Y), 5)
    pygame.draw.line(screen, BLACK, (SLING_X, SLING_Y), (SLING_X, SLING_Y - 30), 5)

def draw_score():
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"Score: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))

def draw_remaining_birds():
    font = pygame.font.Font(None, 36)
    remaining_text = font.render(f"Birds Left: {remaining_birds}", True, BLACK)
    screen.blit(remaining_text, (WIDTH - 200, 10))

def draw_level_number():
    font = pygame.font.Font(None, 36)
    level_text = font.render(f"Level: {current_level + 1}", True, BLACK)
    screen.blit(level_text, (WIDTH // 2 - 50, 10))

# Levels
levels = [
    {
        "structures": [Structure(500, 400, 50, 50, BROWN, 3), Structure(550, 400, 50, 50, GRAY, 2)],
        "pigs": [Pig(525, 385, 20, PINK)]
    },
    {
        "structures": [Structure(600, 350, 70, 70, LIGHT_BLUE, 4), Structure(650, 350, 70, 70, GRAY, 3)],
        "pigs": [Pig(625, 335, 20, PINK)]
    }
]

# Main game loop
running = True
current_state = STATE_MENU
current_level = 0

while running:
    screen.fill(WHITE)

    if current_state == STATE_MENU:
        start_menu()
    elif current_state == STATE_LEVEL_SELECTION:
        level_selection()
    elif current_state == STATE_PLAYING:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN and not birds and remaining_birds > 0:
                dragging_bird = True
                start_pos = pygame.mouse.get_pos()
            elif event.type == pygame.MOUSEBUTTONUP and dragging_bird:
                end_pos = pygame.mouse.get_pos()
                dx, dy = start_pos[0] - end_pos[0], start_pos[1] - end_pos[1]
                bird_velocity = [dx * 0.1, dy * 0.1]

                if len(birds) % 2 == 0:
                    current_bird = RedBird(SLING_X, SLING_Y)
                else:
                    current_bird = BlueBird(SLING_X, SLING_Y)

                current_bird.velocity = bird_velocity
                birds.append(current_bird)
                remaining_birds -= 1
                dragging_bird = False

            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and isinstance(birds[-1], BlueBird):
                new_birds = birds[-1].split_birds()
                if new_birds:
                    birds.pop()  # Remove the original bird
                    birds.extend(new_birds)

        for bird in birds[:]:
            bird.update(dt)
            bird.draw(screen)

            # Check for collision with pigs
            for pig in pigs[:]:
                distance = math.sqrt((bird.pos[0] - pig.pos[0])**2 + (bird.pos[1] - pig.pos[1])**2)
                if distance < bird.radius + pig.radius:
                    if pig.hit(1):
                        score += 50
                        pigs.remove(pig)

            # Check for collision with structures
            for structure in structures[:]:
                if bird.rect.colliderect(structure.rect):
                    damage = 1
                    if structure.hit_structure(damage):
                        score += 10
                        structures.remove(structure)
                    else:
                        bird.velocity[0] *= -1
                        bird.velocity[1] *= -1

            # Check if the bird has fallen out of the window
            if bird.pos[1] > HEIGHT or bird.pos[0] < 0 or bird.pos[0] > WIDTH:
                birds.remove(bird)
                if remaining_birds > 0 and not dragging_bird:
                    ready_bird = RedBird(SLING_X, SLING_Y) if len(birds) % 2 == 0 else BlueBird(SLING_X, SLING_Y)

        for structure in structures:
            structure.draw(screen)
            structure.apply_gravity(dt)

        for pig in pigs:
            pig.draw(screen)

        if ready_bird and dragging_bird:
            mouse_pos = pygame.mouse.get_pos()
            max_distance = 100
            dx, dy = mouse_pos[0] - SLING_X, mouse_pos[1] - SLING_Y
            distance = math.sqrt(dx**2 + dy**2)
            if distance > max_distance:
                dx = (dx / distance) * max_distance
                dy = (dy / distance) * max_distance

            ready_bird.pos = [SLING_X + dx, SLING_Y + dy]
            pygame.draw.line(screen, BLACK, (SLING_X, SLING_Y), (ready_bird.pos[0], ready_bird.pos[1]), 5)
            ready_bird.draw(screen)

        if not dragging_bird and ready_bird:
            ready_bird = RedBird(SLING_X, SLING_Y) if len(birds) % 2 == 0 else BlueBird(SLING_X, SLING_Y)

        draw_sling()
        draw_score()
        draw_remaining_birds()
        draw_level_number()

        # Check for level completion or failure
        if not pigs and (remaining_birds == 0 or all(pig.health <= 0 for pig in pigs)):
            current_state = STATE_MENU
        elif remaining_birds == 0:
            current_state = STATE_MENU

    pygame.display.flip()
    dt = clock.tick(60) / 1000.0  # Delta time in seconds

pygame.quit()
sys.exit()

Sign up or log in to comment