Untitled

 avatar
unknown
plain_text
25 days ago
3.5 kB
1
Indexable
import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)

# Screen setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Stickman Death Maze")

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

# Stickman class
class Stickman(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((20, 40))
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.speed = 5

    def move(self, keys):
        if keys[pygame.K_LEFT]:
            self.rect.x -= self.speed
        if keys[pygame.K_RIGHT]:
            self.rect.x += self.speed
        if keys[pygame.K_UP]:
            self.rect.y -= self.speed
        if keys[pygame.K_DOWN]:
            self.rect.y += self.speed

# Obstacle class
class Obstacle(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, color):
        super().__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)

# Level setup
levels = [
    {  # Level 1: Rotating Platforms
        "obstacles": [(200, 300, 100, 20, RED), (400, 300, 100, 20, GREEN), (600, 300, 100, 20, YELLOW)],
        "goal": (700, 50, 50, 50, BLUE)
    },
    {  # Level 2: Rising Water
        "obstacles": [(100, 500, 600, 20, BLUE), (200, 400, 400, 20, YELLOW)],
        "goal": (700, 50, 50, 50, GREEN)
    },
    {  # Level 3: Laser Grid
        "obstacles": [(150, 0, 10, 600, RED), (300, 0, 10, 600, RED), (450, 0, 10, 600, RED)],
        "goal": (700, 50, 50, 50, YELLOW)
    },
]

# Load level
current_level = 0
stickman = Stickman(50, 550)
all_sprites = pygame.sprite.Group()
all_sprites.add(stickman)

obstacles = pygame.sprite.Group()
goal = None

# Function to load a level
def load_level(level):
    global obstacles, goal
    obstacles.empty()
    for obs in level["obstacles"]:
        obstacle = Obstacle(*obs)
        obstacles.add(obstacle)
    goal_rect = level["goal"]
    goal = Obstacle(*goal_rect)

load_level(levels[current_level])

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

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    stickman.move(keys)

    # Check collision with obstacles
    if pygame.sprite.spritecollideany(stickman, obstacles):
        print("You hit an obstacle! Try again.")
        stickman.rect.topleft = (50, 550)  # Reset position

    # Check if reached goal
    if stickman.rect.colliderect(goal.rect):
        current_level += 1
        if current_level >= len(levels):
            print("You win!")
            running = False
        else:
            load_level(levels[current_level])
            stickman.rect.topleft = (50, 550)

    # Draw everything
    all_sprites.draw(screen)
    obstacles.draw(screen)
    screen.blit(goal.image, goal.rect.topleft)

    # Update display
    pygame.display.flip()

    # Cap frame rate
    clock.tick(60)

pygame.quit()
sys.exit()
Leave a Comment