I'm stuck on the fifth stage of my assignment, the question is;

'Add in code to the key-press event handler so that the 'person' is constrained to move within the array and cant' walk through a wall.'


I'm not sure how i'm supposed to update the curindex, i really need some ideas so any help would be appreacited heaps!

The following is the code;

import maze
import graphics

h = 20
w = 20
cellsize = 20
startpos = maze.Index(1,1)
curindex = startpos # stage 5
endpos = maze.Index(h, w)
win = graphics.GraphWin("my Maze", w*cellsize, h*cellsize)
mymaze = maze.Maze(win, h, w, cellsize,startpos ,endpos )

person = graphics.Circle(graphics.Point(cellsize/2+2, cellsize/2+2), cellsize/2)
person.setFill(mymaze.COLOURS)
person.draw(win)

def handleKeys(event):
global curindex
print('Key pressed: ', event.keysym, event.keycode)
if event.keysym == 'Right':
person.move(cellsize, 0) # stage 5
mymaze.RIGHT = Index(0,1)
curindex = Index(3,4), Index(3,5)

# update curindex
# can do this by adding mymaze.RIGHT to curindex
elif event.keysym == 'Left':
person.move(-cellsize, 0)
elif event.keysym == 'Up':
person.move(0, -cellsize)
elif event.keysym == 'Down':
person.move(0, cellsize)


win.bind_all('<Key>', handleKeys)
win.mainloop()


win.mainloop()

Please push (CODE) button before pasting code:

import maze
import graphics

h = 20
w = 20
cellsize = 20
startpos = maze.Index(1,1)
curindex = startpos # stage 5 
endpos = maze.Index(h, w)
win = graphics.GraphWin("my Maze", w*cellsize, h*cellsize)
mymaze = maze.Maze(win, h, w, cellsize,startpos ,endpos )

person = graphics.Circle(graphics.Point(cellsize/2+2, cellsize/2+2), cellsize/2)
person.setFill(mymaze.COLOURS['person'])
person.draw(win)

def handleKeys(event):
        global curindex 
        print('Key pressed: ', event.keysym, event.keycode)
        if event.keysym == 'Right':
                person.move(cellsize, 0) # stage 5 
        mymaze.RIGHT = Index(0,1)
curindex = Index(3,4), Index(3,5)        

                # update curindex
                # can do this by adding mymaze.RIGHT to curindex
        elif event.keysym == 'Left':
                person.move(-cellsize, 0) 
        elif event.keysym == 'Up':
                person.move(0, -cellsize) 
        elif event.keysym == 'Down':
                person.move(0, cellsize) 


win.bind_all('<Key>', handleKeys)
win.mainloop()


win.mainloop()

Edited 3 Years Ago by mike_2000_17: Fixed formatting

After fixing indention little, I got import error for non existing module maze. For checking available neighbours you could check my code snippet for boggle solver. It has part were I generate all valid letter pairs for all parts of the board. You could adapt that for the maze movement allowed in all parts of maze.

I'm not really sure what you mean? adapt the maze to move?

Moving in the maze, I meant.

You could list of all neighbours valid for each place in maze and then it would be not necessary to check the walls as that direction would not be available from that point of maze.

Did you check my code? This is the part were I generate the valid neighbours, you should add appropriate check for wall in addition to maze dimensions:

neighbours=(
       (-1,-1),    (-1,0), (-1,1), ## above line
       (0,-1),             (0,1),  ## same line
       (1,-1),     (1,0),  (1,1)   ## next line
       )
   neighbour_indexpairs = set(((i,j), (i+di,j+dj))
                            for i in range(dimension)
                            for j in range(dimension)
                            for di,dj in neighbours if  0<=dj+j<dimension and 0<=di+i<dimension
                            )
This question has already been answered. Start a new discussion instead.