2016-04-16 47 views
1

我是第一年計算機工程師,所以我有點在編碼C++ amatuer,無論如何,我正在創造一個遊戲,其中有兩個飛船互相射擊。我現在成功地通過使用while循環和GetASyncKeyState函數使第一個太空船移動。但現在我正在讓子彈旅行。我用於循環,我確實成功地讓子彈向上行進。但有一個問題,我不能移動太空船,直到for循環停止或子彈到達控制檯窗口的頂部(我正在使用控制檯btw)。有沒有辦法同時運行for循環和while循環?或者同時運行兩個不同的函數,因爲GameMovement()用於飛船的移動,而BulletTravel()用於子彈。我從GameMovement()調用BulletTravel()。同時運行兩個循環?移動和旅行使用循環

void GameMovements() 
{ 
bool FirstInitialization = true; 
int Player1XCoordinate = 55, Player2XCoordinate = 55; 
int Player1YCoordinateU = 28, Player1YCoordinateD = 29; 


while (true) 
{ 
    BulletPattern(Player1XCoordinate); 

    if (FirstInitialization == true) 
    { 
     GameBorderAndStatus(); 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateU); 
     cout << " ^\n"; 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateD); 
     cout << "^==|==^ \n"; 
     FirstInitialization = false; 
    } 

    //MOVEMENTS FOR PLAYER 1 
    else if (GetAsyncKeyState(VK_LEFT) && Player1XCoordinate != 16) 
    { 
     system("cls"); 
     GameBorderAndStatus(); 

     Sleep(10); 
     Player1XCoordinate -= 3; 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateU); 
     cout << " ^\n"; 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateD); 
     cout << "^==|==^ \n"; 
    } 
    else if (GetAsyncKeyState(VK_RIGHT) && Player1XCoordinate != 94) 
    { 
     system("cls"); 
     GameBorderAndStatus(); 
     Player1XCoordinate += 3; 

     Sleep(10); 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateU); 
     cout << " ^\n"; 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateD); 
     cout << "^==|==^ \n"; 
    } 
    else if (GetAsyncKeyState(VK_UP) && Player1YCoordinateU != 24) 
    { 
     system("cls"); 
     GameBorderAndStatus(); 
     Player1YCoordinateU -= 2; 
     Player1YCoordinateD -= 2; 

     Sleep(10); 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateU); 
     cout << " ^\n"; 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateD); 
     cout << "^==|==^ \n"; 
    } 
    else if (GetAsyncKeyState(VK_DOWN) && Player1YCoordinateU != 28) 
    { 
     system("cls"); 
     GameBorderAndStatus(); 
     Player1YCoordinateU += 2; 
     Player1YCoordinateD += 2; 

     Sleep(10); 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateU); 
     cout << " ^\n"; 
     SetCoordinate(Player1XCoordinate, Player1YCoordinateD); 
     cout << "^==|==^ \n"; 
    } 

} 
} 

void GameBorderAndStatus() 
{ 
     //Draw game border 
     for (int i = 0; i < 31; i++) 
     { 
     SetCoordinate(15, i); 
     cout << "|"; 
     SetCoordinate(104, i); 
     cout << "|"; 
     } 

} 

void BulletPattern(int Player1MuzzleLocation) 
{ 
    for (int i = 25; i != 3; i--) 
    { 
     Sleep(100); 
     SetCoordinate(Player1MuzzleLocation + 3, i); 

    } 
} 

void SetCoordinate(int CoordinateX, int CoordinateY) 
{ 
    COORD Coordinate; 
    Coordinate.X = CoordinateX; 
    Coordinate.Y = CoordinateY; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Coordinate); 
} 
+0

我不確定你是否知道這一點,但是你使用的任何「遊戲代碼」('GetAsyncKeyState','SetCoordinate'等)都不是標準的C++。這意味着我們不知道它們是什麼,或者如果您使用它們的方式有任何問題。我誠實地建議您與您的教師聯繫以獲得指導,因爲我們可以做的很少,可以幫助您調試具有許多神祕功能的代碼。 –

+0

@ChrisBritt今年夏天,班結束了。我只是這樣做才能殺了一些時間。無論如何,我以爲你們都知道GetASyncKetState()對此很抱歉。我會用SetCoordinate()在其中更新代碼。 – Neo

