My First game!

Hi! I was working with my dad on python and we made this super awesome game!

import pygame
import random
from math import sqrt, sin, cos, radians

# Constants
WIDTH, HEIGHT = 800, 600
SPIDER_SIZE = 40
BUG_COUNT = 7
BUG_SPEED = 1.8
BUG_SIZE = 20
BUG_COLOR = (0, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LEG_LENGTH = 50  # Increased leg length
LEG_SEGMENTS = 2  # Number of leg joints
LEG_WAVE_SPEED = 0.1  # Leg animation speed
INITIAL_SPIDER_SPEED = 5.0  # Changed to float
SPEED_MULTIPLIER = 1.1  # 10% speed increase per wave
SPIDER_SPEED = INITIAL_SPIDER_SPEED
MAX_SPIDER_SPEED = 10
LIVES_COLOR = (255, 0, 0)  # Red color for lives
SPIKE_BALL_SIZE = 25
SPIKE_BALL_COLOR = (255, 0, 0)
SPIKE_BALL_SPEED = 3
SPIKE_LENGTH = 15

# Initialize game
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
score = 0
lives = 5  # Initial number of lives

# Spider initial position (float values)
spider_x, spider_y = WIDTH // 2.0, HEIGHT // 2.0

# Initialize bugs
bugs = [
    {
        "x": random.randint(0, WIDTH),
        "y": random.randint(0, HEIGHT),
        "dx": random.uniform(-1, 1),
        "dy": random.uniform(-1, 1),
        "active": True,
    }
    for _ in range(BUG_COUNT)
]

# Initialize spike ball
spike_ball = {
    "x": random.randint(100, WIDTH - 100),
    "y": random.randint(100, HEIGHT - 100),
    "dx": random.choice([-1, 1]) * SPIKE_BALL_SPEED,
    "dy": random.choice([-1, 1]) * SPIKE_BALL_SPEED,
}

# Game loop
running = True
while running:
    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Handle movement with boundary checking
        keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        spider_x -= SPIDER_SPEED
    if keys[pygame.K_RIGHT]:
        spider_x += SPIDER_SPEED
    if keys[pygame.K_UP]:
        spider_y -= SPIDER_SPEED
    if keys[pygame.K_DOWN]:
        spider_y += SPIDER_SPEED

    # Keep spider on screen
    spider_x = max(0, min(spider_x, WIDTH - SPIDER_SIZE))
    spider_y = max(0, min(spider_y, HEIGHT - SPIDER_SIZE))

    # Bug movement logic
    for bug in bugs:
        if not bug["active"]:
            continue

        # Random directional changes
        if random.random() < 0.02:
            bug["dx"] += random.uniform(-0.5, 0.5)
            bug["dy"] += random.uniform(-0.5, 0.5)

        # Normalize speed
        speed = sqrt(bug["dx"] ** 2 + bug["dy"] ** 2)
        if speed > 0:
            bug["dx"] = (bug["dx"] / speed) * BUG_SPEED
            bug["dy"] = (bug["dy"] / speed) * BUG_SPEED

        # Update position with boundary checks
        bug["x"] = (bug["x"] + bug["dx"]) % WIDTH
        bug["y"] = (bug["y"] + bug["dy"]) % HEIGHT

    # Move spike ball with bounce
    spike_ball["x"] += spike_ball["dx"]
    spike_ball["y"] += spike_ball["dy"]

    if spike_ball["x"] <= 0 or spike_ball["x"] >= WIDTH - SPIKE_BALL_SIZE:
        spike_ball["dx"] *= -1
    if spike_ball["y"] <= 0 or spike_ball["y"] >= HEIGHT - SPIKE_BALL_SIZE:
        spike_ball["dy"] *= -1

    # Collision detection
    spider_rect = pygame.Rect(spider_x, spider_y, SPIDER_SIZE, SPIDER_SIZE)
    bugs_eaten = 0

    # Check bug collisions
    for bug in bugs:
        if not bug["active"]:
            continue

        bug_rect = pygame.Rect(bug["x"], bug["y"], BUG_SIZE, BUG_SIZE)
        if spider_rect.colliderect(bug_rect):
            score += 5
            bug["active"] = False
            bugs_eaten += 1

    # Check spike ball collision
    spike_rect = pygame.Rect(
        spike_ball["x"], spike_ball["y"], SPIKE_BALL_SIZE, SPIKE_BALL_SIZE
    )
    if spider_rect.colliderect(spike_rect):
        lives -= 1
        if lives <= 0:
            running = False
        else:
            # Respawn spike ball away from player
            spike_ball["x"] = random.randint(100, WIDTH - 100)
            spike_ball["y"] = random.randint(100, HEIGHT - 100)

    # Check if all bugs were eaten
    if bugs_eaten > 0 and all(not bug["active"] for bug in bugs):
        # Exponential speed increase
        SPIDER_SPEED *= SPEED_MULTIPLIER

        # Respawn bugs
        for i in range(BUG_COUNT):
            bugs[i] = {
                "x": random.randint(0, WIDTH),
                "y": random.randint(0, HEIGHT),
                "dx": random.uniform(-1, 1),
                "dy": random.uniform(-1, 1),
                "active": True,
            }

    # Draw elements
    screen.fill(WHITE)

    # Draw spider body
    pygame.draw.circle(
        screen,
        BLACK,
        (int(spider_x + SPIDER_SIZE // 2), int(spider_y + SPIDER_SIZE // 2)),
        SPIDER_SIZE // 2,
    )

    # Animated walking legs
    current_time = pygame.time.get_ticks()
    for i in range(8):
        # Calculate leg base angle
        base_angle = i * 45  # 8 legs at 45 degree intervals

        # Create walking wave pattern
        wave_angle = current_time * LEG_WAVE_SPEED + i * 45
        leg_phase = sin(wave_angle * 0.005)

        # Calculate leg positions with animation
        angle = base_angle + leg_phase * 30  # ±30 degree movement

        # Leg segments with articulated joints
        vec = pygame.math.Vector2()
        vec.from_polar((LEG_LENGTH / 2, angle))

        # Thigh segment
        thigh_end = (
            spider_x + SPIDER_SIZE // 2 + vec.x,
            spider_y + SPIDER_SIZE // 2 + vec.y,
        )
        pygame.draw.line(
            screen,
            BLACK,
            (spider_x + SPIDER_SIZE // 2, spider_y + SPIDER_SIZE // 2),
            thigh_end,
            3,
        )

        # Lower leg segment with dynamic bending
        lower_angle = angle + 30 * sin(current_time * 0.005 + i)
        vec.from_polar((LEG_LENGTH / 2, lower_angle))
        lower_end = (thigh_end[0] + vec.x, thigh_end[1] + vec.y)
        pygame.draw.line(screen, BLACK, thigh_end, lower_end, 2)

        # Add subtle foot movement
        foot_vec = pygame.math.Vector2()
        foot_vec.from_polar((10, lower_angle + 90))
        pygame.draw.circle(
            screen,
            BLACK,
            (int(lower_end[0] + foot_vec.x), int(lower_end[1] + foot_vec.y)),
            3,
        )

    # Draw bugs
    for bug in bugs:
        if bug["active"]:
            pygame.draw.circle(
                screen, BUG_COLOR, (int(bug["x"]), int(bug["y"])), BUG_SIZE // 2
            )
            # Animated legs
            for i in range(4):
                angle = i * 90 + pygame.time.get_ticks() // 100
                leg_length = 10 + 3 * sin(pygame.time.get_ticks() / 200)
                end_x = bug["x"] + leg_length * cos(angle)
                end_y = bug["y"] + leg_length * sin(angle)
                pygame.draw.line(screen, BLACK, (bug["x"], bug["y"]), (end_x, end_y), 2)

    # Draw spike ball with spikes
    pygame.draw.circle(
        screen,
        SPIKE_BALL_COLOR,
        (
            int(spike_ball["x"] + SPIKE_BALL_SIZE / 2),
            int(spike_ball["y"] + SPIKE_BALL_SIZE / 2),
        ),
        SPIKE_BALL_SIZE // 2,
    )

    # Add rotating spikes
    spike_angle = pygame.time.get_ticks() / 10  # Rotate over time
    for i in range(8):
        angle = radians(spike_angle + i * 45)
        start_x = (
            spike_ball["x"] + SPIKE_BALL_SIZE / 2 + cos(angle) * SPIKE_BALL_SIZE / 2
        )
        start_y = (
            spike_ball["y"] + SPIKE_BALL_SIZE / 2 + sin(angle) * SPIKE_BALL_SIZE / 2
        )
        end_x = start_x + cos(angle) * SPIKE_LENGTH
        end_y = start_y + sin(angle) * SPIKE_LENGTH
        pygame.draw.line(screen, BLACK, (start_x, start_y), (end_x, end_y), 3)

    # Draw score and speed
    font = pygame.font.Font(None, 36)
    text = font.render(f"Score: {score}", True, BLACK)
    speed_text = font.render(f"Speed: {SPIDER_SPEED:.1f}x", True, BLACK)
    screen.blit(text, (10, 10))
    screen.blit(speed_text, (10, 50))

    # Draw lives
    lives_text = font.render(f"Lives: {lives}", True, LIVES_COLOR)
    screen.blit(lives_text, (WIDTH - lives_text.get_width() - 10, 10))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

8 Likes

Do you have some screenshots! Would love to see :slight_smile:

I tried out your game, and it was a blast!

It really ramped up the challenge toward the end with the ever-increasing speed — and that intense pace really added to the fun! It reminded me how far we’ve come from the days when new programmers were learning BASIC. I remember my very first game, and back then, we didn’t have the power of modern programming languages like Python or cutting-edge tools such as SambaNova to help us level up our coding skills right from the start.

I have two questions, though:

  1. Have you considered trying to add an end-of-game screen that displays the final score after all your lives are gone? It might give players a great sense of closure and let them see how far they got.

  2. If you used SambaNova’s platform to help write the Python code for this game, which model did you use? The game ran flawlessly for me after a simple “pip install pygame” — so I’m curious to try out the same model for my own programming projects!

P.S. Believe it or not, the skills you are developing on your computer right now can open up opportunities to build real, revenue-generating businesses: it’s like a puzzle where the right code unlocks a path to treasures beyond your wildest dreams. With the best tools in the world at your fingertips, the possibilities are endless. Keep up the great work, never stop learning, and don’t forget to thank your dad for being such a cool mentor!

1 Like

Hi @elena13 This is amazing!

I too love this game and love that you created it.

Just like @tonyd mentioned. I’d love to know the model you are using (I hope you are using DeepSeek) and also the prompt you put in!!!

Please keep sharing your progress and what you build!

Thank you for trying my game i will def put that end screen in!

1 Like

Yes im using R1 it was great :slight_smile:

1 Like

This is amazing!! Thank you for sharing!!!