I have to create a maze game, which receives as an input a command from the user in order to play the game. I have written the code for the maze game. What I want to modify is to only show part of the maze when it is printed to the user (after a move has been made). Here is the maze I have:
level = [
["1"," ","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," "," "," "," "," "," "," "," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," "," "," ","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1","1","1"," "," "," "," "," "," ","1"],
["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"," ","1"]
]
start_maze = level[0][1] #start of maze
end_maze = level[9][23] #end of maze
With an output as such:
The initial configuration of the maze is:
1 X 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
How do I make it so that the player can only see part of the maze as he moves around, rather than being able to see the end (which makes it easy)?
So for example to have a view like this:
1 1 1 1
1 1 1
1 X 1 1
1
1
i.e. so that he can only see 2 other cells in each direction.
I hope I made the question clear. Below is the part of the code that is the main game, if that is needed:
player = {'y': 0, 'x': 1}
level[player['y']][player['x']] = 'X'
# Translate keywords into coordinate changes
move_modifications = {'UP': {'y': -1, 'x': 0},
'DOWN': {'y': 1, 'x': 0},
'LEFT': {'y':0, 'x': -1},
'RIGHT': {'y': 0, 'x': 1}}
def player_move(maze):
# Main game loop
play = True
while play:
move = input("Please enter a command (LEFT/RIGHT/UP/DOWN): ")
move = move.upper()
coords = move_modifications[move]
new_y = player['y'] + coords['y']
new_x = player['x'] + coords['x']
#Catch them if they try to leave the map
try:
maze_position = maze[new_y][new_x]
except IndexError:
print("Not on map")
continue
if maze_position != '1':
# Move on the map
maze[player['y']][player['x']] = ' '
maze[new_y][new_x] = 'X'
# Update player coords
player['y'] = new_y
player['x'] = new_x
# Print result
print_level(maze)
The rest of the code is just moving around, it doesn't include any printing of the maze.