# Quiz #5 Solutions # December 4, 2012 # Each problem was worth two points, except for #9, which was worth four points. # 1. The following lines should be included: if event.key == pygame.K_q: flag = False # 2. There are a couple of solutions. The most direct method is to type if len(typed) == 21: # >= 21 or > 20 both work too. flag = False # Notice that we used 21, since typed = ['z'] already has one character to begin with. # An alternate method is to include these lines at strategic spots count = 0 ... count = count + 1 ... if count == 20: flag = False # 3. Using this range ensures that letters stay on the screen, and not even # very close to the edge, when printed on the pygame window. # 4. If we began with typed = [ ], the empty list, then when we first reached the line if typed[-1] == typed[-2]: # there would be a run-time error, since there would only be one item in the list # at that point, so typed[-2] would be "out of range". # 5. It keeps track of the x-coordinate of the left edge of the paddle. # 6. In the line if ballcount < 2: # we should change the 2 to a 1. (Or type if ballcount == 0:) # 7. There are two methods for speeding up the game. We could either # increase the speed of the balls by changing the code to assign # larger values to dx and dy at the start of the program, OR # we could increase the frame rate above 50 per second by typing Clock.tick(80) # Note that the latter results in smoother animation, so is preferable. # 8. These are the lines that bounce the ball off the paddle. # 9. There are five lines that need to be modified. I awarded one point # per line, so only four changes need to be mentioned to receive full credit. screen = pygame.display.set_mode([700,600]) if px > 600: px = 600 if balllist[j][0] > 690: balllist[j][0] = 1380 - balllist[j][0]