package c1Oceandata; import tester.*; import javalib.funworld.*; import javalib.colors.*; import javalib.worldimages.*; import java.util.*; import java.awt.Color; // the basic data used by the ocean world game interface OceanWorldConstants{ // a random number generator public Random rand = new Random(); // the width of the world public int WIDTH = 400; // the height of the world public int HEIGHT = 400; // the color of the ocean public Color oceanColor = new Color(50, 150, 255); // the background image of the ocean public WorldImage oceanImage = new RectangleImage(new Posn(WIDTH/2, HEIGHT/2), WIDTH, HEIGHT, oceanColor); } //To represent a location (x,y) in graphics coordinates //-- adding methods to the library Posn class class CartPt extends Posn implements OceanWorldConstants{ // the standard constructor - invokes the one in the super class CartPt(int x, int y) { super(x, y); } } // A Shark swims in the ocean, looking for fish to eat class Shark implements OceanWorldConstants{ int y; // the horizontal coordinate int health; // the hunger level, when zero, the shark dies // the standard complete constructor Shark(int y, int health) { this.y = y; this.health = health; } } // A Fish swims in the ocean - providing nourishment for the shark class Fish implements OceanWorldConstants{ CartPt p; // the location of this fish int size; // the size of this fish // the standard complete constructor Fish(CartPt p, int size) { this.p = p; this.size = size; } } // A School of Fish is one of: // - new EmptySchool() // - new ConsSchool(Fish, School) // A school of (an arbitrary number of) fish. interface School extends OceanWorldConstants{ } // A school with no fish class EmptySchool implements School, OceanWorldConstants { EmptySchool() {} } // A school with at least one fish class ConsSchool implements School, OceanWorldConstants { Fish first; School rest; // the standard complete constructor ConsSchool (Fish first, School rest) { this.first = first; this.rest = rest; } } //An Ocean has a shark and a fish swimming in it class Ocean extends World implements OceanWorldConstants{ Shark shark; // the hungry shark Fish fish; // the fish the shark will eat (maybe) Ocean(Shark shark, Fish fish) { this.shark = shark; this.fish = fish; } // the fish swim from right to left on each tick public World onTick(){ return this; } // the shark moves up or down as directed by the arrow key public World onKeyEvent(String ke){ return this; } // produce the image of the fish and shark swimming in the sea of blue public WorldImage makeImage(){ return new CircleImage(new Posn(0, 0), 1, Color.red); } public WorldEnd worldEnds(){ return new WorldEnd(false, this.makeImage()); } } // Examples for the ocean game public class ExamplesOceanData implements OceanWorldConstants{ ExamplesOceanData() {} public static ExamplesOceanData examplesInstance = new ExamplesOceanData(); CartPt p1 = new CartPt(WIDTH, HEIGHT/2); CartPt pEnd = new CartPt(3, HEIGHT/2); CartPt pStart = new CartPt(WIDTH, HEIGHT/2); Fish f = new Fish(p1, 50); Shark s = new Shark(HEIGHT/2, 200); Ocean ocean = new Ocean(this.s, this.f); Fish sharkFood = new Fish(new CartPt(3, HEIGHT/2 + 3), 20); Fish outOfBoundsFish = new Fish(new CartPt(3, 100), 20); Ocean sharkEats = new Ocean(this.s, this.sharkFood); Ocean fishEscapes = new Ocean(this.s, this.outOfBoundsFish); // run all tests, then run the game public static void main(String[] argv){ ExamplesOceanData e = new ExamplesOceanData(); Tester.runReport(e, false, false); e.ocean.bigBang(400, 400, 0.1); } }