首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建带场问题的俄罗斯方块游戏

创建带场问题的俄罗斯方块游戏
EN

Stack Overflow用户
提问于 2021-07-23 22:35:21
回答 1查看 293关注 0票数 0

我从编程C++开始,我试着创建一个俄罗斯方块游戏。我增加了资产并定义了字段大小。在添加游戏逻辑之前,我注意到我的领域不是"ok“。它应该是一张桌子,而不是三张小桌子。我不知道是什么问题,也许是在//抽签字段。你能帮帮我吗?

代码语言:javascript
复制
#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);
    }



}

溶液

预先谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2021-07-23 22:45:24

您可以将screen内容写出为一个大字符串。控制台将显示在一行上,只有在到达控制台缓冲区的右侧时才会显示到下一行。

您需要将控制台窗口和缓冲区宽度设置为与内部屏幕缓冲区(80个字符)相同的宽度,或者(最好)将每一行单独写入控制台。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68505616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档