2016-02-29 64 views
-1

我正在使用Allegro爲我們的專業編寫最終項目的遊戲,但碰到一些問題。我的遊戲基本上是戰列艦,而我所做的是在加載屏幕後,會出現一個屏幕,您可以使用箭頭鍵選擇船隻的座標並用空格鍵鎖定它。如果您選擇的座標已被佔用,則空格鍵不起作用。檢查碰撞

我的問題是,我不能在放置前檢查船舶的長度和寬度。只有在您已經放置了船舶並且下一艘船可以與其他船舶重疊時纔會考慮船舶的長度和寬度。我嘗試了一切,但我遇到了一些問題。不僅如此,即使它是空的,也不能選擇網格的某些部分。

void shipplacement(){ 

//something like a cursor for ship placement 
shipx = 0; 
shipy = 0; 

while(!placed) 
{ 
ALLEGRO_EVENT ev; 
al_wait_for_event(prepqueue, &ev); 

    if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { 
    switch(ev.keyboard.keycode) { 
     case ALLEGRO_KEY_UP: 
      key[KEY_UP] = true; 
      break; 

     case ALLEGRO_KEY_DOWN: 
      key[KEY_DOWN] = true; 
      break; 

     case ALLEGRO_KEY_LEFT: 
      key[KEY_LEFT] = true; 
      break; 

     case ALLEGRO_KEY_RIGHT: 
      key[KEY_RIGHT] = true; 
      break; 

     case ALLEGRO_KEY_SPACE: 
     xcheck(); // checks the coordinates of the ship controlled 
     tst();  // checks for collision and if space bar should work 
      break; 
    } 
    } 

    else if(ev.type == ALLEGRO_EVENT_KEY_UP) { 
    switch(ev.keyboard.keycode) { 
     case ALLEGRO_KEY_UP: 
      key[KEY_UP] = false; 
      break; 

     case ALLEGRO_KEY_DOWN: 
      key[KEY_DOWN] = false; 
      break; 

     case ALLEGRO_KEY_LEFT: 
      key[KEY_LEFT] = false; 
      break; 

     case ALLEGRO_KEY_RIGHT: 
      key[KEY_RIGHT] = false; 
      break; 
      } 
      } 

if(redraw && al_is_event_queue_empty(prepqueue)){ 
     if (shippl[1]){ 
     //redraw the background and draw the "selector" 
     } 
     else if (shippl[2]){ 
     //redraw the background and draw the new "selector" and the placed ship. 
     } 
     else if (shippl[3]){ 
     //redraw the background and draw the new "selector" and the placed ships. 
     } 
     else if (shippl[4]){ 
     //redraw the background and draw the new "selector" and the placed ships. 
     } 
     else if (shippl[5]){ 
     //redraw the background and draw the new "selector" and the placed ships. 
     } 
redraw = false; 
al_flip_display();} 
    } 
}  

