在C ++中从2D数组中删除值

时间:2015-04-03 13:38:43

标签: c++ arrays opengl

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()我在其中应用了条件来检测砖块。当球与砖碰撞时,我将其变为不可见,即我将其颜色更改为白色,并且我必须从二维阵列中移除其坐标,以便球不会再次检测到它。我怎么能得到这个?我用来删除坐标的所有方法都没有用。

1 个答案:

答案 0 :(得分:0)

我认为您的“魔数”解决方案可行。只需在碰撞检测中添加一些逻辑,专门查找该幻数,如果匹配则忽略它。即:

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.
祝你好运!