ASSIGNMENT 2: Games
COURSE NUMBER: COMP 1010
COURSE TITLE: Introduction to Computer Science 1 TERM: Fall 2018
Assignment 2: Games
DUE DATE: OCTOBER 21, 2018 AT 11:59PM
Notes:
- Name your sketches using your name, the assignment number, and the question number, exactly as in this example: LastnameFirstnameA2Q1.
- Your programs must run upon download to receive any marks.
- Submit one pde file for each question.
- Assignments must follow the programming standards document published on the course website on UMLearn.
- These assignments are your chance to learn the material for the exams. Code your assignments independently.
We use software to compare all submitted assignments to each other, and pursue academic dishonestly vigorously.
Q1: Dice-Game Simulator – 10 marks
Implement a dice-game simulator that uses the rules below. You will visualize the game as it progresses as shown on the right:
It will show your dice rolls as they happen, and keep track of your bank balance on the left. Each round of the game you roll one die to get a random number from 1 to 6. But this is a fancy spherical die. Show it by drawing a new circular image like the ones shown in the example. For each die roll, draw a white circle, with from 1 to 6 dots (called “pips”) equally spaced in a ring within the circle. Do not erase the old rolls, so that you see all of your rolls, as shown. You can see rolls from 1 to 6 in the sample image. Each die should be drawn in a random location, but be sure that the whole circle fits inside the window (and does not overlap the money bar on the left).
The game rules are as follows. Depending on what you roll, your money gets a multiplier. If you roll a:
- 1 – multiply your money by 1.5
- 2 = multiply your money by 1.3
- 3 = there is no change to your money
- 4 – multiply your money by 0.2 (lose 80%!)
- 5 – multiply your money by 0.8 (lose 20%!)
- 6 – double your money
There are two additional rules: if you get two 4s in a row, the second 4 will give you 5 times your money. (This will also happen for a 3rd, 4th, … one, if you get really lucky.) But if you get two 6s in a row, the second six bankrupts you to $0.
You will start the game with a certain amount of money ($40) and you must announce (that is, print to the console) a goal that you hope to reach (try $400). You must keep playing until either:
1
ASSIGNMENT 1: Games
DEPARTMENT AND COURSE NUMBER: COMP 1010
- 1) you reach or exceed your goal, or
- 2) you have less than $1 left.
Show the amount of money as a vertical bar against a white background on the left side of the screen. A small bar near the bottom, as shown, means you haven’t got much money yet. A bar that reaches the top of the window means you’ve reached or exceeded your goal. If the bar disappears, you’re broke. You should also draw a horizontal line to show the most money you’ve ever had during this game. In the example, the player has about $39.94 of the goal of $400, but at some point, had a bit more (about $52), as shown by the small line. You should also show the current money left in the bank after each roll as a text at the top of the left bar (money bar).
Notes:
- You should make global constants for the parameters of the game, such as the colors used, what happens on the possible die rolls, etc. You should be able to quickly and easily change the appearance of the dice, the starting money or the goal, etc.
- You will also need global variables to keep track of your money, the maximum money you ever had, your previous roll, etc.
- Use a boolean variable to indicate whether or not the game is over. If the game is over, you shouldn’t roll any more dice or change any money.
- Each frame you should play one round of the game. In order to slow it down so that you can watch the game, use the command frameRate(1); which will give only one frame per second.
- You can use (int)random(N)+1 to get a random integer value between 1 and N.
- Use println to print out the amount of money the player has, after each round, to the
console, so that you can see what’s going on. This will help a lot in testing your program.
- You can use the text(currentMoney, x, y) command to add the current amount of money
in the bank after each roll.
- You should not erase the dice each frame, but you should erase and redraw the “money
bar” each frame.
Q2: Bumper Golf – 10 marks
You have a golf ball (the white circle) and a randomly placed hole (the grey circle). The white line is controlled by the mouse, and indicates how hard you will hit the ball, and in what direction. The ball will be hit when you press the mouse button. Don’t shoot too hard, or the ball will go right over the hole and keep moving. Shoot straight, since the ball needs to be completely within the hole to fall in.
This assignment is quite big compared to what you may be used to. Good commenting and explanations of what each region of code will do (basically, to help you break it down…) will be very beneficial and will save you a lot of debugging time. It will probably be helpful to write out what should happen in English first (in comments!!).
2
ASSIGNMENT 1: Games
DEPARTMENT AND COURSE NUMBER: COMP 1010
The challenge here is to keep track of what state everything is in (what is currently happening). Is the ball moving? How fast? In what direction? Is there a hole? Is the ball inside the hole? etc.
You will need global constants for all of the game parameters. This includes the MIN_SPEED of the ball (below which it is considered to be not moving – try 0.5 pixels per frame), the FRICTION (how much of the velocity is retained from frame to frame – try 0.98). You will develop others, and some are mentioned below. You will also need global variables for the hole location (both x and y coordinates), a boolean variable for whether there is currently a hole, variables for the ball location (x and y), the ball speed in x and y directions, a boolean variable indicating if the ball is moving, and others.
In the draw block, you need to balance the following tasks:
- If there is a hole, draw it. If not, create one at a random location. The hole size should be 1.5 times the ball size.
- If the ball is not moving, then draw a line from the ball to the mouse and draw a white ball. If the mouse is pressed, then shoot:
o Determine the x and y distances from ball to the mouse cursor and scale them down to get the initial x and y velocities (try a scaling factor of 0.04). That means, the velocity in the x direction in pixels per frame will be this scaling factor times the distance (in pixels) in the x direction, and similarly for the y direction.
o Add 1 stroke to the player’s score, and print this to the console using println. • If the ball is moving,
o Draw the ball.
o Add the x velocity to the x position and the y velocity to the y position.
o If the ball is too close to the left or right side of the canvas, (less than one ball
radius), reverse the sign of the horizontal velocity.
o If the ball is too close to the top or bottom of the canvas, (less than one ball
radius), reverse the sign of the vertical velocity.
o Apply friction to the ball speed (multiply both velocity components by the friction
factor).
o If the ball is completely inside the hole (hint: if the distance between the two
centers is less than the hole radius minus the ball radius), and the ball isn’t moving too fast (try 2 pixels per frame), then the ball is sunk. You can calculate the velocity as the length of the hypotenuse (is the longest side of a right-angled triangle) if you make a triangle with the x and y speeds. Move the hole and move the ball back to the center of the screen (with no speed!) when the user clicks to reset the game.
Remember that If the total ball velocity is too slow, mark it as not moving at all.
Note for Implementation: Write the program in English first. Implement and test your program in phases. First get the ball on the screen. Then be able to shoot it using the mouse. Then implement the wall rebounding. Finally, add the hole and the scoring.
Assignment Notes:
- Hand in three pde files, one each for Questions 1 and 2.
- Make sure your files are named exactly as specified in the instructions at the beginning of
the assignment.
3