为什么创建一个新对象 - Java Tetris 教程

发布于 2024-10-22 04:09:49 字数 715 浏览 1 评论 0原文

我刚刚接触 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

固执像三岁 2024-10-29 04:09:49

本教程中的命名似乎有点误导。名为 Shape 的类表示单个掉落的项目。 Tetrominoes enum 似乎描述了它是哪一种项目(即它的“形状”!)。

因此,您发布的代码创建了一个新的项目并指定了其形状。

rotateRight()rotateLeft() 方法不会修改形状本身,以允许 tryMove() 方法检查移动是否正确合法,如果不合法则忽略它(例如,如果您将一个项目旋转到墙上)。当不允许移动时,tryMove() 只是保留旧值(包括旧的 Shape 实例)。如果 rotateLeft()/rotateRight() 修改了 Shape 则必须撤消该操作,这会使代码变得复杂。

此外,此代码还有一些挑剔之处:

  • 我将 Tetrominoes 类称为 Tetromino,因为 enum 类型通常被命名为以单数形式(因为您经常引用单个元素:Tetromino.SquareShape

  • 我将有关每个 Tetromino 的具体坐标的信息添加到该 enum 中,有效地将 setShape() 方法中的大部分逻辑放入其中。

  • Board 类混合了逻辑和表示,它应该分开(使其更容易测试)。

    例如,Board 类可以实现所有逻辑,而无需任何图形(即不引用 java.awtjavax.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. The Tetrominoes 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() and rotateLeft() methods don't modify the shape itself to allow the tryMove() 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 old Shape instance) when the move is not allowed. If rotateLeft()/rotateRight() modified the Shape 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 class Tetromino, as enum 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 that enum, effectively putting much of the logic from the setShape() 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 from java.awt or javax.swing). Then you'd write a BoardPanel that draws the state of the Board and interacts with the user, calling the appropriate Board methods.

爱你不解释 2024-10-29 04:09:49

您发布的方法返回一个向左旋转的形状。如果您没有创建新的形状,则原始形状(该类的一个字段并在其他地方使用)将被旋转。

对于正方形,向左旋转时不会改变,您仍然可以返回原始形状。

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.

奢华的一滴泪 2024-10-29 04:09:49

如果不仔细阅读教程,我会这样说:由于 Shape 似乎代表一个单独的项目,并且 rotateLeft() 可能是 Shape 的实例方法code> 也可以就地旋转项目,即不返回旋转的副本,而是更改当前形状的块坐标。创建一个要掉落的新项目意味着创建一个具有默认方向的新Shape

Without looking thoroughly at the tutorial I'd say this: Since Shape seems to represent an individual item and rotateLeft() might be a instance method of Shape 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 new Shape with default orientation.

一直在等你来 2024-10-29 04:09:49

看来作者没有改变 rotateLeft() 中的 Shape,因为它可能不会被接受为有效的移动。在 Board 中,内部 TAdapter 类调用 tryMove() 并且仅设置当前 Shape(变量 curShape)如果可以接受的话。如果它在检查之前发生了变异,则必须在无效时将其设置回来。该方法或许应该命名为rotatedLeftCopy(),以表明它不会更改状态。或者应该事先执行检查,然后 rotateLeft() 就可以安全地更改 Shape 了。

It appears that the author doesn't mutate the Shape in rotateLeft() because it might not be accepted as a valid move. In Board the inner TAdapter class calls tryMove() and only sets the current Shape (variable curShape) 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 named rotatedLeftCopy() to indicate that it's not changing the state. Or the check should be performed beforehand and then rotateLeft() would be safe to change the Shape in place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文