From 8e048b5d34887d9d0247c452861f8ee8f5e7ee0e Mon Sep 17 00:00:00 2001 From: Massena Date: Sat, 13 Mar 2021 18:39:09 +0100 Subject: [PATCH] a lil' spawn for the player (wtf it's working?!) --- assets-cg/img/spike.png | Bin 0 -> 164 bytes include/main.h | 13 +++++++-- src/levels.c | 6 ++-- src/main.c | 59 ++++++++++++++++++++++++++++------------ 4 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 assets-cg/img/spike.png diff --git a/assets-cg/img/spike.png b/assets-cg/img/spike.png new file mode 100644 index 0000000000000000000000000000000000000000..d7afa620d1e8e024636f8db9fff1aace67afaa5b GIT binary patch literal 164 zcmeAS@N?(olHy`uVBq!ia0vp^JRr=$1|-8uW1a&k#^NA%Cx&(BWL^R}ah@)YAsQ36 zPITmJFyLTmKbHPQeebuMd%vmwWtY3+!Z9o1kqqY>J%!b!PaQV=*u^9=DJM?xfJJSQ z!KqR{#)!4m>~XTMWD@tzx%sDcks_ #include "main.h" -int collide_pixel(int x, int y, int level[LEVEL_SIZE][LEVEL_SIZE]) { - // Check if there's something in (x, y) - return level[x][y]; +struct Pos search(int x, int level[16][16]) { + // Search for x in a given matrix. + // If x is found, return it coordinates + struct Pos coordinates = {0, 0}; + for(int m = 0; m < LEVEL_SIZE; ++m) { + for(int n = 0; n < LEVEL_SIZE; ++n) { + if(level[m][n] == x) { + // idk why m and n are inversed but it works kek + coordinates.x = n; + coordinates.y = m; + return coordinates; + } + } + } + return coordinates; } -int collide(int x, int y, int level[LEVEL_SIZE][LEVEL_SIZE]) { +int collide_pixel(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) { + // Check if there's something in (x, y) + if(obj == level[x][y]) { + return 1; + } + else { + return 0; + } +} + +int collide(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) { // Check if there's something in // the square (x + 1, y + 1, x + 11, y + 11) - if( collide_pixel(x / TILE_SIZE, y / TILE_SIZE, level) || - collide_pixel(x / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, level) || - collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, y / TILE_SIZE, level) || - collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, level) + if( collide_pixel(x / TILE_SIZE, y / TILE_SIZE, obj, level) || + collide_pixel(x / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, obj, level) || + collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, y / TILE_SIZE, obj, level) || + collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, obj, level) ) { return 1; } @@ -29,9 +51,12 @@ int main(void) int running = 1; - // player (i don't know how to use struct sorry) - int player_x = 50; - int player_y = 50; + // player + struct Player player = {0, 0}; + struct Pos spawn = {0, 0}; + spawn = search(2, level); + player.x = spawn.x * 12; + player.y = spawn.y * 12; // main loop while(running) { @@ -39,12 +64,12 @@ int main(void) dclear(C_BLACK); // drawing the player - dimage(player_x, player_y, &img_player); + dimage(player.x, player.y, &img_player); // drawing the level for(int m = 0; m < LEVEL_SIZE; ++m) { for(int n = 0; n < LEVEL_SIZE; ++n) { - if(level[m][n]) { + if(level[m][n] == 1) { dimage(m * TILE_SIZE, n * TILE_SIZE, &img_wall); } } @@ -56,12 +81,12 @@ int main(void) clearevents(); // trying to move the player >w< - if(!collide(player_x + mov_x, player_y, level)) { - player_x += mov_x; + if(!collide(player.x + mov_x, player.y, 1, level)) { + player.x += mov_x; } - if(!collide(player_x, player_y + mov_y, level)) { - player_y += mov_y; + if(!collide(player.x, player.y + mov_y, 1, level)) { + player.y += mov_y; } if(keydown(KEY_EXIT)) {running = 0;}