diff --git a/08_simplejumping/main.c b/08_simplejumping/main.c index 8a3fe1a..ec6e6be 100644 --- a/08_simplejumping/main.c +++ b/08_simplejumping/main.c @@ -1,7 +1,7 @@ #include #include -INT16 playerlocation[2]; // stores two INT16 x and y position of player +UINT16 playerlocation[2]; // stores two UINT16 x and y position of player BYTE jumping; INT8 gravity = -2; INT16 currentspeedY; diff --git a/09_metasprites/GameCharacter.c b/09_metasprites/GameCharacter.c index b895813..17f8024 100644 --- a/09_metasprites/GameCharacter.c +++ b/09_metasprites/GameCharacter.c @@ -1,10 +1,8 @@ #include //generical character structure: id, position, graphics -struct GameCharacter { +typedef struct { UBYTE spritids[4]; // all characters use 4 sprites UINT8 x; UINT8 y; - UINT8 width; - UINT8 height; -}; \ No newline at end of file +} GameCharacter; diff --git a/09_metasprites/main.c b/09_metasprites/main.c index 880dcfa..5bfc791 100644 --- a/09_metasprites/main.c +++ b/09_metasprites/main.c @@ -3,66 +3,47 @@ #include "GameCharacter.c" #include "GameSprites.c" -struct GameCharacter ship; -struct GameCharacter bug; -UBYTE spritesize = 8; +#define SPRITE_SIZE 8 + +GameCharacter ship; +GameCharacter bug; void performantdelay(UINT8 numloops){ - UINT8 i; - for(i = 0; i < numloops; i++){ + for(UINT8 i = 0; i < numloops; i++){ wait_vbl_done(); } } -void movegamecharacter(struct GameCharacter* character, UINT8 x, UINT8 y){ +void movegamecharacter(GameCharacter* character, UINT8 x, UINT8 y){ + // implies that four tiles from the value of tile_start and up are for the same meta sprite + // and are arranged clockwise, starting from the top left 8x8 quadrant move_sprite(character->spritids[0], x, y); - move_sprite(character->spritids[1], x + spritesize, y); - move_sprite(character->spritids[2], x, y + spritesize); - move_sprite(character->spritids[3], x + spritesize, y + spritesize); + move_sprite(character->spritids[1], x + SPRITE_SIZE, y); + move_sprite(character->spritids[2], x, y + SPRITE_SIZE); + move_sprite(character->spritids[3], x + SPRITE_SIZE, y + SPRITE_SIZE); } -void setupship(){ - ship.x = 80; - ship.y = 130; - ship.width = 16; - ship.height = 16; - - // load sprites for ship - set_sprite_tile(0, 0); - ship.spritids[0] = 0; - set_sprite_tile(1, 1); - ship.spritids[1] = 1; - set_sprite_tile(2, 2); - ship.spritids[2] = 2; - set_sprite_tile(3, 3); - ship.spritids[3] = 3; - - movegamecharacter(&ship, ship.x, ship.y); -} +void setup_GameCharacter(GameCharacter* character, UINT8 x_position, UINT8 y_position, UINT8 tile_start){ + character->x = x_position; + character->y = y_position; + + // load sprites for sprite + // starts reading from tile_start and increments up by one to retrieve sprite ids + for(UINT8 i=0; i<4; i++) { + set_sprite_tile(tile_start, tile_start); + character->spritids[i] = tile_start; -void setupbug(){ - bug.x = 30; - bug.y = 0; - bug.width = 16; - bug.height = 16; - - // load sprites for bug - set_sprite_tile(4, 4); - bug.spritids[0] = 4; - set_sprite_tile(5, 5); - bug.spritids[1] = 5; - set_sprite_tile(6, 6); - bug.spritids[2] = 6; - set_sprite_tile(7, 7); - bug.spritids[3] = 7; - - movegamecharacter(&bug, bug.x, bug.y); + tile_start++; + } + + movegamecharacter(character, character->x, character->y); } void main(){ set_sprite_data(0, 8, GameSprites); - setupship(); - setupbug(); + + setup_GameCharacter(&ship, 80, 130, 0); + setup_GameCharacter(&bug, 30, 0, 4); SHOW_SPRITES; DISPLAY_ON; diff --git a/10_sprite_collision/main.c b/10_sprite_collision/main.c index 7d33630..f2adfe7 100644 --- a/10_sprite_collision/main.c +++ b/10_sprite_collision/main.c @@ -3,70 +3,62 @@ #include "GameCharacter.c" #include "GameSprites.c" +#define SPRITE_SIZE 8 +#define GAMECHARACTER_SIZE 16 + GameCharacter ship; GameCharacter bug; -UBYTE spritesize = 8; void performantdelay(UINT8 numloops){ - UINT8 i; - for(i = 0; i < numloops; i++){ + for(UINT8 i = 0; i < numloops; i++){ wait_vbl_done(); } } -UBYTE checkcollisions(GameCharacter* one, GameCharacter* two){ - return (one->x >= two->x && one->x <= two->x + two->width) && (one->y >= two->y && one->y <= two->y + two->height) || (two->x >= one->x && two->x <= one->x + one->width) && (two->y >= one->y && two->y <= one->y + one->height); -} +// takes two GameCharacter's and return's true if the sprites overlap +UBYTE checkcollisions(GameCharacter* one, GameCharacter* two){ + // the width and height of a box is always the same and for this box its always 16 + + return (one->x >= two->x && one->x <= two->x + GAMECHARACTER_SIZE) + && (one->y >= two->y && one->y <= two->y + GAMECHARACTER_SIZE) + // check if the first sprite x and y position is either on or inside the borders of the second sprite + || + + (two->x >= one->x && two->x <= one->x + GAMECHARACTER_SIZE) + && (two->y >= one->y && two->y <= one->y + GAMECHARACTER_SIZE); + // or check if the second sprite is inside the second +} void movegamecharacter(GameCharacter* character, UINT8 x, UINT8 y){ + // implies that four tiles from the value of tile_start and up are for the same meta sprite + // and are arranged clockwise, starting from the top left 8x8 quadrant move_sprite(character->spritids[0], x, y); - move_sprite(character->spritids[1], x + spritesize, y); - move_sprite(character->spritids[2], x, y + spritesize); - move_sprite(character->spritids[3], x + spritesize, y + spritesize); + move_sprite(character->spritids[1], x + SPRITE_SIZE, y); + move_sprite(character->spritids[2], x, y + SPRITE_SIZE); + move_sprite(character->spritids[3], x + SPRITE_SIZE, y + SPRITE_SIZE); } -void setupship(){ - ship.x = 80; - ship.y = 130; - ship.width = 16; - ship.height = 16; - - // load sprites for ship - set_sprite_tile(0, 0); - ship.spritids[0] = 0; - set_sprite_tile(1, 1); - ship.spritids[1] = 1; - set_sprite_tile(2, 2); - ship.spritids[2] = 2; - set_sprite_tile(3, 3); - ship.spritids[3] = 3; - - movegamecharacter(&ship, ship.x, ship.y); -} +void setup_GameCharacter(GameCharacter* character, UINT8 x_position, UINT8 y_position, UINT8 tile_start){ + character->x = x_position; + character->y = y_position; -void setupbug(){ - bug.x = 30; - bug.y = 0; - bug.width = 16; - bug.height = 16; - - // load sprites for bug - set_sprite_tile(4, 4); - bug.spritids[0] = 4; - set_sprite_tile(5, 5); - bug.spritids[1] = 5; - set_sprite_tile(6, 6); - bug.spritids[2] = 6; - set_sprite_tile(7, 7); - bug.spritids[3] = 7; - - movegamecharacter(&bug, bug.x, bug.y); + // load sprites for sprite + // starts reading from tile_start and increments up by one to retrieve sprite ids + for(UINT8 i=0; i<4; i++) { + set_sprite_tile(tile_start, tile_start); + character->spritids[i] = tile_start; + + tile_start++; + } + + movegamecharacter(character, character->x, character->y); } void main(){ set_sprite_data(0, 8, GameSprites); - setupship(); - setupbug(); + + setup_GameCharacter(&ship, 80, 130, 0); + setup_GameCharacter(&bug, 30, 0, 4); SHOW_SPRITES; DISPLAY_ON;