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

2017/4/19

Breakout - cant place paddle and blocks

Phirippu Phirippu

2017/4/19

#
For my Breakout-Game for a schoolproject I have to place a ball, a paddle, to bounce the ball back, and many blocks. My first problem now is that I cant place the paddle. There isnt an error or something like that. It`s just nothing there. And the ball is every time there, with and without the lines which has to place it. Maybe you can tell me my mistake My second problem is that i dont now how to place many objects with one command. My task here: The area is 640x480. A block is 40x15 and between the block there has to be a distance of 5 pixels. So i can place 16 blocks per line and i want 5 lines with blocks. How i have to write that ? The area:
public class Spielfeld extends World
{

    /**
     * Constructor for objects of class Spielfeld.
     */
    public Spielfeld()
    {
        super(640, 480, 1);
    }

    public void act() {
        if (Greenfoot.isKeyDown("space") && 
            getObjects(Ball.class).size() == 0) {
            Ball ball = new Ball ();
            addObject(ball, 320, 240);
            
        }
        fuellen();
        if (Greenfoot.isKeyDown("space") &&
            getObjects(Paddel.class).size() == 0){
                Paddel paddel = new Paddel();
                addObject(paddel,320, 100);
            }
    }
    
    private void fuellen(){
        if (Greenfoot.isKeyDown("space") && 
            getObjects(Block.class).size() != 16) {
            Block block = new Block ();
            addObject(block, 320, 240);
        }
    }

}
The ball: public class Ball extends Actor { private int dx; private int dy; public Ball() { dx = 10 - Greenfoot.getRandomNumber(21); dy = -5 - Greenfoot.getRandomNumber(6); } /* * Check the 4 sides */ private void pruefeKontaktRandRechts() { if (getX() >= getWorld().getWidth()-1) { dx = -dx; } } private void pruefeKontaktRandLinks() { if (getX() <= 0 ){ dx = -dx; } } private void pruefeKontaktRandUnten() { if (getY() >= getWorld().getHeight()-1) { getWorld().removeObject(this); } } private void pruefeKontaktRandOben() { if(getY() <= 0) { dy = -dy; } } private void pruefeKontaktPaddel() { if (getOneIntersectingObject(Paddel.class) !=null){ dy = -dy; } } private void pruefeKontaktBlock(){ Actor block = getOneIntersectingObject(Block.class); if(block !=null){ dy = -dy; getWorld().removeObject(block); } } public void act() { pruefeKontaktPaddel(); pruefeKontaktBlock(); pruefeKontaktRandRechts(); pruefeKontaktRandLinks(); pruefeKontaktRandUnten(); pruefeKontaktRandOben(); setLocation (getX() + dx, getY() + dy); pruefeKontaktRandUnten(); } } The paddle: public void act() { if(Greenfoot.isKeyDown("right")){ setLocation (getX() + 6 , getY()); } if(Greenfoot.isKeyDown("left")){ setLocation (getX() - 6 , getY()); } } } And the block hasnt code (Im sorry for english-language mistakes...)
Phirippu Phirippu

2017/4/19

#
Here are the ball and the paddle again in code ...
public class Ball extends Actor
{
            private int dx;
            private int dy;
        public Ball() {
            dx = 10 - Greenfoot.getRandomNumber(21);
            dy = -5 - Greenfoot.getRandomNumber(6);
        }
        /*
         * Check the 4 sides
         */
    private void pruefeKontaktRandRechts() {
        if (getX() >= getWorld().getWidth()-1) {
            dx = -dx;
        } 
    }
    
    private void pruefeKontaktRandLinks() {
        if (getX() <= 0 ){
            dx = -dx;
        }
    }
    
    private void pruefeKontaktRandUnten() {
        if (getY() >= getWorld().getHeight()-1) {
            getWorld().removeObject(this);
        }
    }
    
    private void pruefeKontaktRandOben() {
        if(getY() <= 0) {
            dy = -dy;
        }
    }
    
    private void pruefeKontaktPaddel() {
        if (getOneIntersectingObject(Paddel.class) !=null){
            dy = -dy;
        }
    }
    
    private void pruefeKontaktBlock(){
        Actor block = getOneIntersectingObject(Block.class);
        if(block !=null){
            dy = -dy;
            getWorld().removeObject(block);
        }
    }

    
    public void act()
    {
        pruefeKontaktPaddel(); 
        pruefeKontaktBlock();
        pruefeKontaktRandRechts();
         pruefeKontaktRandLinks();
         pruefeKontaktRandUnten();
         pruefeKontaktRandOben();
         setLocation (getX() + dx, getY() + dy);
         pruefeKontaktRandUnten();
        }
    }        
Phirippu Phirippu

2017/4/19

#
public class Paddel extends Actor
{


    /**
     * Act - do whatever the Paddel wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    
    public void act()
    {
        if(Greenfoot.isKeyDown("right")){
            setLocation (getX() + 6 , getY());
        }
        
        if(Greenfoot.isKeyDown("left")){
            setLocation (getX() - 6 , getY());
        }
        }
}
danpost danpost

2017/4/19

#
The paddle is the only constant in the world and should be added by the constructor of your world class. The blocks should only be added when no blocks are currently present in the world. Usually, a ball is added when lives remain and no ball is currently in the world; and the press of the spacebar will set the ball in motion. However, adding a moving ball when the spacebar is pressed is a viable alternative. Sixteen blocks will fit across the width of the world; however, there would be no room for the 5 pixel gaps between them. You will have to suffice with 14 blocks across, which will cover 560; the gaps will cover an additional 65; this leaves 15 for the two ends; so, the left edge of the first block will be at about the 8th column of pixels in the world (or the x-coordinate of the first block will be about 28) and they will be place 45 pixels from each other across the line. The y-coordinate of the top row should be about 13 (half the height of a block plus a 5 pixel gap). Using these values, you can use a double 'for' loop to add the blocks (the outer loop to iterate down the rows and the inner one to iterate across each row.
Phirippu Phirippu

2017/4/19

#
And how i have to write it then ?
danpost danpost

2017/4/19

#
Phirippu wrote...
And how i have to write it then ?
You can start by re-writing your 'fuellen' method. Put the for loops in it to add the blocks into the world. This page of the java tutorials shows how to use for loops.
You need to login to post a reply.