Professional Documents
Culture Documents
Lab 05
Lab 05
Lab 05
Lab 5
Notes:
• Remember that you only need to do one of the three levels (Bronze, Silver, or Gold) to obtain 2/2 on
the lab, although you are encouraged to do as many as you can.
• All three exercises are independent, and can be done in any order.
• The Gold exercise is larger than a normal lab exercise (about twice as big). Treat it more like a “mini
assignment” to create a fun game. It would not be practical to do it, from start to finish, in the available
lab time. Create it on your own, in advance, if you’re up to the challenge.
The number of lines of code that you will have to add to the old solution will be quite small. But you will have to
read, understand, and modify, some existing code, which is always a big part of computer programming.
Make a simple version of the game “Whack-a-Mole”, where you have
to hit targets with your mouse before they disappear. This will be
challenging, with quite a bit of logic, and many things to keep track of,
using global state variables. There’s no template this time – you’re on
your own.
The game should work as follows. A “mole” (a white circle) is
generated at a random location. It starts as a circle of size 0 and then
grows at some constant speed until a maximum size is reached (use
global named constants, of course). Then it shrinks slowly, at the same
speed, back to size 0, and then a new mole starts to grow at a new
random location. But if the user clicks on a mole before it disappears,
the mole is instantly “killed”, the player’s score increases by one (use
println to output the score to the console), and a new mole immediately Quick! Click on the circle before it disappears!
begins somewhere else.
You should:
• Use the distance from the mouse to the centre of the mole to detect a “hit”. This should be based on its
current size, not the maximum size.
• Use a boolean variable to control whether the mole is shrinking or growing.
• Use several small functions to do each of the main tasks that are required (for example, change the
mole's size, create a new mole, or detect a hit on the mole).
Hint: If you provide a void mouseClicked(){ } function, then it will automatically be called, once only,
every time the user clicks the mouse button. Use mouseX and mouseY to determine the location of the click.
You may find this much easier that checking mousePressed every frame in the draw() function.
Optional extras: After this much of the game is working, you can make it fancier. Make the moles different
colours or sizes or speeds. Give more points for smaller or faster ones. Make the moles go faster the longer the
game lasts. Add a “game over” after a certain number of moles, or after a certain amount of time. Print the score
in the canvas itself (look up the text and textSize commands). Have fun with it!