15-110 Fall 2010 Quiz 7
20 Minutes

* No calculators, no notes, no books, no computers.

Part 1:  Multiple Choice  (25 pts;  5 pts each)

1.       In the keyPressed function, how do we determine which key the user pressed?
A) canvas.data[“keysym”]
B) The keysym parameter
C) event.keysym
D) The keysym global variable
E) None of the above.

2.       How is the mousePressed function called?
A) By the run function.
B) By the init function.
C) By the timerFired function.
D) By the Tkinter event loop (mainloop).
E) None of the above.

3.       Which is the best way to speed up a game such as Snake ?
A) Call timerFired repeatedly from redrawAll.
B) Decrease the delay parameter in timerFired.
C) Increase canvas.data[“speed”].
D) Use smaller values in canvas.data[“snakeBoard”].
E) None of the above.

4.       What is the main purpose of the init function?
A) To draw the board in its initial configuration.
B) To register or “bind” the initial functions that handle events.
C) To load canvas.data with default or initial values.
D) None.  This function is not used in our events and animations model.
E) None of the above.

5.       Which of the following is not true about the redrawAll function?
A) It is called by event handlers (mousePressed, keyPressed, timerFired).
B) It first calls canvas.delete(ALL) to delete whatever was previously drawn.
C) It may call helper functions to draw different parts of the game or animation.
D) It (or its helper functions) are the only place where we draw on the canvas.
E) None of the above.

Part 2:  Short Answers  (25 pts;  12.5 pts each)

6.       Briefly, why is canvas.data a dictionary and not, say, a list?


7.       Briefly, why do we need canvas.data at all?

Part 3:  Writing Code (50 pts)

8.  (50 pts) Write the init function, the keyPressed function, and the redrawAll function (and nothing else) for a program where a red ball of radius 10 starts in the middle of a 250x250 window and each time the user presses “d”, the ball moves down one pixel until the bottom of the ball reaches the bottom of the window, after which it ignores the “d”.
Hint:  You may want to use event.widget.canvas and also canvas.delete(ALL) in your answer.


Part 4:  Bonus/Optional (5 pts)

9.  Assuming the usual run function, state in just a few words of plain English what the following animation does in general:
def timerFired(canvas):
    canvas.data["c"] = canvas.data.get("c",0) + 1
    redrawAll(canvas)
    canvas.after(50, timerFired, canvas)
def redrawAll(canvas):
    c = canvas.data["c"] % 20
    if (c == 0): canvas.delete(ALL)
    canvas.create_rectangle(20*c,300-10*c,20*c+10,20)