On this page:
Instructions
5.1 The javalib library
5.2 A note about randomness
8.5

Assignment 5: Feeding Frenzy

Goals: Design a game.

Instructions

Due Date:
  • Thursday May 30th, 9:00pm

Introduction

In this assignment, you’ll design and develop a simple version of the game Feeding Frenzy. In this game, you start off as a small fish in a pond of smaller and larger fish, and to survive you must eat the smaller fish while avoiding being eaten by the larger ones. Move your fish with the arrow keys. You win when you are larger than all the other fish in the pond.

To implement this game, you will need to complete the following tasks.

Be sure to test your game’s behavior thoroughly.

If you wish to embellish the game, you can add additional features for extra credit. Here are some suggestions:
  • Add inertia: Once you let go of an arrow key, the player fish should not stop immediately, but should drift along until it slows to a halt.

  • Add more inertia: The bigger the player gets, the harder it should become to accelerate the fish...and also to stop!

  • Keep score: eating bigger fish is worth more than eating several little fish.

  • Add size snacks: eating these can make the player’s size grow immediately.

  • Add speed snacks: eating these can give the player a speed boost.

  • Add multiple lives.

If you decide to implement any extra features, make sure to document them well. Remember – you will be graded for your program design, not making a cool video game. So whatever you add, make sure it’s well-designed. But have fun with it, too!

5.1 The javalib library

The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files. See The Image Library for more information.

To use the library, download the javalib file above and add it to your project the same way you have added the tester library.

At the top of the .java file where the library is used, add the following import statements:

import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.funworld.*; // the abstract World class and the big-bang library import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Red, Green, Yellow, Blue, Black, White)
5.2 A note about randomness

There are two ways to generate random numbers in Java. The easiest is to use Math.random(), which generates a double between 0 (inclusive) and 1 (exclusive). You can multiply this number by some integer to make it bigger, then coerce to an int to produce a random integer in the range you wish. However, this is not easily testable: you’ll get different random values every time.

The better way to generate random numbers is: First, import java.util.Random at the top of your file. Next, create a new Random() object, and use its nextInt(int maxVal) method, which will give you a random integer between zero (inclusive) and maxVal (exclusive).

This is known as a "pseudorandom number generator", since the numbers aren’t really random if they can be reliably repeated...

The reason this is better is because there is a second constructor, new Random(int initialSeed), which has the property that every time you create a Random with the same initial seed, it will generate the same "random" numbers in the same order every time your program runs. You should therefore design your world classes with two constructors: