Breakout in Greenfoot Barb Ericson Georgia Institute of Technology June 2007 Georgia Institute of Technology.

Download Report

Transcript Breakout in Greenfoot Barb Ericson Georgia Institute of Technology June 2007 Georgia Institute of Technology.

Breakout in Greenfoot
Barb Ericson
Georgia Institute of Technology
June 2007
Georgia Institute of Technology
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
Georgia Institute of Technology
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 new
ball added
Georgia Institute of Technology
Moving the Paddle
• Check if the left key is down
– Greenfoot.isKeyDown("left")
– if it is move left
• setLocation(getX() – moveAmount, y);
• else check if the right key is down
– Greenfoot.isKeyDown("right")
– if it is move right
• setLocation(getX() + moveAmount,y);
Georgia Institute of Technology
The Ball's Act Method
• Move the ball
• 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
Georgia Institute of Technology
Move the Ball
• The ball's y velocity is 3 but 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
– add velX to the current x (getX)
– add velY to the current y (getY)
– Use setLocation(x,y);
Georgia Institute of Technology
Checking if hit any Actors
•
Get an actor (object of the class Actor)
that intersects with this one
Actor actor =
this.getOneIntersectingObject(Actor.class);
•
If the ball did hit an actor and it isn't the
message (object of the Message class)
– if (actor != null && !(actor instanceof
Message))
Georgia Institute of Technology
Bounce the Ball off an Actor
• The actor must be a
paddle or a brick (if it
isn't a message)
– So negate velY
velY = -velY;
Georgia Institute of Technology
If the Ball hit a Brick
• Check using
– if (actor instanceof Brick)
• If the ball did hit a brick
– remove it
• world.removeObject(actor);
– check if won
• world.checkIfWon();
Georgia Institute of Technology
Checking Walls
• If the ball hit the left wall
– if (getX() - radius <= 0)
• negate velX
• else If the ball hit the right wall
– if (getX() + radius >= BreakoutWorld.WIDTH)
• negate velX
• else If the ball hit the top wall
– else if (getY() - radius <= 0)
• negate velY
Georgia Institute of Technology
Checking Bottom Wall
• else 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
Georgia Institute of Technology
Additional Ideas
• Add sounds
– bounce sound and win or lose sounds
• If hit at edge of paddle then bounce in x as
well as y
• Speed up the velocity
– after each 7th hit
• double the x velocity
• See the documentation for the Greenfoot
classes
– http://www.greenfoot.org/doc/javadoc/
Georgia Institute of Technology