我正在尝试在 Pygame 中创建一个迷宫游戏,但它不起作用

问题描述 投票:0回答:0
# Import the Pygame library
import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the display
WIDTH = 800
HEIGHT = 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Maze")

# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Define the size of each cell in the maze
CELL_SIZE = 20

# Define the number of cells in the maze
NUM_ROWS = int(HEIGHT / CELL_SIZE)
NUM_COLS = int(WIDTH / CELL_SIZE)

# Define a 2D array to store the maze
maze = [[0 for i in range(NUM_COLS)] for j in range(NUM_ROWS)]

# Define a function to generate the maze using a depth-first search algorithm
def generate_maze():
    # Start at the top-left corner
    stack = [(0, 0)]
    
    # Loop until the stack is empty
    while stack:
        # Pop the current cell off the stack
        current = stack.pop()
        
        # Mark the current cell as visited
        maze[current[1]][current[0]] = 1
        
        # Get the neighbors of the current cell
        neighbors = []
        if current[0] > 0:
            neighbors.append((current[0] - 1, current[1]))
        if current[0] < NUM_COLS - 1:
            neighbors.append((current[0] + 1, current[1]))
        if current[1] > 0:
            neighbors.append((current[0], current[1] - 1))
        if current[1] < NUM_ROWS - 1:
            neighbors.append((current[0], current[1] + 1))
        
        # Shuffle the neighbors
        random.shuffle(neighbors)
        
        # Loop through the neighbors
        for neighbor in neighbors:
            # If the neighbor has not been visited
            if maze[neighbor[1]][neighbor[0]] == 0:
                # Mark the neighbor as visited
                maze[neighbor[1]][neighbor[0]] = 1
                
                # Remove the wall between the current cell and the neighbor
                if neighbor[0] < current[0]:
                    maze[current[1]][current[0] - 1] = 1
                elif neighbor[0] > current[0]:
                    maze[current[1]][current[0] + 1] = 1
                elif neighbor[1] < current[1]:
                    maze[current[1] - 1][current[0]] = 1
                else:
                    maze[current[1] + 1][current[0]] = 1
                
                # Add the neighbor to the stack
                stack.append(neighbor)

# Define a function to draw the maze
def draw_maze():
    for i in range(NUM_ROWS):
        for j in range(NUM_COLS):
            x = j * CELL_SIZE
            y = i * CELL_SIZE
            
            # Draw the walls of the cell
            if maze[i][j] == 0:
                pygame.draw.line(SCREEN, BLACK, (x, y), (x + CELL_SIZE, y))
                pygame.draw.line(SCREEN, BLACK, (x + CELL_SIZE, y), (x + CELL_SIZE, y + CELL_SIZE))
                pygame.draw.line(SCREEN, BLACK, (x + CELL_SIZE, y + CELL_SIZE), (x, y + CELL_SIZE))
                pygame.draw.line(SCREEN, BLACK, (x, y + CELL_SIZE), (x, y))

# Define the player's starting position
player_row = 0
player_col = 0

# Define a function to draw the player
def draw_player(row, col):
    x = col * CELL_SIZE
    y = row * CELL_SIZE
    pygame.draw.rect(SCREEN, RED, (x, y, CELL_SIZE, CELL_SIZE))

# Generate the maze
generate_maze()

# Draw the maze
draw_maze()

# Game loop
player_row = 0
player_col = 0
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player_row -= 1
            elif event.key == pygame.K_DOWN:
                player_row += 1
            elif event.key == pygame.K_LEFT:
                player_col -= 1
            elif event.key == pygame.K_RIGHT:
                player_col += 1
    
    # Clear the screen
    SCREEN.fill(WHITE)
    
    # Draw the maze and player
    draw_maze()
    draw_player(player_row, player_col)
    
    # Update the display
    pygame.display.update()

# Quit Pygame
pygame.quit()

我正在使用 Repl.it,它只是输出一个空白的白色屏幕,屏幕上有一个红点。我已经尝试对其进行故障排除并重写等等,但它不起作用。这真的很令人沮丧,请帮助我。我还使用 Chat GPT 尝试解决问题,但一点帮助也没有。令人难以置信的是它是多么无用。

python pygame maze
© www.soinside.com 2019 - 2024. All rights reserved.