Simple game (part III)

PART I

PART II

REPOSITORY

I think this could be the final version. I just could make the player fire the aliens.

Avoid the Falling Aliens: An Exciting Arcade Adventure

Welcome to “Avoid the Falling Aliens,” an exhilarating arcade game where quick reflexes and strategic shooting are key to survival. In this game, you control a basket at the bottom of the screen, aiming to dodge and shoot colorful aliens that descend from the sky. Use the left and right arrow keys to move the basket horizontally and press the “x” key to fire bullets upwards, targeting the falling aliens.

As time progresses, the game ramps up in difficulty. The aliens not only increase in speed but also in number, making your task increasingly challenging. When aliens collide with the basket, you lose a life, and if your lives drop to zero, the game is over. However, the fun doesn’t end there—when the game is over, the screen displays your score along with a “Game Over” message and prompts you to press any key to restart the adventure.

The game’s vibrant graphics, featuring red, green, and blue aliens, create a visually engaging experience. The added mechanic of aliens bouncing off each other and generating new aliens upon collision adds an extra layer of complexity, ensuring no two games are ever the same.

“Avoid the Falling Aliens” is not just about dodging; it’s about precision shooting, timely movements, and managing an ever-increasing onslaught of alien invaders. Whether you’re looking to beat your high score or simply enjoy a fun and dynamic gaming experience, “Avoid the Falling Aliens” offers endless entertainment. So, get ready, aim, and fire—see how long you can survive the alien invasion!

import pygame
import random
import time

# Initialize Pygame
pygame.init()

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
MAX_ALIENS = 20  # Cap the maximum number of aliens

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Avoid the Falling Aliens")

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

# Basket properties
basket_width = 100
basket_height = 20
basket_x = (SCREEN_WIDTH - basket_width) // 2
basket_y = SCREEN_HEIGHT - basket_height - 10
basket_speed = 10

# Load sprite sheet and split into individual images
def load_spritesheet(filename, sprite_width, sprite_height, num_sprites):
    sheet = pygame.image.load(filename).convert_alpha()
    sprites = []
    for i in range(num_sprites):
        rect = pygame.Rect(i * sprite_width, 0, sprite_width, sprite_height)
        image = sheet.subsurface(rect)
        sprites.append(image)
    return sprites

# Load alien images from sprite sheet
alien_images = load_spritesheet('aliens_spritesheet2.png', 30, 30, 3)
alien_width = alien_images[0].get_width()
alien_height = alien_images[0].get_height()

# Object properties
base_speed = 3

# Create initial objects
num_objects = 5
objects = [{'x': random.randint(0, SCREEN_WIDTH - alien_width), 
            'y': -alien_height, 
            'image': random.choice(alien_images), 
            'speed_x': random.choice([-1, 0, 1]) * random.randint(1, 3), 
            'speed_y': random.randint(base_speed, base_speed + 3)} for _ in range(num_objects)]

# Basket movement
def move_basket(keys, basket_x):
    if keys[pygame.K_LEFT] and basket_x > 0:
        basket_x -= basket_speed
    if keys[pygame.K_RIGHT] and basket_x < SCREEN_WIDTH - basket_width:
        basket_x += basket_speed
    return basket_x

# Check for collision with basket
def check_collision(basket_x, basket_y, object_x, object_y):
    if (object_x < basket_x + basket_width and
        object_x + alien_width > basket_x and
        object_y < basket_y + basket_height and
        object_y + alien_height > basket_y):
        return True
    return False

# Check for collision between two objects
def check_object_collision(obj1, obj2):
    if (obj1['x'] < obj2['x'] + alien_width and
        obj1['x'] + alien_width > obj2['x'] and
        obj1['y'] < obj2['y'] + alien_height and
        obj1['y'] + alien_height > obj2['y']):
        return True
    return False

# Add a new alien
def add_new_alien():
    if len(objects) < MAX_ALIENS:  # Add a new alien only if under the cap
        new_alien = {
            'x': random.randint(0, SCREEN_WIDTH - alien_width),
            'y': -alien_height,
            'image': random.choice(alien_images),
            'speed_x': random.choice([-1, 0, 1]) * random.randint(1, 3),
            'speed_y': random.randint(base_speed, base_speed + 3)
        }
        objects.append(new_alien)

# Main game loop
running = True
score = 0
lives = 3
start_time = time.time()
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    basket_x = move_basket(keys, basket_x)

    # Move the objects
    current_time = time.time()
    time_elapsed = current_time - start_time

    # Periodically add new aliens at a controlled rate
    if int(time_elapsed) % 10 == 0 and len(objects) < num_objects + int(time_elapsed / 10):
        add_new_alien()

    for obj in objects:
        obj['x'] += obj['speed_x']
        obj['y'] += obj['speed_y'] + time_elapsed * 0.01  # Increase speed over time
        
        if obj['x'] < 0 or obj['x'] > SCREEN_WIDTH - alien_width:
            obj['speed_x'] *= -1
            if len(objects) < MAX_ALIENS:  # Add new alien only if under the cap
                add_new_alien()

        if obj['y'] > SCREEN_HEIGHT:
            obj['y'] = -alien_height
            obj['x'] = random.randint(0, SCREEN_WIDTH - alien_width)
            obj['image'] = random.choice(alien_images)
            obj['speed_x'] = random.choice([-1, 0, 1]) * random.randint(1, 3)
            obj['speed_y'] = random.randint(base_speed, base_speed + 3) + int(time_elapsed / 10)
            score += 1  # Increase score when the object safely passes

        # Check for collision with basket
        if check_collision(basket_x, basket_y, obj['x'], obj['y']):
            lives -= 1
            obj['y'] = -alien_height
            obj['x'] = random.randint(0, SCREEN_WIDTH - alien_width)
            obj['image'] = random.choice(alien_images)
            obj['speed_x'] = random.choice([-1, 0, 1]) * random.randint(1, 3)
            obj['speed_y'] = random.randint(base_speed, base_speed + 3) + int(time_elapsed / 10)

    # Check for collisions between objects
    for i in range(len(objects)):
        for j in range(i + 1, len(objects)):
            if check_object_collision(objects[i], objects[j]):
                objects[i]['speed_x'], objects[j]['speed_x'] = objects[j]['speed_x'], objects[i]['speed_x']
                objects[i]['speed_y'], objects[j]['speed_y'] = objects[j]['speed_y'], objects[i]['speed_y']
                if len(objects) < MAX_ALIENS:  # Add new alien only if under the cap
                    add_new_alien()

    if lives <= 0:
        running = False

    # Fill the screen with a color
    screen.fill(WHITE)

    # Draw the basket
    pygame.draw.rect(screen, BLACK, (basket_x, basket_y, basket_width, basket_height))

    # Draw the objects
    for obj in objects:
        screen.blit(obj['image'], (obj['x'], obj['y']))

    # Display the score and lives
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"Score: {score}", True, BLACK)
    lives_text = font.render(f"Lives: {lives}", True, BLACK)
    screen.blit(score_text, (10, 10))
    screen.blit(lives_text, (10, 50))

    # Update the display
    pygame.display.flip()

    # Control the frame rate
    clock.tick(FPS)

pygame.quit()


Subscribe to the newsletter for updates
Tkinter templates

Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Claude's Games

Arkanoid
Platform 2d

1. Memory game

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Advertisement