Assignment 6

COMP112 2020 Tri 1: Assignment 6

  • Due 28 May 10am

Note: you have a choice of either doing the separate assignment 6 and assignment 7 (1 week each) or the combined assignment 6+7 (2 weeks). Only do 6+7 if you have been following the 112 lectures and are doing well in the assignments. It is perfectly acceptable to do the separate 6 and 7.


The BallGame and FrogSnakeGame programs will give you experience in defining classes for objects with fields and constructors. Both games involve objects that move around in a simple world. It will also give you some examples of programs with simple event-driven input (mouse and buttons).


Download the zip file for assignment 6 and extract it to the COMP112-2020T1-Assig6 folder in your home folder. It should contain templates for the Java programs you are to complete, along with image files. Read through the whole assignment to see what you need to do. You can run the demo programs on the ECS lab computers (they are in the COMP112 folder on your desktop)


  • Ball Game: (45%)
    right Write a program for a simple projectile game. Complete the Ball class.
  • FrogSnake Game: (45%)
    right Write a program to play the FrogSnake game. Complete the Frog and Snake classes.
  • Understanding Java. (10%)
    right Answer some short questions about Java programming.

Note, the lecture video on the FireworksDisplay program is a useful model for this assignment.

To Submit

You should submit your versions of,,,,, and, and the file UnderstandingJava.txt by the due date.

Ball Game

The BallGame program is a very simple projectile game that involves launching a ball from one side of the window to knock target balls off their shelves. You launch the ball by clicking the mouse; the position of the mouse governs the direction and speed of the ball.

The structure of the program is similar to the PetShow program and the "new objects" exercises in assignment 2: there is one class (Ball) that represents individual balls, and another class (BallGame) that creates some Ball objects, and calls methods on them. For the PetShow program, the class for the individual objects (Animal) was written for you, and you had to write the class that controlled the objects. For this program, it is the opposite: the "controlling" class is written for you, but you have to write the Ball class for the objects.

Watch the demo video, and read through the BallGame program to see how it makes the new Ball objects, and calls methods on then. Each time round the while loop in runGameCore(ie, each "time tick"), the program will
  • check whether the projectile ball has gone off the right side, in which case it creates a new projectile ball.
  • move both balls by one step.
  • redraw the world and the balls in their new position

The Ball class represents a ball. This is the class that you must complete. The state of a ball consists of
  • its horizontal position,
  • its height above the floor (from the floor to the bottom of the Ball),
  • its horizontal speed - how far it will move to the right in each step,
  • its vertical speed - how far it will move up (or down if the speed is negative) in each step, and
  • its color.

Each Ball object will have a different starting position. Therefore, the constructor for Ball must have parameters for the initial position (x and height). The initial speed (horizontal and vertical) is always 0.

A Ball should have at least the following methods:
  • The draw() method should draw the ball at its current position. The ball is a circle of radius RADIUS. (Note, the draw() method should not call UI.sleep(...) )
  • The step() method should make the ball move one step (depending on the speeds). (Note, the step() method should not call draw() )
  • The getHeight() method should return the height of the ball above the floor.
  • The getX() method should return the horizontal position of the ball.
  • The setSpeed(double x, double y) method should set the speed of the ball to the given parameters.

  • In the Ball class, complete the definitions of the fields, the constructor, and the methods.
    The step method can just change the current position according to the current speed, which will make the balls move in straight lines only.

  • Make the step method work better so that the ball will move more realistically:
    • The step() method never lets the ball go below the floor (the height should never be negative).
    • If the ball is not stationary, then gravity should act on the ball by reducing its vertical speed by 0.2. (the vertical speed can be negative - that just means the ball is falling downwards).
  • Add a second target ball to the game (on a separate shelf). This requires changing the BallGame class.


