1

我正在尝试将 Java 中的俄罗斯方块游戏作为一个有趣的项目。

我的游戏板是一个瓷砖网格:

grid = new Tile[height][width];

在网格中,我创建了一个新的 Tile 对象:activetile = new Tile(this,0, 0); //add new tile to "this" board

目前:

  • 我能够控制单个图块 - 将其向下、向左和向右移动

     public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_DOWN) {
            checkBottomFull(0,4);
            collisionCheck(activetile.getX(),activetile.getY());
            checkEndGame(activetile.getX(), activetile.getY());
    
            activetile.setLocation(activetile.getX(), activetile.getY()+1); 
            System.out.println("coordinates: " + activetile.getX() + ", " + activetile.getY());
    
            repaint();
        } 
            ...right key and left key code omitted 
    
    • 从 keyPressed 方法中可以看出,checkBottomFull()如果已满,将清除底部行,collisionCheck()如果块击中地板或下面的另一块,将生成一个新块,checkEndGame()如果块卡在顶部,则结束游戏。

在此处输入图像描述


我正在努力解决以下问题:

  • 要创建一个实际的俄罗斯方块,我想我应该只生成 3 个其他 Tile 实例,并根据它是什么(L、O、Bar、Z 等),根据 activetile 将它们的位置设置在适当的位置(我可以控制的单个图块),如下所示:

    if (piece == "Bar") {
        block2 = new Tile(this, activetile.getX(), activetile.getY());
        block3 = new Tile(this, activetile.getX()+2, activetile.getY());
        block4 = new Tile(this, activetile.getX()+3, activetile.getY());
    }
    

这个问题是,我的碰撞检测activetile不允许它适当地移动,因为它会碰到它的其他块。我试图keyPressed()通过在设置block2, block3, block4活动图块的新位置后设置位置来解决这个问题,如下所示:(所以一旦activetile向下移动,所有其他的都可以向下移动,这样它们就不会重叠)

        activetile.setLocation(activetile.getX(), activetile.getY()+1); 
        block2.setLocation(activetile.getX(), activetile.getY()+1); 
        block3.setLocation(activetile.getX(), activetile.getY()+1); 
        block4.setLocation(activetile.getX(), activetile.getY()+1);

这可能适用于向下移动,但不适用于向左或向右移动,因为瓷砖会重叠。


那么,我是否Bar通过生成这样的新块正确地创建了一个新的片段?我的想法正确吗?


可执行的

https://www.dropbox.com/s/oyh26dfbmsvt5c8/my_tetris_test.jar

链接到源代码 zip

https://www.dropbox.com/s/9kt3sl6qqo54amk/Tetris%20Two.rar

谢谢!

4

1 回答 1

1

我会看一下 Polygon 类:http
: //docs.oracle.com/javase/6/docs/api/java/awt/Polygon.html 提供的方法可以用点来测试碰撞(内部)另一个对象。您还可以使用它translate(deltaX, deltaY)来大大简化对象的“运动”。

于 2013-03-07T06:06:23.007 回答