In this game, you collect as many stars as you can in 30 seconds. Each star is worth 10 points. Your goal is to get as many points as you can in the 30 seconds.

Click here for a demonstration 

In order to play the game, copy the code below and paste it into your python file. Run the code and enjoy!

Copy the code for FREE!:

import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Star collector")

# Set up colors
black = (0, 0, 0)
white = (255, 255, 255)
yellow = (255, 255, 0)
red = (255, 0, 0)

# Set up player
player_size = 50
player_x = width // 2 - player_size // 2
player_y = height // 2 - player_size // 2
player_speed = 5

# Set up stars
stars = []
star_size = 20
num_stars = 10

# Set up obstacles
obstacles = []
obstacle_size = 30
num_obstacles = 5

# Set up projectiles
projectiles = []
projectile_size = 10
projectile_speed = 7

# Set up score and timer
score = 0
font = pygame.font.SysFont(None, 30)
timer = 30  # in seconds
start_time = pygame.time.get_ticks()

# Set up end game screen
end_game_font = pygame.font.SysFont(None, 60)

def respawn_stars():
    for _ in range(num_stars):
        star = {
            'x': random.randint(0, width - star_size),
            'y': random.randint(0, height - star_size)
        }
        stars.append(star)

def draw_objects():
    for star in stars:
        pygame.draw.rect(screen, yellow, (star['x'], star['y'], star_size, star_size))

    for obstacle in obstacles:
        pygame.draw.rect(screen, red, (obstacle['x'], obstacle['y'], obstacle_size, obstacle_size))

    for projectile in projectiles:
        pygame.draw.rect(screen, white, (projectile['x'], projectile['y'], projectile_size, projectile_size))

    pygame.draw.rect(screen, white, (player_x, player_y, player_size, player_size))

    # Display score and timer
    score_text = font.render(f"Score: {score}", True, white)
    screen.blit(score_text, (10, 10))

    time_passed = (pygame.time.get_ticks() - start_time) // 1000
    remaining_time = max(timer - time_passed, 0)
    timer_text = font.render(f"Time: {remaining_time}", True, white)
    screen.blit(timer_text, (width - 120, 10))

def end_game():
    end_text = end_game_font.render(f"Game Over\nYour Score: {score}\nCan you do better?", True, white)
    screen.blit(end_text, (width // 2 - 200, height // 2 - 60))
    pygame.display.flip()
    pygame.time.wait(3000)  # Display the end screen for 3 seconds
    pygame.quit()
    sys.exit()

# Main game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # Handle shooting projectiles
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            projectile = {
                'x': player_x + player_size // 2 - projectile_size // 2,
                'y': player_y,
                'direction': 'up'
            }
            projectiles.append(projectile)

    # Handle player movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_x > 0:
        player_x -= player_speed
    if keys[pygame.K_RIGHT] and player_x < width - player_size:
        player_x += player_speed
    if keys[pygame.K_UP] and player_y > 0:
        player_y -= player_speed
    if keys[pygame.K_DOWN] and player_y < height - player_size:
        player_y += player_speed

    # Update stars and obstacles
    if not stars:
        respawn_stars()

    for obstacle in obstacles:
        obstacle['y'] += 3  # Obstacles move down the screen

        # Check for collisions with projectiles
        for projectile in projectiles:
            if (
                obstacle['x'] < projectile['x'] < obstacle['x'] + obstacle_size and
                obstacle['y'] < projectile['y'] < obstacle['y'] + obstacle_size
            ):
                obstacles.remove(obstacle)
                projectiles.remove(projectile)
                score += 20

    # Update projectiles
    projectiles = [{'x': proj['x'], 'y': proj['y'] - projectile_speed, 'direction': proj['direction']} for proj in projectiles if proj['y'] > 0]

    # Check for star collisions
    collected_stars = [star for star in stars if
                       player_x < star['x'] < player_x + player_size and
                       player_y < star['y'] < player_y + player_size]

    for star in collected_stars:
        stars.remove(star)
        score += 10

    # Check for obstacle collisions with the player
    if any(
        player_x < obstacle['x'] + obstacle_size and
        player_x + player_size > obstacle['x'] and
        player_y < obstacle['y'] + obstacle_size and
        player_y + player_size > obstacle['y']
        for obstacle in obstacles
    ):
        end_game()

    # Update display
    screen.fill(black)

    # Calculate remaining time and check if the game is over
    time_passed = (pygame.time.get_ticks() - start_time) // 1000
    remaining_time = max(timer - time_passed, 0)

    if remaining_time <= 0:
        end_game()
    else:
        draw_objects()
        pygame.display.flip()

    # Cap the frame rate
    pygame.time.Clock().tick(60)

Leave a Reply

Your email address will not be published. Required fields are marked *