1

Sorry if this question has already been asked, but I have not managed to find any advice on the internet for my issue. I am currently trying to program a little game on the Nintendo DS, in which the player has to move a sprite (currently a square) until it reaches the exit. For this, I use a sprite I have included using a grit file, and also a background enabled in tiled mode. However, I am having a problem when it comes to checking if the sprite is going to collide with a wall. Here is the code I have both for the background configuration (where I declare the tiles and the map) and also for the sprite movements (I didn't add the condition for all cases yet, as it didn't work well) :

void configureMaze_Sub() {

int row, col;

for (row = 0; row < 24; row ++) {
    for (col = 0; col < 32; col ++) {
        BG_MAP_RAM_SUB(3)[row * 32 + col] = 1;

        if (col == 15 && (row != 12 && row !=4 && row != 19)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

        if ((row == 1 || row == 22) && (col > 2 && col < 29)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

        if ((col == 3 || col == 28) && (row > 1 && row < 22 && row != 12)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

        if ((row == 3 || row == 20) && (col > 4 && col < 27)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

        if ((col == 5 || col == 26) && (row != 9 && row != 15 && row > 3 && row < 20)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

        if (row == 8 && (col > 5 && col < 15)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

        if (row == 16 && (col > 15 && col < 26)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

        if ((row == 12) && (col > 5 && col < 26)) {
            BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
        }

    }
}

}

void gameplayMaze() {

int x = 103, y = 41, keys;
int maze_success = 0;

while (maze_success == 0) {

    scanKeys();
    keys = keysHeld();
    int xmod = x / 8;
    int ymod = x / 8;

    if ((keys & KEY_RIGHT) && BG_MAP_RAM_SUB(3)[xmod + 32 * ymod] == 1) { 
        x++;
        printf("%d \n", x);
    }

    if ((keys & KEY_LEFT) && BG_MAP_RAM_SUB(3)[xmod + 32 * ymod] == 1) {
        x--;
    }

    if (keys & KEY_UP) {
        y--;
    }

    if (keys & KEY_DOWN) {
        y++;
    }

    oamSet(&oamSub,
        0,
        x, y,
        0,
        0,
        SpriteSize_8x8,
        SpriteColorFormat_256Color,
        gfxSub,
        -1,
        false,
        false,
        false, false,
        false
        );

    swiWaitForVBlank();

    oamUpdate(&oamSub);
}

The main problem I have is to try to change from the coordinates of the tiles (which are 8x8) to the ones of the map, as for the coordinates of the sprite (256x192). If any of you have any hint to help me, I would be very grateful! I am still new to programming on the NDS, so I am still struggling to get the hang of it.

Joris R.
  • 11
  • 3
  • Here's a tip: extract the coordinate transformation logic in a method/function, make sure it works and use it everywhere. No more obscure multiplications all over the code. – Sergio Tulentsev Dec 24 '17 at 23:34
  • Also, you seem to be missing boundary checks for vertical movement. – Sergio Tulentsev Dec 24 '17 at 23:37
  • Sergio : do you mean I should have a function, which would for instance check the available movements for the sprite, instead of having all the code in the same function? I tried to do it, but my way of checking the boundaries doesn't seem to work. By adding it for the horizontal movement, I can't move the sprite neither left nor right, even if it is on a floor tile – Joris R. Dec 25 '17 at 00:08

0 Answers0