Common Errors¶
On this page we will learn
- how to read a Python traceback from a GameFrame game
- what the most common GameFrame errors mean
- how to fix each one
Terminology
- indentation – the spaces at the start of a line of code, which Python uses to show which block the line belongs to.
When something goes wrong, Python stops the game and prints a traceback in the terminal. A traceback lists the path Python took to reach the error, with the most recent step last. So the most useful lines are usually at the bottom:
- the last line says what went wrong (the type of error and a message)
- the
Filelines above it say where: look for the last one that's in your own Objects or Rooms folder, not in GameFrame
The errors below are the ones we're most likely to see. The file paths will be different on your computer.
No module named 'pygame'¶
- lines 2–3 → the error happened on line 4 of MainController.py, the very first thing the game does.
- line 4 → Python can't find Pygame.
Why: the game is running without our virtual environment, so it can't see the Pygame we installed there.
Fix: check the bottom-right of VS Code shows .venv (see Setup). If it doesn't, press Ctrl+Shift+P, choose Python: Select Interpreter and pick the one with .venv. In Thonny, see Using Thonny.
'module' object is not callable¶
- lines 2–4 → the error happened when GameFrame tried to create a Room.
- line 5 → GameFrame found a module (a file) instead of a class.
Why: we created a new Room but didn't add it to Rooms/__init__.py.
Fix: add from Rooms.RoomName import RoomName to Rooms/__init__.py. New objects need the same in Objects/__init__.py. We first saw this in Lesson 1.
No file found in working directory¶
- lines 2–7 → the game was running, and a timer called a method.
- lines 8–9 → Zork's
spawn_asteroidmethod created an asteroid. - lines 10–11 → this is the last line in our code: line 18 of Asteroid.py, where we set the image.
- lines 12–13 → GameFrame tried to load the image file.
- line 14 → there's no file called asteriod.png in the Images folder.
Why: the file name in our code doesn't match the real file. Here it's a typo (asteriod instead of asteroid).
Fix: check the file name in the Images (or Sounds) folder and copy it exactly, including capital letters. Windows doesn't mind if the capitals are different, but macOS and Linux do, so "Background.png" works on one computer and crashes on another.
name 'Globals' is not defined¶
- lines 4–5 → GameFrame was running each object's
stepmethod. - lines 6–9 → the Ship's
stepcalledkeep_in_room, and line 43 of Ship.py usedGlobals. - line 10 → Python doesn't know what
Globalsis.
Why: we used Globals in a file without importing it.
Fix: add it to the import at the top of the file: from GameFrame import RoomObject, Globals. The same error happens with any class we forget to import, like Asteroid in Zork.py.
object has no attribute¶
- lines 4–7 → GameFrame found a collision and called
handle_collision. - lines 8–9 → line 47 of Laser.py tried to use
self.score. - line 10 → the Laser doesn't have a
scoreattribute.
Why: the Score belongs to the Room, not the Laser. We wrote self.score instead of self.room.score.
Fix: use self.room. to reach anything that belongs to the Room: the score, the lives, and the sounds. If the message names the Room (for example 'GamePlay' object has no attribute 'lives'), check that the Room creates that attribute with self.lives = ….
'NoneType' object is not callable¶
- lines 4–7 → GameFrame was running a timer's method.
- line 8 → the timer was given
Noneinstead of a method.
Why: we put brackets after the method name in set_timer, for example self.set_timer(10, self.reset_shot()). The brackets run the method straight away, and the timer gets what it returns (None).
Fix: leave the brackets off: self.set_timer(10, self.reset_shot). See Passing a method as an argument. The traceback doesn't show our file here, so search our code for set_timer( and check each one.
unindent does not match any outer indentation level¶
- lines 2–9 → the game was still loading its files when the error happened.
- lines 10–11 → the problem is line 43 of Zork.py.
- line 12 → that line's indentation doesn't line up with the lines around it.
Why: the line has the wrong number of spaces (here 7 instead of 8). This often happens when pasting code.
Fix: line the code up with the lines above it. Inside a method, the code should be indented 8 spaces (two levels). VS Code shows faint vertical guide lines to help.
Nothing happens, but there's no error¶
Some mistakes don't cause an error. The game runs, but something doesn't work. Check these:
- Collision does nothing: the class name in
register_collision_object("Ship")andother_type == "Ship"must match the class name exactly, capital letters and all. - Key press does nothing: check
self.handle_key_events = Trueis in__init__, and the method is speltkey_pressed. steporhandle_collisionnever runs: check the method name is spelt exactly right. A misspelt method is just a new method that GameFrame never calls.- Object doesn't appear: check it's added to the Room with
add_room_object, and that itsxandyare on the screen.
Use print to investigate
Like we did in Lesson 8, add a print inside the method you think isn't running. If nothing appears in the terminal, the method isn't being called.