+1

@Neo由於您的代碼嚴重依賴於Windows API,因此我已經提前發佈並重新提出了您的問題,希望能夠讓熟悉相關功能的人員獲得更好的曝光。如果您覺得沒有幫助,請隨時恢復編輯。 – computerfreaker

回答

1

而不是使用兩個不同的功能和移動中的每一個對象,你可能會發現更好的效果跟蹤,其中每個對象應該是,用一個函數來繪製兩者。

由於缺少一些函數和變量聲明(例如,我沒有看到你真的畫過子彈,可能是BulletPattern中的一個錯誤),所以很難說出代碼中發生了什麼,但這樣的事情可能會訣竅:

void GameMovements() 
{ 
    while (true) 
    {   
     //MOVEMENTS FOR PLAYER 1 
     if (GetAsyncKeyState(VK_LEFT) && Player1XCoordinate != GAMEBOARD_LEFT) 
     { 
      Player1XCoordinate -= 3; 
     } 
     else if (GetAsyncKeyState(VK_RIGHT) && Player1XCoordinate != GAMEBOARD_RIGHT) 
     { 
      Player1XCoordinate += 3; 
     } 
     else if (GetAsyncKeyState(VK_UP) && Player1YCoordinate != SPACESHIP_TOP) 
     { 
      Player1YCoordinate -= 2; 
     } 
     else if (GetAsyncKeyState(VK_DOWN) && Player1YCoordinate != SPACESHIP_BOTTOM) 
     { 
      Player1YCoordinate += 2; 
     } 

     Sleep(10); 

     UpdateBulletPosition(); 
     DrawObjects(); 
    } 
} 

void UpdateBulletPosition() 
{ 
    //if the bullet hits the top of the screen, remove it 
    if (bulletYCoordinate == GAMEBOARD_TOP) 
    { 
     bulletXCoordinate = 0; 
     bulletYCoordinate = 0; 
    } 

    //I assume you're automatically generating bullets whenever possible; you'll have to adjust this conditional if that's not the case 

    //no bullet? generate one 
    if (bulletXCoordinate == 0) 
    { 
     bulletXCoordinate = Player1XCoordinate + 3; 
     bulletYCoordinate = 25; 
    } 
    else 
    { 
     bulletYCoordinate--; 

     Sleep(100); 
    } 
} 

void DrawObjects() 
{ 
     //wipe the screen and show status first 
     system("cls"); 
     GameBorderAndStatus(); 

     SetCoordinate(Player1XCoordinate, Player1YCoordinate); 
     cout << " ^\n"; 
     SetCoordinate(Player1XCoordinate, Player1YCoordinate + 1); 
     cout << "^==|==^ \n"; 

     //only draw the bullet if there's a bullet there to draw 
     if (bulletXCoordinate != 0) 
     { 
      SetCoordinate(bulletXCoordinate, bulletYCoordinate); 
      cout << ".\n"; 
     } 
} 

const int GAMEBOARD_LEFT = 16; 
const int GAMEBOARD_RIGHT = 94; 
const int GAMEBOARD_TOP = 3; 
const int SPACESHIP_TOP = 24; 
const int SPACESHIP_BOTTOM = 28; 

int Player1XCoordinate = 55, Player2XCoordinate = 55; 
int Player1YCoordinate = 28; 
int bulletXCoordinate = 0, bulletYCoordinate = 0; 

我也對你的其他代碼做了一些調整。 if-else塊中的每個案例都使用了相同的基本繪圖代碼,因此我完全從if-else中取出了該代碼。這也讓我放棄了整個初始化if-else

我也放棄了你的一個飛船的垂直座標。你真的不需要兩個;只需跟蹤上部座標並在Player1YCoordinate + 1處繪製船的下半部分即可。

最後,我用常量代替了硬編碼的電路板邊緣。魔術數字通常被壓抑;使用命名常量可以更容易地確定爲什麼您在給定位置使用給定值,並且更容易在未來更新代碼(也許您需要更新不同的控制檯大小)。

+0

@Neo認爲一個你認爲有用並且接受爲正確答案的答案是很好的做法。 :-) –

+0

@CareyGregory我很喜歡upvote,但我需要至少15聲望upvote :( – Neo