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

2017/5/13

set next world if score = 20

skitlesas skitlesas

2017/5/13

#
Hello. I need help with score. Now i have counter in my game, it's working but i din't know how to make when i get like 20 score game set next World. I use this lesson from youtube: https://www.youtube.com/watch?v=KVQU3sTSR7k Ball:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    
    private boolean stuck = true;   // stuck to paddle
    public Ball()
    {
        
    }
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            checkOut();
            if (getWorld() == null) return;
            checkBlock();
        }
    }
    
    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }
    
    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }
    
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        if(block != null)
        {
            getWorld().removeObject(block);
            deltaY=-deltaY;
            Board myBoard = (Board) getWorld();
            Counter counter = myBoard.getCounter();
            counter.bumpCount(2);
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
    }
    
    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;
            }
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The game board. The board had a paddle that can move.
 * 
 * @author mik
 * @version 1.0
 */
public class Board extends World
{
    private static final int GAP = 12;

    private Paddle paddle;
    private Counter theCounter;
    
    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {    
        super(460, 460, 1);
        setPaintOrder (Ball.class);
        theCounter = new Counter();
        addObject(theCounter , 34, 449);

        paddle = new Paddle();
        addObject ( paddle, getWidth() / 2, getHeight() - 40);

        createBlocks();
    }
    
    public Counter getCounter()
    {
        return theCounter;
    }
    
    /**
     * Create the blocks at the top of the world.
     */
    private void createBlocks()
    {
        int y = 30;

        while ( y <= 94 ) {
            createRow(y);
            y = y + 20 + GAP;
        }
    }

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

    public void ballIsOut()
    {
        paddle.newBall();
    }
}
Counter:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int totalCount = 0;
    public Counter()
    {
        setImage(new GreenfootImage("Taskai:", 20, Color.WHITE, Color.BLACK));
    }
    
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("Taskai: " + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
danpost danpost

2017/5/13

#
Add the following to the Counter class:
public int getCount()
{
    return totalCount;
}
Then, in Board class act method (add the method), add this:
if (theCounter.getCount() >= 20)
{
    Greenfoot.setWorld(new Board2()); // adjust next world class name as needed
    return;
}
skitlesas skitlesas

2017/5/14

#
Yes it's working, but when it's set Board2(Lvl2) and when my ball touch Block i get this error:
java.lang.ClassCastException: Lvl2 cannot be cast to Lvl1
	at Ball.checkBlock(Ball.java:65)
	at Ball.act(Ball.java:31)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
And getting this error when ball touch bottom
java.lang.ClassCastException: Lvl2 cannot be cast to Lvl1
	at Ball.checkOut(Ball.java:77)
	at Ball.act(Ball.java:29)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
Ball:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    
    private boolean stuck = true;   // stuck to paddle
    private int counter;
    public Ball()
    {
        counter = 0;
    }
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            checkOut();
            if (getWorld() == null) return;
            checkBlock();
        }
    }
    
    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }
    
    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }
    
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        if(block != null)
        {
            getWorld().removeObject(block);
            deltaY=-deltaY;
            Lvl1 myBoard = (Lvl1) getWorld();
            Counter counter = myBoard.getCounter();
            counter.bumpCount(3);
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Lvl1) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
        if (getY() == getWorld().getHeight()-1) {
            ((Lvl2) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
    }
    
    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;
            }
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
danpost danpost

2017/5/14

#
You have the Ball only looking for the counter in the Lvl1 world. Use the following to determine if the type of World object the actor is in is a Lvl1 world:
if (getWorld() instanceof Lvl1)
and similar for other type worlds. So, to bump the counter, no matter the world, replace lines 65 through 67 with:
if (getWorld() instanceof Lvl1)  ((Lvl1)getWorld()).getCounter().bumpCount(3);
else if (getWorld() instanceof Lvl2) ((Lvl2)getWorld()).getCounter().bumpCount(3);
You can remove line 77 and lines 80 through 83.
skitlesas skitlesas

2017/5/14

#
danpost wrote...
You have the Ball only looking for the counter in the Lvl1 world. Use the following to determine if the type of World object the actor is in is a Lvl1 world:
if (getWorld() instanceof Lvl1)
and similar for other type worlds. So, to bump the counter, no matter the world, replace lines 65 through 67 with:
if (getWorld() instanceof Lvl1)  ((Lvl1)getWorld()).getCounter().bumpCount(3);
else if (getWorld() instanceof Lvl2) ((Lvl2)getWorld()).getCounter().bumpCount(3);
You can remove line 77 and lines 80 through 83.
Where add this:
if (getWorld() instanceof Lvl1)
My world(first level)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The game board. The board had a paddle that can move.
 * 
 * @author mik
 * @version 1.0
 */
public class Lvl1 extends World
{
    private static final int GAP = 12;

    private Paddle paddle;
    private Counter theCounter;
    
    /**
     * Constructor for objects of class Board.
     * 
     */
    public Lvl1()
    {    
        super(700, 600, 1);
        setPaintOrder (Ball.class);
        theCounter = new Counter();
        
        addObject(theCounter , 35, 590);
        paddle = new Paddle();
        addObject ( paddle, getWidth() / 2, getHeight() - 40);
        createBlocks();
    }
    
    public Counter getCounter()
    {
        return theCounter;
    }
    
    /**
     * Create the blocks at the top of the world.
     */
    private void createBlocks()
    {
        int y = 30;

        while ( y <= 94 ) {
            createRow(y);
            y = y + 20 + GAP;
        }
    }

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

    public void ballIsOut()
    {
        paddle.newBall();
    }
    public void act() 
    {
        if (theCounter.getCount() >= 20)
{
    Greenfoot.setWorld(new Lvl2()); // adjust next world class name as needed
    return;
}
    }    
}
danpost danpost

2017/5/14

#
skitlesas wrote...
Where add this:
if (getWorld() instanceof Lvl1)
It is just the beginning of the second code-set given for the Ball class.
skitlesas skitlesas

2017/5/14

#
Thanks a lot, it's working but I have littile problem. My ball when hit bottom just disappear. Look this .gif I made this for you it's my game problem. Link: https://gyazo.com/1061099963ee06165001bc83b1250fbd I want like, if my ball hit bottom my world restart( restart blocks, counter...) My ball:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    
    private boolean stuck = true;   // stuck to paddle
    private int counter;
    public Ball()
    {
        counter = 0;
    }
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            checkOut();
            if (getWorld() == null) return;
            checkBlock();
        }
    }
    
    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }
    
    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }
    
    private void checkBlock()
    {
        Actor block = getOneIntersectingObject(Block.class);
        if(block != null)
        {
            getWorld().removeObject(block);
            deltaY=-deltaY;
            if (getWorld() instanceof Lvl1)  ((Lvl1)getWorld()).getCounter().bumpCount(3);
            else if (getWorld() instanceof Lvl2) ((Lvl2)getWorld()).getCounter().bumpCount(4);
            else if (getWorld() instanceof Lvl3) ((Lvl3)getWorld()).getCounter().bumpCount(5);
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            getWorld().removeObject(this);
        }
    }
    
    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;
            }
        }            
    }
    
    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
danpost danpost

2017/5/14

#
In world act method, do this: If no ball in world: * remove all blocks * remove paddle * (re) prepare the world (add blocks, paddle and ball)
You need to login to post a reply.