在数组上存储 Piece 对象 - java Tetris Tutorial

发布于 2024-10-21 18:21:35 字数 908 浏览 0 评论 0原文

我刚刚接触 Java,我发现了这个关于创建 Java 俄罗斯方块游戏的好教程。

我没有导师或导师来帮助我解决这个问题 - 我一直在寻找一位导师或导师:( 所以目前我正在自学java和PHP:)

无论如何,这是我找到的网站 http://zetcode.com/ Tutorials/javagamestutorial/tetris/

有人可以从教程中解释一下这个方法是如何工作的吗?

Tetrominoes shapeAt(int x, int y) { return board[(y * BoardWidth) + x]; }

我所知,它是通过 Paint() 方法调用的

for (int i = 0; i < BoardHeight; ++i) {
 for (int j = 0; j < BoardWidth; ++j) {
     Tetrominoes shape = shapeAt(j, BoardHeight - i - 1);
     if (shape != Tetrominoes.NoShape)
         drawSquare(g, 0 + j * squareWidth(),
                    boardTop + i * squareHeight(), shape);
 }
}

据 - 它在棋盘的每个方格处循环,并确定 board[] 数组中是否存储有形状(Enum)。

我只需要有人向我解释这部分如何绘制所有形状或形状的剩余部分,这些形状已落到板的底部?

以及如何将所有方块记住在 board[] 数组中?

谢谢

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/

can someone explain how this method works from the tutorial?

Tetrominoes shapeAt(int x, int y) { return board[(y * BoardWidth) + x]; }

I know it gets called form the Paint() Method

for (int i = 0; i < BoardHeight; ++i) {
 for (int j = 0; j < BoardWidth; ++j) {
     Tetrominoes shape = shapeAt(j, BoardHeight - i - 1);
     if (shape != Tetrominoes.NoShape)
         drawSquare(g, 0 + j * squareWidth(),
                    boardTop + i * squareHeight(), shape);
 }
}

From what i understand
- it loops at each square of the board and determines if there is a shape (Enum) stored on the board[] array.

I just need someone to explain to me how this portion paints all shapes, or remains of the shapes, that have been dropped to the bottom of the board?

And how All the squares are rememberd in the board[] array?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧伤慢歌 2024-10-28 18:21:35

简而言之,board 数组是一个一维数组,它记住每个正方形上的形状。虽然它是一维的,但它的排列方式是第一行在前,然后是第二行,依此类推。因此,在像这样的 3x5 板上:

A B C
D E F
G H I
J K L
M N O

该数组将包含 board[0] A 处的形状,board[3] 将包含 D 等。

需要注意的是,它实际上并不会记住每个正方形的整个形状 - 只是记住形状的种类在那里。因此,当形状下降到底部时,每个单独的方块都会保留在板阵列中,即使它所代表的部分形状被删除的线擦除了。删除一行实际上只需要“向下”移动数组的第一部分以覆盖要删除的行,并清除顶行的元素(到Tetrominoes.NoShape)。

Put simply, the board array is a single-dimensional array which remembers what sort of shape is on each square. Although it's single-dimensional, it's arranged so that the first row comes first, then the second row, etc. So on a 3x5 board like this:

A B C
D E F
G H I
J K L
M N O

The array would be such that board[0] would contain the shape at A, board[3] would contain D, etc.

It's important to note that it's not actually remembering a whole shape for each square - just what kind of shape was there. So as shapes drop to the bottom, each individual square stays in the board array even if part of the shape it represented is wiped out by a line being removed. Removing a line really just involves shifting the first part of the array "down" to overwrite the row being removed, and clearing out the top row's-worth of elements (to Tetrominoes.NoShape).

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