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

2017/3/9

Need help

1
2
3
Nooooob Nooooob

2017/3/10

#
Super_Hippo wrote...
Add it to the Block class.
Got it. No errors anymore but the blocks are still not falling or forming the tetromino shapes.
Super_Hippo Super_Hippo

2017/3/10

#
I think you have to post the whole code again to see how everything looks right now.
Nooooob Nooooob

2017/3/10

#
Super_Hippo wrote...
I think you have to post the whole code again to see how everything looks right now.
World:
private Block[] tetromino = new Block[4];
 
    int[][]line =  {{1,1,1,1}, 
                    {0,0,0,0}, 
                    {0,0,0,0}, 
                    {0,0,0,0}};     
                            
    int[][]thunder = {{1,1,0,0}, 
                   {0,0,1,1}, 
                   {0,0,0,0}, 
                   {0,0,0,0}};
                    
    int[][]pyramid = {{0,1,0,0}, 
                      {1,1,1,0}, 
                      {0,0,0,0}, 
                      {0,0,0,0}};                                      
    public MyWorld()
    {    
        super(15, 20, 40);
        addObject(new Block(), 10, 2);
    }
    public void create()
    {
    if (!moveTetromino(1))
    {
    moveTetromino(-1);
    int[][][] layouts = { line, thunder, pyramid };
    int[][] layout = layouts[Greenfoot.getRandomNumber(layouts.length)];
    int x = Greenfoot.getRandomNumber(15);
    int y = Greenfoot.getRandomNumber(3);
    createTetromino(layout, x, y);
    if (!moveTetromino(0)) gameOver();
    }
    }
    public void createTetromino(int[][] layout, int x, int y)
    {
    int counter = 0;
    for (int a=0; a <4; a++) for (int b=0; b<4; b++)
    {
        if (layout[a][b] == 1)
        {
            tetromino[counter++] = new Block();
            addObject(tetromino[counter], x+b, y+a);
        }
    }
    }
    private boolean moveTetromino(int dir)
    {
    {
        boolean result = true;
        for (Block block : tetromino) result = result && block.fall(dir);
        return result;
    }
    }
    private void gameOver()
    {
        Greenfoot.stop();
    }
Block:
public boolean fall(int dir)
    {
    setLocation(getX(), getY()+dir);
    return getObjectsAtOffset(0, 0, Block.class).isEmpty();
    }
Super_Hippo Super_Hippo

2017/3/10

#
Try to change line 20 to:
create();
and line 24 to:
if (tetromino[0]==null || !moveTetromino(1))
If you wouldn't pass anything to the createTetromino method and make the random things in the method, you would only need to make the first change (and change it to createTetromino instead).
Nooooob Nooooob

2017/3/10

#
Super_Hippo wrote...
Try to change line 20 to:
create();
and line 24 to:
if (tetromino[0]==null || !moveTetromino(1))
Done. Error:
Super_Hippo Super_Hippo

2017/3/10

#
Oh yes, that was pretty obvious... Okay, try this:
public void createTetromino()
{
    int[][][] layouts = { line, thunder, pyramid };
    int[][] layout = layouts[Greenfoot.getRandomNumber(layouts.length)];
    int x = Greenfoot.getRandomNumber(15);
    int y = Greenfoot.getRandomNumber(3);
    //everything which is in the createTetromino method right now goes here
}
public void create()
{
    if (!moveTetromino(1))
    {
        moveTetromino(-1);
        createTetromino();
        if (!moveTetromino(0)) gameOver();
    }
}
public MyWorld()
{    
    super(15, 20, 40);
    createTetromino();
}
Nooooob Nooooob

2017/3/10

#
Super_Hippo Super_Hippo

2017/3/10

#
Try this there:
if (layout[a][b] == 1)
{
    tetromino[counter] = new Block();
    addObject(tetromino[counter], x+b, y+a);
    counter++;
}
Nooooob Nooooob

2017/3/11

#
Super_Hippo wrote...
Try this there:
if (layout[a][b] == 1)
{
    tetromino[counter] = new Block();
    addObject(tetromino[counter], x+b, y+a);
    counter++;
}
Works but it doesn't fall.
Super_Hippo Super_Hippo

2017/3/11

#
Change
public void create()
to
public void act()
Nooooob Nooooob

2017/3/11

#
Super_Hippo wrote...
Change
public void create()
to
public void act()
Done, not falling yet.
Nooooob Nooooob

2017/3/11

#
When I click "Run" it kinda stops after seconds.
Super_Hippo Super_Hippo

2017/3/11

#
I think the problem is that the blocks in the tetromino are blocked by the other blocks in the tetromino when falling.
private boolean moveTetromino(int dir)
{
    boolean result = true;
    for (Block block : tetromino) block.fall(dir);
    for (Block block : tetromino) result = result && block.fall(0);
    return result;
}
Doesn't really look optimal, but test this for now.
danpost danpost

2017/3/11

#
I think the problem is in the 'fall' method I provided. Evidently, when using 'getObjectsAtOffset', if the calling object is of the type given, it is included in the list. Therefore, line 4 of the method should be:
return getObjectsAtOffset(0, 0, Block.class).size() == 1;
My apologies.
Super_Hippo Super_Hippo

2017/3/11

#
Still, if you move and check intersection for each block alone, it can move upon another block of this tetromino.
There are more replies on the next page.
1
2
3