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

2017/12/3

Need Help with Tetris Game

Arifwc Arifwc

2017/12/3

#
I am new with Java and Greenfoot things. But my lecturer give me final project to create a tetris game with greenfoot. I just downloaded any tetris scenario , but i always failed to create tetris game. Maybe someone have a tutorial to create tetris game using greenfoot sorry for bad english.
Super_Hippo Super_Hippo

2017/12/3

#
Basic thoughts: The tetromino is a class. It consists of 4 blocks (another class). The possible figures can be saved in the tetromino class and are chosen randomly. The tetromino also controls the moving of its blocks. When one of the blocks can't move, the whole tetromino stops moving. The world checks if any row is full and can be cleared. Then the next tetromino is added to the world and is set 'active', so only this tetromino will react to key presses.
Arifwc Arifwc

2017/12/3

#
i have this classes JTetromino and the others
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class JTetromino here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class JTetromino extends Tetromino
{
    /**
     * Act - do whatever the JTetromino wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public JTetromino(){
        int x0 = Greenfoot.getRandomNumber(14)+1;
        int y0 = 5;
        getWorld().addObject(block[0], x0 , y0 );
        getWorld().addObject(block[1], x0 , y0+1 );
        getWorld().addObject(block[2], x0 , y0+2 );
        getWorld().addObject(block[3], x0-1 , y0+2 );
    }
    public void act()
    {
        // Add your action code here.
    }   
}
Tetromino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
{
    Block block[] = new Block[4];
    public Tetromino(){
    for(int i = 0; i<4; i++){
        block[i] = new Block();
    }
    }
    public void act(){
     
    }
}   
and also TetrisWorld
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TetrisWorld extends World
{
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    
    public TetrisWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(16, 24, 20);
        prepare();
        spawnTetromino();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
    }
     
    Tetromino currentTetromino;
    public void spawnTetromino() {
        int random = Greenfoot.getRandomNumber(7);
        switch (random) {
            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;
        }
    }
}
But the blocks didnt showed up :(( please help
Super_Hippo Super_Hippo

2017/12/3

#
I personally wouldn't have an own subclass for all tetrominos because they are all doing exactly the same. The constructor of your tetromino subclasses should give you an error because the object isn't in the world, so you can't add objects to the world which 'getWorld' returns (it returns 'null'.). And the tetromino superclass's construtor isn't automatically executed when you create a tetromino subclass. But you could do the following, so you don't need that constructor:
1
private Block[] block = {new Block(), new Block(), new Block(), new Block()};
(Change 'private' to 'protected' if you want to use the subclasses like you do right now)
Arifwc Arifwc

2017/12/6

#
Thankyou super_hippo. really appreciated it
You need to login to post a reply.