Game Development Reference
In-Depth Information
carefully thought about what exactly the program is doing. There are three types of bugs
that can happen with your program:
— Syntax Errors are a type of bug that comes from typos in your program. When the
Python interpreter sees a syntax error, it is because your code is not written in proper
Python language. A Python program with even a single syntax error will not run.
— Runtime Errors are bugs that happen while the program is running (that is,
executing). The program will work up until it reaches the line of code with the error,
and then the program terminates with an error message (this is called crashing ).
The Python interpreter will display something called a "traceback" and show the line
where the problem happens.
— Semantic Errors are the trickiest bugs to fix. This bug does not crash the program,
and the program appears to work fine. However, it is not doing what the programmer
intended for the program to do. For example, if the programmer wants the variable
total to be the sum of the values in variables a , b , and c but writes total = a
+ b * c , then the value in total will be wrong. This won't cause the program to
crash immediately, but may or may not cause some other code to crash later on
because of the unexpected value in total .
Finding bugs in our program can be hard, if you even notice them at all! When running
your program, you may discover that sometimes functions are not called when they are
suppose to be, or maybe they are called too many times. You may code the condition for a
while loop wrong, so that it loops the wrong number of times. (A loop in your program
that never exits is a kind of bug is called an infinite loop . In order to stop this program,
you can press Ctrl-C in the interactive shell.) Any of these things could mistakenly happen
in your code if you are not careful.
It can be hard to figure out how your code could be producing a bug because all the lines
of code get executed very quickly and the values in variables change so often. A
debugger is a program that lets you step through your code one line at a time (in the same
order that Python executes them), and shows what values are stored in all of the variables.
A debugger lets you look at how each line of code affects your program. This can be very
helpful to figure out what exactly the program is doing.
A video tutorial on using the debugger that comes with IDLE can be found on this topic's
website at http://inventwithpython.com/videos/
Starting the Debugger
In IDLE, go ahead and open the Dragon Realm game that you made in the last chapter.
In the interactive shell, click on File and then Open , and then select dragon.py (or
whatever you named the file when you saved it).
After opening the dragon.py file, click on the Debug menu item at the top of the
interactive shell, and then click Debugger to make the Debug Control window appear
(Figure 7-1).
Search WWH ::




Custom Search