俄罗斯方块:类的布局
我已经编写了一个可用的俄罗斯方块克隆,但它的布局相当混乱。我能否获得有关如何重组我的课程以使我的编码更好的反馈。我专注于使我的代码尽可能通用,试图使其成为仅使用块的游戏引擎。
每个块在游戏中都是单独创建的。 我的游戏有 2 个 BlockList(链接列表):StaticBlocks 和 Tetroid。 StaticBlocks 显然是所有不移动块的列表,tetroid 是当前 tetroid 的 4 个块。
在 main 中创建了一个 World。
首先由 (NewTetroid) 创建一个新的 tetroid(Tetroid 列表中的 4 个块)
通过以下方式检测到碰撞(***Collide) 函数,通过使用 (If*****) 函数将每个 Tetroid 与所有 StaticBlock 进行比较。
当 tetroid 停止(到达底部/块)时,它被复制(CopyTetroid)到 StaticBlocks 并将 Tetroid 清空,然后通过使用(SearchY)搜索 StaticBlocks 来测试完整的行,块被销毁/丢弃等)。
创建了一个新的 tetroid。
(TranslateTetroid) 和 (RotateTetroid) 对 Tetroid 列表中的每个块一一执行操作(我认为这是不好的做法)。
(DrawBlockList) 只是遍历一个列表,为每个块运行 Draw() 函数。
当调用 (NewTetroid) 时,通过设置相对于 Tetroid 中第一个块的旋转轴来控制旋转。我的每个块的旋转函数(旋转)将其绕轴旋转,使用输入 +-1 进行左/右旋转。 RotationModes 和 States 用于以 2 或 4 种不同方式旋转的块,定义它们当前所处的状态,以及它们是否应该向左或向右旋转。 我对“世界”中的定义方式不满意,但我不知道将它们放在哪里,同时仍然保持每个块的(旋转)函数通用。
我的课程如下,
class World
{
public:
/* Constructor/Destructor */
World();
~World();
/* Blocks Operations */
void AppendBlock(int, int, BlockList&);
void RemoveBlock(Block*, BlockList&);;
/* Tetroid Operations */
void NewTetroid(int, int, int, BlockList&);
void TranslateTetroid(int, int, BlockList&);
void RotateTetroid(int, BlockList&);
void CopyTetroid(BlockList&, BlockList&);
/* Draw */
void DrawBlockList(BlockList&);
void DrawWalls();
/* Collisions */
bool TranslateCollide(int, int, BlockList&, BlockList&);
bool RotateCollide(int, BlockList&, BlockList&);
bool OverlapCollide(BlockList&, BlockList&); // For end of game
/* Game Mechanics */
bool CompleteLine(BlockList&); // Test all line
bool CompleteLine(int, BlockList&); // Test specific line
void ColourLine(int, BlockList&);
void DestroyLine(int, BlockList&);
void DropLine(int, BlockList&); // Drops all blocks above line
int rotationAxisX;
int rotationAxisY;
int rotationState; // Which rotation it is currently in
int rotationModes; // How many diff rotations possible
private:
int wallX1;
int wallX2;
int wallY1;
int wallY2;
};
class BlockList
{
public:
BlockList();
~BlockList();
Block* GetFirst();
Block* GetLast();
/* List Operations */
void Append(int, int);
int Remove(Block*);
int SearchY(int);
private:
Block *first;
Block *last;
};
class Block
{
public:
Block(int, int);
~Block();
int GetX();
int GetY();
void SetColour(int, int, int);
void Translate(int, int);
void Rotate(int, int, int);
/* Return values simulating the operation (for collision purposes) */
int IfTranslateX(int);
int IfTranslateY(int);
int IfRotateX(int, int, int);
int IfRotateY(int, int, int);
void Draw();
Block *next;
private:
int pX; // position x
int pY; // position y
int colourR;
int colourG;
int colourB;
};
抱歉,如果这有点不清楚或冗长,我只是在寻求一些帮助重组。
I've written a working tetris clone but it has a pretty messy layout. Could I please get feedback on how to restructure my classes to make my coding better. I focuses on making my code as generic as possible, trying to make it more an engine for games only using blocks.
Each block is created seperately in the game.
My game has 2 BlockLists (linked lists): StaticBlocks and Tetroid.
StaticBlocks is obviously the list of all non-moving blocks, and tetroid are the 4 blocks of the current tetroid.
In main a World is created.
First a new tetroid (4 blocks in a list Tetroid) is created by (NewTetroid)
Collision is detected by the (***Collide) functions, by comparing each of Tetroid with all of the StaticBlocks using the (If*****) functions.
When the tetroid stops (hits the bottom/blocks), it is copied (CopyTetroid) to the StaticBlocks and Tetroid is made empty, then tests are made for complete lines, blocks are destroyed/dropped etc by searching StaticBlocks with (SearchY).
A new tetroid is created.
(TranslateTetroid) and (RotateTetroid) perform operations on each block in the Tetroid list one by one ( I think this is bad practise).
(DrawBlockList) just goes through a list, running the Draw() function for each block.
Rotation is controlled by setting rotation axis relative to the first block in Tetroid when (NewTetroid) is called. My rotation function (Rotate) for each block rotates it around the axis, using an input +-1 for left/right rotation. RotationModes and States are for blocks that rotate in 2 or 4 different ways, defining what state they are currently in, and whether they should be rotated left or right. I am not happy with how these are defined in "World", but I don't know where to put them, whilst still keeping my (Rotate) function generic for each block.
My classes are as follows
class World
{
public:
/* Constructor/Destructor */
World();
~World();
/* Blocks Operations */
void AppendBlock(int, int, BlockList&);
void RemoveBlock(Block*, BlockList&);;
/* Tetroid Operations */
void NewTetroid(int, int, int, BlockList&);
void TranslateTetroid(int, int, BlockList&);
void RotateTetroid(int, BlockList&);
void CopyTetroid(BlockList&, BlockList&);
/* Draw */
void DrawBlockList(BlockList&);
void DrawWalls();
/* Collisions */
bool TranslateCollide(int, int, BlockList&, BlockList&);
bool RotateCollide(int, BlockList&, BlockList&);
bool OverlapCollide(BlockList&, BlockList&); // For end of game
/* Game Mechanics */
bool CompleteLine(BlockList&); // Test all line
bool CompleteLine(int, BlockList&); // Test specific line
void ColourLine(int, BlockList&);
void DestroyLine(int, BlockList&);
void DropLine(int, BlockList&); // Drops all blocks above line
int rotationAxisX;
int rotationAxisY;
int rotationState; // Which rotation it is currently in
int rotationModes; // How many diff rotations possible
private:
int wallX1;
int wallX2;
int wallY1;
int wallY2;
};
class BlockList
{
public:
BlockList();
~BlockList();
Block* GetFirst();
Block* GetLast();
/* List Operations */
void Append(int, int);
int Remove(Block*);
int SearchY(int);
private:
Block *first;
Block *last;
};
class Block
{
public:
Block(int, int);
~Block();
int GetX();
int GetY();
void SetColour(int, int, int);
void Translate(int, int);
void Rotate(int, int, int);
/* Return values simulating the operation (for collision purposes) */
int IfTranslateX(int);
int IfTranslateY(int);
int IfRotateX(int, int, int);
int IfRotateY(int, int, int);
void Draw();
Block *next;
private:
int pX; // position x
int pY; // position y
int colourR;
int colourG;
int colourB;
};
Sorry if this is a bit unclear or long winded, I'm just looking for some help restructuring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它只是一个包含几乎所有功能的 blob。这不是好的设计。一项明显的职责是“代表放置块的网格”。但这与创建 tetroids 或操作块列表或绘图无关。事实上,其中大部分可能根本不需要在课堂上进行。我希望
World
对象包含您称为 StaticBlocks 的BlockList
,以便它可以定义您正在玩的网格。阻止列表
?您说您希望您的代码是通用的,那么为什么不允许使用任何容器呢?如果我愿意,为什么不能使用std::vector
?或者一个std::set
,或者一些自制的容器?TranslateTetroid
不翻译 tetroid。它会翻译阻止列表中的所有块。所以它应该是 TranslateBlocks 或其他东西。但即便如此也是多余的。我们可以从签名(它需要一个BlockList&
)中看到它适用于区块。因此,只需将其命名为Translate
即可。/*...*/
)。 C++ 风格 (//..
) 的表现要好一些,因为如果您在整个代码块中使用 C 风格注释,那么如果该代码块也包含 C 风格注释,则会中断。 (举个简单的例子,/*/**/*/
不起作用,因为编译器会将第一个*/
视为注释的结尾,并且所以最后一个*/
不会被视为注释。int
参数是怎么回事 复制对象的方法是使用它的复制构造函数,而不是使用CopyTetroid
函数,而是给BlockList
一个复制构造函数,然后如果我需要复制一个,我可以。只需执行BlockList b1 = b0
即可,void SetX(Y)
和Y GetX()
方法,而是删除多余的 Get/Set 前缀和只需有void X(Y)
和YX()
我们知道它是一个 getter,因为它不带参数并返回一个值,而且我们知道另一个是 setter。因为它需要一个参数并返回 void。BlockList
不是一个很好的抽象。您对“当前 tetroid”和“当前网格上的静态块列表”有非常不同的需求。静态块可以用简单的块序列来表示(尽管行序列或二维数组可能更方便),但当前活动的 tetroid 需要附加信息,例如旋转中心(其中不属于世界
)。(5,8)
中存在哪个块,它应该能够返回该块。但块本身不如果确实如此,那么如果由于一些微妙的错误,两个块最终具有相同的坐标,那么如果块存储自己的坐标,则可能会发生这种情况,但如果网格则不会。保存哪个块在哪里的列表。)因此,这是代码的第一步,只需重命名、注释和删除代码,而不需要过多更改结构。
现在,让我们添加一些缺失的部分:
首先,我们需要表示“动态”块、拥有它们的 tetroid 以及网格中的静态块或单元格。
(我们还将向世界/网格类添加一个简单的“碰撞”方法)
并且我们需要重新实现推测碰撞检查。考虑到非变异的平移/旋转方法,这很简单:我们只需创建旋转/平移的副本,并测试它们的碰撞:
World
class?It's just a blob containing practically every kind of functionality. That's not good design. One obvious responsibility is "represent the grid onto which blocks are placed". But that has nothing to do with creating tetroids or manipulating block lists or drawing. In fact, most of that probably doesn't need to be in a class at all. I would expect the
World
object to contain theBlockList
you call StaticBlocks so it can define the grid on which you're playing.Blocklist
? You said you wanted your code to be generic, so why not allow any container to be used? Why can't I use astd::vector<Block>
if I want to? Or astd::set<Block>
, or some home-brewed container?TranslateTetroid
doesn't translate a tetroid. It translates all the blocks in a blocklist. So it should beTranslateBlocks
or something. But even that is redundant. We can see from the signature (it takes aBlockList&
) that it works on blocks. So just call itTranslate
./*...*/
). C++-style (//..
)behaves a bit nicer in that if you use a C-style comment out an entire block of code, it'll break if that block also contained C-style comments. (As a simple example,/*/**/*/
won't work, as the compiler will see the first*/
as the end of the comment, and so the last*/
won't be considered a comment.int
parameters? It's making your code impossible to read.CopyTetroid
function, giveBlockList
a copy constructor. Then if I need to copy one, I can simply doBlockList b1 = b0
.void SetX(Y)
andY GetX()
methods, drop the redundant Get/Set prefix and simply havevoid X(Y)
andY X()
. We know it's a getter because it takes no parameters and returns a value. And we know the other one is a setter because it takes a parameter and returns void.BlockList
isn't a very good abstraction. You have very different needs for "the current tetroid" and "the list of static blocks currently on the grid". The static blocks can be represented by a simple sequence of blocks as you have (although a sequence of rows, or a 2D array, may be more convenient), but the currently active tetroid needs additional information, such as the center of rotation (which doesn't belong in theWorld
).(5,8)
, it should be able to return the block. but the block itself doesn't need to store the coordinate. If it does, it can become a maintenance headache. What if, due to some subtle bug, two blocks end up with the same coordinate? That can happen if blocks store their own coordinate, but not if the grid holds a list of which block is where.)So here's a first pass on your code, simply renaming, commenting and removing code without changing the structure too much.
Now, let's add some of the missing pieces:
First, we'll need to represent the "dynamic" blocks, the tetroid owning them, and the static blocks or cells in a grid.
(We'll also add a simple "Collides" method to the world/grid class)
and we'll need to re-implement the speculative collision checks. Given the non-mutating Translate/Rotate methods, that is simple: We just create rotated/translated copies, and test those for collision:
我个人会放弃静态块并将它们作为行处理。拥有静态块,您将保留比您需要的更多的信息。
世界是由行组成的,行是单个正方形的数组。这些方块可以是空的,也可以是颜色的(如果你有特殊的方块,也可以扩展它)。
世界也拥有一个活跃的区块,就像你现在一样。该类应该有一个旋转和平移方法。该块显然需要维护对世界的引用,以确定它是否会与现有的砖块或板的边缘发生碰撞。
当活动块停止运行时,它将调用类似 world.update() 的方法,它将活动块的各个部分添加到适当的行,清除所有完整行,确定是否丢失等,最后创建一个如果需要的话,新的活动块。
I would personally ditch the static blocks and deal with them as rows. Having a static block you are keeping a lot more information than you need.
A world is made of rows, which is an array of single squares. The squares can be either empty, or a color (or extend it if you have special blocks).
The world also owns a single active block, as you have now. The class should have a rotate and translate method. The block will obviously need to maintain a reference to the world to determine if it will collide with existing bricks or the edge of the board.
When the active block goes out of play, it will call something like world.update() which will add the pieces of the active block to the appropriate rows, clear all full rows, decide if you have lost, etc, and finally create a new active block if needed.