This site requires JavaScript, please enable it in your browser!
Greenfoot back
theDoctor
theDoctor wrote ...

2012/3/27

Breakout

1
2
theDoctor theDoctor

2012/3/27

#
I'm making a breakout game. I have everything working, but I need to make power-ups. small balls that fall from the bricks once they've been hit, and make the platform smaller or larger depending on whether they are red (bad) or green (good). Any help here would be nice. My Brick class code is posted below. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; /** * A brick is a filled rectangle that doesn't do * anything when it gets the act message. * * @author Barb Ericson, Georgia Tech * @version 1.0, April 6, 2007 */ public class Brick extends Actor { /** the width of the brick */ private int width = 36; /** the height of the brick */ private int height = 8; /** the color of the brick */ private Color color; /** * Constructor that takes the color for the brick * @param theColor the color to use for this brick */ public Brick(Color theColor) { color = theColor; updateImage(); } /** * Constructor that takes the width, height, and color for this * brick. */ public Brick(int theWidth, int theHeight, Color theColor) { width = theWidth; height = theHeight; color = theColor; updateImage(); } /** * Method to act during a time step. */ public void act() {} /** * Method to create the image and set it for this brick. * If you change the width, height, or color, invoke this * method. */ public void updateImage() { GreenfootImage image = new GreenfootImage(width,height); image.setColor(this.color); image.fillRect(0,0,width,height); setImage(image); } }
danpost danpost

2012/3/28

#
One might ask: What determines when a ball falls from a hit brick? (a) random chance, random ball (b) pre-set brick, pre-set ball (c) next brick after score reaches a landmark (either or random ball) (d) the 'n'th brick (either or random ball) (e) something else I will assume that the platform must 'catch' the ball for the power up to take effect, otherwise nothing happens. The ball class should contain the code that 'looks' for, and acts on, an intersecting platform.
theDoctor theDoctor

2012/3/29

#
Yes, you're correct in saying the platform must 'catch' the power up for it to take effect.
theDoctor theDoctor

2012/3/30

#
I'm not quite sure what code I should implement into the Ball class to 'look for' an intersecting platform...
davmac davmac

2012/3/30

#
You could use, for example, the getOneIntersectingObject(...) method from Actor.
theDoctor theDoctor

2012/4/9

#
And when the Ball intersects the Brick, a power up will appear? That's a good idea...
theDoctor theDoctor

2012/4/9

#
Ok! I've got my power ups working, but I'm having a small problem. I've set the Ball to bounce off of any Actor it hits, and my power ups are Actors. I need help finding a way to keep my power ups working in the World, but not as an Actor.
davmac davmac

2012/4/9

#
I need help finding a way to keep my power ups working in the World, but not as an Actor.
I think what you mean is that you need a way to stop the ball from bouncing off power-ups? Just modify your Ball so that it ignores power-ups.
theDoctor theDoctor

2012/4/10

#
That makes sense, I'm just not quite sure how to go about doing that.
davmac davmac

2012/4/10

#
Where you check for collisions, check only for the types that want the Ball to bounce off. All the Greenfoot intersection methods have a class parameter which can be used to specify what type to look for.
theDoctor theDoctor

2012/4/10

#
Here's my Ball code, maybe this will help: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.util.List; /** * A ball is an object that can hit other objects and bounce off the * edges of the world (except the bottom edge). It will bounce off of * a paddle as well. * * @author Barbara Ericson Georgia Tech * @version 1.0 April 6, 2007 */ public class Ball extends Actor { /** the radius of this ball */ private int radius = 10; /** the width of this ball (diameter) */ private int width = radius * 2; /** the color of the ball */ private Color color = Color.BLACK; /** the amount of change in x during each act */ private int velX; /** the amount of change in y during each act */ private int velY = 3; /** * Constructor that takes no arguments */ public Ball() { velX = Greenfoot.getRandomNumber(3) + 1; if (Greenfoot.getRandomNumber(2) == 0) velX = -1 * velX; updateImage(); } /** * Constructor that takes initial values for all fields * @param theRadius the radius to use * @param theColor the color to use * @param theVelX the amount to change in X per act * @param theVelY the amount to change in Y per act */ public Ball(int theRadius, Color theColor, int theVelX, int theVelY) { radius = theRadius; color = theColor; velX = theVelX; velY = theVelY; updateImage(); } /** * Balls will move and check if they have hit a brick or paddle or * one of the edges of the world */ public void act() { BreakoutWorld world = (BreakoutWorld) getWorld(); setLocation(getX() + velX, getY() + velY); Actor actor = this.getOneIntersectingObject(Actor.class); if (actor != null && !(actor instanceof Message)) { velY = -velY; } if (actor instanceof Brick) { world.removeObject(actor); world.addObject(new PowerUp(), getX(), getY()); world.checkIfWon(); } if (getX() - radius <= 0) velX = -velX; if (getX() + radius >= BreakoutWorld.WIDTH) velX = -velX; else if (getY() - radius <= 0) velY = -velY; if (getY() + radius >= BreakoutWorld.HEIGHT) { world.removeObject(this); world.newBall(); } } /** * Method to set the ball color */ public void setColor(Color theColor) { this.color = theColor; updateImage(); } /** * Method to create the image and set it for the ball * If you change the ball width or color you should * invoke this again */ public void updateImage() { GreenfootImage image = new GreenfootImage(width,width); image.setColor(Color.BLACK); image.fillOval(0,0,width,width); setImage(image); } }
davmac davmac

2012/4/10

#
Where you check for collisions:
Actor actor = this.getOneIntersectingObject(Actor.class);
... check only for the types that want the Ball to bounce off:
Actor actor = this.getOneIntersectingObject(Brick.class);
All the Greenfoot intersection methods have a class parameter which can be used to specify what type to look for.
theDoctor theDoctor

2012/4/10

#
I've tried this before, but then it only passes through the Brick class and nothing else. I have a class for Paddle as well, and it needs to bounce off of that, the edges of the world, and the Bricks, just not the PowerUps. I've been struggling with this for a while.
davmac davmac

2012/4/10

#
Ok, so think about what this line actually does: Actor actor = this.getOneIntersectingObject(Brick.class); What will 'actor' be if the ball doesn't see a brick? What should it do in that case?
davmac davmac

2012/4/10

#
A further hint: There are two separate cases - the ball sees a brick, and the ball doesn't see a brick. The first case is handled correctly (ball bounces off the brick). The second case isn't. How do you differentiate between two different cases (conditions, situations, states) in Java, and do something different depending on which one you have?
There are more replies on the next page.
1
2