/*********************************************** * CS2510 Fall 2011 * Lecture #7.2 * Java World: Rockets ***********************************************/ import tester.*; import image.*; import world.*; abstract class BaseRocket extends World { // Image of the rocket Image rocket = new FromFile("rocket.png"); // Image of rocket flames Image flames = new FromFile("flame.png"); } class TakeOff extends BaseRocket { int h; /** * Template * * Fields * this.h - int * * Methods * * this.onDraw - Scene * this.onTick - TakeOff */ TakeOff(int h) { this.h = h; } // Redraws the world according to the rocket position public Scene onDraw() { return new EmptyScene(200,500).placeImage(rocket,100,500-h); } // Creates a new world to move the rocket public TakeOff onTick() { return new TakeOff(h+5); } } class LandRocket extends BaseRocket { int h; LandRocket(int h) { this.h = h; } public Scene onDraw() { return new EmptyScene(200,500).placeImage(rocket,100,500-h); } public LandRocket onTick() { if (h > 50) { return new LandRocket(h-5); } else { return new LandRocket(h); } } } class TakeOffAndLandRocket extends BaseRocket { int h; boolean landing; /** * Template * * Fields * this.h - int * this.landing - boolean * * Methods * this.onDraw - Scene * this.onTick - TakeOffAndLandRocket * this.addFlames(Scene) - Scene */ TakeOffAndLandRocket(int h, boolean landing) { this.h = h; this.landing = landing; } // Draws the scene of the state of the rocket with flames public Scene onDraw() { return addFlames(new EmptyScene(200,500).placeImage(rocket,100,500-h)); } Scene addFlames(Scene s) { if (landing) { return s; } else { return s.placeImage(flames, 100,550-this.h); } } public TakeOffAndLandRocket onTick() { if (landing) { if (h > 50) { return new TakeOffAndLandRocket(h-5,landing); } else { return new TakeOffAndLandRocket(h,false); } } else { if (h > 400) { return new TakeOffAndLandRocket(h+5,true); } else { return new TakeOffAndLandRocket(h+5,landing); } } } } class Balloon extends World { int x; int y; /** * Template * * Fields * this.x - int * this.y - int * * Methods * this.onDraw - Scene * this.onTick - Balloon * this.onMouse - Balloon */ Balloon(int x, int y) { this.x = x; this.y = y; } // Draw the balloon into the empty scene public Scene onDraw() { return new EmptyScene(400,400).placeImage(new Circle(20,"solid","red"), x, y); } // Floats the balloon up on tick public Balloon onTick() { return new Balloon(this.x,this.y-2); } // Creates a new balloon when you click the mouse public Balloon onMouse(int mx, int my, String me) { if(me.equals("button-down")) { return new Balloon(mx,my); } else { return this; } } } class RocketExamples { BaseRocket toff = new TakeOff(50); //World endtoff = toff.bigBang(); // Creates a landing rocket starting at 450 BaseRocket land = new LandRocket(450); // World endland = land.bigBang(); BaseRocket toffland = new TakeOffAndLandRocket(50,false); //World endtoffland = toffland.bigBang(); Balloon bl = new Balloon(200,350); World blworld = bl.bigBang(); }