C++ Tetris Program bucket not being displayed

时间:2015-09-01 21:56:27

标签: c++ tetris

I am making a C++ Tetris game for my programming class and write now I am working on displaying the bucket and having the pieces drop into the bucket, but I am not yet moving the pieces in the bucket. I am able to build the program successfully, but when the program is ran, nothing is displayed.

Also, I am unsure of how I would be able to animate dropping the pieces into the bucket.

Here is what I have so far.

 #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;
}

Thank you in advance.

1 个答案:

答案 0 :(得分:0)

As @ataman has (more-or-less) pointed out in the comments, you aren't actually calling your drawBucket() function in your while loop: you are redeclaring a function named drawBucket(). This is perfectly valid because it's in a different scope; however, it's probably not what you intend.

Change your loop to:

while(gameOver == 0){
    drawBucket();
    ...
}