// TimerWorld.java import draw.Posn; import draw.World; import draw.Color; import draw.Red; import draw.White; import draw.Green; import draw.Blue; import java.util.Random; // ------------------------------------------------------------------------------ // represent the world of a Blob class TimerWorld extends World { int width = 200; int height = 400; Blob blob; TimerWorld(Blob blob) { this.blob = blob; } // what happens when the player presses a key World onKeyEvent(String ke) { return new TimerWorld(this.blob.moveBlob(ke)); } // what happens when the clock ticks World onTick() { // if the blob is outside the canvas, stop if (this.blob.outsideBounds(this.width, this.height)) return this.endOfWorld(); // else move the blob randomly at most 5 pixels in any direction else return new TimerWorld(this.blob.randomMove(5)); } // draw this world boolean draw() { return this.drawRect(new Posn(0, 0), this.width, this.height, new Blue()) && this.blob.draw(this); } } /* ; ; ; ; ;;;;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;; ; ;; ; ;;;; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ;;;;; ; ;;; ; ;; ; ; ; */ // to represent a blob on the canvas class Blob { Posn center; int radius; Blob(Posn center, int radius) { this.center = center; this.radius = radius; } // draw this blob in the given World boolean draw(World w) { return w.drawDisk(this.center, this.radius, new Red()); } // move this blob 20 pixels in the direction given by the ke Blob moveBlob(String ke){ if (ke.equals("right")) return new Blob(new Posn(this.center.x + 20, this.center.y), this.radius); else if (ke.equals("left")) return new Blob(new Posn(this.center.x - 20, this.center.y), this.radius); else if (ke.equals("up")) return new Blob(new Posn(this.center.x, this.center.y - 20), this.radius); else if (ke.equals("down")) return new Blob(new Posn(this.center.x, this.center.y + 20), this.radius); else return this; } // produce a new blob moved by a random distance < n pixels Blob randomMove(int n){ return new Blob(new Posn(this.center.x + this.randomInt(n), this.center.y + this.randomInt(n) ), this.radius); } // helper method to generate a random number in the range -n to n int randomInt(int n){ return -n + (new Random().nextInt()) % (2 * n); } // is the blob outside the bounds given by the width and height boolean outsideBounds(int width, int height) { return this.center.x < 0 || this.center.x > width || this.center.y < 0 || this.center.y > height; } // determine whether this blob is the same as than given blob boolean same(Blob b){ return this.center.x == b.center.x && this.center.y == b.center.y && this.radius == b.radius; } } /* ; ; ; ; ;;;;;; ; ; ; ; ; ; ; ; ; ; ; ;;; ; ;; ;; ; ;; ; ;;; ;;; ; ;;;;; ; ; ; ; ;; ;; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ;;;; ; ; ; ; ; ; ;;;;;; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ;;;;;;; ; ;;;;; ; ; ; ; ;; ; ;;;; ;;; ; ; ; ; ; ; */ class Examples { Examples() {} // Tests for the Blob class: Blob b1 = new Blob(new Posn(100, 100), 50); Blob b2 = this.b1.moveBlob("left"); boolean testMoveLeft = this.b1.moveBlob("left").same(new Blob(new Posn(80, 100), 50)); boolean testMoveRight = this.b1.moveBlob("right").same(new Blob(new Posn(120, 100), 50)); boolean testMoveUp = this.b1.moveBlob("up").same(new Blob(new Posn(100, 80), 50)); boolean testMoveDown = this.b1.moveBlob("down").same(new Blob(new Posn(100, 120), 50)); // this cannot really be tested boolean testRandomInt = this.b1.randomInt(5) <= 5 && this.b1.randomInt(5) >= -5; // this cannot really be tested boolean testRandomMove = this.b1.randomMove(5).center.x <= 105 && this.b1.randomMove(5).center.x >= 95 && this.b1.randomMove(5).center.y <= 105 && this.b1.randomMove(5).center.y >= 95; boolean testOutsideBounds = this.b1.outsideBounds(60, 200) == true && this.b1.outsideBounds(100, 90) == true && new Blob(new Posn(-5, 100), 50).outsideBounds(100, 110) == true && new Blob(new Posn(80, -5), 50).outsideBounds(100, 90) == true && this.b1.outsideBounds(200, 400) == false; // construct an instance of a TimerWorld TimerWorld w = new TimerWorld(new Blob(new Posn(100, 200), 20)); // start the world, start the timer boolean testWorld = this.w.start(200,400) && this.w.bigBang(0.3); }