Question: Code Language: Processing Set up an Active Processing program that will draw the initial configuration of the game. setupGame() function that will set the initial

Code Language: Processing

Set up an Active Processing program that will draw the initial configuration of the game.

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 follows:

  • 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 green.
  • Place the hole (black in images at
  • right) randomly in the right (if
  • horizontal hole) or top (if vertical
  • hole)thirdofthegreen. Makesurethattheholeisatleastoneholewidthawayfromthe 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 program runs.

Copy and paste the following drawScore() function into your program. Call it from your drawGame() function.

void drawScore(){

textSize(20);

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 named shotsTaken.

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 Determinethelargestpossibledistancefromtheballtothemouse,keeping in mind that a mouse click will only register if the mouse is on the canvas.
  • o ChooseaMAX_SPEED(try10pixelsinitially),and scale the distance from he 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.
    • keep the ball on the green,
    • detect when the ball is in the hole,
    • and make the ball bounce off the obstacle.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!