Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 08_simplejumping/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gb/gb.h>
#include <stdio.h>

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;
Expand Down
6 changes: 2 additions & 4 deletions 09_metasprites/GameCharacter.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include <gb/gb.h>

//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;
};
} GameCharacter;
73 changes: 27 additions & 46 deletions 09_metasprites/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
84 changes: 38 additions & 46 deletions 10_sprite_collision/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down