为什么创建一个新对象 - Java Tetris 教程
我刚刚接触 Java,我发现了这个关于创建 Java 俄罗斯方块游戏的好教程。
我没有导师或导师来帮助我解决这个问题 - 我一直在寻找一位导师或导师:( 所以目前我正在自学 Java 和 PHP :)
无论如何,这是我找到的网站: http://zetcode.com /tutorials/javagamestutorial/tetris/
我在 Shape.java 类中没有得到的程序的一种方法:
public Shape rotateLeft()
{
if (pieceShape == Tetrominoes.SquareShape)
return this;
Shape result = new Shape();
result.pieceShape = pieceShape;
for (int i = 0; i < 4; ++i) {
result.setX(i, y(i));
result.setY(i, -x(i));
}
return result;
}
为什么我们需要创建一个新的对象 Shape result = new Shape ();
是否已经可以从 pieceShape
变量中获取当前块?
Im just new to Java and I found this good tutorial for creating a Java Tetris Game.
I dont have a mentor or a tutor to help me with this - Ive been looking for one for ages :(
so currently im self learning Java and PHP :)
Anyways heres the website I found: http://zetcode.com/tutorials/javagamestutorial/tetris/
One method of the program I dont get in the Shape.java
class:
public Shape rotateLeft()
{
if (pieceShape == Tetrominoes.SquareShape)
return this;
Shape result = new Shape();
result.pieceShape = pieceShape;
for (int i = 0; i < 4; ++i) {
result.setX(i, y(i));
result.setY(i, -x(i));
}
return result;
}
Why do we need to create a new Object Shape result = new Shape();
if can already get the current piece from the pieceShape
variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
本教程中的命名似乎有点误导。名为
Shape
的类表示单个掉落的项目。Tetrominoes
enum
似乎描述了它是哪一种项目(即它的“形状”!)。因此,您发布的代码创建了一个新的项目并指定了其形状。
rotateRight()
和rotateLeft()
方法不会修改形状本身,以允许tryMove()
方法检查移动是否正确合法,如果不合法则忽略它(例如,如果您将一个项目旋转到墙上)。当不允许移动时,tryMove()
只是保留旧值(包括旧的Shape
实例)。如果rotateLeft()
/rotateRight()
修改了Shape
则必须撤消该操作,这会使代码变得复杂。此外,此代码还有一些挑剔之处:
我将
Tetrominoes
类称为Tetromino
,因为enum
类型通常被命名为以单数形式(因为您经常引用单个元素:Tetromino.SquareShape
。我将有关每个
Tetromino
的具体坐标的信息添加到该enum
中,有效地将setShape()
方法中的大部分逻辑放入其中。Board 类混合了逻辑和表示,它应该分开(使其更容易测试)。
例如,
Board
类可以实现所有逻辑,而无需任何图形(即不引用java.awt
或javax.swing 中的任何内容) )。然后,您将编写一个
BoardPanel
来绘制Board
的状态并与用户交互,调用适当的Board
方法。It seems the naming is a bit misleading in this tutorial. The class called
Shape
represents a single item that falls down. TheTetrominoes
enum
seems to be describing which kind of item it is (i.e. it's "shape"!).So the code you posted creates a new item and specifies its shape.
The
rotateRight()
androtateLeft()
methods don't modify the shape itself to allow thetryMove()
method to check if the move is legal and ignore it if it isn't (for example if you'd rotate an item into the wall).tryMove()
simply keeps the old values (including the oldShape
instance) when the move is not allowed. IfrotateLeft()
/rotateRight()
modified theShape
then it would have to undo that operation, which would complicate the code.Also, there are a few nitpicks with this code:
I'd call the
Tetrominoes
classTetromino
, asenum
types are usually named in the singular (since you often reference a single element:Tetromino.SquareShape
.I'd add the information about the concrete coordinate of each
Tetromino
into thatenum
, effectively putting much of the logic from thesetShape()
method into it.The
Board
class mixes the logic and the presentation, it should be separated (makes it much easier to test).For example the
Board
class could implement all the logic without any of the graphics (i.e. don't reference anything fromjava.awt
orjavax.swing
). Then you'd write aBoardPanel
that draws the state of theBoard
and interacts with the user, calling the appropriateBoard
methods.您发布的方法返回一个向左旋转的形状。如果您没有创建新的形状,则原始形状(该类的一个字段并在其他地方使用)将被旋转。
对于正方形,向左旋转时不会改变,您仍然可以返回原始形状。
The method you've posted returns a shape that is rotated left. If you didn't create a new Shape, the original shape, which is a field of the class and used everywhere else, would have been rotated.
In the case of the square shape, which isn't changed when rotated left, you can still return the original one.
如果不仔细阅读教程,我会这样说:由于
Shape
似乎代表一个单独的项目,并且rotateLeft()
可能是Shape
的实例方法code> 也可以就地旋转项目,即不返回旋转的副本,而是更改当前形状的块坐标。创建一个要掉落的新项目意味着创建一个具有默认方向的新Shape
。Without looking thoroughly at the tutorial I'd say this: Since
Shape
seems to represent an individual item androtateLeft()
might be a instance method ofShape
it might also be ok to rotate the item in place, i.e. not to return a rotated copy but change the block' coordinates of the current shape. Creating a new item to fall down would then mean to create a newShape
with default orientation.看来作者没有改变
rotateLeft()
中的Shape
,因为它可能不会被接受为有效的移动。在Board
中,内部TAdapter
类调用tryMove()
并且仅设置当前Shape
(变量curShape
)如果可以接受的话。如果它在检查之前发生了变异,则必须在无效时将其设置回来。该方法或许应该命名为rotatedLeftCopy()
,以表明它不会更改状态。或者应该事先执行检查,然后rotateLeft()
就可以安全地更改Shape
了。It appears that the author doesn't mutate the
Shape
inrotateLeft()
because it might not be accepted as a valid move. InBoard
the innerTAdapter
class callstryMove()
and only sets the currentShape
(variablecurShape
) if it's acceptable. If it were mutated prior to that check it'd have to set it back when invalid. The method should perhaps be namedrotatedLeftCopy()
to indicate that it's not changing the state. Or the check should be performed beforehand and thenrotateLeft()
would be safe to change theShape
in place.