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

Removing values from a 2D array in C++

本文关键字:删除 数组 2D C++ 中从      更新时间:2023-10-16
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(),其中我应用了检测砖的条件。当球与砖块碰撞时,我使它无敌,即我将其颜色更改为白色,并且我还必须从 2-D 数组中删除它的坐标,这样球就不会再次检测到它。我怎样才能实现这一点?我用于删除坐标的所有方法都不起作用。

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

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.

祝你好运!