如何在c++中显示《俄罗斯方块》中的桶呢?
How would I go about displaying my bucket for Tetris in C++?
我学编程已经有一年了,我的任务是创造一款俄罗斯方块游戏。我试图显示用于游戏的桶,它必须是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]
可能更有意义(注意height
和width
的顺序)因为我们通常认为数组的"高度"是行数(你列出初始化项的方式表明这是
我认为您可以尝试使用二进制数来表示任何一行。然后你可以把它表示成一个数字数组。数组中的每个单元格都是一行。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来理解
- OpenGL只是渲染一个黑色的方块
- 如何打印空心方块?
- 使用 FreeType 和 SDL2 渲染文本会生成白色方块而不是字符
- C 中俄罗斯农民算法中的整数溢出
- 如何用更少的代码在俄罗斯方块中绘制块
- D3D11:渲染(深度)到纹理会导致红色方块,正常渲染有效
- SFML雪碧白色方块
- 为什么 SFML 加载的是白色方块而不是纹理
- 没有数组的俄罗斯方块旋转
- SFML中的白色方块,角色和纹理存储在不同的对象中
- 俄罗斯农民乘法算法的时间复杂度.
- 俄罗斯方块移动2D阵列
- 俄罗斯方块对象模型在C++
- 为什么渲染引擎将图像细分为小方块
- C++扫雷检查相邻方块并添加提示
- 选择一个随机方块并生成一个随机数
- C++在俄罗斯方块这样的游戏中设置帧速率
- 使用valgrind读取大小为1的俄罗斯方块无效
- 在运行时旋转俄罗斯方块
- 如何在c++中显示《俄罗斯方块》中的桶呢?