void tst(){ 
     if(logic == 0){ 
              makeittrue(); 
              shippl[count] = false; 
              shipfx[count] = shipx; 
              shipfy[count] = shipy; 
              count++; 
              logic++; 
              shipx = 0; 
              shipy = 0; 
              } 

     else if(logic == 1){ 
        if(!s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){ 
               makeittrue(); 
              shippl[count] = false; 
              shipfx[count] = shipx; 
              shipfy[count] = shipy; 
              count++; 
              logic++; 
              shipx = 0; 
              shipy = 0; 
              } 
        } 
     else if(logic == 2){ 
        if(!s2[xholder][yholder] && !s2[xholder][y2] && !s2[xholder][y3] && !s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){ 
               makeittrue(); 
              shippl[count] = false; 
              shipfx[count] = shipx; 
              shipfy[count] = shipy; 
              count++; 
              logic++; 
              shipx = 0; 
              shipy = 0; 
              } 
        } 
     else if(logic == 3){ 
        if(!s3[xholder][yholder] && !s3[x2][yholder] && !s3[x3][yholder] && !s2[xholder][yholder] && !s2[xholder][y2] && !s2[xholder][y3] && !s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){ 
               makeittrue(); 
              shippl[count] = false; 
              shipfx[count] = shipx; 
              shipfy[count] = shipy; 
              count++; 
              logic++; 
              shipx = 0; 
              shipy = 0; 
              } 
        } 
     else if(logic == 4){ 
        if(!s4[xholder][yholder] && !s4[xholder][y2] && !s3[xholder][yholder] && !s3[x2][yholder] && !s3[x3][yholder] && !s2[xholder][yholder] && !s2[xholder][y2] && !s2[xholder][y3] && !s1[xholder][yholder] && !s1[xholder][y2] && !s1[xholder][y3] && !s1[xholder][y4]){ 
               makeittrue(); 
              shippl[count] = false; 
              shipfx[count] = shipx; 
              shipfy[count] = shipy; 
              count++; 
              logic++; 
              shipx = 0; 
              shipy = 0; 
              } 
        } 
     } 


void makeittrue(){ 
     // s1 is ship1 with length of 4 blocks 
     // s2 is ship2 with length of 3 blocks 
     // s3 is ship3 with width of 3 blocks 
     // s4 is ship4 with length of 2 blocks 
     // s5 is ship5 with width of 2 blocks 
     if(count == 1){ 
       y2 = yholder + 1; 
       y3 = yholder + 2; 
       y4 = yholder + 3; 
     s1[xholder][yholder] = true; 
     s1[xholder][y2] = true; 
     s1[xholder][y3] = true; 
     s1[xholder][y4] = true; 
     placeflag[xholder][yholder] = true; 
     placeflag[xholder][y2] = true; 
     placeflag[xholder][y3] = true; 
     placeflag[xholder][y4] = true; 
     } 

     else if(count == 2){ 
       y2 = yholder + 1; 
       y3 = yholder + 2; 
     s2[xholder][yholder] = true; 
     s2[xholder][y2] = true; 
     s2[xholder][y3] = true; 
     placeflag[xholder][yholder] = true; 
     placeflag[xholder][y2] = true; 
     placeflag[xholder][y3] = true; 
     } 

     else if(count == 3){ 
       x2 = xholder + 1; 
       x3 = xholder + 2; 
     s3[xholder][yholder] = true; 
     s3[x2][yholder] = true; 
     s3[x3][yholder] = true; 
     placeflag[xholder][yholder] = true; 
     placeflag[x2][yholder] = true; 
     placeflag[x3][yholder] = true; 
     } 

     else if(count == 4){ 
       y2 = yholder + 1; 
     s4[xholder][yholder] = true; 
     s4[xholder][y2] = true; 
     placeflag[xholder][yholder] = true; 
     placeflag[xholder][y2] = true; 
     } 

     else if(count == 5){ 
       x2 = xholder + 1; 
     s5[xholder][yholder] = true; 
     s5[x2][yholder] = true; 
     placeflag[xholder][yholder] = true; 
     placeflag[x2][yholder] = true; 
     }  
    } 

請幫幫我。我沒有想法。

編輯

忘了提網格是10×10。

Sample of what happens

注:使用FF船精靈,因爲我還沒有完成創建我自己的。

+0

這裏有很多代碼,聽起來像你有多個錯誤。我建議你將這個問題分解成兩個或三個更具體的問題,只用與每個問題相關的代碼部分。 – pzp

+1

對不起。我修剪了代碼。並刪除不相關的。感謝您指出。 –

+0

我希望看到[MCVE](http://stackoverflow.com/help/mcve),因此分析問題會更容易。 – TobiMcNamobi

回答

0

所以看來我只是需要從編碼中休息一下。我設法通過爲每艘船製造單獨的變量來修復它,以便我可以準確地檢查它的每個部分並避免重載一個變量。