在C ++输出中重置或清除行

时间:2016-03-18 18:53:17

标签: c++ visual-studio-2010

我正在尝试制作一款简单的游戏。基本概念是玩家(P)在地图上移动。最终玩家将不得不避开敌人并完成关卡。目前我让玩家能够四处移动抓住弹药并向前发射。我正在清理整个屏幕并经常重写地图,以便用户可以实时输入和玩游戏。这是我目前的代码:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, ammoX, ammoY, score;
bool shot;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN, ALL};
eDirecton dir,shoot;
void Setup()
{
 gameOver = false;
 dir = STOP;
 x = width / 2;
 y = height / 2;
 ammoX = rand() % width;
 ammoY = rand() % height;
 score = 0;

}
void Draw()
{
system("cls"); //system("clear");
for (int i = 0; i < width+2; i++)
    cout << "-";
cout << endl;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        if (j == 0)
            cout << "|";
        if(shoot == UP){    
            if(i < y && j == x && score > 0){
                cout << ":";
                shot = true;
            }
        }
        /*if(shoot == LEFT || shoot == ALL){    
            if(i == y && j < x && score > 0){
                cout << "~";
                shot = true;
            }
        }
        if(shoot == RIGHT || shoot == ALL){
            if(i == y && j > x && score > 0){
                cout << "~";
                shot = true;
            }
        }
        if(shoot == DOWN){
            if(i > y && j == x && score > 0){
                cout << ":";
                shot = true;
            }
        }*/
        else if (i == y && j == x)
            cout << "P";
        else if (i == ammoY && j == ammoX && score < 50)
            cout << "A";
        else
        {
            bool print = false;
            /*for (int k = 0; k < nTail; k++)
            {
                if (tailX[k] == j && tailY[k] == i)
                {
                    cout << "o";
                    print = true;
                }
            }*/
            if (!print)
                cout << " ";
        }


        if (j == width - 1)
            cout << "|";
    }
    cout << endl;
}

for (int i = 0; i < width+2; i++)
    cout << "-";
cout << endl;
cout << "Score:" << score << endl;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
/*
if(_kbhit()){
    cout << _getch() << endl;
}//*/
}

void Input()
{
if (_kbhit())
{
    switch (_getch())
    {
    case 75:
        dir = LEFT;
        break;
    case 77:
        dir = RIGHT;
        break;
    case 72:
        dir = UP;
        break;
    case 80:
        dir = DOWN;
        break;
    case 'w':
        shoot = UP;
        break;
    case 'a':
        shoot = LEFT;
        break;
    case 's':
        shoot = DOWN;
        break;
    case 'd':
        shoot = RIGHT;
        break;
    case 32:
        shoot = ALL;
        break;
    case 27:
        gameOver = true;
        break;
    }
}
}
void Logic()
{
/*int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
    prev2X = tailX[i];
    prev2Y = tailY[i];
    tailX[i] = prevX;
    tailY[i] = prevY;
    prevX = prev2X;
    prevY = prev2Y;
}*/
switch (dir)
{
case LEFT:
    x--;
    break;
case RIGHT:
    x++;
    break;
case UP:
    y--;
    break;
case DOWN:
    y++;
    break;
default:
    break;
}

if(x>=width){
    x--;
}
else if(x < 0){
    x++;
}
if(y>=height){
    y--;
}
else if(y < 0){
    y++;
}
//if (x > width || x < 0 || y > height || y < 0)
//  gameOver = true;
//if (x >= width) x = 0; else if (x < 0) x = width - 1;
//if (y >= height) y = 0; else if (y < 0) y = height - 1;

/* for (int i = 0; i < nTail; i++)
    if (tailX[i] == x && tailY[i] == y)
        gameOver = true;*/

if (x == ammoX && y == ammoY)
{
    score += 10;
    ammoX = rand() % width;
    ammoY = rand() % height;
    //nTail++;
}
if(shot == true){
    score -= 10;
}
}
int main()
{
Setup();
while (!gameOver)
{
    Draw();
    shoot = STOP;
    Input();
    Logic();
    shot = false;
    dir = STOP;
    Sleep(30); //sleep(10);
}
return 0;
}

有没有办法重写某一行(例如玩家所在的行和玩家正在移动的那一行),这样我只需要重写一部分地图而不是整个地图。它目前闪烁,很难看到,因为它经常闪烁。如果我可以在用户输入密钥时重写某些行。我尝试过\ r和setCursorPos()函数,但我认为我并不完全理解它们。我使用的是Windows 10和Windows 7,并且正在使用Microsoft Visual 2010.请帮忙! 感谢

1 个答案:

答案 0 :(得分:0)

“我尝试了\ r和setCursorPos()函数,但我认为我并不完全理解它们。”

这应该有效:

void setcp( int x, int y )
{
  HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  GetConsoleScreenBufferInfo( hStdout, &csbiInfo );
  csbiInfo.dwCursorPosition = COORD{ x, y };
  SetConsoleCursorPosition( hStdout, csbiInfo.dwCursorPosition );
}

有关更多Windows控制台功能,请参阅this