Extend the program to make it a better game. For example:
  • Allow lots of targets
  • Make the ball bounce off the ground.
  • Make the collisions more realistic (the provided version knocks the target off the shelf at a fixed velocity, regardless of how the ball hit the target, and allows the launched ball to go right through the target when they are both moving). It would be nice if you could bounce the ball off a target and hit other targets as well.
  • Show the expected speed of the ball as a dotted line while moving the mouse around (before clicking the mouse to launch the ball).
  • Make the targets be a different kind of object that fall in a different way
  • Add extra objects that can be knocked over and might fall against the targets (think "angry birds"!).

Part 2: FrogSnake Game

The Frog game (Core) involves two frogs, controlled by the players, moving around a square arena, trying to catch the mosquitoes. Each time a frog catches a mosquito, the frog gets a little bigger, and a new mosquito reappears at a new location. You can start the game by moving the mouse over the arena, and pressing the space key. The players control the direction of the frogs with keys: a s w d for the lighter frog and j k i l for the darker frog. The Frogs cannot move outside the square arena.

The Frog-Snake game (Completion) adds two snakes to the game. The snakes move around in random directions. If a snake touches a frog, the frog "dies" and is reset to its original size.

You are to complete two classes: and

Watch the demo video to see how your frog and snake should work.

Core: Frog Class

Complete the Frog class to allow you to play the basic version of FrogGame. The initial position of a Frog is specified when it is created, but all Frogs start facing "Up" and at the same initial size.

  • A Frog should have fields to keep track of:
    • The current position
    • The current direction ("Up", "Down", "Left", or "Right")
    • Which frog picture it draws (so the different players can be told apart)
    • The frog's size
  • Frog should have a constructor, which will initialise the fields.
  • The draw() method, which should draw the frog in its current state at its current position. You may use the provided images (lightfrog.jpg & darkfrog.jpg) to draw the frog, or you can create better ones.
  • The turnLeft(), turnRight(), turnUp(), and turnDown() methods, which set the direction the frog is moving. This does not actually move the frog.
  • The move() method, which moves the frog by a fixed amount (SPEED) in its current direction, ensuring that it doesn't go outside the walls of the arena.
  • The grow() method, which increases the frog's size.
  • The touching(double x, double y) method, which should return true or false to say whether the frog is on top of the position (x,y). The method needs to take into account the size of the frog!

Note that the move and turn... methods do not redraw the frog - the main program will call the draw method explicitly. This is different from the CartoonCharacter class in the lectures.

Completion: Snake Class

Implement the Snake class so that you can play the FrogSnakeGame. Snakes are created at a random position. They move in a fixed random direction (not just up/down/left/right) until they hit a wall, at which point they pick a new random direction and keep moving. If a snake touches a Frog the frog loses all the mosquitoes it ate, and goes back to its original size. The snake image is snake.jpg provided courtesy of taesmileland at

  • A Snake should have fields to keep track of its position and direction (an angle).
  • Snake should have a constructor with no parameters. A snake's starting position and direction should be random.
  • The draw() method, which should draw the snake in its current position.
  • The move() method, which should make the snake move by one unit in the direction it is going. If the snake has hit a wall it should first move back from the wall, then change direction.
  • The getX() and getY() methods, which should return the position of the centre of the snake.

You will also need to implement the shrink() method in the Frog class which makes the frog go back to its original size.

The game as above is too simple to be an interesting game. It could be improved in many ways. Here are some ideas of how it could be improved:
  • Make the snakes move not randomly, but in an interesting way (eg, a snake could chase after a frog)
  • As the player gets a higher score, make the snakes gradually move faster.
  • Display a score and lives on the graphics pane.
  • Let the player play against the computer (rather than another human).

Understanding Java

Answer the following questions in the UnderstandingJava.txt file, and make sure you submit it.

  1. What is a constructor? What are the differences between a constructor and a method?
  2. What is a field in a class? What are the differences between a field and a local variable?
  3. When do you need to store values in a field rather than a local variable?