import draw.*; import colors.*; import geometry.*; import java.util.Random; /* A Game of Pong +-------------------------------+ | | | | |* | | * | | * | | * | | * | | * | +-----------XXXX----------------+ Rules: A ball starts at a random height on the left and falls from the left side diagonally down. At the bottom is a paddle that can move left and right controlled by the arrow keys.When the ball hits the bottom, but misses the paddle, it disappears from the game. When the ball hits the paddle, it bounces back and continues diagonally up to the right. When the ball exits the playing field, a new ball comes into play. Classes needed: Ball - a Posn Paddle - a Posn World - contains Ball and Paddle */ /* // // // ++++: ++++: // +$$$$* ---$* ---$* // ++ +$ $* $* // ++ :$ -+$$+ $* $* // ++ -++ :* -++ $* $* // +$$@+ ++ $* $* // ++ *++ *+++ $* $* // ++ -@: *$+:++ $* $* // ++ $+ $+ ++ $* $* // ++ *@: ++ -+$ $* $* // +@@$$+ :$$+:$$ $* $* // // // */ // to represent a ball in a Pong game class Ball { Posn pos; int direction; // direction: +1 down-right, -1 up-right int UP = -1; int DOWN = 1; Ball(Posn pos, int direction) { this.pos = pos; this.direction = direction; } // move the ball diagonally down-right or up-right by three pixels Ball moveBall(){ return this; } // reverse the ball direction as it bounces off the paddle // and move it a bit up as well Ball bounce(){ return this; } // did the ball hit the bottom, given the height of the canvas? boolean hitBottom(int height){ return false; } // is the ball out of bounds, given the width of the canvas // we only are concerned about the right and top bounds boolean outOfBounds(int width, int height){ return false; } // draw this ball on the given canvas boolean draw(Canvas c){ return c.drawDisk(this.pos, 10, new Red()); } } /* // // // +: +: ++++: // +$$$$+ $* $* ---$* // +$ $+ $* $* $* // +$ ++ -+$$+ +$$*$* +$$*$* $* -+$+- // +$ $+ :* -++ *$+-+$* *$+-+$* $* -$+-+$ // +$:*+$ ++ ++ $* ++ $* $* ++ -@: // +$+++ *+++ $* $* $* $* $* $++++$* // +$ *$+:++ @: $* @: $* $* $+++++- // +$ $+ ++ ++ $* ++ $* $* ++ // +$ ++ -+$ ++--+$* ++--+$* $* *$* // +$ :$$+:$$ $@$-$* $@$-$* $* +$$$$* // // // */ // to represent a paddle in a Pong game class Paddle { Posn pos; Paddle(Posn pos) { this.pos = pos; } // move the paddle left or right, depending on the given key Paddle movePaddle(String ke){ return this; } // did this paddle hit the given ball? boolean hitBall(Ball b){ return false; } // helper method: is x between low and high? boolean isWithin(int x, int low, int high){ return true; } // draw this paddle on the given canvas boolean draw(Canvas c){ return true; } } /* // // // +: +: ++++: ++++: +: // +$$$$+ $* $* ---$* +* +- ---$* $* // +$ $+ $* $* $* ++ $ $* $* // +$ ++ -+$$+ +$$*$* +$$*$* $* -+$+- ++ *+ $ ++$+ +**+$+ $* +$$*$* // +$ $+ :* -++ *$+-+$* *$+-+$* $* -$+-+$ *+ +@--+ +$: +$ $++*++ $* *$+-+$* // +$:*+$ ++ ++ $* ++ $* $* ++ -@::+ ++*:+ $+ @+ $+ -- $* ++ $* // +$+++ *+++ $* $* $* $* $* $++++$*-$ $++++-@: $+ $+ $* $* $* // +$ *$+:++ @: $* @: $* $* $+++++- @:+:++*-@- ++ $+ $* @: $* // +$ $+ ++ ++ $* ++ $* $* ++ $++ $+- @+ $+ $+ $* ++ $* // +$ ++ -+$ ++--+$* ++--+$* $* *$* +$: $$ ++- +$ $+ $* ++--+$* // +$ :$$+:$$ $@$-$* $@$-$* $* +$$$$* +@ +$ +$$$: $+ $* $@$-$* // // // */ // to represent a the Pong game world class PaddleWorld extends World{ Ball ball; Paddle paddle; Color BACKG = new Black(); int HEIGHT = 200; int WIDTH = 300; int UP = -1; int DOWN = 1; PaddleWorld(Ball ball, Paddle paddle) { this.ball = ball; this.paddle = paddle; } // draw this game world boolean draw() { return true; } // erase this game world boolean erase() { return this.theCanvas.drawRect(new Posn(0, 0), this.WIDTH, this.HEIGHT, this.BACKG); } // produce a new game world form this game world moving the ball // in the given direction PaddleWorld move() { return this; } // produce a new game world from this game world in response to a key event World onKeyEvent(String ke) { return this; } // produce a new game world from this game world as it changes at each tick World onTick() { return this; } // produce a random initial height of the ball int randomHeight(){ return new Random().nextInt() % (this.HEIGHT / 2); } } /* // // // ++++: // +$$$$$* ---$* // ++ $* // ++ ++ +: -+$$+ :+*$-*$* ++*+$* $* -+$+- *+$$+ // ++ *$: ++ :* -++ *+++++++ +++*+@ $* -$+-+$ -$: :* // +$$$$+ +$:$ ++ *$ *$ *+ ++ $* $* ++ -@: *$ // ++ $$+ *+++ *+ *+ *+ ++ ++ $* $++++$* +$+- // ++ +@* *$+:++ *+ *+ *+ ++ ++ $* $+++++- *+@+ // ++ +++$ $+ ++ *+ *+ *+ ++ $+ $* ++ *@ // ++ :$ ++ ++ -+$ *+ *+ *+ +$- +$ $* *$* :- +$ // +@@@@@+ $* -$+ :$$+:$$*+ *+ *+ +++$$: $* +$$$$* +$$$+: // ++ // ++ // +* */ // Examples of data and tests of all methods class Examples { Examples() {} }