Breakout in Greenfoot Barb Ericson ericsoncc gatech edu
Breakout in Greenfoot Barb Ericson ericson@cc. gatech. edu Georgia Institute of Technology Feb 2010 Breakout 1
The Breakout Game • Open Breakout. Start – There are Bricks, a Message that says we are starting with Ball 1, a Ball, and a Paddle • But if you click on act or run nothing happens Breakout 2
Game Rules • Use the left and right arrow keys to move the paddle – Use the paddle to hit the ball into the bricks to get rid of them – Get rid of all the bricks to win the game • The ball will bounce off most of the walls, paddle, and bricks – You get up to three balls to use • If you still have bricks after using 3 balls you lose • If a ball hits the bottom wall it is removed and a new ball is added Breakout 3
Moving the Paddle (act method) • Check if the left key is down if (Greenfoot. is. Key. Down("left")) – if it is move the paddle left set. Location(get. X()-move. Amount, get. Y()); • Check if the right key is down if (Greenfoot. is. Key. Down("right")) – if it is move the paddle right set. Location(get. X()+move. Amount, get. Y()); Breakout 4
The Ball's Act Method • Move the ball – Set the location using the current x and y and vel. X and vel. Y (amount to move in X and Y) • Check if hit any actors – If hit an actor and that actor isn't the message • Bounce off the object • Check if the object was a brick – if it was a brick remove the brick from the world and have the world check if the player won (no more bricks) • Check if at any walls – Bounce off top or side walls – If at bottom wall remove ball and create a new one Breakout 5
Move the Ball • The ball's y velocity is 3, but it is a field – vel. Y • The ball's x velocity should be a random value between 1 and 3 or -1 to -3 – set in the constructor – vel. X • To move the ball set. Location(get. X() + vel. X, get. Y() + vel. Y); Breakout 6
Checking if hit any Actors • Get an actor (an object of the class Actor) that intersects with this one Actor actor = this. get. One. Intersecting. Object(Actor. class); • If the ball did hit an actor and the actor isn't the message (object of the Message class) if (actor != null && !(actor instanceof Message)) { Breakout 7
Bounce the Ball off an Actor • The actor must be a paddle or a brick (if it isn't a message) – So negate vel. Y to make it switch direction in Y vel. Y = -vel. Y; Breakout 8
If the Ball hit a Brick • Check using if (actor instanceof Brick) { • If the ball did hit a brick – remove the brick from the world. remove. Object(actor); – check if won (last brick was removed) world. check. If. Won(); } } Breakout 9
Checking Walls • If the ball hit the left wall bounce in x if (get. X() - radius <= 0) vel. X = -vel. X; // negate velocity in X • if the ball hit the right wall bounce in x if (get. X() + radius >= Breakout. World. WIDTH) vel. X = -vel. X; // negate velocity in X • if the ball hit the top wall bounce in y else if (get. Y() - radius <= 0) vel. Y = -vel. Y; // negate velocity in Y Breakout 10
Checking Bottom Wall if (get. Y() + radius >= Breakout. World. HEIGHT) { – remove the current ball from the world. remove. Object(this); – add a new ball to the world. new. Ball(); • this also checks for the end of the game } Breakout 11
Additional Ideas • Add sounds – bounce sound and win or lose sounds • If the ball hits at the edge of paddle then bounce in x as well as y – if the distance from the center of the ball to the left or right edge of the paddle is less than or equal the radius of the ball • Speed up the velocity – after each 7 th hit with the paddle • double the x velocity • Make the paddle smaller over time • Change the ball color to match the last hit brick's color – Add a get. Color() method to Brick • See the documentation for the Greenfoot classes – http: //www. greenfoot. org/doc/javadoc/ Breakout 12
Ideas for Breakout • Introduce object-oriented analysis using Breakout-export – Have them name the classes shown and describe the methods needed • Give students Breakout-Start – Have them create the full code for Breakout 13
- Slides: 13