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()