import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 1200, 1200
ROWS, COLS = 60, 60
SQUARE_SIZE = WIDTH // COLS
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Setup the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(“Maze Game”)
# Load and transform images
player_img = pygame.transform.scale(pygame.image.load(‘player.png’), (SQUARE_SIZE, SQUARE_SIZE))
def generate_complex_maze(rows, cols, num_exits=1):
“”” Generate a complex maze using DFS algorithm “””
# Initialize maze with walls
maze = [[‘X’ for _ in range(cols)] for _ in range(rows)]
exit_row = 0
exit_col = 0
# Directions (up, right, down, left)
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
def is_valid_cell(r, c):
“”” Check if the cell is valid for carving a path “””
if 0 <= r < rows and 0 <= c < cols:
return True
return False
def carve_path(r, c):
“”” Carve a path in the maze from the current cell “””
maze[r][c] = ‘ ‘
random.shuffle(directions)
for dr, dc in directions:
new_r, new_c = r + dr*2, c + dc*2
if is_valid_cell(new_r, new_c) and maze[new_r][new_c] == ‘X’:
# Carve the passage
maze[r + dr][c + dc] = ‘ ‘
#exit_row = new_r
carve_path(new_r, new_c)
# Start carving from the top-left corner
carve_path(1, 1)
# Create entrance and exits
maze[1][0] = ‘E’ # Entrance
exits_created = 0
while exits_created < num_exits:
exit_row, exit_col = random.randint(1, rows – 2), random.randint(1, cols – 2)
#exit_row, exit_col = new_r, new_c
if maze[exit_row][exit_col] == ” ” and sorted([maze[exit_row+1][exit_col],maze[exit_row][exit_col+1],maze[exit_row-1][exit_col],maze[exit_row][exit_col-1]]) == [” “,”X”,”X”,”X”] :
maze[exit_row][exit_col] = ‘O’ # Exit
exits_created += 1
return maze
# Generate a complex maze with specified dimensions and number of exits
maze = generate_complex_maze(ROWS, COLS, num_exits=1)
def draw_maze(screen, maze):
for r in range(ROWS):
for c in range(COLS):
color = WHITE
if maze[r][c] == ‘X’:
color = BLACK
elif maze[r][c] == ‘E’:
color = GREEN
elif maze[r][c] == ‘O’:
color = RED
pygame.draw.rect(screen, color, (c * SQUARE_SIZE, r * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
def main():
clock = pygame.time.Clock()
player_pos = [1, 0] # Starting position
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
new_row, new_col = player_pos[0], player_pos[1]
if event.key == pygame.K_UP:
new_row -= 1
if event.key == pygame.K_DOWN:
new_row += 1
if event.key == pygame.K_LEFT:
new_col -= 1
if event.key == pygame.K_RIGHT:
new_col += 1
if 0 <= new_row < ROWS and 0 <= new_col < COLS:
if maze[new_row][new_col] != ‘X’:
player_pos = [new_row, new_col]
if maze[new_row][new_col] == ‘O’: # Exit
print(“Congratulations! You’ve reached the exit!”)
running = False
screen.fill(WHITE)
draw_maze(screen, maze)
screen.blit(player_img, (player_pos[1] * SQUARE_SIZE, player_pos[0] * SQUARE_SIZE))
pygame.display.flip()
clock.tick(60)
if __name__ == “__main__”:
main()
發佈留言