package c2Oceanimages; import tester.*; import javalib.funworld.*; import javalib.colors.*; import javalib.worldcanvas.WorldCanvas; import javalib.worldimages.*; import java.util.*; import java.awt.Color; import c1Oceandata.ExamplesOceanData; // 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{ // teh 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; } // produce the position of this shark CartPt sharkPos(){ return new CartPt(20, this.y); } // produce the image of this shark at its position WorldImage sharkImage(){ return new FromFileImage(this.sharkPos(), "small-shark.png"); } } // 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; } // produce the image of this fish at its position WorldImage fishImage(){ return new FromFileImage(this.p, "small-green-fish.png"); } } // 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{ public WorldImage fishesImage(); } // A school with no fish class EmptySchool implements School, OceanWorldConstants { EmptySchool() {} // produce an empty blue dot in the sea of blue public WorldImage fishesImage(){ return new CircleImage(new Posn(0, 0), 1, new Blue()); } } // 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; } // produce the image of this school of fish public WorldImage fishesImage(){ return new OverlayImages(this.first.fishImage(), this.rest.fishesImage()); } } //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 oceanImage.overlayImages(this.shark.sharkImage(), this.fish.fishImage()); } public WorldEnd worldEnds(){ return new WorldEnd(false, this.makeImage()); } } // Examples for the ocean game public class ExamplesOceanImages implements OceanWorldConstants{ ExamplesOceanImages() {} public static ExamplesOceanImages examplesInstance = new ExamplesOceanImages(); 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); // test the method onKeyEvent for the Ocean class public void testOnKeyEvent(Tester t){ t.checkExpect(this.ocean.onKeyEvent("x"), this.ocean); } 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); // tests for the method worldEnds public void testWorldEnds(Tester t){ t.checkExpect(this.ocean.worldEnds(), new WorldEnd(false, this.ocean.makeImage())); } // test the drawing of the ocean world image public void testDrawing(Tester t){ WorldCanvas canvas = new WorldCanvas(WIDTH, HEIGHT); canvas.drawImage(this.ocean.makeImage()); canvas.show(); } // run all tests, then run the game public static void main(String[] argv){ ExamplesOceanImages e = new ExamplesOceanImages(); Tester.runReport(e, false, false); e.ocean.bigBang(400, 400, 0.1); } }