我正在尝试在Java中制作游戏俄罗斯方块作为有趣的一面项目。java俄罗斯方块:如何使俄罗斯方块片移动为4个不同的瓷砖
我的游戏板是块网格:
grid = new Tile[height][width];
和电网内,我创建了一个新的瓷砖对象: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()
将结束游戏,如果块卡在顶部。
- 你可以从的keyPressed方法看,
我用下面的挣扎:
要创建一个实际的俄罗斯方块一块,我想我应该只产生3其他Tile的实例,并基于它是什么(L,O,Bar,Z等),将它们的位置设置为合适的pl根据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
片的新instane?我的想法是否正确?
可执行
https://www.dropbox.com/s/oyh26dfbmsvt5c8/my_tetris_test.jar
链接到源代码zip
https://www.dropbox.com/s/9kt3sl6qqo54amk/Tetris%20Two.rar
谢谢!
为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-03-07 06:06:14
@AndrewThompson我已经上传了一个可运行的jar到dropbox以及作为zip的源代码。你可以试试吗? – Growler 2013-03-07 06:15:54
@AndrewThompson我已经读过它,我认为我的EXE实现了所有这些东西。没关系,如果你不想看看它。 – Growler 2013-03-07 06:25:43