1 / 13

Breakout in Greenfoot

Breakout in Greenfoot. Barb Ericson ericson@cc.gatech.edu Georgia Institute of Technology Feb 2010. 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. Game Rules.

nansen
Télécharger la présentation

Breakout in Greenfoot

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Breakout in Greenfoot Barb Ericson ericson@cc.gatech.edu Georgia Institute of Technology Feb 2010 Breakout

  2. 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

  3. 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

  4. Moving the Paddle (act method) • Check if the left key is down if (Greenfoot.isKeyDown("left")) • if it is move the paddle left setLocation(getX()-moveAmount, getY()); • Check if the right key is down if (Greenfoot.isKeyDown("right")) • if it is move the paddle right setLocation(getX()+moveAmount, getY()); Breakout

  5. The Ball's Act Method • Move the ball • Set the location using the current x and y and velX and velY (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

  6. Move the Ball • The ball's y velocity is 3, but it is a field • velY • The ball's x velocity should be a random value between 1 and 3 or -1 to -3 • set in the constructor • velX • To move the ball setLocation(getX() + velX, getY() + velY); Breakout

  7. Checking if hit any Actors • Get an actor (an object of the class Actor) that intersects with this one Actor actor = this.getOneIntersectingObject(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

  8. Bounce the Ball off an Actor • The actor must be a paddle or a brick (if it isn't a message) • So negate velY to make it switch direction in Y velY = -velY; Breakout

  9. 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 world.removeObject(actor); • check if won (last brick was removed) world.checkIfWon(); } } Breakout

  10. Checking Walls • If the ball hit the left wall bounce in x if (getX() - radius <= 0) velX = -velX; // negate velocity in X • if the ball hit the right wall bounce in x if (getX() + radius >= BreakoutWorld.WIDTH) velX = -velX; // negate velocity in X • if the ball hit the top wall bounce in y else if (getY() - radius <= 0) velY = -velY; // negate velocity in Y Breakout

  11. Checking Bottom Wall if (getY() + radius >= BreakoutWorld.HEIGHT) { • remove the current ball from the world world.removeObject(this); • add a new ball to the world world.newBall(); • this also checks for the end of the game } Breakout

  12. 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 7th 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 getColor() method to Brick • See the documentation for the Greenfoot classes • http://www.greenfoot.org/doc/javadoc/ Breakout

  13. 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 Breakout

More Related