0

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)
  • 1
    Using tuples of floats as a key is a bad idea because of round-off error. If your keys are really int tuples, you will have to use `int()` to change those floats to ints. If you want detailed help, post a [mcve] with an emphasis on "minimal". – John Coleman Dec 14 '22 at 22:16
  • Rather than trying to track the graphical position of the turtle, try to remember it as a simple, integer position in "world coordinates", which you can easily look up in a dictionary, and translate that to a screen position when drawing (do whatever arithmetic in order to figure out how to turn and move the turtle). Rather than trying to use the graphics API to figure out where the turtle is, just remember that information. This will make your life much easier in the long run. This kind of separation of graphics and logic is a fundamental game design principle. – Karl Knechtel Dec 14 '22 at 22:25
  • `turtle.pos()` returns a pair of floats (which seems like a strange design decision, using int pixels strikes me as more natural. Doubtless there is a reason for it). Your keys are int pairs. You would have to do something like `x,y = self.turt.pos()` and then use `(round(x), round(y))` as the lookup key. – John Coleman Dec 14 '22 at 22:29

0 Answers0