Question 1: Setup and Background (5 marks)
Create a scene like the one shown below.
You must include the following elements.
• A horizon line, such as between the grass and the sky, near the bottom of the canvas. • A repeating pattern of shapes in the background. In the example above, these are the tufts of grass. Create a function that receives x and y coordinates, and draws one copy of your design at that position. Then use this function inside a loop to draw the repeating pattern. The repeated item must involve more than one shape. You can also add more features to the background, like the sun above. Be creative! • The slingshot (which is just a tall, narrow rectangle), standing on the left side of the canvas. Don’t put it directly against the edge of the canvas: later, the player will want to pull the ball back in order to shoot it. • The ball, and the band that connects the ball to the top of the slingshot. Use separate functions to draw each of these.
– The function to draw the band should receive a set of x and y coordinates, and draw a thick line from those coordinates to the top of the slingshot. – Introduce two global float variables to hold the current x and y coordinates of the ball. For the moment, the ball should hang partway down the slingshot, as shown. This is the initial position of the ball: name the global constants accordingly, and initialize the global variables to these values in setup(). – Since the position of the ball is stored globally, the function to draw the ball should not receive any values.
Question 2: Drag the Ball (4 marks)
You will need Processing’s void mouseReleased() function, which is called when the mouse button has been clicked and released. You will also need at least one of the mousePressed variable, the mousePressed() function, or the mouseDragged() function. • Add code to mouseReleased(), and edit other functions as necessary, so that if the player clicks and drags the ball, the center of the ball follows the mouse position, and so does the end of the band. When the player releases the mouse, the ball and band go back to their starting positions. As part of this code, write a function that tests if the mouse is over the ball, and returns a boolean value. • Add conditions so that the ball cannot be dragged below the horizon or off the screen in any direction.
Motion Under Gravity – Information for Questions 3 and 4
We want the ball to move as if it is being acted on by gravity. To do this, we model the motion of the ball as follows. If the ball is released at coordinates (x0,y0), with initial speeds vx and vy in the x and y directions, respectively, then its x and y coordinates after time t are given by
x = x0 + vxt, (1)
y = y0 + vyt +
1 2
gt2, (2)
where g is a constant, the acceleration due to gravity. In these formulas, t = 0 corresponds to the moment that the ball is released.
If you have never seen these formulas before, don’t worry about where they came from – just use them as written. The formulas are already set up to be consistent with Processing’s coordinate system: the positive y-direction is pointing down the canvas.
Note that this is NOT the same method that we have used to create motion in previous assignments. We are not adding a quantity to the position of the ball in each frame – we are calculating the position of the ball, using the time elapsed and the initial position and speed.
Question 3: Plotting Trajectories (7 marks)
Implement the following features. • The player will click and drag the ball to stretch the band, then release the mouse in order to shoot the ball. When the mouse is released, the length of the band will be scaled to give the initial speed of the ball, v. The direction of the band, from the ball to the top of the slingshot, will give the initial angle of the ball, θ. Set up the scaling calculation by defining global constants for the maximum initial speed and the maximum length of the band. Set the maximum length of the band to be the result of dragging the ball to the corner where the horizon meets the left edge of the canvas. You can experiment with different maximum initial speeds; a reasonable starting point is 20. Use 0 for the minimum length and the minimum speed.
• Create functions that use the current location of the ball to calculate the initial speed v and the initial angle θ. Since the top of the slingshot and the current location of the ball are stored globally, these functions do not have to be passed any data. Each one should return a float. • Once v and θ are known, the initial speeds in the x and y directions are vx = v cos(θ), vy = v sin(θ).
Create two more functions that calculate vx and vy. These values will be used in equations (1) and (2). • Let’s help the player out. While the player is dragging the ball, plot the trajectory that the ball would follow if the mouse were released at that moment.
– Our time t will be measured in frames, and our distances will be measured in pixels. To produce reasonable motion, we need to pick an appropriate value of the constant g. Create a global constant GRAVITY, and start by setting it to 0.4. You can experiment with this value to produce an effect that you like. – Create functions that calculate x and y using equations (1) and (2). Each function should be passed the initial position, the initial speed, and the time elapsed since the ball was released. – Now create a function to plot the trajectory as a series of dots. The current position of the ball determines vx and vy as described above, and the current coordinates of the ball are the values of x0 and y0. Start from t = 0, then find the location of each successive dot by increasing t, and end where the ball would hit the ground. Plot the entire trajectory in one frame, not one dot at a time. You can spread the dots out: this should just be a guideline, not a continuous curve. – This plotted trajectory should vanish when the ball is released. – Make sure that your trajectory function does not actually move the ball.
Question 4: Shooting the Ball (4 marks)
To shoot the ball, the player must use the mouse to pull the ball back, then release the mouse. At the moment when the mouse is released, the initial x and y speeds are calculated, and the band returns to its default position. In subsequent frames, the ball flies through the air according to equations (1) and (2) until it hits the ground.
• Create a global variable that stores the time elapsed since the ball was released, measured in frames. Set this variable to 0 at the moment that the ball is released, and increment it by 1 in every subsequent frame. • You will also need to store globally the ball’s initial positions and speeds in the x and y directions. Use a global boolean to distinguish between a ball that is still being aimed and one that has been released. • For each frame that the ball is in the air, use the functions that you created in Question 3 to calculate the ball’s current position, given the initial speeds, the initial positions, and the time elapsed since the ball was released. • Write a boolean function that tests whether the ball has hit the ground. Once the ball hits the ground, the program should reset to the beginning with a new ball hanging from the slingshot. • Clicking or releasing the mouse should have no effect while the ball is in the air.
Question 5: Targets, Hits, Scoring (4 marks)
Now give the player something to aim at.
• Write a function to generate a target at a random location on the ground to the right of the slingshot. Pick a reasonable size, shape, and range of positions, and use global constants as needed. Store the position of the target globally. • Write a boolean function that determines whether the ball has hit the target. • Once the ball hits the ground, determine whether it hit the target. Store the number of hits and the number of misses globally and update them with every shot. Print the number of hits and the number of misses on the screen. These two counts should remain on the screen for the entire game.
?dditional Marks(bonus marks)
Thefollowingfeaturesareoptional,andwillearnupto3bonusmarksintotal. • 1bonus mark.Createachallengemodeinwhichtheplotofthetrajectoryisnotdisplayed. Atthestartofthegame,allowtheplayertoselectregularmodeorchallengemodebypressing akeyonthekeyboard(youcanchoosethedetailsofhowthisdecisionismade).Duringthe game, display‘Regularmode’or‘Challengemode’onthescreenalongwiththehitsand misses. • 2bonus mark.Astheplayer’sscoreincreases,thegameshouldgoupinlevel,witha correspondingincreaseindifficulty.Implementamechanismtoincreasethelevel:forexample, thelevelgoesupafterthreehits.ThegameyoujustbuiltisLevel1.Createatleasttwomore levels,whereeachnewlevelintroducesoneofthefollowingcomplications.
– The target is smaller. – The target moves back and forth horizontally within some range. – The target moves up and down vertically within some range.
During the game, display the current level on the screen.
Programming Standards are worth 6 marks.