VS Express 无法正确编译代码(?)

VS Express dosen't compile the code properly (?)

所以我以前用Code::BlocksIDE,很喜欢。最近我切换到 Visual Studio。我下载了 VS Express,它是 600mb,我每天只能使用 1gb 数据,我没有 Wi-Fi,所以我那是我唯一的 option.I 插入了在 [=18= 中正确编译的相同代码] 在其中,需要进行一些调整才能使其在 VS 中运行,但是当我最终检查它时,输出完全不同,而不是命令行俄罗斯方块,它出现故障并在命令提示符中填充了奇怪的字符。

这是我稍微调整了一下以使其在 VS 中工作的代码:

#include <iostream>
#include <time.h>
#include <string>
#include <windows.h>
using namespace std;

int nScreenHeight = 30;
int nScreenWidth = 80;
int nFieldWidth = 10;
int nFieldHeight = 25;
unsigned char *pField = NULL;
wstring tetromine[7];

int currentPiece = 0;
int currentRotation = 0;
int currentX = (nFieldWidth/2);
int currentY = 0;
unsigned int score = 0;

int pieceCounter = 0;
int speed = 20;
int speedCounter = 0;
bool forcePieceDown =false;
bool key[4];
bool shiftGridDown = false;

int rotate(int px,int py,int r)
{
    switch(r/90)
    {
    case 0:
        return py*4+px;//0 degs
    case 1:
        return 12+py - (px*4);//90 degs
    case 2:
        return 15 - (py*4) - px;//180 degs
    case 3:
        return 3 - py + (px*4);//270 degs
    }
    return 0;
}

int doesPieceFit(int id,int rot, int x, int y)
{
    for(int px = 0;px<4;px++){
        for(int py = 0;py<4;py++){
            int pi = rotate(px,py,rot);
            int fi = (y+py) * nFieldWidth + (x+px);
            if(x + px>= 0 && x+px < nFieldWidth){
                if(tetromine[id][pi] == L'X' && pField[fi]!=0){
                    return false;
                }
            }
        }
    }
    return true;
}

void lineCheck(){
    bool line = true;
    int lines = 0;
    for(int y = 0; y<= nFieldHeight-1;y++){
        for(int x = 1; x< nFieldWidth-1;x++){
            if(pField[(y)*nFieldWidth+x]!=0){
                line &= true;
            } else line &= false;
        }
        if(line) lines++;
        if(line){
            for(int x = 1; x< nFieldWidth-1;x++){
                pField[(y)*nFieldWidth+x] = 8;
            }
        }
    }
}


