如何在c++中显示《俄罗斯方块》中的桶呢?

How would I go about displaying my bucket for Tetris in C++?

本文关键字:方块 俄罗斯 俄罗斯方块 c++ 显示      更新时间:2023-10-16

我学编程已经有一年了,我的任务是创造一款俄罗斯方块游戏。我试图显示用于游戏的桶,它必须是25 x 12。我试着思考和研究一种使用嵌套循环来做到这一点的方法,但无济于事。一切看起来都很好,除了我得到一个错误C2078:太多的初始化器。有人可以看看代码,找到我只是没有看到的东西,或者找到一个更有效的方法来画桶?如有任何帮助,我将不胜感激,谢谢。

#include "stdafx.h"
    #include <iostream>
    #include <Windows.h>

    using namespace std;
    const int width = 12;
    const int height = 25;
    char bucket [width][height] ={"x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x"," "," "," "," "," "," "," "," "," "," ","x",
                                  "x","x","x","x","x","x","x","x","x","x","x","x",
    };
    void setCursorTo(int x, int y){
        HANDLE handle;
        COORD position;
        handle = GetStdHandle(STD_OUTPUT_HANDLE);
        position.X = x;
        position.Y = y;
        SetConsoleCursorPosition(handle, position);
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
            setCursorTo(0,0);
            cout<<bucket<<endl;

        return 0;
    }enter code here

"x"" "不属于char类型。它们是字符串(char[1]类型),假设前12个字符串是你想要的初始化bucket[0]bucket[11]

如果你使用'x'' ',它们的类型将是char

还要检查数组的尺寸。char bucket [12][25]是一个12行的数组,每行25个字符。初始化列表中的前25个字符将变成bucket第一行的内容

声明char bucket [height][width]可能更有意义(注意heightwidth的顺序)因为我们通常认为数组的"高度"是行数(你列出初始化项的方式表明这是

我认为您可以尝试使用二进制数来表示任何一行。然后你可以把它表示成一个数字数组。数组中的每个单元格都是一行。short数组是可以的,因为short有16位。(所有25X12只占用50字节。不像矩阵需要12*25字节。)如果第i位是x,那么第i位就是1。我认为这是一个更优雅的解决方案。

例如

"x"," "," "," "," "," "," "," "," "," "," ","x"

必须是

'x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x'
在这种情况下,

打开了2位。这一行的二进制是

100000000001
值为 的

2049

.

if ((number & 2^i) !=0){
   //The bit i is 1 so you draw x in column i.
}
我不是说这是更好的解决方案,但这是另一种思考方式。

初始化矩阵的正确方法如下:

char bucket[width][height] = { { 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
                               { 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
                               ...
                             };

char bucket[width + 1][height] = { "X          X",
                               "X          X",
                               ...
                             };

您将需要在其中一个函数中使用for循环来显示bucket。它看起来像这样。

    int main(){
        for (x = 0; x < height; x++){
        //loops for height
        for (y = 0; y < a; y++){
            //loops for width
            }
        }
    }

这只是一个结构的例子。你应该像这样初始化bucket:

    char bucket[height][width]

请参考David K. comment来理解