2016-03-18 19 views
0

Basit bir oyun yapmaya çalışıyorum. Temel kavram, oyuncunun (P) bir harita üzerinde hareket etmesidir. Sonunda oyuncu düşmanları önlemek ve seviyesini tamamlamak zorunda kalacak. Şu anda oyuncunun kepçe dolabını hareket ettirebiliyor ve ileri ateş edebiliyor. Ekranın tamamını temizliyorum ve haritayı her seferinde yeniden yazarım, böylece kullanıcı oyunu gerçek zamanlı olarak girip oynayabilir. İşte benim geçerli kod:C++ Çıkışında Satır Sıfırlanması veya Temizlenmesi

#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; 
} 

belli bir çizgi yeniden yazmak için bir yol var mı (örneğin bir çizgi olarak oyuncu üzerindedir ve oyuncu taşınıyor biri) Ben sadece bir kısmını yeniden yazmak zorunda böylece haritanın tamamı yerine harita. Şu anda yanıp sönüyor ve sık sık yanıp sönüyor beri görmek zordur. Kullanıcı bir anahtar girdiğinde bazı satırları yeniden yazabilirsem. Ben ve setCursorPos() işlevini denedim, ama tamamen anladığımı sanmıyorum. Windows 10 ve Windows 7 kullanıyorum ve Microsoft Visual 2010 kullanıyorum. Lütfen Yardım! Teşekkür

+0

'SetCursorPos' hakkında ne anlamadınız? Bu ve muhtemelen 'FillConsoleOutputCharacter' çözümünüz gibi görünüyor. –

+0

Kodunuzu + güvende tutun –

cevap

0

"Ben denedim \ r ve SetCursorPos() işlevi, ama ben tamamen onları anlamak sanmıyorum."

Bu çalışması gerekir:

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 Bkz Daha fazla Windows konsol işlevleri için.

İlgili konular