Lab 05

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

COMP 1010

Lab 5

New Material Covered


• boolean variables
• IF statements (including nested IF statements)

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.

Start with the supplied file Lab05BronzeTemplate.pde which


will contain the basic program structure, and the global constants
defined below. Complete the program by adding a few state
variables, and two functions, so that it will draw a circle that moves
back and forth along a horizontal line, as shown here. The circle will
normally move at a constant speed, but will freeze in place whenever
the mouse button is pressed, resuming its motion when the mouse button is released.
1. The following constants are supplied. X_LEFT and X_RIGHT are the x coordinates of the ends of the
line, and Y is the y coordinate of everything (both ends of the line, and the centre of the circle).
BALL_DIAM and BALL_SPEED give the diameter of the circle (in pixels) and the speed of motion of
the circle (in pixels per frame). (But that motion will change between right-to-left and left-to-right.)
2. Define the “state variables” needed to keep track of the current situation. You need to know the
current position of the centre of the circle (use a float) and whether the circle is moving to the left,
or to the right (you must use a boolean variable for this).
3. Add the drawAll() function which will erase the window to grey, and draw the line and the circle.
4. Add the moveBall() function which will cause the circle to move back and forth along the line.
Whenever the centre of the circle goes beyond either end of the line, it should change direction.
Whenever the mouse button is pressed, the circle should not move. Otherwise it should move
BALL_SPEED pixels to the left, or to the right, according to the value of your boolean state
variable. Use IF statements to do these things (you will need at least 3 of them, perhaps 4).
Begin with the solution to the Gold exercise from Lab 4. Use your own solution
if you did that exercise. If not, you can use the posted solution from the website.
That program drew a "rose" such as the one shown on the right, but it used a
white line. In this exercise, modify that program so that will draw each "petal" of
the rose using a different random colour.

• In each frame, determine whether the newly calculated (x,y) point is


farther from the centre than the previous one. Use a boolean variable to
store this information. That tells you whether the line is moving away
from the middle, or toward it.
• When should you change the colour? When the line is now moving away
from the centre, but it wasn’t last time. Use a boolean expression to figure this out.
• Now you need a few more state variables to keep more information from the last frame. You will need
to know how far away the last point was, and whether it was moving toward the centre, or away from it.
One of these new state variables must be a boolean variable.
• To pick a random colour you can use stroke(random(256),random(256),random(256));

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!

You might also like