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

2017/12/6

How to add level

Arifwc Arifwc

2017/12/6

#
So I have developed tetris game from original scenario which is written by Jedi_Knight but I dont understand all of part of the code. Here is the code : Tetris World :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

/**
 * Write a description of class TetrisWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TetrisWorld  extends World
{
    List<Block> blocks;
    Block[] block = new Block[4];
    public int getTetrominoSpeed()
    {
        if (speed==9) return 4;
        return 39-4*speed;
    }
    
    public int getTetrominoMaxSpeed()
    {
        return 1;
    }

    /**
     * Constructor for objects of class TetrisWorld.
     * 
     */
    public TetrisWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(16, 24, 20); 
        for (int i=0; i<4; i++)
        {
            block[i] = new Block();
        }
        blocks = new ArrayList<Block>();
        started = false;
        prepare();
    }
    
    public void addBlock(int x, int y)
    {
        Block p = new Block();
        addObject(p, x, y);
        blocks.add(p);
    }
    
    public void addExistentBlock(Block p)
    {
        blocks.add(p);
    }
    
    public boolean isEmpty(int x, int y)
    {
        if (x<0 || y<0 || x>=getWidth() || y>=getHeight()-2)
            return false;
        for (Block p: blocks)
            if (p.getX()==x && p.getY()==y)
                return false;
        return true;
    }
