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

2017/5/10

Need help to create counter

skitlesas skitlesas

2017/5/10

#
Hey. I need help with counter.. I need add this but i don't know how. I want like when ball hit BLOCK i get like +points. WORLD:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The game board. The board should have a paddle that can move.
 * 
 * @author 
 * @version 
 */
public class Board extends World
{
    private Paddle paddle;
    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {    
        super(800, 600, 1);
        paddle = new Paddle();
        addObject ( paddle, getWidth() / 2, getHeight() - 40);
        prepare();
    }

   public void ballOut()
    {
        paddle.newBall();
    }
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */

    private void prepare()
    {
        Block block = new Block();
        addObject(block, 326, 199);
        Block block2 = new Block();
        addObject(block2, 534, 248);
        Block block3 = new Block();
        addObject(block3, 531, 135);
        Block block4 = new Block();
        addObject(block4, 262, 392);
        Block block5 = new Block();
        addObject(block5, 458, 253);
        Block block6 = new Block();
        addObject(block6, 476, 248);
        block6.setLocation(485, 319);
        block.setLocation(398, 83);
        block.setLocation(399, 52);
        block3.setLocation(447, 75);
        block5.setLocation(343, 74);
        block3.setLocation(451, 75);
        block5.setLocation(352, 75);
        block4.setLocation(302, 99);
        block2.setLocation(403, 100);
        block2.setLocation(399, 99);
        block6.setLocation(499, 99);
        block6.setLocation(498, 98);
        block6.setLocation(498, 99);
        Block block7 = new Block();
        addObject(block7, 259, 123);
        Block block8 = new Block();
        addObject(block8, 361, 123);
        block7.setLocation(258, 122);
        block8.setLocation(356, 122);
        Block block9 = new Block();
        addObject(block9, 459, 123);
        block9.setLocation(459, 123);
        block9.setLocation(455, 122);
        Block block10 = new Block();
        addObject(block10, 557, 120);
        block10.setLocation(554, 122);
        Block block11 = new Block();
        addObject(block11, 206, 147);
        Block block12 = new Block();
        addObject(block12, 319, 153);
        Block block13 = new Block();
        addObject(block13, 419, 153);
        block12.setLocation(313, 149);
        block11.setLocation(206, 146);
        block12.setLocation(305, 146);
        block13.setLocation(404, 146);
        Block block14 = new Block();
        addObject(block14, 506, 147);
        block14.setLocation(501, 147);
        block14.setLocation(504, 145);
        block14.setLocation(503, 146);
        Block block15 = new Block();
        addObject(block15, 604, 148);
        block15.setLocation(602, 146);
        Block block16 = new Block();
        addObject(block16, 168, 177);
        block16.setLocation(161, 169);
        Block block17 = new Block();
        addObject(block17, 265, 169);
        block17.setLocation(260, 169);
        Block block18 = new Block();
        addObject(block18, 363, 169);
        block18.setLocation(359, 169);
        Block block19 = new Block();
        addObject(block19, 462, 168);
        block19.setLocation(457, 169);
        Block block20 = new Block();
        addObject(block20, 558, 168);
        block20.setLocation(557, 169);
        Block block21 = new Block();
        addObject(block21, 195, 197);
        block21.setLocation(205, 191);
        Block block22 = new Block();
        addObject(block22, 310, 185);
        Block block23 = new Block();
        addObject(block23, 310, 185);
        block23.setLocation(413, 199);
        block22.setLocation(305, 191);
        block21.setLocation(206, 191);
        block23.setLocation(403, 191);
        Block block24 = new Block();
        addObject(block24, 506, 190);
        block24.setLocation(501, 191);
        Block block25 = new Block();
        addObject(block25, 656, 170);
        block25.setLocation(656, 169);
        Block block26 = new Block();
        addObject(block26, 606, 192);
        block26.setLocation(601, 191);
        Block block27 = new Block();
        addObject(block27, 256, 218);
        Block block28 = new Block();
        addObject(block28, 357, 217);
        block27.setLocation(257, 214);
        block28.setLocation(358, 214);
        block27.setLocation(259, 214);
        Block block29 = new Block();
        addObject(block29, 462, 213);
        Block block30 = new Block();
        addObject(block30, 548, 226);
        block29.setLocation(457, 214);
        block30.setLocation(555, 214);
        Block block31 = new Block();
        addObject(block31, 324, 240);
        block31.setLocation(303, 235);
        Block block32 = new Block();
        addObject(block32, 406, 234);
        block32.setLocation(403, 235);
        Block block33 = new Block();
        addObject(block33, 507, 236);
        block33.setLocation(507, 236);
        block32.setLocation(408, 236);
        block31.setLocation(308, 236);
        Block block34 = new Block();
        addObject(block34, 361, 259);
        Block block35 = new Block();
        addObject(block35, 455, 260);
        block34.setLocation(361, 257);
        block35.setLocation(460, 257);
        Block block36 = new Block();
        addObject(block36, 416, 282);
        block36.setLocation(414, 277);
        block.setLocation(402, 56);
        block36.setLocation(409, 277);
        block3.setLocation(453, 76);
        block5.setLocation(353, 78);
        block3.setLocation(455, 79);
        block5.setLocation(355, 79);
        block.setLocation(404, 60);
        block4.setLocation(302, 101);
        block2.setLocation(396, 101);
        block6.setLocation(494, 101);
        block5.setLocation(352, 81);
        block3.setLocation(455, 82);
        block5.setLocation(354, 82);
        block3.setLocation(451, 82);
        block7.setLocation(258, 127);
        block8.setLocation(356, 126);
        block9.setLocation(455, 126);
        block10.setLocation(556, 127);
        block9.setLocation(456, 126);
        block8.setLocation(358, 127);
        block9.setLocation(456, 127);
        block10.setLocation(555, 127);
    }
}
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
 * @version 
 */
