I am making a turtle dungeon escape game in python. The coordinates of the turtle are locked to be within the area of the dungeon walls. The coordinates of the turtle are used in order to write the correct description for the room the user is in. Some coordinates that are listed within the dictionaries used to translate the coordinates into the description of the room are returning KeyErrors despite the keys being in the dictionaries.
import turtle
from inspect import getframeinfo, stack
class Story:
def __init__(self, location, qdescription):
self.location = location
self.qdescription = qdescription
self.wn = turtle.Screen() # Get a reference to the window
self.turt = turtle.Turtle() # Create our favorite turtle
self.turtwrite = turtle.Turtle() # Creates another turtle specifically for writing
# These lines "wire up" keypresses to the handlers we've defined.
self.wn.onkey(self.h1, "Up")
self.wn.onkey(self.h2, "Left")
self.wn.onkey(self.h3, "Right")
self.wn.onkey(self.h4, "Down")
self.wn.onkey(self.h5, "q")
# Now we need to tell the window to start listening for events,
# If any of the keys that we're monitoring is pressed, its
# handler will be called.
self.wn.listen()
self.wn.mainloop()
def h1(self):
up = [(-37, 62), (-62, 62), (-37, 87), (-37, 112), (-37, 137), (-37, 162), (-37, 187), (-37, 212), (-37, 237), (-12, 62), (13, 62), (-12, 37), (13, 87), (38, 62), (13, 112), (38, 37), (88, 62), (63, 37)]
roomdict = {(13, 87): 'maze5'}
roomdesc = {'maze5': 'You see a tunnel that is lit with a single torch burning a green flame. There are 2 ways to go: [North and South].'}
if self.turt.pos() in up:
self.turt.setheading(90)
self.turt.forward(25)
self.location = roomdict[self.turt.pos()]
self.qdescription = roomdesc[self.location]
The error that is returning is:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\M\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\M\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 685, in eventfun
fun()
h1
self.location = roomdict[self.turt.pos()]
KeyError: (13.00,87.00)