dungeon_crawler.py

Created by naul

Created on June 02, 2023

4.16 KB


import random
from kandinsky import *
from ion import *

# Screen dimensions
WIDTH = 222
HEIGHT = 320

# Constants for grid and square size
GRID_SIZE = 10
SQUARE_SIZE = WIDTH // GRID_SIZE

# Player and exit colors
PLAYER_COLOR = color(0, 0, 255)  # Blue
EXIT_COLOR = color(0, 255, 0)  # Green
WALL_COLOR = color(100, 100, 100)  # Gray
BACKGROUND_COLOR = color(0, 0, 0)  # Black

# Maze grid
maze = [[0 for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]

# Player position
player_x = 0
player_y = 0

# Exit position
exit_x = GRID_SIZE - 1
exit_y = GRID_SIZE - 1

def draw_square(x, y, color):
    """Draw a square at the given coordinates with the specified color."""
    for i in range(x, x + SQUARE_SIZE):
        for j in range(y, y + SQUARE_SIZE):
            set_pixel(i, j, color)

def generate_maze():
    """Generate a random solvable maze."""
    stack = [(0, 0)]
    visited = set([(0, 0)])
    while stack:
        x, y = stack[-1]
        neighbors = []
        if x > 0 and (x - 1, y) not in visited:
            neighbors.append((x - 1, y))
        if x < GRID_SIZE - 1 and (x + 1, y) not in visited:
            neighbors.append((x + 1, y))
        if y > 0 and (x, y - 1) not in visited:
            neighbors.append((x, y - 1))
        if y < GRID_SIZE - 1 and (x, y + 1) not in visited:
            neighbors.append((x, y + 1))
        if neighbors:
            next_x, next_y = random.choice(neighbors)
            maze[next_y][next_x] = 1
            visited.add((next_x, next_y))
            stack.append((next_x, next_y))
        else:
            stack.pop()

def draw_maze():
    """Draw the maze on the screen."""
    for y in range(GRID_SIZE):
        for x in range(GRID_SIZE):
            if maze[y][x] == 0:
                draw_square(x * SQUARE_SIZE, y * SQUARE_SIZE, WALL_COLOR)
            else:
                draw_square(x * SQUARE_SIZE, y * SQUARE_SIZE, BACKGROUND_COLOR)

def place_exit():
    """Place the exit randomly in the maze."""
    maze[exit_y][exit_x] = 2
    draw_square(exit_x * SQUARE_SIZE, exit_y * SQUARE_SIZE, EXIT_COLOR)

def place_player():
    """Place the player randomly in the maze."""
    global player_x, player_y
    while True:
        player_x = random.randint(0, GRID_SIZE - 1)
        player_y = random.randint(0, GRID_SIZE - 1)
        if maze[player_y][player_x] == 1:
            break
    draw_square(player_x * SQUARE_SIZE, player_y * SQUARE_SIZE, PLAYER_COLOR)

def move_player(dx, dy):
    """Move the player by dx and dy."""
    global player_x, player_y
    new_x = player_x + dx
    new_y = player_y + dy
    if 0 <= new_x < GRID_SIZE and 0 <= new_y < GRID_SIZE and maze[new_y][new_x] != 0:
        draw_square(player_x * SQUARE_SIZE, player_y * SQUARE_SIZE, BACKGROUND_COLOR)
        player_x = new_x
        player_y = new_y
        draw_square(player_x * SQUARE_SIZE, player_y * SQUARE_SIZE, PLAYER_COLOR)

def solve_maze():
    """Simple recursive maze-solving algorithm."""
    if player_x == exit_x and player_y == exit_y:
        return True
    if maze[player_y][player_x] == 0:
        return False
    maze[player_y][player_x] = 0
    if player_x > 0 and solve_maze():
        move_player(-1, 0)
        return True
    if player_x < GRID_SIZE - 1 and solve_maze():
        move_player(1, 0)
        return True
    if player_y > 0 and solve_maze():
        move_player(0, -1)
        return True
    if player_y < GRID_SIZE - 1 and solve_maze():
        move_player(0, 1)
        return True
    return False

def initialize_game():
    """Initialize the game by generating maze, placing objects, and drawing them."""
    generate_maze()
    draw_maze()
    place_exit()
    place_player()

# Main game loop
while True:
    # Initialize the game
    initialize_game()

    # Main game loop
    while True:
        # Check for arrow key inputs
        dx = 0
        dy = 0
        if keydown(KEY_UP):
            dy = -1
        elif keydown(KEY_DOWN):
            dy = 1
        elif keydown(KEY_LEFT):
            dx = -1
        elif keydown(KEY_RIGHT):
            dx = 1

        # Move the player
        move_player(dx, dy)

        # Check if the player reached the exit
        if player_x == exit_x and player_y == exit_y:
            break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.