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

2013/10/9

Trying to Reset (Breakout Game)

OhSoFoxy OhSoFoxy

2013/10/9

#
I'm trying to make it where the game resets the blocks after they're all gone. Can someone help me with this? Here's my code: For the Board (or the World):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Board here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Board extends World
{
    private static final int GAP = 12;
    private Paddle paddle;
    private Block block;
    private Counter counter;
    public int totalblocks = 0;
    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {  
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(460, 460, 1); 

        

        counter = new Counter();
        addObject (counter, 113,400);

        setPaintOrder (Counter.class, Ball.class, Smoke.class);
        
        paddle = new Paddle();
        addObject (paddle, getWidth() / 2, getHeight() - 40);
        createCircles();

        createBlocks();

        prepare();
    }  

    public void ballIsOut()

    {
        paddle.newBall();
    }

    public void score()
    {
        counter.addScore();
    }

    public void score(int x)
    {
        counter.addScore(x);
    }

    private void createBlocks()
    {
        int y = 30;
        int x = 50;
        while ( y <= 94) {
            createRow(y);
            y = y + 20 + GAP;
        }
    }

    private void createRow(int y)
    {
        int x = 50;
        while (x < 460)
        {
            addObject ( new Block(counter), x, y);
            totalblocks++;
            x = x + 60 + GAP;
        }
    }

    public void drawCircle() { 
        GreenfootImage image = getBackground();
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        int r = Greenfoot.getRandomNumber(256);
        int g = Greenfoot.getRandomNumber(256);
        int b = Greenfoot.getRandomNumber(256);
        int size = Greenfoot.getRandomNumber(100) +50;

        Color color = new Color(r,g,b,20);

        image.setColor (color);
        image.fillOval(x,y,size,size);
    }

    private void createCircles() {
        for (int x = 0; x< 50; x++) {
            drawCircle();
        }
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        BonusBlock bonusblock = new BonusBlock();
        addObject(bonusblock, 94, 175);
        BonusBlock bonusblock2 = new BonusBlock();
        addObject(bonusblock2, 320, 179);
    }
}
for the Ball:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    private int deltaX;
    private int deltaY;
    private int count = 2;

    private boolean stuck = true;
    private Counter counter;

    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Ball()
    {
    }

    public Ball(Counter newCounter) 
    {
        counter = newCounter;
    }    

    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null){
            deltaY = -deltaY;
            int offset = getX() - paddle.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX > 7){
                deltaX = 7;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }

        }
    }

    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }

    public void move ()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }

    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()). ballIsOut();
            getWorld().removeObject(this);
        }
    }

    private void makeSmoke()
    {
        count--;
        if (count == 0) {
            getWorld().addObject (new Smoke(), getX(), getY());
            count = 2;
        }
    }

    public void act()
    {
        if (!stuck)
        {
            move();
            makeSmoke();
            checkBlock();
            //checkBonusBlock();
            checkOut();
        }
    }

    public void release ()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }

    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);

        if (block != null)
        {
            if (block instanceof BonusBlock) 
                checkBonusBlock();
            else {
                getWorld().removeObject(block);

                deltaY = -deltaY;

                Board myBoard = (Board)getWorld();
                myBoard.score();
            }
        }

    }

    private void checkBonusBlock()
    {
        Actor block = getOneIntersectingObject(BonusBlock.class);

        if (block != null)
        {

            getWorld().removeObject(block);

            deltaY = -deltaY;

            Board myBoard = (Board)getWorld();
            myBoard.score(5);
        }

    }
}
Counter:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int score;
    private int value;
    private int target;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Counter() 
    {
        value = 0;
        target = 0;
        score = 0;
        setImage (new GreenfootImage(200,30));
        update();
    }    
    
    public void addScore()
    {
        score++;
        update();
    }
     public void addScore(int x)
    {
        score+= x;
        update();
    }
    private void update()
    { 
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.BLACK);
        img.drawString("Score: " + score, 4, 20);
    }
}
Paddle:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Paddle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Paddle extends Actor
{
    private Ball myBall;

    /**
     * Act - do whatever the Paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       if (Greenfoot.isKeyDown ("left")) {
           moveSideways(-9);
        }
        if (Greenfoot.isKeyDown ("right")) {
            moveSideways(9);
        }
        if (haveBall() && Greenfoot.isKeyDown ("space")) {
            releaseBall();
        }
        }    
    public void newBall()
    {
        myBall = new Ball();
        getWorld().addObject (myBall, getX(), getY()-20);
    }
    
    public void addedToWorld(World world)
    {
        newBall();
    }
    
    private void moveSideways(int dist)
    {
        setLocation (getX() + dist, getY());
        if (myBall != null)
        {
            myBall.move (dist);
        }
    }
    
    public void releaseBall()
    {
        myBall.release();
        myBall = null;
    }
    
    public boolean haveBall()
    {
        return myBall != null;
    }
    
    
}
Block
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Block here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Block extends Actor
{
    private Counter counter;
    
    public Block() {}
    
    public Block(Counter count)
    {
        counter = count;
    }
    /**
     * Act - do whatever the Block wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
BonusBlock
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BonusBlock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BonusBlock extends Block
{
    
    public BonusBlock() {}
    

    
    
    
    
    /**
     * Act - do whatever the BonusBlock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
danpost danpost

2013/10/9

#
You should be able to just use the following in the act method of your subclass to World:
if (getObjects(Block.class).isEmpty())
{
    level++; // increment level counter (if any)
    createBlocks(); // add new blocks
}
I guess that would mean add an act method with that code to your subclass of World; and I guess you can leave out line 3 since you do not have levels, per se.
OhSoFoxy OhSoFoxy

2013/10/10

#
Thank you so much. I was struggling on that part lol
You need to login to post a reply.