15-112Fall 2011 Quiz 7
20 Minutes.  No calculators, no notes, no books, no computers.

1.       [12 pts] True or False:   Circle all the TRUE statements:
A)  root.mainloop() should be called only once, and it does not return until the graphics window closes.
B)  root.mainloop() calls your mouse and key event handlers when mouse and key presses occur.
C)  root.mainloop() calls your timerFired event handler at regular intervals.
D)  root.mainloop() calls redrawAll after it calls event handlers.
E)  We create a Struct() to hold the values in canvas.data, but we could have used a set instead.
F)  If we do not call root.bind(), our mouse and key event handlers will not be called by root.mainloop().

2.       [8 pts] Fill in the blanks so the following draws a 20-wide by 30-high rectangle centered on the point (cx, cy).

canvas.create_rectangle(______________, ___________________, _______________, ______________)

3.       [15 pts] Sketch a picture of what this code draws, assuming the canvas is 300x300:
canvas.create_rectangle(50, 50, 250, 250)
canvas.create_oval(50, 50, 250, 100)
canvas.create_polygon(50, 150, 150, 250, 150, 50, fill="gray")
canvas.create_line(225, 225, 250, 250, width=6)
canvas.create_text(150, 50, text="ABC", anchor=S)

 

4.       [25 pts] These questions cover the Snake case study and tutorial.  Answer very briefly.

a.       What value represents food in the 2d board?

 

b.      How do we tell if the snake crosses its own path (runs into itself)?

 

c.       Why did we need ignoreLastTimerEvent?

 

d.      The removeTail function “gets rid of the old tail and makes the snake shrink back to its correct size (and thus appear to move forward one step in the given direction). “  In just a few words, how does it do this?

 

e.      What happens in placeFood if the new random food position is occupied by the snake?

 

5.       [40 pts] On the back of this sheet, assuming run() is already written and the canvas is 500x500, write init, timerFired, keyPressed, and redrawAll to get a 50x50 blue square bouncing up-and-down on the window’s right edge.   Centered in the square is a number, initially 0.  Each time the user presses any key except “q”, the number increases by 1.  When the user presses “q”, the animation quits, and (for bonus) displays “Done!” centered in the window.

6.       Bonus/Optional [5pts] Assuming a 500x500 window, what will the following draw:
z = 250
while (z < 500):
    r = (500-z)/2; canvas.create_oval(z-r,z-r,z+r,z+r); z = 500-r