public class Ball extends Actor
{
    
    /**
     * changeInX and ChangeInY are the movement in the X and Y directions for the ball.
     * If changeInX is positive, the ball will travel right, if negative the ball will travel left.
     * If changeInY is positive, the ball will travel down, if negative the ball will travel up.
     */
    private int changeInX;         // x movement speed
    private int changeInY;         // y movement speed
    
    private boolean stuck = true;   // stuck to paddle
    
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            checkOut();
        }
    }
    
    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        /** setLocation moves the possition of the ball, by placing the ball in its 
         * old location, PLUS the movement in the X and Y direction.
         */
        setLocation (getX() + changeInX, getY() + changeInY);
        checkPaddle();
        checkWalls();
        checkBlock();
    }
    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            changeInX = -changeInX;
        }
        if (getY() == 0) {
            changeInY = -changeInY;
        }
    }
    
    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()).ballOut();
            getWorld().removeObject(this);
        }
    }
    
    /**
     * Check to see if we've hit the paddle, then adjust our movement.
     */
    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        // the next if statement chacks to see if we've hit the paddle
        if (paddle != null) {
            changeInY = -changeInY;
            int offset = getX() - paddle.getX();
            changeInX = changeInX + (offset/10);
            if (changeInX > 7) {
                changeInX = 7;
            }
            if (changeInX < -7) {
                changeInX = -7;
            }
        }            
    }
      /**
     * Check whether we've hit one of the blocks. Reverse direction if necessary.
     */
    private void checkBlock()
    {
      Actor block = getOneIntersectingObject(Block.class);
        // the next if statement chacks to see if we've hit the paddle
        if (block != null) {
            changeInY = -changeInY;
            int offset = getX() - block.getX();
            changeInX = changeInX + (offset/10);
            if (changeInX > 7) {
                changeInX = 7;
            }
            if (changeInX < -7) {
                changeInX = -7;
            }
        }   
        World world;
        world = getWorld();
        world.removeObject(block);
    }
    /**
     * 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()
    {
        changeInX = 0; //Greenfoot.getRandomNumber(11) - 5;
        changeInY = -5;
        stuck = false;
    }
}
danpost danpost

2017/5/10

#
For more uniformly placed blocks, change your prepare method to this:
private void prepare()
{
    for (int y=0; y<11; y++)
    {
        for (int x=0; x<11; x++)
        {
            if ((x+y)%2 == 1 && Math.abs(x-5)+Math.abs(y-5) <= 6)
            {
                addObject(new Block(), 160+x*50, 60+y*22);
            }
        }
    }
}
I do not see anything related to a counter anywhere in the given code (nothing added to the world, no collision detection and not even a class for the counter). Please refer to my Value Display Tutorial scenario.
You need to login to post a reply.