Java Tetris - 为每件作品设置新颜色

时间:2013-04-30 16:14:38

标签: java swing

我正在尝试为每个新作品(由4个图块组成)设置新的随机颜色。为了将完整的部分绘制到电路板上,我在Board类中有一个paint组件:

public void paintComponent(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(0, 0, getWidth(), getHeight());
    for(int row = 0; row < grid.length; row++) {
        for(int col = 0; col < grid[row].length; col++) {
            if(grid[row][col] != null) {
                //if there is a non-null space, that is a Tetris piece.. fill it red
                    g.setColor(color);
                    g.fillRect(row * tilesize, col * tilesize, tilesize, tilesize);
                    g.setColor(Color.WHITE);
                    g.drawString("(" + row + ", " + col + ")", row * tilesize, col * tilesize+10);
                }           
            }
        }
    }

您可以看到g.setColor()被赋予全局变量color

在Board构造函数中定义:

color = setColor();

的setColor():

public Color setColor() {

    Random rand = new Random();

    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();
    Color randomColor = new Color(r, g, b);

    return randomColor;
}

当发生碰撞时,会生成一个新作品,用新的随机颜色覆盖color全局变量......

public void collisionCheck() {
    if (newPiece.isCollision()){
        newPiece = new Piece(this, randomPiece());
        color = setColor();
    }       
}

这给了我这个结果:

所有形状都设置为相同的颜色......不是我想要的 enter image description here

然后,如果生成了一个新片段,那么所有片段的颜色都会再次变化,而不是我想要的。 enter image description here

我知道 问题是什么......我不应该覆盖全局颜色变量...但是如果我没有从棋盘类中分配颜色......而是从tile类中获取颜色,如下:

g.setColor(grid[row][col].getColor());

public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());
        for(int row = 0; row < grid.length; row++) {
            for(int col = 0; col < grid[row].length; col++) {
                if(grid[row][col] != null) {
                    //if there is a non-null space, that is a Tetris piece.. fill it red
                        g.setColor(grid[row][col].getColor());
                        g.fillRect(row * tilesize, col * tilesize, tilesize, tilesize);
                        g.setColor(Color.WHITE);
                        g.drawString("(" + row + ", " + col + ")", row * tilesize, col * tilesize+10);
                    }           
                }
            }
        }

每次重新绘制图块时,每个单独的图块都会生成一种新颜色...

enter image description here

我的目标是给一件单件(由4个瓷砖组成)一个随机颜色...然后当一件新件产生时,第一件保留其颜色......并且新件保留其颜色......

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:4)

我刚想出怎么做...在Board类中,当我创建newPiece时,将randomColor()作为参数传递:

public Color randomColor() {

    Random rand = new Random();

    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();
    Color randomColor = new Color(r, g, b);

    return randomColor;
}

newPiece = new Piece(this, randomPiece(), randomColor());. 

在Piece类中,遍历切片tile[i].setColor(color);,设置每个切片的颜色。

for (int i = 0; i < tile.length; i++) {
    tile[i].setColor(color);
}

在Tile中我添加了Color getColor(),返回setColor(color);

public Color getColor() {
    return setColor(color);
}

public Color setColor(Color myColor) {  
    color = myColor;
    return color;
}

然后我在Board类的绘图组件中使用它:g.setColor(grid[row][col].getColor());

但它绝对不是最优雅的解决方案。如jimmt描述的那样,学习如何做得更好,并在“游戏循环”中做俄罗斯方块会很酷。

enter image description here