如何使用GBDK创建图块地图碰撞?

时间:2018-12-17 13:13:10

标签: c gbdk

我正在开发Game Boy平台游戏。我无法解决与地图项的冲突。 这是带有注释注释的代码:

#include <gb/gb.h>
#include <gb/drawing.h>

#include <stdio.h>

#include "dratini.c"

#include "background.c"
#include "map.c"

#include "collmap.c"

#include "collision.c"

#define JUMP_HEIGHT 5
#define JUMP_START_POWER (0 - JUMP_HEIGHT)

#define TRUE 1
#define FALSE 0

void init();
void input();
void draw();
void update();

void init_collision_array();

UINT8 collmap[gameMapWidth][gameMapHeight] = {{0}};

UINT8 collisionCheck(UINT8 x1, UINT8 y1, UINT8 w1, UINT8 h1, UINT8 x2, UINT8 y2, UINT8 w2, UINT8 h2);

UBYTE time = 0;

UBYTE x = 16 + 8, y = 111;
UBYTE vx = 16 + 8;
UINT16 mx = 0;
UBYTE menu = 0;
UBYTE jumping = 0;
UBYTE jump_dir = 0;


void main() {
    SPRITES_8x16;
    init();
    init_collision_array();
    if(collisionCheck(1, 1, 2, 2, 2, 2, 3, 3)) {
        gprint("Collide");
    }
    while(1) {
        input();
        update();
        draw();
        wait_vbl_done();
    }
}

void init() {
    DISPLAY_ON;
    set_bkg_data(0x0, 6, sand);
    set_bkg_tiles(0, 0, gameMapWidth, gameMapHeight, gameMap);
    set_sprite_data(0, 8, dratini);
    set_sprite_tile(0, 0);
    move_sprite(0, x, y);
    set_sprite_tile(1, 2);
    move_sprite(1, x + 8, y);
    SHOW_SPRITES;
}

void input() {
    if (joypad() & J_UP) {
        UBYTE tileX;
        UBYTE tileY;
        y--;
        tileX = (x-8)/8;
        tileY = (y-16)/8;
        if(collmap[tileX][tileY] != 0x00) {
            y++;
        }
    }
    if (joypad() & J_DOWN) {
        UBYTE tileX;
        UBYTE tileY;
        y++;
        tileX = (x-8)/8;
        tileY = (y-16)/8;
        if(collmap[tileX][tileY] != 0x00) {
            y--;
        }
    }
    if (joypad() & J_LEFT) {
        UBYTE tileX;
        UBYTE tileY;
        set_sprite_tile(0, 0);
        set_sprite_tile(1, 2);
        scroll_bkg(-1, 0);
        tileX = (vx-8)/8;
        tileY = (y-16)/8;
        vx++;
        //gprintf("%d %d", tileX, tileY);
        if(collmap[tileX][tileY] != 0x00) {
            scroll_bkg(1, 0);
            vx--;
        }
    }
    if (joypad() & J_RIGHT) {
        UBYTE tileX;
        UBYTE tileY;
        set_sprite_tile(0, 4);
        set_sprite_tile(1, 6);
        scroll_bkg(1, 0);
        vx++;
        tileX = (vx-8)/8;
        tileY = (y-16)/8;
        //gprintf("tx:%d ty:%d vx:%d ", tileX, tileY, vx);
        //gprintf("%d\n", collmap);
        if(collmap[tileX][tileY] != 0x00) {
            scroll_bkg(-1, 0);
            vx--;
        }
    }
}

void update() {
    time++;
    delay(20);
    move_sprite(0, x, y);
    move_sprite(1, x+8, y);
}

void draw() {
    SHOW_SPRITES;
    SHOW_BKG;
}

void init_collision_array() {
    UBYTE i;
    UBYTE j;
    for(i = 0; i < gameMapWidth; i++)
    {
        for(j = 0; j < gameMapHeight; j++)
        {
            collmap[i][j] = *(coll_map+i*gameMapHeight+j);
            //gprintf("%d", collmap[i][j]);
        }
    }
}

代码中有一些未使用的代码部分。 如何添加碰撞? 当我尝试这种解决方案时,子画面只会在墙壁上行走。其他提供崩溃或显示故障文本和地图的游戏。

0 个答案:

没有答案