Game Development Reference
In-Depth Information
Break Points
Stepping through the code one line at a time might still be too slow. Often you will want
the program to run at normal speed until it reaches a certain line. You can do this with
break points. A break point is set on a line when you want the debugger to take control
once execution reaches that line. So if you think there is a problem with your code on, say,
line 17, just set a break point on line 17 and when execution reaches that line, the debugger
will stop execution. Then you can step through a few lines to see what is happening. Then
you can click Go to let the program execute until it reaches the end (or another break
point).
To set a break point, right-click on the line that you want a break point on and select "Set
Breakpoint" from the menu that appears. The line will be highlighted with yellow to
indicate a break point is on that line. You can set break points on as many lines as you
want. To remove the break point, click on the line and select "Clear Breakpoint" from the
menu that appears.
Figure 7-5: The file editor with two break points set.
Example of Using Break Points
Let's try debugging a program with break points. Here is a program that simulates coin
flips by calling random.randint(0, 1) . Each time this function call returns the
integer 1 , we will consider that "heads" and increment a variable called heads . We will
also increment a variable called flips to keep track of how many times we do this "coin
flip".
The program will do "coin flips" one thousand times. This would take a person over an
hour to do, but the computer can do it in one second! Type in the following code into the
file editor and save it as coinFlips.py . You can also download this code from
http://inventwithpython.com/coinFlips.py
Search WWH ::




Custom Search