Assignment 2

A soccer game

For this assignment you will build a simulation of a soccer game.

Specifically, the game starts with the ball in the bottom left corner of the screen, and the user can “pick it up” and move it around. When the user releases the mouse button, the ball will be thrown in the appropriate direction. If the ball makes it into the goal, it will “glide” into the centre of the goal opening, fade out and reappear at the bottom left corner. Further, the user will be awarded one point. Since we have no AI to serve as a goal keeper, to make things more interesting the goal will be moving up and down at a constant speed. Each time the user scores, the speed should increase.

For this assignment you are given some starter code, available for download here. You should build on this code to complete the assignment. See the marking scheme below for more details

Marking

This assignment is marked out of 50

3 Marks. Boolean Functions.

Complete the three boolean functions at the bottom of soccerGame.pde. Specifically:

  • 1 Mark. The function ballPastHalf() should return true if the ball is past the half mark of the screen (in the x direction), and false otherwise.
  • 2 Marks. Implement the functions pointInCircle() and pointInRect() as discussed in class.

13 Marks. Structure.

Notice that the draw() function in soccerGame.pde is written like so:

void draw() {
    drawField();
    drawGoal();
    drawBall();
    drawScore();
}

This is so that the code is more organized, and easier to read and modify.

  • 3 Marks: don’t place any code of your own inside the draw() function.

The entire program should be written using setup() and the helper functions provided for you in soccerGame.pde, or in any functions of you own that you write. Specifically (2 Marks each):

  • drawField() will contain all of the code necessary to draw the field.
  • drawGoal() will contain all of the code necessary to draw and animate the goal posts.
  • drawBall() will contain all of the code necessary to draw and animate the ball, with the exception of some code, required for dragging and throwing functionality, as in the lecture notes.
  • restart() will contain all of the code necessary to restart the game.
  • drawScore() will contain all of the code necessary to display and update the score.

As mentioned, you may write any additional functions you require, however they should only be called inside the relevant pre-defined functions. For example, we have left the function signature moveBallToGoal() in the starter code file. You may wish to use this function to move the ball to the goal. However, since this functionality is related to the ball, you should only call this function from drawBall(). Similarly, you may wish to write a function that draws confetti on the screen whenever the user scores. This functionality belongs under drawing the field, and so a function such as this one must be called from inside drawField().

2 Marks. Field.

The field should be drawn by you using Processing functions (i.e. not an image from the Internet, or drawn by you in some other program). The half-point of the field should be clearly marked.

4 Marks. Style.

  • 1 Mark. The only occurrence of the dimensions of the screen are in the call to size(). Anywhere else in the program, if you need to refer to the dimensions of the screen, you should use the variables width and height
  • 1 Mark. Descriptive variable names. e.g. ballX to denote the ball’s x position.
  • 2 Marks. Consistent and clear indentation, as seen in class and all of the demo programs.

The game should work as follows:

10 Marks. Beginning

  • 1 Mark. The ball begins at the bottom left corner.

  • 2 Mark. The ball has some markings showing it to be a soccer ball. (e.g.

    see the image examples). Pictures of a soccer ball are not acceptable. The ball should be made using calls to processing functions. Hint: You may wish to use the Processing arc() function.

  • 2 Mark. The score should be displayed at the top, using a font of

    your choosing.

  • 5 Marks. Goal and goal Posts.

    • The goal posts may be any (Processing) shape you wish, however you will need to write hit detection code for the goal posts and the goal itself, so sticking to rectangles is recommended.
    • The goal posts should be clearly marked using a fill colour (off-white in the example) and a thicker stroke (at least 5). The colour of the stroke is up to you, but to be visible, it should differ from the fill colour.
    • The goal itself should NOT be a shape. Rather the goal is the gap between the top goal post’s bottom edge and the bottom goal post’s top edge, see the next point.
    • The goal posts should move up and down, leaving an opening between them as they do so.
    • Aside from an opening, the size of which is determined by you, their height should cover the entire screen (as in the picture examples).

18 Marks. Play.

  • The user will throw the ball, attempting to get it into the opening (aka the goal)

    A goal.

    That is, you should implement throwing the ball, subject to the following conditions:

    • 2 Marks. Clicking and dragging on the ball picks it up and moves it around.
    • 1 Mark. Releasing the mouse button will propel the ball at a speed proportional to the motion of the mouse.
    • 2 Marks. The user can drag the ball only to the half-line of the screen. If the ball is dragged past that point, the user loses control of the ball, and is unable to pick it up as long as it is in the right half of the screen. Hint: the function ballPastHalf() that you implemented should be useful here.
    • 1 Mark. There is a force of gravity acting on the ball in the left direction. That is, after the ball is thrown, it should not bounce indefinitely, but “want” to return to the left side of the screen. Keep in mind that it should bounce, just not forever. Optionally, after having stuck to the left edge, you may want to apply gravity in the bottom y direction, to return the ball the bottom left corner. This is strictly optional, however.
    • 1 Mark. Elasticity should also apply to the ball when bouncing. Whether to apply different elasticity constraints based on which edge is hit by the ball, is up to you.
    • 3 Marks. If the user misses the goal, but hits the goal posts or any of the other walls, the ball should bounce back (with some measure of elasticity as mentioned). Further, no interpenetration should be allowed to occur.
  • If the ball goes into the goal, several things need to happen:

    • 1 Mark. As soon as the ball crosses the goal line, the goal posts should stop moving.

    • 2 Marks. The ball should move toward the centre of the goal post, and ease to stop at the centre.

    • 2 Marks. Once the ball has reached the centre it should start to fade out.

      Note: Nothing else should fade.

      A fading ball
    • 1 Mark. Once finished fading, the ball should reappear at the bottom left corner of the screen.

    • 1 Mark. Once the ball has finished fading, the score should be updated.

    • 1 Mark. Once finished fading, the goal posts should resume moving at a speed proportional to the score. That is, the higher the score that harder it should be to score more goals.

Hints

  • Look at the Processing arc() function.

  • To achieve the desired effect with the goal posts, you may (though are not required to) use the following construction:

    The goal posts are made of two shapes (rectangles in the example), each having height equal to the height of the screen minus whatever you size opening you wish to allow. Suppose this opening has width x and height y. Then both goal posts should have width at least x. Further, the top goal post should have height height - y where height is the height of the screen, and similarly for the bottom goal post.

  • The function ballPastHalf() will help you determine where the ball is.

  • You can determine whether the ball is in the goal using one call to pointInRect(), assuming you’ve implemented it as discussed.

What to Submit

Please rename the given file (soccerGame.pde) to firstname_lastname_studentNum_ass2.pde, where firstname_lastname is your full name separated by an underscore and studentNum is your student number. Would you should submit is a folder called firstname_lastname_studentNum_assn2 that contains the pde file as well as the vlw file for the font you created inside a subdirectory named data. Compress this folder as an archive named firstname_lastname_studentNum_assn2.zip.

Submitting the Assignment & Lateness Policy

Please submit this assignment on-line no later than the due date listed on the course marking scheme.

If you submit the assignment after the deadline, you will be deducted %10 of the maximum points available for the assignment, for each day that you are late. For example if the assignment when submitted on time is marked out of 100, then after one day the maximum mark will be 90, after two days it will be 80, and so on.