检查青少年游戏中像素是否发生碰撞

时间:2019-10-21 05:20:32

标签: c teensy

我正在尝试检查操纵杆控制的角色是否与存储在数组中的墙像素发生冲突。角色的坐标与其位置的左上角有关,并且二维数组表示绘制该角色的“位图”。但是我要么没有碰撞,要么角色无法移动,因为代码认为它发生了碰撞。

我已经完成了多种调用碰撞测试的方法-在碰撞函数中以及在move函数中都尝试它。我最近切换到2进行循环,因为1仅比较它们各自阵列中相同位置的像素,而不是与所有像素比较。 我按照下面的注释中的建议进行了一些更改,现在我将问题范围缩小到始终返回0的碰撞函数。我是通过与敌方角色发生碰撞而发现的,当temp == 0时,玩家死亡而没有会移动,但是将其设置为if(temp == 1)时,即使玩家触摸敌人角色也不会死亡。

int wall_collide(int x1, int y1, int i_count, int j_count, int array1[i_count][2], int array2[j_count][2]) {
    for (int i = 0; i < i_count; ++i)
    {
        for (int j = 0; j < j_count; ++j)
        {
            if ((x1 + array1[i][0] == array2[j][0]) && (y1 + array1[i][1] == array2[j][1]))
            {
                 return 1;
            }
        }
    }
    return 0;
}

void p_move() {
    int temp = 0;
    if (LBpressed == 1 && !(c_x == 0))
    {
        temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
        if (temp == 0) c_x -= 1;
    }
    if (RBpressed == 1 && !(c_x == (LCD_X - 5)))
    {
        temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
        if (temp == 0) c_x -= 1;
    }
    if (UBpressed == 1 && !(c_y == 8))
    {
        temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
        if (temp == 0) c_y -= 1;
    }
    if (DBpressed == 1 && !(c_y == (LCD_Y - 7))) 
    {
        temp = wall_collide(c_x, c_y, 22, 64, c_xy, pixelbank);
        if (temp == 0) c_y += 1;
    }
}

int walls[4][4] = { {18,15, 13,25}, {25,35, 25,45}, {45,10, 60,10}, {58,25, 72,30} };
This is how I'm storing the pixels for the walls when I draw them
void pixelbank(int x1, int y1, int x2, int y2) {
    if ( x1 == x2 ) {
        // Draw vertical line
        for ( int i = y1; (y2 > y1) ? i <= y2 : i >= y2; (y2 > y1) ? i++ : i-- ) {
            inputoarray(x1, i);
        }
    }
    else if ( y1 == y2 ) {
        // Draw horizontal line
        for ( int i = x1; (x2 > x1) ? i <= x2 : i >= x2; (x2 > x1) ? i++ : i-- ) {
            inputoarray(i, y1);
        }
    }
    else {
        //  Always draw from left to right, regardless of the order the endpoints are 
        //  presented.
        if ( x1 > x2 ) {
            int t = x1;
            x1 = x2;
            x2 = t;
            t = y1;
            y1 = y2;
            y2 = t;
        }

        // Get Bresenhaming...
        float dx = x2 - x1;
        float dy = y2 - y1;
        float err = 0.0;
        float derr = ABS(dy / dx);

        for ( int x = x1, y = y1; (dx > 0) ? x <= x2 : x >= x2; (dx > 0) ? x++ : x-- ) {
            inputoarray(x, y);
            err += derr;
            while ( err >= 0.5 && ((dy > 0) ? y <= y2 : y >= y2) ) {
                inputoarray(x, y);
                y += (dy > 0) - (dy < 0);
                err -= 1.0;
            }
        }
    }
}

pixelbank是存储墙壁像素的位置(int pixelbank [64] [2]) c_x和c_y是绘制字符的起点,而c_xy包含偏移量,例如c_xy [0] [0] = 1和c_xy [0] [1] = 0,因为在c_x + 1,c_y + 0处绘制了一个像素。

我希望角色不能穿过墙壁,但是他要么可以移动,要么根本不能移动,但是我的代码在编译时不会提供任何错误。

如果有帮助,我的代码也使用48x84 LCD屏幕

0 个答案:

没有答案