Canelone
Canelone

Reputation: 9

Creating a Tetris Game with field problem

I'm starting with programming C++ and I was trying to create a Tetris game. I've added assets and defined the field size. Before adding Game Logic, I've noticed that my field wasn't "ok". It should be a table and not three small tables. I'm not sure what is the problem, maybe it's in the //draw field. Can you help me?

Code

#include <string>
#include "Windows.h"
using namespace std;

int nScreenWidth = 80;          // Console Screen Size X (columns)
int nScreenHeight = 30;         // Console Screen Size Y (rows)
wstring tetro[7];
int nFieldW = 12;
int nFieldH = 18;
unsigned char* pField = nullptr;

int rotation(int ex, int ey, int r) {
    switch (r % 4) {
    case 0: return ey * 4 + ex;               // 0 graus
    case 1: return 12 + ey - (ex * 4);        // 90 graus
    case 2: return 15 - (ey * 4) - ex;        // 180 graus
    case 3: return 3 - ey + (ex * 4);         // 270 graus
    }
    return 0;
}

int main()
{
    //create assets
    tetro[0].append(L"..X.");
    tetro[0].append(L"..X.");
    tetro[0].append(L"..X.");
    tetro[0].append(L"..X.");

    tetro[1].append(L"..X.");
    tetro[1].append(L".XX.");
    tetro[1].append(L".X..");
    tetro[1].append(L"....");

    tetro[3].append(L"....");
    tetro[3].append(L".XX.");
    tetro[3].append(L".XX.");
    tetro[3].append(L"....");

    tetro[4].append(L"..X.");
    tetro[4].append(L".XX.");
    tetro[4].append(L".X..");
    tetro[4].append(L"....");

    tetro[5].append(L"....");
    tetro[5].append(L".XX.");
    tetro[5].append(L"..X.");
    tetro[5].append(L"..X.");

    tetro[6].append(L"....");
    tetro[6].append(L".XX.");
    tetro[6].append(L".X..");
    tetro[6].append(L".X..");

    pField = new unsigned char[nFieldW*nFieldH];
    for (int x = 0; x < nFieldW; x++)  //Board Boundary
        for (int y = 0; y < nFieldH; y++)
            pField[y*nFieldW + x] = (x == 0 || x == nFieldW - 1 || y == nFieldH - 1) ? 9 : 0;

    // Create Screen Buffer
    wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
    for (int i = 0; i < nScreenWidth * nScreenHeight; i++) screen[i] = L' ';
    HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleActiveScreenBuffer(hConsole);
    DWORD dwBytesWritten = 0;

    bool bGameOver = false;
    while (!bGameOver) {
        //draw field
        for (int x = 0; x < nFieldW; x++)
            for (int y = 0; y < nFieldH; y++)
                screen[(y + 2)*nScreenWidth + (x + 2)] = L" ABCDEFG=#"[pField[y*nFieldW + x]];

        //display frame
        WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
    }



}

Solution enter image description here

Thank you in advance!

Upvotes: 0

Views: 513

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32717

You write out your screen contents as one big string. The console will display this on one line, only wrapping to the next line when it reaches the right side of the console buffer.

You need to either set the console window and buffer widths to the same width as your internal screen buffer (80 characters), or (preferably) write each line individually to the console.

Upvotes: 1

Related Questions