2016-03-18 39 views
0

我正在嘗試做一個簡單的遊戲。基本概念是玩家(P)在地圖上移動。最終玩家將不得不避開敵人並完成關卡。目前我有玩家可以在搶彈藥和火力前進的情況下移動。我正在清理整個屏幕並每隔一段時間重寫地圖,以便用戶可以實時輸入和玩遊戲。這裏是我當前的代碼:在C++輸出中重置或清除行

#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.請幫助! 感謝

+0

你對'SetCursorPos'有什麼不瞭解?這和可能'FillConsoleOutputCharacter'聽起來像你的解決方案。 –

+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); 
} 

this獲取更多Windows控制檯功能。