Assignment 6

COMP102/112 2022 Tri 1: Assignment 6

days Due 5 May 10am

Goals

The BallGame program will give you experience in defining classes for objects with fields and constructors. This program involves objects that move around in a simple world.

Preparation

Download the zip file for assignment 6 and extract it to the COMP-102-112-2022T1-Assig6 folder in your home folder. It should contain templates for the Java programs you are to complete. Read through the whole assignment to see what you need to do.

Read through the exercises and run or view the demo so that you know what you need to do.

Later when the model answer to assignment 5 becomes available, make sure you understand all the components of the programs.

Summary

  • Ball Game:
    right Complete a program for a simple projectile game.

To Submit

You should submit your versions of Ball.java, BallGame.java 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.

  • If you are using a computer in a lab, you can run the BallGame demo to see what your program should do (look in the assig6-demos folder).
  • Or, you can watch the demo video.

The structure of the program is similar to the PetShow program and the "new objects" exercises in LabExercise 3: 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.

Run the demo (or watch the 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 runGameOneTarget (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 diameter DIAM. It should not draw anything if the ball is past the RIGHT_END. (Note, the draw() method should not call UI.sleep(...) )
  • The step() method should make the ball move one step (depending on the horizontal speed and vertical speed). (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 setXSpeed(double xSpeed) method should set the horizontal speed of the ball.
  • The setYSpeed(double ySpeed) method should set the vertical speed of the ball.

Core
  • In the Ball class, complete the definitions of the fields, the constructor, and the methods.
    The step method should change the current position according to the current speed but not change the speed. This will make the balls move in straight lines only.

Completion
  • Extend the step method 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 writing the runGameTwoTargets() method in the BallGame class.

Challenge

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 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"!).
  • Allow two players to compete against each other with a projectile ball at each side.
Note: You do not have to do all of these extensions to get 100%! For example, it would be possible to get 100% with just three extensions done well.

To go further

It is important to understand how Java works, as well as writing programs in Java.

  1. Did your Ball move realistically in parabolic arcs?
  2. What is the advantage of having a Ball class? What would be harder if the ball code was all in the BallGame class?