COMP112 2020 Tri 1: Assignment 6
: 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.
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).
Resources and links
Download the zip file for assignment 6 and extract it to the
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%)
Write a program for a simple projectile game. Complete the
- FrogSnake Game: (45%)
Write a program to play the FrogSnake game. Complete the
- Understanding Java. (10%)
Answer some short questions about Java programming.
Note, the lecture video on the FireworksDisplay
program is a useful model for this assignment.
You should submit your versions of
, and the file
by the due date.
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
and the "new objects" exercises in assignment 2: there is one class
) that represents individual balls, and another class
) that creates some
objects, and calls methods on
them. For the
program, the class for the individual
) 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
class for the objects.
Watch the demo video
, and read through the
program to see how it makes the
new Ball objects, and calls methods on then. Each time round the while loop in runGameCore
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
class represents a ball. This is the class that you must complete. The state of a ball
- 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.
object will have a different starting position. Therefore, the constructor for
parameters for the initial position (x and height). The initial speed (horizontal and vertical) is always 0.
should have at least the following methods:
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
step() method should make the ball move one step (depending on the speeds). (Note, the
step() method should not call
getHeight() method should return the height of the ball above the floor.
getX() method should return the horizontal position of the ball.
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.
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:
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
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
The players control the direction of the frogs with keys: a s w
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:
Watch the demo video
to see how your frog and snake should work.
class to allow you to play the basic version of
. The initial position of a Frog is specified when it is
created, but all Frogs start facing "Up"
and at the same
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.
draw() method, which should draw the frog in its current state at its current position. You may use the provided images (
darkfrog.jpg) to draw the frog, or you can create better ones.
turnDown() methods, which set the direction the frog is moving. This does not actually move the frog.
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.
grow() method, which increases the frog's size.
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
methods do not redraw the frog - the main program will call the
method explicitly. This is different from the
class in the lectures.
class so that you can play the
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
loses all the mosquitoes it ate, and goes back to its original size. The snake image is
provided courtesy of taesmileland at FreeDigitalPhotos.net.
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.
draw() method, which should draw the snake in its current position.
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.
getY() methods, which should return the position of the centre of the snake.
You will also need to implement the
method in the
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).
Answer the following questions in the
file, and make sure you submit it.
- What is a constructor? What are the differences between a constructor and a method?
- What is a field in a class? What are the differences between a field and a local variable?
- When do you need to store values in a field rather than a local variable?