//real-time    
    int speed;
    static Counter score = new Counter("score : ");
    Tetromino currentTetromino;
    boolean started;
    public void prepare(){
        addObject(score,8,1);
        LeftButton leftbutton = new LeftButton();
        addObject(leftbutton,3,22);
    }


    public void startTheGame()
    {
        for (Block p: blocks)
            removeObject(p);
        blocks.clear();
        speed = 0;
        score.setValue(0);
        currentTetromino = null;
        spawnTetromino();
    }
    
    public void spawnTetromino()
    {
        checkTheLines();
        
        if (currentTetromino!=null)
            removeObject(currentTetromino);
        
        switch (Greenfoot.getRandomNumber(7))
        {
            case 0: currentTetromino = new ITetromino();
            break;
            case 1: currentTetromino = new JTetromino();
            break;
            case 2: currentTetromino = new LTetromino();
            break;
            case 3: currentTetromino = new OTetromino();
            break;
            case 4: currentTetromino = new STetromino();
            break;
            case 5: currentTetromino = new TTetromino();
            break;
            case 6: currentTetromino = new ZTetromino();
            break;
        }
        int x0 = Greenfoot.getRandomNumber(12)+2, y0 = 5;
        addObject(currentTetromino, x0, y0);
        if (!currentTetromino.check(currentTetromino.getX(), y0, 0))
        {
            currentTetromino.freeBlocks();
            removeObject(currentTetromino);
            currentTetromino = null;
            Greenfoot.stop();
        }
        
    }
   //////////// 
    public void checkTheLines()
    {
        int[] c = new int[getHeight()];
        for (Block p: blocks)
            c[p.getY()]++;
        int[] cnt = new int[getHeight()+1];
        cnt[getHeight()] = 0;
        for (int i = getHeight()-1; i>=0; i--)
            cnt[i] = cnt[i+1] + (c[i]==getWidth()?1:0);
        if (cnt[0]==0) return;
        List<Block> removed = new ArrayList<Block>();
        for (Block p: blocks)
        {
            if (c[p.getY()]==getWidth())
            {
                removeObject(p);              
                removed.add(p);
            }
        }
        for (Block p: removed)
            blocks.remove(p);
        for (Block p: blocks)
            p.setLocation(p.getX(), p.getY()+cnt[p.getY()]);
        switch (cnt[0])
        {
            case 1: score.add(100);
            break;
            case 2: score.add(300);
            break;
            case 3: score.add(600);
            break;
            case 4: score.add(1200);
            break;
        }
        speed = Math.min(9, score.getValue()/1000);
    }
    
    public void act()
    {
        if (!started)
        {
            started = true;
            startTheGame();
        }
    }
    
    public int getSpeed()
    {
        return speed;
    }

}
Tetromino :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tetromino here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tetromino extends Actor
{
    int size, center;
    int[][] matrix;
    int rotation;
    int posX, posY;
    int maxRot;
    Block[] blocks;
    int counter;
    boolean maxSpeed;
    
    static InputController inputRight, inputRotate, inputLeft, inputSpeed;
    static void initControllers()
    {
        if (inputRotate!=null) return;
        inputRotate = new InputController("up", 10);
        inputRight = new InputController("right",5);
        inputLeft = new InputController("left",5);
        inputSpeed = new InputController("space",20);
    }
   
    public Tetromino( int[][] matrix, int maxRot)
    {
        initControllers();
        size = matrix.length;
        center = (size-1)/2;
        posX = 0;
        posY = 0;
        rotation = 0;
        this.matrix = matrix;
        this.maxRot = maxRot;
    }
    
    public void addedToWorld(World world)
    {
        setImage(new GreenfootImage(1, 1));
        super.addedToWorld(world);
        initBlocks();
    }
        
    /**
     * Act - do whatever the Tetromino wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act()
    {
        if (inputRotate.pressed2())
            if (rotate()) inputRotate.freeze();
        if (inputLeft.pressed2())
            if (goLeft()) inputLeft.freeze();
        if (inputRight.pressed2())
            if (goRight()) inputRight.freeze();
        if (inputSpeed.pressed2())
            if (!maxSpeed)
            {
                maxSpeed = true;
                inputSpeed.freeze();
            }
            
        counter++;
        if (counter>=(maxSpeed?getMyWorld().getTetrominoMaxSpeed():getMyWorld().getTetrominoSpeed()))
        {
            if (!fall())
            {
                freeBlocks();
                getMyWorld().spawnTetromino();
            } else
                if (!canFall())
                    maxSpeed = false;
            counter = 0;
        }
        
    }
    
    
    public void setLocation(int x, int y)
    {
        posX = x; posY = y;
        if (blocks!=null)
            updateBlocks();
        super.setLocation(posX-center, posY-center);
    }
    
    private void initBlocks()
    {
        int cnt = 0;
        for (int i=0; i<size; i++)
            for (int j=0; j<size; j++)
                if (!isEmpty(i, j, rotation)) cnt++;
        blocks = new Block[cnt];
        int k = 0;
        for (int i=0; i<size; i++)
            for (int j=0; j<size; j++)
                if (!isEmpty(i, j, rotation))
                {
                   blocks[k] = new Block();
                   getWorld().addObject(blocks[k], posX+i-center, posY+j-center);
                   k++;
                }
    }
    
    private void updateBlocks()
    {
        int k = 0;
        for (int i=0; i<size; i++)
            for (int j=0; j<size; j++)
                if (!isEmpty(i, j, rotation))
                {
                   blocks[k].setLocation(posX+i-center, posY+j-center);
                   k++;
                }
    }

    public void freeBlocks()
    {
        for (Block p: blocks)
            getMyWorld().addExistentBlock(p);
    }
        
    private TetrisWorld getMyWorld()
    {
        return (TetrisWorld)getWorld();
    }
    
    private boolean isEmpty(int x, int y, int rotation)
    {
        for (int i=0; i<rotation; i++)
        {
            int x1 = y, y1 = size-x-1;
            x = x1; y = y1;
        }
        if (x<0||y<0||x>=size||y>=size)
            return true;
        return matrix[y][x]==0;
    }
    
    public boolean check(int x, int y, int rotation)
    {
        x-=center;
        y-=center;
        for (int i=0; i<size; i++)
            for (int j=0; j<size; j++)
                if (!isEmpty(i, j, rotation)&&!getMyWorld().isEmpty(x+i, y+j))
                    return false;
        return true;
    }  
    
    public boolean canRotate()
    {
        return check(posX, posY, (rotation+1)%maxRot);
    }
    
    public boolean rotate()
    {
       if (canRotate())
       {    
        rotation =  (rotation+1)%maxRot;
        updateBlocks();
        return true;
       } 
       return false;
    }
    
    public boolean goRight()
    {
        if (check(posX+1, posY, rotation))
        {    
            setLocation(posX+1, posY);
            return true;
        } 
        return false;  
    }
    
    public boolean goLeft()
    {
        if (check(posX-1, posY, rotation))
        {    
            setLocation(posX-1, posY);
            return true;
        } 
        return false;  
    }
    
    public boolean canFall()
    {
        return check(posX, posY+1, rotation);
    }
    
    public boolean fall()
    {
        if (canFall())
        {    
            setLocation(posX, posY+1);
            return true;
        } 
        return false;  
    }  
    
    static class InputController
    {
        String key;
        int countdown;
        int cooldown;
        public InputController(String key)
        {
            this(key, 5);
        }
        public InputController(String key, int cooldown)
        {
            this.key = key;
            this.cooldown = cooldown;
        }
        public boolean pressed()
        {
            if (countdown==0)
            {
                if (Greenfoot.isKeyDown(key))
                    return true;
            } else if (!Greenfoot.isKeyDown(key))
                countdown = 0;
            return false;
        }
        
        public boolean pressed2()
        {
            update();
            return pressed();
        }
        
        public void freeze()
        {
            countdown = cooldown;
        }
        
        public void update()
        {
            if (countdown>0)
                countdown--;
        }
    }    
}
I still cant change the level when score hit 1000 and score will return to 0 and how to change level without created new world? I would really appreciate someone who help me solve this problem sorry for my bad english.
danpost danpost

2017/12/6

#
It appears that there is code that increases the speed as the score increases (see line 156 of the TetrisWorld class). Apparently, the value of 'speed' is dependent and in proportion to the score at a 1:1000 ratio. The value of 'speed' is convoluted in that it is not actually the speed, but is a term used in some expression that evaluates to the actual speed (the code could be more concise and clear).
You need to login to post a reply.