From 7df632e645733e61db89aaeec095d989deedcd5b Mon Sep 17 00:00:00 2001 From: Anis <59984748+anistarafdar@users.noreply.github.com> Date: Mon, 15 Mar 2021 21:55:50 -0400 Subject: [PATCH 1/4] typedef'ed the struct and removed useless fields an easier way to refer to a structure that doesn't require copious use of the struct keyword in functions is to typedef it using the syntax "typedef struct {content} name;" also the width and heigth variables are useless for a structure thats only going to make 16x16 meta sprites so i got rid of it --- 09_metasprites/GameCharacter.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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; From c1d51fe03845b4fd639aabbe3c2c0deff628ac7f Mon Sep 17 00:00:00 2001 From: Anis <59984748+anistarafdar@users.noreply.github.com> Date: Mon, 15 Mar 2021 22:15:09 -0400 Subject: [PATCH 2/4] updated functions to work with new struct def and replaced repetitive code updated syntax for portions of code using the old version of the struct there was two functions that did the same thing so i condensed it into a reusable function that can be used for any 16x16 meta sprite GameCharacter --- 09_metasprites/main.c | 73 ++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 46 deletions(-) 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; From a79b7c18ecc27150b5b635f1d517e8a1adb88fde Mon Sep 17 00:00:00 2001 From: Anis <59984748+anistarafdar@users.noreply.github.com> Date: Mon, 15 Mar 2021 22:49:44 -0400 Subject: [PATCH 3/4] fixed UINT16 and INT16 conflict the resulting code is no different and the compiler didnt send a warning but i felt it was necessary to fix a conflict where a function calling playerlocation asked for a UINT16 but was given a signed INT16 --- 08_simplejumping/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From f9b97cd592704ed26fd928ac16813dc49d0ce391 Mon Sep 17 00:00:00 2001 From: Anis <59984748+anistarafdar@users.noreply.github.com> Date: Tue, 16 Mar 2021 00:00:55 -0400 Subject: [PATCH 4/4] attempted to fix readability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit im trying to help make this code a little more readable by using constants and good variable names but im not sure if im causing more issues than im solving with readability 😆 --- 10_sprite_collision/main.c | 84 +++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 46 deletions(-) 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;