Computer Science 15-112, Fall 2011
Class Notes:  Quiz8 Practice


These notes were prepared by CA's with the intention of helping students prepare for quiz8.  The CA's who wrote these notes have not seen or discussed the actual quiz8, which may or may not actually cover the material in these notes.


15-112 Fall 2011 Practice Quiz 8

30 minutes. No calculators, no notes, no books, no computers

Hint: You might need an additional piece of paper

 

1.      (40 points) 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 and increases its radius by one pixel until the bottom of the ball reaches the bottom of the window, after which it ignores the “d”.

 

2.      (20 points) Write the function fallingPieceIsLegal().  This function extracts the fallingPiece and other important variables from canvas.data, and returns True if the fallingPiece is legal and False otherwise.

 

3.      (10 points) Write the function placeFood() in snake. It generates a random location on the snakeBoard and keep picking random locations until it finds one that is not part of the snake.

 

Brief Questions:

 

4.      (5 points) Briefly, why do we need canvas.data at all?

 

5.      (2 points) What is the main purpose of the init() function?

 

6.      (3 points) List the two ways we can speed up the movement of an animation!

 

Tracing:

 

7.      (10 points) Assuming the usual run function with a 300x300 canvas, state in just a few words of plain English what this animation does in general:

 

def keyPressed():

key = event.char

if ((key < "A") or (key > "Z")):

canvas.data.keys += key

redrawAll()


def redrawAll():

canvas.delete(ALL)

keys = canvas.data.keys

x = len(keys)

canvas.create_text(150,100,text=str(x))

canvas.create_text(150,200,text=keys[x-3:x])

def init():

canvas.data.keys = "..."

 

(10 points) In the following 300x300 canvas, draw the preceding animation after the user types this:  Hi#Ho! 














 

 

Bonus:

 

8.      (5 points) Assuming the usual run function, state in just a few words of plain English what the following animation does in general:

 

def timerFired():

    canvas.data.c += 1

    redrawAll()

    canvas.after(50, timerFired)

   

def redrawAll():

    c = canvas.data.c % 20

    if (c == 0): canvas.delete(ALL)

    canvas.create_rectangle(20*c,300-10*c,20*c+10,20)

 

def init():

    canvas.data.c = 0