• Name your sketch using your name, the assignment number, and the question number, exactly
as in this example: LastnameFirstnameA2Q1.pde. You will submit only one program
in this assignment, for the highest question completed.
• Your program must run upon download to receive any marks.
• You may submit your program multiple times, but only the most recent version will be
• The marker will run your program, and may change the canvas size and the values of
constants. Your program should still work if any constants are changed in a reasonable way.
In this assignment you will create a miniature golf game. Each question builds on the previous.
Ensure one question is working before moving on to the next.
When complete, your program will generate a different hole configuration each time it is run.
The user will putt the ball by clicking the mouse, and the ball will move initially in the direction
of the mouse, at a speed that depends on the distance between the mouse and the ball. The ball
will slow down as it travels, and the ball will rebound off walls and obstacles. You will keep
track of the number of putts required to get the ball in the hole. See detailed instructions below.
You must use functions. Each function should have a single purpose. Your draw block should be
very simple, and contain calls to your other functions that do the work for you.
You need to determine which variables and constants are needed to satisfy the program
requirements. You must follow good programming practices and use global and local variables
Q1: Game Setup [8 marks]
Set up an Active Processing program that
will draw the initial configuration of the
Write a setupGame() function that will set
the initial value for all necessary variables
(see below), and a drawGame() function
that will draw the canvas as described below,
similar to the samples shown at right.
You are responsible for drawing the green,
ball, hole, and obstacle. A function to draw
the score is provided below.
Use globals to store the position and size of
the ball, hole, and obstacle, and to keep track
of the score. (Which globals should be
variable and which should be constant?)
Generate the initial game configuration as
• Choose a reasonable ball size, and set
the hole size to be twice the ball size.
• The green should be randomly
chosen to be either horizontal or
vertical (50-50 probability).
• The green should occupy the middle
third of the canvas.
• Place the ball (white in images at
right) on an imaginary tee, randomly
placed in the left third (if the hole is
horizontal) or bottom third (if the
hole is vertical) of the green. Make
sure that the entire ball is within the
• Place the hole (black in images at
right) randomly in the right (if
horizontal hole) or top (if vertical
hole) third of the green. Make sure that the hole is at least one hole width away from the
edge of the green.
• Place a rectangular obstacle randomly in the middle third of the green. Make sure that the
obstacle is entirely within the green. Set a minimum and maximum length for the width
and height of the obstacle, and choose different (random) dimensions each time the
Copy and paste the following drawScore() function into your program. Call it from your
drawGame() function. We will learn about text in Unit 8.
String toPrint = “Number of shots taken: ” + shotsTaken;
text(toPrint, width/2-textWidth(toPrint)/2, 50);
NOTE: In order for this function to work, you must keep track of the score in a global variable
Q2: Move the Ball [6 marks]
Make the ball move, such that:
• The initial speed is based on the distance from the ball to the mouse, where a small
distance between the mouse and ball gives a low speed and a large distance between the
mouse and ball gives a high speed.
To implement this variety in the speed:
o Determine the largest possible distance from the ball to the mouse, keeping in
mind that a mouse click will only register if the mouse is on the canvas.
o Choose a MAX_SPEED (try 10 pixels initially), and scale the distance from the
ball to the mouse into a ball speed between 0 and MAX_SPEED.
• Store the direction of motion for the ball as the angle from the ball to the location of the
mouse when clicked.
• Choose a SPEED_STEP (try 0.05 pixels initially), and reduce the ball speed by this
amount each frame.
• In each frame, convert the direction and speed to a change in x and y coordinates, and
update the ball position to make the ball move.
Running your program at this point should move the ball in a straight line, and the ball might
disappear off the canvas. In the next questions you will keep the ball on the green, detect when
the ball is in the hole, and make the ball bounce off the obstacle.
Q3: Keep the ball on the green [4 marks]
If the ball hits the edge of the green, it should bounce off of the edge to remain in bounds.
Test for each of the four edges of the green, for both the vertical and horizontal orientations:
• Use nested ifs and logic to reduce the number of tests that will be performed. (YOU need
to use your problem-solving ability to design the code.)
• If the edge of the ball hits the edge of the green, the ball should rebound.
• Adjust the direction of the ball so that it reflects off the edge, rather than bounces back
along the direction it came. It should be the same type of redirection as that which occurs
when you are playing a game of billiards. Play with the Demo page in UM Learn for
Running your program at this point should keep the ball on the green, but the ball will still be
ignoring the obstacle and hole.
Q4: Bounce off the obstacle [3 marks]
If the ball hits the obstacle, it should rebound, similar to the rebound off the edge of the green .
Add a test for each edge of the obstacle, so that the ball never enters or overlaps the obstacle.
Note that the rebounds don’t need to be perfect – the edge of the ball may pass through the
corner of the obstacle.
Q5: Keep Score and End the Game [3 marks]
Increase the number of shots taken each time the mouse is clicked.
To test if the ball is in the hole, test if the centre of the ball is inside the hole.
Once the ball is in the hole, the game should end. Use a boolean gameOver variable to control
the end of the game. When this variable is set to true then nothing should happen. Don’t move
the ball or respond to the mouse in any way. The game should be frozen.
[Programming Standards are worth 6 marks]
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.