2015-04-03 56 views
0
void Draw() { 
int c1; 
int x = 59; 
int y = 500; 
int temp = x; 
for (int i = 0; i < 13; ++i){ 
    for (int j = 0; j < 10; ++j){ 
     x_coordinates[i][j] = x; 
     y_coordinates[i][j] = y; 

    c1 = temp_color[i][j]; 
    DrawRectangle(x, y, 65, 25, colors[c1]); 
      x += 67; 
     } 
     x = temp; 
     y -= 28; 
} 
DrawRectangle(tempx, 0, 85, 12, colors[5]); 
DrawCircle(templx, temply, 10, colors[7]); 
    } 
    // This function will be called automatically by this frequency 1000.0/FPS 
    void Animate() { 
     //if (temply < - 10) 
     //exit(1); 
     Brick_collision(); 
     glutPostRedisplay(); // Once again call the Draw member function 
    } 
    int Brick_collision(){ 
    for (int i=0; i<13; ++i){ 
     for (int j=0; j<10; ++j){ 
      if (((templx >= x_coordinates[i][j]) && (templx <= x_coordinates[i][j] + 65)) && ((temply + 5 >= y_coordinates[i][j]) && (temply + 5 <= y_coordinates[i][j] + 35 ))){ 
       vy = -vy; 

       temp_color[i][j] = 2; 
       // x_coordinates[i][j] -= 300; 
       // y_coordinates[i][j] -= 300; 
       // I HAVE USED THESE VALUES BECAUSE NOW THE BRICK WOULD BE OUTSIDE THE SCREEN AND THE BALL WILL NOT COLLIDE WITH IT AGAIN BUT THIS DOESN'T WORK. 

       return 1; 
       } 
      } 
     } 
    } 

我正在尝试使用OpenGL制作BrickSlayer游戏。在Draw()函数中,我绘制了游戏的结构,即砖块,踏板和球。现在我将这些砖的x和y坐标存储在一个二维数组中。在Animate()函数中,我调用函数Brick_collision()其中我已经应用了检测砖的条件。当球与砖块发生碰撞时,我使它变得不可思议,即我将它的颜色更改为白色,并且我必须从二维阵列中移除它的坐标,以便球不再检测到它。我怎样才能做到这一点?我用于去除坐标的所有方法都没有奏效。从C++中删除二维数组中的值

+2

你是什么意思_remove_?您不能从固定大小的阵列中移除元素。你的意思是你需要重置这些值吗?否则,我会推荐使用'std :: vector >'。 – 2015-04-03 13:42:39

+0

通过删除我的意思是我可以更改数组中的'x'和'y'坐标的值。 – 2015-04-03 13:56:29

+1

供参考:这是*不是*二维阵列,它是一个锯齿状阵列 – 2015-04-03 13:57:10

回答

0

我认为你的“Magic Number”解决方案可以工作。只需在碰撞检测中添加一些逻辑即可专门查找该幻数并在匹配时忽略它。 I.E .:

int Brick_collision(){ 
    for (int i=0; i<13; ++i){ 
     for (int j=0; j<10; ++j){ 
      if (((x_coordinates[i][j] != -300) && 
       (y_coordinates[i][j] != -300) && 
       (templx >= x_coordinates[i][j]) && 
       (templx <= x_coordinates[i][j] + 65)) && 
       ((temply + 5 >= y_coordinates[i][j]) && 
       (temply + 5 <= y_coordinates[i][j] + 35))){ 

       vy = -vy; 

       temp_color[i][j] = 2; 

       // x_coordinates[i][j] -= 300; 
       // y_coordinates[i][j] -= 300; 
       // I HAVE USED THESE VALUES BECAUSE NOW THE BRICK 
       // WOULD BE OUTSIDE THE SCREEN AND THE BALL WILL NOT 
       // COLLIDE WITH IT AGAIN BUT THIS DOESN'T WORK. 

祝你好运!

+0

我不认为你完全得到我的问题。简单地添加这个新条件不会改变任何东西。这只会检查坐标值是否不等于-300。 – 2015-04-03 14:03:07

+0

我在考虑添加此检查可确保在成功碰撞期间将其设置为-300之后。随后使用这些坐标运行将会使IF块失败。我可能误解了这个游戏的逻辑是如何工作的,但它似乎是一个版本的集合,并检查一个神奇的数字应该允许你逃避碰撞逻辑。 – 2015-04-03 14:10:51