Any game developer or coder knows all about errors! Even the pros know they can't expect to create anything cool - a game, a program, an animation, or even a character - without running into some errors (often called bugs) along the way.
So if you're getting errors as you're working on a project, don't worry! Errors are just mysteries to solve on the way to creating something awesome.
Once you've got an error, how do you solve it? Turn into a detective! Grab that deerstalker cap and special magnifying glass and start investigating the mystery. You're going to search for clues, come up with a solution, and then test your hypothesis to make sure it's correct.
Searching for Clues
Troubleshooting detectives also know to look for error messages - more hints from your game engine or code editor that tell you what type of error happened and perhaps where the error occurred. Suppose, while building a snake game in Python, you tried to test the game and discovered this error message:
Traceback (most recent call last): File "C:/Users/info/PycharmProjects/SnakeGame/slither_like_snake.py", line 56, in <module> screen.onkey(up, "Up") NameError: name 'up' is not defined
Hmm... What's happening here? Well, we have two clues! The first is a place, "Line 56." That means that on or about this line, the error occurred. The second clue is the NameError, or name of the error - the name "up" is not defined. (In other words, the project interpreter doesn't know what you mean when you say "up.") What do you do with those clues?
Solving the Mystery
Start by going to the first clue, the line number. What's on or around line 56?
# create key bindings to control the snake screen.onkey(up, "Up") screen.onkey(up, "w")
Lines 56 and 57 tell the game's screen to call a certain function (up) whenever the "Up" key is pressed. The up function is defined elsewhere in our script and contains instructions that moves the snake up. This code looks okay, right? The Up Arrow key is identified correctly ("Up") and the function name, up, is too. The function and the key are also listed in the right order (first the function, and then the key). What could be wrong?
Let's take a closer look at our thinking. While the Up Arrow key is identified correctly, are we really sure the function "up" is? The error message - our second clue - specifically said it didn't know what "up" meant. So maybe we'd better double-check that function.
def Up(): move("up")
There's the error! While we did define a function named "up," it's the word "up," with a capital U, not a lowercase U! The Python interpreter doesn't connect this Up() function with the lowercase "up" function we mentioned in line 56, because it sees that one has an uppercase letter and one has a lowercase letter and decided these are different.
Testing Our Hypothesis
Okay, so we think we know what the problem is now. Let's test! Change the function to a lowercase U, like this:
def up(): move("up")
Now try to run the program and see what happens.
The program works! Our error mystery is solved.
Thanks, Detective Troubleshooter
Well done, Detective! Searching for clues, coming up with a solution, and then testing the hypothesis worked perfectly. Take a well-deserved break, and we'll call you again if another error pops up.
Pssst. Students, does your Detective Troubleshooter need some help? Call on one of our instructors to see if their detectives can give yours a hand! Head over to Discord and we'll solve this as a team.