importrandomfromkandinskyimport*fromionimport*# Screen dimensions
WIDTH=222HEIGHT=320# Constants for grid and square size
GRID_SIZE=10SQUARE_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=[[0for_inrange(GRID_SIZE)]for_inrange(GRID_SIZE)]# Player position
player_x=0player_y=0# Exit position
exit_x=GRID_SIZE-1exit_y=GRID_SIZE-1defdraw_square(x,y,color):"""Draw a square at the given coordinates with the specified color."""foriinrange(x,x+SQUARE_SIZE):forjinrange(y,y+SQUARE_SIZE):set_pixel(i,j,color)defgenerate_maze():"""Generate a random solvable maze."""stack=[(0,0)]visited=set([(0,0)])whilestack:x,y=stack[-1]neighbors=[]ifx>0and(x-1,y)notinvisited:neighbors.append((x-1,y))ifx<GRID_SIZE-1and(x+1,y)notinvisited:neighbors.append((x+1,y))ify>0and(x,y-1)notinvisited:neighbors.append((x,y-1))ify<GRID_SIZE-1and(x,y+1)notinvisited:neighbors.append((x,y+1))ifneighbors:next_x,next_y=random.choice(neighbors)maze[next_y][next_x]=1visited.add((next_x,next_y))stack.append((next_x,next_y))else:stack.pop()defdraw_maze():"""Draw the maze on the screen."""foryinrange(GRID_SIZE):forxinrange(GRID_SIZE):ifmaze[y][x]==0:draw_square(x*SQUARE_SIZE,y*SQUARE_SIZE,WALL_COLOR)else:draw_square(x*SQUARE_SIZE,y*SQUARE_SIZE,BACKGROUND_COLOR)defplace_exit():"""Place the exit randomly in the maze."""maze[exit_y][exit_x]=2draw_square(exit_x*SQUARE_SIZE,exit_y*SQUARE_SIZE,EXIT_COLOR)defplace_player():"""Place the player randomly in the maze."""globalplayer_x,player_ywhileTrue:player_x=random.randint(0,GRID_SIZE-1)player_y=random.randint(0,GRID_SIZE-1)ifmaze[player_y][player_x]==1:breakdraw_square(player_x*SQUARE_SIZE,player_y*SQUARE_SIZE,PLAYER_COLOR)defmove_player(dx,dy):"""Move the player by dx and dy."""globalplayer_x,player_ynew_x=player_x+dxnew_y=player_y+dyif0<=new_x<GRID_SIZEand0<=new_y<GRID_SIZEandmaze[new_y][new_x]!=0:draw_square(player_x*SQUARE_SIZE,player_y*SQUARE_SIZE,BACKGROUND_COLOR)player_x=new_xplayer_y=new_ydraw_square(player_x*SQUARE_SIZE,player_y*SQUARE_SIZE,PLAYER_COLOR)defsolve_maze():"""Simple recursive maze-solving algorithm."""ifplayer_x==exit_xandplayer_y==exit_y:returnTrueifmaze[player_y][player_x]==0:returnFalsemaze[player_y][player_x]=0ifplayer_x>0andsolve_maze():move_player(-1,0)returnTrueifplayer_x<GRID_SIZE-1andsolve_maze():move_player(1,0)returnTrueifplayer_y>0andsolve_maze():move_player(0,-1)returnTrueifplayer_y<GRID_SIZE-1andsolve_maze():move_player(0,1)returnTruereturnFalsedefinitialize_game():"""Initialize the game by generating maze, placing objects, and drawing them."""generate_maze()draw_maze()place_exit()place_player()# Main game loop
whileTrue:# Initialize the game
initialize_game()# Main game loop
whileTrue:# Check for arrow key inputs
dx=0dy=0ifkeydown(KEY_UP):dy=-1elifkeydown(KEY_DOWN):dy=1elifkeydown(KEY_LEFT):dx=-1elifkeydown(KEY_RIGHT):dx=1# Move the player
move_player(dx,dy)# Check if the player reached the exit
ifplayer_x==exit_xandplayer_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:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.