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

2012/3/27

Breakout

1
2
tkiesel tkiesel

2012/4/10

#
I'll add a hint to what davmac said... you are free to create new Classes.. and to make Classes you already have into children of your new Classes. And Classes can be functional in your Ball code.. as in "Things I want to be able to bounce off of." Right now, an Actor is your "Things I want to be able to bounce off of." and you've tried Brick for your "Things I want to be able to bounce off of." Neither worked. There's a solution lurking here.
theDoctor theDoctor

2012/4/12

#
Should I make a List of the actors I want the Ball to bounce off of in a different class called "ThingsIWantToBounceOffOf", and then substitute "(Actor.class)" in "getOneIntersectingObject(Actor.class)" for "(ThingsIWantToBounceOffOf.class)"?
theDoctor theDoctor

2012/4/12

#
Ok guys! I got the PowerUps working! Now I need some help with something else. I need it to work where when the PowerUp intersects the Paddle, a new Ball appears where the PowerUp intersected the Paddle.
theDoctor theDoctor

2012/4/12

#
Here's my PowerUp code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class PowerUp here. * * @author Connor Hale * @version 4/8/12 */ public class PowerUp extends Actor { private int radius = 10; /** * Act - do whatever the PowerUp wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY() + 1); checkIntersect(); } public void checkIntersect() { Actor paddle = getOneIntersectingObject(Paddle.class); if(getY() + radius >= BreakoutWorld.HEIGHT) { getWorld().removeObject(this); } if(paddle != null) { BreakoutWorld world = (BreakoutWorld) getWorld(); world.addObject(new BallX(), getWorld().getWidth(), getWorld().getHeight()); } } } and here's my Paddle code: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; /** * A paddle is a rectangular object that can be moved via the left * and right arrow keys. * * @author Barbara Ericson, Georgia Tech * @version 1.0 April 6, 2007 */ public class Paddle extends Actor { /** Width of the paddle */ private int width = 60; /** Height of the paddle */ private int height = 10; /** amount to move */ private int moveAmount = 5; /** color of this paddle */ private Color color = Color.BLACK; /** * No argument constructor */ public Paddle() { updateImage(); } /** * Constructor that takes the width, height, color, and moveAmount * @param theWidth the width to use * @param theHeight the height to use * @param theColor the color to use * @param theAmount the number of cells (pixels) to move */ public Paddle(int theWidth, int theHeight, Color theColor, int theAmount) { width = theWidth; height = theHeight; color = theColor; moveAmount = theAmount; updateImage(); } /** * Act - a paddle will move in reaction to a left or right * arrow key being pressed */ public void act() { if(Greenfoot.isKeyDown("left")) { setLocation(getX()-moveAmount, getY()); } if(Greenfoot.isKeyDown("right")) { setLocation(getX()+moveAmount, getY()); } } /** * Method to create and set the image for this paddle. Invoke * this method again when the width, height, or color change. */ public void updateImage() { GreenfootImage image = new GreenfootImage(width,height); image.setColor(color); image.fillRect(0,0,width,height); setImage(image); } } I hope this helps.
davmac davmac

2012/4/12

#
Should I make a List of the actors I want the Ball to bounce off of in a different class called "ThingsIWantToBounceOffOf"
That works, sure. For the record, my solution would have just been to check both cases:
Actor actor = this.getOneIntersectingObject(Brick.class);
if (actor == null) {
    // didn't see a brick, so look for a paddle
    actor = this.getOneIntersectingObject(Paddle.class);
}
theDoctor theDoctor

2012/4/13

#
Now I need some help with something else. I need it to work where when the PowerUp intersects the Paddle, a new Ball appears where the PowerUp intersected the Paddle. I posted my PowerUp and Paddle code above davmac's last reply. Thanks guys!
MarkooP MarkooP

2012/4/13

#
Such a cheater.
theDoctor theDoctor

2012/4/13

#
Thanks MarkooP. Thanks.
davmac davmac

2012/4/13

#
Well, the code is pretty much identical. Check whether you're intersecting (getOneIntersectingObject(...) returns non-null) and if so, add a new object at the position of the actor that getOneIntersectingObject(...) did return.
theDoctor theDoctor

2012/4/17

#
So I need to put in the same code as I did for the Ball and Paddle, but edit it for the PowerUps?
theDoctor theDoctor

2012/4/17

#
I actually have the code working, but it keeps adding the new Ball (which I have called BallX in my code) in the very bottom right hand corner of the World. It's really getting on my nerves! I'll repost my PowerUp code here: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class PowerUp here. * * @author Connor Hale * @version 4/8/12 */ public class PowerUp extends Actor { private int radius = 10; /** * Act - do whatever the PowerUp wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY() + 1); checkIntersect(); } public void checkIntersect() { Actor paddle = getOneIntersectingObject(Paddle.class); if(getY() + radius >= BreakoutWorld.HEIGHT) { getWorld().removeObject(this); } if(paddle != null) { BreakoutWorld world = (BreakoutWorld) getWorld(); world.addObject(new BallX(), getWorld().getWidth(), getWorld().getHeight()); } } }
theDoctor theDoctor

2012/4/17

#
Ok! Nevermind! I got the the PowerUp to generate a new Ball when it intersects the Paddle. Don't need help with that anymore! But, I just thought of something. I want to make it where as the game progresses, the Paddle slowly gets smaller and smaller. I'm not sure how to go about doing this!
IsVarious IsVarious

2012/4/20

#
theDoctor wrote...
Ok! Nevermind! I got the the PowerUp to generate a new Ball when it intersects the Paddle. Don't need help with that anymore! But, I just thought of something. I want to make it where as the game progresses, the Paddle slowly gets smaller and smaller. I'm not sure how to go about doing this!
Create a variable that keeps track of say how many times the paddle has been hit, then inside a method that uses this information, have an if statement that either checks to see if the variable is at certain values, (e.g paddle has been hit 1, 5, 10 times) and have this statement affect the paddle. Here's an example...
public void PaddelCounter(){
private int pCount = 0;
      
    if (pCount <= 5){
           PaddleSize = 5; // This example can either be a variable that keeps track, or a method.
       }
     if (pCount == 10){
           PaddleSize = 10; // This example can either be a variable that keeps track, or a method.
       }
     if (pCount == 15){
           PaddleSize = 15; // This example can either be a variable that keeps track, or a method.
       }
}
You can do most anything in programming if you just think about what it is you're wanting to do, then look for the code that fits the actions you need to be taken. What I do that helps a ton when working through a project, and that is to use MindMaps, or a flow chart. You can just use block images and walk step by step through your project and use pseudo code for what you want to happen. Then you have a map to follow when writing code. It's just like writing an outline for an essay, or a book. Here's two mind map program's I use. .. The first is a website at https://bubbl.us/ and the second is a UML program which can be found at... Here Hope this helps..
theDoctor theDoctor

2012/4/20

#
Thanks a lot IsVarious. That's helped a LOT!
IsVarious IsVarious

2012/4/21

#
Sure thing bud :)
You need to login to post a reply.
1
2