Trouble registering screen boundaries for certain inputs (down and right)

61 views Asked by At

I am creating a scrolling shooter for DMG using gbdk, it is based off some youtube tutorials and this example. In fact the link is the skeleton of my program. My issue is that the screen boundary conditions aren't working properly for down and right inputs. For up and left, they work correctly however, and the code for those is basically the exact same. I have also compiled the code from the link above, and it works correctly there. Apologies in advance, I have a childish sense of humor, so the game is penis-based.

The main differences between the skeleton code and mine is that I use a meta-sprite for the player, and an array for the x and y coordinates of the player. I have tried using individual integers for the locations and changing the bounds of the screen, but nothing seems to work.

#include <gb/gb.h>
#include <stdio.h>
#include "gameDicks.c"
#include "DickSprites.c"

#define SCREEN_WIDTH 160

BOOLEAN ishard = TRUE, playing = TRUE;
struct gameDicks flacid;
struct gameDicks hard;
INT8 spritesize = 8, dicklocation[2] = {20, 80};
int i;

void moveGameDicks(struct gameDicks* Dick, UINT8 x, UINT8 y){
    move_sprite(Dick->spriteids[0], x, y);
    move_sprite(Dick->spriteids[1], x + spritesize, y);
    move_sprite(Dick->spriteids[2], x, y + spritesize);
    move_sprite(Dick->spriteids[3], x + spritesize, y + spritesize);
}

void setuphard(INT8 dicklocation[2]){
    hard.x = dicklocation[0];
    hard.y = dicklocation[1];
    hard.width = 16;
    hard.height = 16;

    //load sprites

    set_sprite_tile(0,0);
    hard.spriteids[0] = 0;
    set_sprite_tile(1,1);
    hard.spriteids[1] = 1;
    set_sprite_tile(2,2);
    hard.spriteids[2] = 2;
    set_sprite_tile(3,3);
    hard.spriteids[3] = 3;

}   
void init_screen()
{
   SHOW_BKG;
   SHOW_SPRITES;
   DISPLAY_ON;
}

void init_player()
{
 SHOW_SPRITES;
 set_sprite_data(0, 8, DickSprites);
 setuphard(dicklocation);  
}

void input()
{
    if (joypad() & J_UP && dicklocation[1])
    {
        if (dicklocation[1] <= 16){ 
            dicklocation[1] = 16;
        } 
        else{
            dicklocation[1]--; 
        }    
    }
    if (joypad() & J_DOWN && dicklocation[1])
    {
        if (dicklocation[1] >= 150){
            dicklocation[1] = 150;
        }    
        else{
            dicklocation[1]++;          
        }   
    }
}


void update_sprites()
{
        moveGameDicks(&hard, dicklocation[0], dicklocation[1]);
}

int main()
{
    init_screen();
    init_player();
    init_screen();


    while(playing)
    {
        wait_vbl_done(2); 
        input();
        update_sprites();
    }
    return 0;
}

What I expect is to be able to move the player up to y = 16, and down to y = 150. When it hits these values, it stops moving until you go the other direction. Instead, what I see happen is that the up direction works as expected, but as soon as the down key is pressed - no matter the y-location - the player is immediately sent to the bottom of the screen. From there, pressing up sends it to the very top. Further, the player can only move from the top position to the bottom, and not scroll in between. I'm baffled by this because the conditions are the exact same (except for the y-values), so I don't understand why they behave so differently.

1

There are 1 answers

1
GMoney On BEST ANSWER

Using an unsigned int may help here as an 8-bit integer will only hold values from -128 to 127, which might cause undefined behaviour when you compare it with over 150, pushing it to a negative value?

You have defined dicklocation as an INT8, when it would be better as a UINT8 or even longer if you plan on ever having a screen size larger than 255 bytes.