未显示 C++ 俄罗斯方块程序存储桶
C++ Tetris Program bucket not being displayed
我正在为我的编程制作一个 C++ 俄罗斯方块游戏 class 现在我正在写我正在展示桶并将碎片放入桶中,但我还没有移动桶中的碎片.我能够成功构建程序,但是当程序为 运行 时,什么也没有显示。
此外,我不确定如何制作将碎片放入桶中的动画。
这是我目前的情况。
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <time.h>
using namespace std;
class TetrisShape{
public:
int shapeTopLeftX;
int shapeTopLeftY;
TetrisShape(){
shapeTopLeftX = 6;
shapeTopLeftY = 0;
}
char shapeArray[4][4];
void populateShapeArray(int shapeType){
switch(shapeType){
case 1:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 2:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 3:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 4:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
shapeArray[0][1] = 'X'; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 5:
shapeArray[0][0] = 'X'; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 6:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = 'X'; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
}
}
};
const int width = 12;
const int height = 25;
char bucket [height][width] ={'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);
}
void drawBucket(){
setCursorTo(0,0);
for(int x = 0; x <= height; x++){
int a = width;
a = width;
int b = width;
for(int y = 0; y < a; y++){
cout<<bucket[x][y];
}
if(b == width){
cout<<bucket[x][b]<<endl;
}
}
}
void updateBucket(TetrisShape localTetrisShape){
int shapeTopLeftX = 6;
int shapeTopLeftY = 0;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
bucket[shapeTopLeftX+i][shapeTopLeftY+j] = localTetrisShape.shapeArray[i][j];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
TetrisShape shape;
int gameOver = 0;
while(gameOver == 0){
void drawBucket();
srand(time(NULL));
int number = rand() % 6 + 1;
shape.populateShapeArray(number);
updateBucket(shape);
int newshapeTopLeftY = shape.shapeTopLeftY + 1;
}
return 0;
}
提前致谢。
正如@ataman 在评论中(或多或少)指出的那样,您实际上并没有在 while
循环中调用 drawBucket()
函数:您正在重新声明一个名为drawBucket()
。这是完全有效的,因为它在不同的范围内;但是,这可能不是您想要的。
将循环更改为:
while(gameOver == 0){
drawBucket();
...
}
我正在为我的编程制作一个 C++ 俄罗斯方块游戏 class 现在我正在写我正在展示桶并将碎片放入桶中,但我还没有移动桶中的碎片.我能够成功构建程序,但是当程序为 运行 时,什么也没有显示。
此外,我不确定如何制作将碎片放入桶中的动画。
这是我目前的情况。
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <time.h>
using namespace std;
class TetrisShape{
public:
int shapeTopLeftX;
int shapeTopLeftY;
TetrisShape(){
shapeTopLeftX = 6;
shapeTopLeftY = 0;
}
char shapeArray[4][4];
void populateShapeArray(int shapeType){
switch(shapeType){
case 1:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 2:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 3:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 4:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
shapeArray[0][1] = 'X'; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 5:
shapeArray[0][0] = 'X'; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 6:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = 'X'; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
}
}
};
const int width = 12;
const int height = 25;
char bucket [height][width] ={'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);
}
void drawBucket(){
setCursorTo(0,0);
for(int x = 0; x <= height; x++){
int a = width;
a = width;
int b = width;
for(int y = 0; y < a; y++){
cout<<bucket[x][y];
}
if(b == width){
cout<<bucket[x][b]<<endl;
}
}
}
void updateBucket(TetrisShape localTetrisShape){
int shapeTopLeftX = 6;
int shapeTopLeftY = 0;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
bucket[shapeTopLeftX+i][shapeTopLeftY+j] = localTetrisShape.shapeArray[i][j];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
TetrisShape shape;
int gameOver = 0;
while(gameOver == 0){
void drawBucket();
srand(time(NULL));
int number = rand() % 6 + 1;
shape.populateShapeArray(number);
updateBucket(shape);
int newshapeTopLeftY = shape.shapeTopLeftY + 1;
}
return 0;
}
提前致谢。
正如@ataman 在评论中(或多或少)指出的那样,您实际上并没有在 while
循环中调用 drawBucket()
函数:您正在重新声明一个名为drawBucket()
。这是完全有效的,因为它在不同的范围内;但是,这可能不是您想要的。
将循环更改为:
while(gameOver == 0){
drawBucket();
...
}