Difference between revisions of "Debugging short exercise"
From WLCS
(Created page with "'''Objective''': Practice debugging techniques This program is supposed to break once the ball has been caught, but it doesn't work. Your job is to work out why, and then fix it...") |
|||
Line 4: | Line 4: | ||
Your job is to work out why, and then fix it. | Your job is to work out why, and then fix it. | ||
− | # | + | # Copy the program at the bottom of this page, and open it in Dr. Python |
− | # Add print statements to trace program execution | + | # Add print statements to trace program execution, like this: <syntaxhighlight lang="python"> print "Trace: At start" </syntaxhighlight> Add at least five statements: |
##Start of the program | ##Start of the program | ||
##Once the game window has been created | ##Once the game window has been created | ||
Line 13: | Line 13: | ||
# Run the program and stop it ''as soon as the buggy behavior has occurred''. | # Run the program and stop it ''as soon as the buggy behavior has occurred''. | ||
# Analyse the console output (the debug 'trace'). <br /> What would you expect to see if the program was running properly? | # Analyse the console output (the debug 'trace'). <br /> What would you expect to see if the program was running properly? | ||
− | #Is the problem that the distance isn't being properly calculated? <br /> Add another print statement that prints it | + | #Is the problem that the distance isn't being properly calculated? <br /> Add another print statement that prints it, like this: <syntaxhighlight lang="python"> print "Distance: "+ distance(ball_x, ball_y, mitt_x, mitt_y) </syntaxhighlight> |
# Once you've worked out what the real problem is, fix it. | # Once you've worked out what the real problem is, fix it. | ||
# Submit your code to school web locker | # Submit your code to school web locker | ||
+ | |||
+ | <syntaxhighlight lang="python"> | ||
+ | from gasp import * | ||
+ | |||
+ | def distance(x1, y1, x2, y2): | ||
+ | return ((x2 - x1)**2 + (y2 - y1)**2)**0.5 | ||
+ | |||
+ | begin_graphics(800, 600, title="Catch", background=color.YELLOW) | ||
+ | set_speed(120) | ||
+ | |||
+ | ball_x = 10 | ||
+ | ball_y = 300 | ||
+ | ball = Circle((ball_x, ball_y), 10, filled=True) | ||
+ | dx = 4 | ||
+ | dy = random_between(-4, 4) | ||
+ | |||
+ | mitt_x = 740 | ||
+ | mitt_y = 300 | ||
+ | mitt = Circle((mitt_x, mitt_y), 60) | ||
+ | |||
+ | while True: | ||
+ | # move the ball | ||
+ | if ball_y >= 590 or ball_y <= 10: | ||
+ | dy *= -1 | ||
+ | |||
+ | ball_x += dx | ||
+ | if ball_x >= 810 or ball_x <= 10: | ||
+ | dx *= -1 | ||
+ | |||
+ | ball_y += dy | ||
+ | ball_x += dx | ||
+ | move_to(ball, (ball_x, ball_y)) | ||
+ | |||
+ | # check on the mitt | ||
+ | if key_pressed('k') and mitt_y <= 580: | ||
+ | mitt_y += 5 | ||
+ | elif key_pressed('j') and mitt_y >= 20: | ||
+ | mitt_y -= 5 | ||
+ | |||
+ | if key_pressed('escape'): | ||
+ | break | ||
+ | |||
+ | move_to(mitt, (mitt_x, mitt_y)) | ||
+ | |||
+ | if distance(ball_x, ball_y, mitt_x, mitt_y) <= -60: # ball is caught | ||
+ | remove_from_screen(ball) | ||
+ | break | ||
+ | |||
+ | update_when('next_tick') | ||
+ | |||
+ | end_graphics() | ||
+ | </syntaxhighlight> |
Revision as of 23:39, 2 February 2011
Objective: Practice debugging techniques
This program is supposed to break once the ball has been caught, but it doesn't work. Your job is to work out why, and then fix it.
- Copy the program at the bottom of this page, and open it in Dr. Python
- Add print statements to trace program execution, like this: Add at least five statements:
print "Trace: At start"
- Start of the program
- Once the game window has been created
- Inside of the main game update loop
- Inside of the if statement that checks if the ball has been caught
- End of program
- Run the program and stop it as soon as the buggy behavior has occurred.
- Analyse the console output (the debug 'trace').
What would you expect to see if the program was running properly? - Is the problem that the distance isn't being properly calculated?
Add another print statement that prints it, like this:print "Distance: "+ distance(ball_x, ball_y, mitt_x, mitt_y)
- Once you've worked out what the real problem is, fix it.
- Submit your code to school web locker
from gasp import *
def distance(x1, y1, x2, y2):
return ((x2 - x1)**2 + (y2 - y1)**2)**0.5
begin_graphics(800, 600, title="Catch", background=color.YELLOW)
set_speed(120)
ball_x = 10
ball_y = 300
ball = Circle((ball_x, ball_y), 10, filled=True)
dx = 4
dy = random_between(-4, 4)
mitt_x = 740
mitt_y = 300
mitt = Circle((mitt_x, mitt_y), 60)
while True:
# move the ball
if ball_y >= 590 or ball_y <= 10:
dy *= -1
ball_x += dx
if ball_x >= 810 or ball_x <= 10:
dx *= -1
ball_y += dy
ball_x += dx
move_to(ball, (ball_x, ball_y))
# check on the mitt
if key_pressed('k') and mitt_y <= 580:
mitt_y += 5
elif key_pressed('j') and mitt_y >= 20:
mitt_y -= 5
if key_pressed('escape'):
break
move_to(mitt, (mitt_x, mitt_y))
if distance(ball_x, ball_y, mitt_x, mitt_y) <= -60: # ball is caught
remove_from_screen(ball)
break
update_when('next_tick')
end_graphics()