如何在C++中显示俄罗斯方块游戏中的桶?

3
我已经学习编程一年了,现在被分配制作一个俄罗斯方块游戏。我试图显示游戏所用的桶,它必须是25 x 12。我尝试使用嵌套循环的方式思考和研究如何实现这一点,但一直没有成功。除此之外,我发现我的代码有错误C2078: too many initializers。请问有人可以看看我的代码并找到我没有发现的问题,或者找到更有效绘制桶的方法吗?感谢您提供的任何帮助。谢谢。
#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
4个回答

1
我认为您可以尝试使用二进制数字来表示任何行,并将其显示为数字数组。数组中的每个单元格都将是一行。 使用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.
}

我不是说这是更好的解决方案,但这是另一种思考方式。

1
""x"" "不是char类型,它们是字符串(char[1]类型), 因此这些字符串的前12个被认为是您想要初始化bucket[0]bucket[11]的内容。
如果使用'x'' ',它们将是char类型。
但也要检查数组维度。 char bucket [12][25]是一个由12行每行25个字符组成的数组。 您初始化列表中的前25个字符将成为bucket的第一行的内容。
可能更有意义的是声明char bucket [height][width] (注意heightwidth的顺序) 因为我们通常认为数组的“高度”是行数 (您列出的初始化程序的方式表明这是您想要的)。"

0

初始化矩阵的正确方式是:

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

或者

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

你的第二个例子需要另一个插槽来存放终止的空字符。第一个例子与容量完全匹配。 - Thomas Matthews
@ThomasMatthews 你说得对。需要更多的空间来存储 null 字符。感谢您指出这一点。 - Vinícius Gobbo A. de Oliveira

0
你会想在其中一个函数中使用for循环来显示你的桶(bucket)。它会看起来像这样。
    int main(){
        for (x = 0; x < height; x++){

        //loops for height

        for (y = 0; y < a; y++){
            //loops for width
            }
        }
    }

这只是一个结构示例。您应该像这样初始化您的桶:

    char bucket[height][width]

请参考David K.的评论以了解此内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接