在 C++ 中从 2D 数组中删除值
Removing values from a 2D array in C++
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.
祝你好运!
相关文章:
- 你能帮我解决删除数组 c++ 中重复值的挑战吗?
- 删除[i] 数组和删除数组 [i] 之间的区别
- 如何实现删除数组的功能?
- 删除数组 c++ 崩溃问题
- 如何删除数组列表中的元素
- C++删除数组只会解除分配第一项
- 删除数组成员的更好方法是什么?
- 如何使用 deleteEntry 函数 c++ 删除数组中的单个字符串
- 删除数组中所有出现的数字 C++
- C++ 删除数组时检测到堆损坏
- 不允许删除数组的一部分,为什么
- 如何删除数组中第一次出现的值
- 当我删除数组时,指针在哪里?指向数组的指针与数组中的元素之间的区别
- 尝试用C 中的空地址(而非null)删除数组指针
- 为什么 std::remove 不删除数组的最后一个元素
- 如何使用c++删除数组中的一些值
- 仅删除数组的第一个元素
- 删除数组时断言失败
- 如何并行删除数组中的零值
- 访问指针数组/通过另一个指针并删除数组中的单个元素而不删除第一个指针或整个数组