2016-02-29 126 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

所以看来我只是需要从编码中休息一下。我设法通过为每艘船制造单独的变量来修复它,以便我可以准确地检查它的每个部分并避免重载一个变量。