检查冲突

Checking for collision

本文关键字:冲突 检查      更新时间:2023-10-16

我正在使用 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;
      }       
  }

请帮助我。我没主意了。

编辑

忘了说网格是10x10。

发生的情况示例

注意:使用过的FF飞船精灵,因为我还没有完成自己的创建。

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