Create two global integer variables, one to increment the x direction of the target and one to increment the y direction (assign them number values between 0 and 5).
-
Inside the conditional in the draw() function, after you draw the target, add code to increment the target’s x and y coordinates stored in the coordinate list.
-
Now we need to make sure the target stays on the canvas. Add if statements that check if the target’s x or y coordinate has gone outside the canvas width or height.
Follow the sub-steps below for this part.
-
Write an if statement that checks whether the current x coordinate is less than the radius of the target OR greater than the width of the window minus the radius. If either of these conditions is true, reverse the direction of the x increment variable by multiplying it by -1.
Repeat the above sub-step for the y coordinate, checking to see if the current y is less than the radius or greater than the height of the window minus the radius. If either condition is true, multiply the y increment variable by -1.
Run your code now, the target should move around the screen, bouncing off the edges of the canvas.
Create a mouseclick() handler and define its function (see Docs->Graphics Modules->Control Objects ->Mouse input handler).
-
We want to check if the mouse has clicked on the target. To do this, you need to check if the x click location is between target_x - radius and target_x + radius, and if the y click location is between target_y - radius and target_y + radius. If so, change the global boolean flag so the game stops and print out a winning message.
Show your instructor to get credit for Level 2 and Level 3.