int main()
{

    //assets
    tetromine[0].append(L"..X.");
    tetromine[0].append(L"..X.");
    tetromine[0].append(L"..X.");
    tetromine[0].append(L"..X.");

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

    tetromine[2].append(L".X..");
    tetromine[2].append(L".XX.");
    tetromine[2].append(L"..X.");
    tetromine[2].append(L"....");

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

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

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

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

    pField = new unsigned char[nFieldWidth*nFieldHeight];
    for(int x = 0; x<nFieldWidth; x++)
    {
        for(int y = 0; y<nFieldHeight; y++)
        {
            pField[y*nFieldWidth + x] = (x==0||x==nFieldWidth -1 || y == nFieldHeight - 1) ? 9 : 0;
        }
    }

    char *screen = new char [nScreenWidth * nScreenHeight];
    HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleActiveScreenBuffer(hConsole);
    DWORD dwBytesWritten = 0;

    //Display frame
    COORD here;
    here.X = 0;
    here.Y = 0;
    WriteConsoleOutputCharacter(hConsole, (LPCWSTR)screen, nScreenWidth * nScreenHeight,here, &dwBytesWritten);

    bool gameOver = false;

    while(!gameOver)
    {
        Sleep(100);
        speedCounter++;
        if(speedCounter>=speed){
            forcePieceDown = true;
            speedCounter = 0;
        } else {
            forcePieceDown = false;
        }

        if(shiftGridDown){
            score++;
            for(int y = nFieldHeight-2;y > 0;y--){
                for(int x = 1;x<nFieldWidth -1;x++){
                    if((pField[(y)*nFieldWidth+x]) != 0){
                        pField[(y+1)*nFieldWidth+x] = pField[(y)*nFieldWidth+x];
                        pField[(y)*nFieldWidth+x] = 0;
                    }
                }
            }
            shiftGridDown = false;
            lineCheck();
        }

        for(int x = 1; x< nFieldWidth-1;x++){
            if(pField[(nFieldHeight-2)*nFieldWidth+x]==8){
                pField[(nFieldHeight-2)*nFieldWidth+x]=0;
                if(x==nFieldWidth-2){
                    shiftGridDown = true;
                    score+=100;
                }
            }
        }

        for(int k = 0;k<4;k++){                              //  R   L   D  Z
            key[k] = (0x8000 & GetAsyncKeyState((unsigned char)("DASZ"[k]))) != 0;
        }
        if(key[1]){
            if(doesPieceFit(currentPiece,currentRotation,currentX-1,currentY)){
                currentX = currentX-1;
            }
        }else if(key[0]){
            if(doesPieceFit(currentPiece,currentRotation,currentX+1,currentY)){
                currentX = currentX+1;
            }
        }if(key[2]){
            speedCounter = speed;
        }
        if(key[3]&&doesPieceFit(currentPiece,currentRotation+90,currentX,currentY)){
            (currentRotation+90<=270)?currentRotation+=90:currentRotation=0;
        }

        if(forcePieceDown){
            if(doesPieceFit(currentPiece,currentRotation,currentX,currentY+1))
                currentY++;
            else {
                //lock piece
                pieceCounter++;
                if(pieceCounter%5==0){
                    speed-=1;
                }
                for(int px = 0;px<4;px++){
                    for(int py = 0;py<4;py++){
                        if(tetromine[currentPiece][rotate(px,py,currentRotation)]==L'X'){
                            pField[(currentY+py)*nFieldWidth+(currentX+px)] = currentPiece+1;
                        }
                    }
                }
                score+=20;
                //check lines
                lineCheck();

                //get next piece
                currentX = nFieldWidth/2;
                currentY = 0;
                currentRotation = 0;
                srand(time(0));
                currentPiece = rand() % 7;

                //check game over
                gameOver = !doesPieceFit(currentPiece,currentRotation,currentX,currentY);
            }
        }

        //draw field
        for(int x = 0; x < nFieldWidth; x++)
        {
            for(int y = 0; y < nFieldHeight; y++)
            {
                screen[(y+2)*nScreenWidth + (x+  2)] = L" xxxxxxx=#"[pField[y*nFieldWidth + x]];
            }
        }

        //draw piece
        for(int px = 0;px<4;px++){
            for(int py = 0;py<4;py++){
                    if(tetromine[currentPiece][rotate(px,py,currentRotation)] == L'X'){
                        screen[(currentY+py+2)*nScreenWidth+(currentX+px+2)] = '+';
                    }
            }
        }
        string s("Score -> ");
        string num;
        int tmp = score;
        while(tmp!=0){
            int rem = tmp%10;
            tmp /= 10;
            num = ((char)(48+rem)) + num;
        }
        s+=num;
        for(int i = 0; i<s.size();i++){
            screen[i] = s[i];
        }

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

    return 0;
}

这是原代码:

#include <iostream>
#include <time.h>
#include <string>
#include <windows.h>
using namespace std;

int nScreenHeight = 30;
int nScreenWidth = 80;
int nFieldWidth = 10;
int nFieldHeight = 25;
unsigned char *pField = NULL;
wstring tetromine[7];

int currentPiece = 0;
int currentRotation = 0;
int currentX = (nFieldWidth/2);
int currentY = 0;
unsigned int score = 0;

int pieceCounter = 0;
int speed = 20;
int speedCounter = 0;
bool forcePieceDown =false;
bool key[4];
bool shiftGridDown = false;

int rotate(int px,int py,int r)
{
    switch(r/90)
    {
    case 0:
        return py*4+px;//0 degs
    case 1:
        return 12+py - (px*4);//90 degs
    case 2:
        return 15 - (py*4) - px;//180 degs
    case 3:
        return 3 - py + (px*4);//270 degs
    }
    return 0;
}

int doesPieceFit(int id,int rot, int x, int y)
{
    for(int px = 0;px<4;px++){
        for(int py = 0;py<4;py++){
            int pi = rotate(px,py,rot);
            int fi = (y+py) * nFieldWidth + (x+px);
            if(x + px>= 0 && x+px < nFieldWidth){
                if(tetromine[id][pi] == L'X' && pField[fi]!=0){
                    return false;
                }
            }
        }
    }
    return true;
}

void lineCheck(){
    bool line = true;
    int lines = 0;
    for(int y = 0; y<= nFieldHeight-1;y++){
        for(int x = 1; x< nFieldWidth-1;x++){
            if(pField[(y)*nFieldWidth+x]!=0){
                line &= true;
            } else line &= false;
        }
        if(line) lines++;
        if(line){
            for(int x = 1; x< nFieldWidth-1;x++){
                pField[(y)*nFieldWidth+x] = 8;
            }
        }
    }
}


int main()
{

    //assets
    tetromine[0].append(L"..X.");
    tetromine[0].append(L"..X.");
    tetromine[0].append(L"..X.");
    tetromine[0].append(L"..X.");

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

    tetromine[2].append(L".X..");
    tetromine[2].append(L".XX.");
    tetromine[2].append(L"..X.");
    tetromine[2].append(L"....");

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

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

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

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

    pField = new unsigned char[nFieldWidth*nFieldHeight];
    for(int x = 0; x<nFieldWidth; x++)
    {
        for(int y = 0; y<nFieldHeight; y++)
        {
            pField[y*nFieldWidth + x] = (x==0||x==nFieldWidth -1 || y == nFieldHeight - 1) ? 9 : 0;
        }
    }

    char *screen = new char [nScreenWidth * nScreenHeight];
    HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleActiveScreenBuffer(hConsole);
    DWORD dwBytesWritten = 0;

    //Display frame

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

    bool gameOver = false;

    while(!gameOver)
    {
        Sleep(100);
        speedCounter++;
        if(speedCounter>=speed){
            forcePieceDown = true;
            speedCounter = 0;
        } else {
            forcePieceDown = false;
        }

        if(shiftGridDown){
            score++;
            for(int y = nFieldHeight-2;y > 0;y--){
                for(int x = 1;x<nFieldWidth -1;x++){
                    if((pField[(y)*nFieldWidth+x]) != 0){
                        pField[(y+1)*nFieldWidth+x] = pField[(y)*nFieldWidth+x];
                        pField[(y)*nFieldWidth+x] = 0;
                    }
                }
            }
            shiftGridDown = false;
            lineCheck();
        }

        for(int x = 1; x< nFieldWidth-1;x++){
            if(pField[(nFieldHeight-2)*nFieldWidth+x]==8){
                pField[(nFieldHeight-2)*nFieldWidth+x]=0;
                if(x==nFieldWidth-2){
                    shiftGridDown = true;
                    score+=100;
                }
            }
        }

        for(int k = 0;k<4;k++){                              //  R   L   D  Z
            key[k] = (0x8000 & GetAsyncKeyState((unsigned char)("DASZ"[k]))) != 0;
        }
        if(key[1]){
            if(doesPieceFit(currentPiece,currentRotation,currentX-1,currentY)){
                currentX = currentX-1;
            }
        }else if(key[0]){
            if(doesPieceFit(currentPiece,currentRotation,currentX+1,currentY)){
                currentX = currentX+1;
            }
        }if(key[2]){
            speedCounter = speed;
        }
        if(key[3]&&doesPieceFit(currentPiece,currentRotation+90,currentX,currentY)){
            (currentRotation+90<=270)?currentRotation+=90:currentRotation=0;
        }

        if(forcePieceDown){
            if(doesPieceFit(currentPiece,currentRotation,currentX,currentY+1))
                currentY++;
            else {
                //lock piece
                pieceCounter++;
                if(pieceCounter%5==0){
                    speed-=1;
                }
                for(int px = 0;px<4;px++){
                    for(int py = 0;py<4;py++){
                        if(tetromine[currentPiece][rotate(px,py,currentRotation)]==L'X'){
                            pField[(currentY+py)*nFieldWidth+(currentX+px)] = currentPiece+1;
                        }
                    }
                }
                score+=20;
                //check lines
                lineCheck();

                //get next piece
                currentX = nFieldWidth/2;
                currentY = 0;
                currentRotation = 0;
                srand(time(0));
                currentPiece = rand() % 7;

                //check game over
                gameOver = !doesPieceFit(currentPiece,currentRotation,currentX,currentY);
            }
        }

        //draw field
        for(int x = 0; x < nFieldWidth; x++)
        {
            for(int y = 0; y < nFieldHeight; y++)
            {
                screen[(y+2)*nScreenWidth + (x+  2)] = L" xxxxxxx=#"[pField[y*nFieldWidth + x]];
            }
        }

        //draw piece
        for(int px = 0;px<4;px++){
            for(int py = 0;py<4;py++){
                    if(tetromine[currentPiece][rotate(px,py,currentRotation)] == L'X'){
                        screen[(currentY+py+2)*nScreenWidth+(currentX+px+2)] = '+';
                    }
            }
        }
        string s("Score -> ");
        string num;
        int tmp = score;
        while(tmp!=0){
            int rem = tmp%10;
            tmp /= 10;
            num = ((char)(48+rem)) + num;
        }
        s+=num;
        for(int i = 0; i<s.size();i++){
            screen[i] = s[i];
        }

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

    return 0;
}

您混合使用了窄字符和宽字符。您对 WriteConsoleOutputCharacter 的调用中的强制转换 (LPCWSTR)screen 表明有些事情不对。

在这种情况下,screenchar,但您希望它是 wchar_t。您已经为 tetromineL 前缀字符串使用了 wstring。您只需要确保其余代码也使用宽字符。