检查碰撞

时间:2016-02-29 03:26:56

标签: c++ allegro5

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

Sample of what happens

注意:使用FF船精灵导致我还没有完成自己的创作。

1 个答案:

答案 0 :(得分:0)

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