CPSC120A
Fundamentals of Computer Science I

Lab 16

Mouse Input

Use the command line to create a new directory called lab16 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Draw

Create a Python program in a file called draw.py. The program should allow the user to draw in the window by clicking and dragging the mouse. Do this by drawing a line between the mouse's previous location and the mouse's current location every time the mouse is dragged. Note, in order to get the drawing to persist calls to the pygame.fill function must be removed.

Whack-a-Mole

Download the whack_a_mole.py Python program to your lab16 directory. The program contains an outline for a Pygame program. Modify the program so that it plays the game whack-a-mole. Whack-a-mole is an old arcade game where moles pop up out of holes and you whack them back down with a giant foam mallet. In your version of whack-a-mole an image of a mole, or anything else you whant to hit with a giant foam mallet, appears at a random location on the screen. After a short random amount of time the mole moves to another random location. The user whacks the mole by clicking on it. If a mole is whacked, it appears in another random location and the amount of time until it moves is reduced. The whack-a-mole program should do the following:

  1. Modify the program so that it initializes and draws a sprite with an image of a mole. The initial location of the mole should be random.

  2. Modify the update function so that there is a small chance that the mole will move to another random location when the function is called. Do this by generating a random number between 0.0 and 1.0 and if the random number is less than 0. 03, then the sprite's location should change.

  3. Modify the handle_input function so that if the user clicks on the sprite, it moves to another random location. Use the Pygame Rect object's collidepoint function to test if the mouse click is on the sprite.

  4. Modify the handle_input function so that if the user clicks on the sprite, the probability that the sprite will move each time the update function is called increases.

Design Document

Create a design document for your assignment 5 game, on paper. The design document should include a diagram of what the game will look like. It should also have a text description of how to play the game including how to win or lose.

Please give me your design document before beginning work on the assignment.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only programs that have perfect style and flawless functionality will be accepted as complete.