diff --git a/l0dables/Makefile b/l0dables/Makefile index 2b35a3b7..5c732895 100644 --- a/l0dables/Makefile +++ b/l0dables/Makefile @@ -1,4 +1,4 @@ -C1D=cube.c1d blinky.c1d invaders.c1d mandel.c1d snake.c1d snake2.c1d bricks.c1d schedule.c1d ws2812b.c1d battery.c1d fire.c1d colorp.c1d tetris.c1d sysinfo.c1d colors.c1d 0xb.c1d +C1D=cube.c1d blinky.c1d invaders.c1d mandel.c1d snake.c1d bricks.c1d schedule.c1d ws2812b.c1d battery.c1d fire.c1d colorp.c1d tetris.c1d sysinfo.c1d colors.c1d 0xb.c1d N1K=nick_scr0ll.n1k nick_w0rpcore.n1k nick_matrix.n1k nick_plain.n1k nick_invaders.n1k nick_anim.n1k nick_image.n1k nick_netz39.n1k nick_life.n1k nick_colplasm.n1k nick_ledbow.n1k diff --git a/l0dables/snake.c b/l0dables/snake.c index 8d03f047..04fd6fed 100644 --- a/l0dables/snake.c +++ b/l0dables/snake.c @@ -3,6 +3,7 @@ ******** * a snake clone for the r0ket * created by Flori4n (DrivenHoliday) & MascH (CCCHB tent) + * snake II implementation by ElSjaako and OriginalSouth ***************************************/ #include @@ -14,6 +15,7 @@ #include #include #include +#include #include "invfont.c" @@ -23,10 +25,10 @@ #define SNAKE_DIM (3) #define MIN_SPEED (25) #define MAX_SPEED (3) -#define MIN_X 2 -#define MAX_X (RESX-3) -#define MIN_Y 8 -#define MAX_Y (RESY-2) +#define MIN_X 3 +#define MAX_X (RESX-5) +#define MIN_Y 12 +#define MAX_Y (RESY-5) #define SIZE_X ((MAX_X-MIN_X)/SNAKE_DIM) #define SIZE_Y ((MAX_Y-MIN_Y)/SNAKE_DIM) @@ -35,6 +37,14 @@ #define UP 3 #define DOWN 1 +//define how long you have to hold down the button to quit +#define QUIT_DELAY 1000 + +typedef enum { + SNAKE_STANDARD, + SNAKE_WRAPPING +} gametype_e; + struct pos_s { int x,y; }; @@ -48,7 +58,7 @@ static void reset(); static void next_level(); static void render_level(); static void draw_block(); -static void handle_input(); +static int handle_input(); static void death_anim(); static struct pos_s getFood(void); static int hitWall(); @@ -57,70 +67,114 @@ static int hitSelf(); static void renderHighscore(); static int showHighscore(); static uint32_t highscore_get(); +static char* highscore_filename(); int points = 0; int highscore = 0; +gametype_e gametype = SNAKE_STANDARD; struct snake_s snake = { NULL, 3, 0, MIN_SPEED, 2}; struct pos_s food; void ram(void) { - int c=0, pos=0,del=0; + while (1) { - struct pos_s tail[MAX_SNAKE_LEN]; - snake.tail = tail; + int c=0, pos=0,del=0; - // load the highscore - highscore = highscore_get(); + struct pos_s tail[MAX_SNAKE_LEN]; + snake.tail = tail; - // initially reset everything - reset(); + lcdClear(); - while (1) { - if(!(++c % snake.speed)) { - handle_input(); - - pos = (snake.t_start+1) % MAX_SNAKE_LEN; - snake.tail[pos].x = snake.tail[snake.t_start].x; - snake.tail[pos].y = snake.tail[snake.t_start].y; - - if(snake.dir == 0) - snake.tail[pos].x++; - else if(snake.dir == 1) - snake.tail[pos].y++; - else if(snake.dir == 2) - snake.tail[pos].x--; - else if(snake.dir == 3) - snake.tail[pos].y--; - - snake.t_start = pos; - - if (pos < snake.len) { - del = MAX_SNAKE_LEN - (snake.len - pos); - } else - del = pos - snake.len; + setTextColor(0xff,0b11100000); + DoString(0,10, " SNAKE"); - // remove last, add first line - draw_block(snake.tail[del].x, snake.tail[del].y, 0xFF); - draw_block(snake.tail[pos].x, snake.tail[pos].y, 0b00011000); + setTextColor(0xff,0x00); + DoString(0,RESY/2-33, " Choose the"); + DoString(0,RESY/2-25, " game type:"); - // check for obstacle hit.. - if (hitWall() || hitSelf()) { - death_anim(); - if (showHighscore()) - break; - reset(); - } else if (hitFood()) - next_level(); + setTextColor(0xff,0x00); + DoString(0, RESY/2+10, " LEFT: standard"); + DoString(0, RESY/2+18, " RIGHT: wrapping "); + DoString(0, RESY/2+26, " DOWN: quit "); - lcdDisplay(); + lcdDisplay(); + + int key = getInputRaw(); + + while(1) { + key = getInputWait(); + getInputWaitRelease(); + + if (key&BTN_DOWN) { + return; + } else if (key&BTN_LEFT) { + gametype = SNAKE_STANDARD; + break; + } else if (key&BTN_RIGHT) { + gametype = SNAKE_WRAPPING; + break; + } } + // load the highscore + highscore = highscore_get(); + + // initially reset everything + reset(); + + while (1) { + if(!(++c % snake.speed)) { + if (handle_input()) { //handle_input returns 1 to quit + break; + } + + pos = (snake.t_start+1) % MAX_SNAKE_LEN; + snake.tail[pos].x = snake.tail[snake.t_start].x; + snake.tail[pos].y = snake.tail[snake.t_start].y; + + if(snake.dir == 0) { + snake.tail[pos].x++; + if(gametype == SNAKE_WRAPPING && snake.tail[pos].x>SIZE_X) snake.tail[pos].x=0; + } else if(snake.dir == 1) { + snake.tail[pos].y++; + if(gametype == SNAKE_WRAPPING && snake.tail[pos].y>SIZE_Y) snake.tail[pos].y=0; + } else if(snake.dir == 2) { + snake.tail[pos].x--; + if(gametype == SNAKE_WRAPPING && snake.tail[pos].x<0) snake.tail[pos].x=SIZE_X; + } else if(snake.dir == 3) { + snake.tail[pos].y--; + if(gametype == SNAKE_WRAPPING && snake.tail[pos].y<0) snake.tail[pos].y=SIZE_Y; + } + + snake.t_start = pos; + + del = pos - snake.len; + if (pos < snake.len) del += MAX_SNAKE_LEN; + + // remove last, add first line + draw_block(snake.tail[del].x, snake.tail[del].y, 0xFF); + draw_block(snake.tail[pos].x, snake.tail[pos].y, 0b00011000); + + // check for obstacle hit.. + if (hitWall() || hitSelf()) { + death_anim(); + if (showHighscore()) + break; + highscore = highscore_get(); + reset(); + } else if (hitFood()) + next_level(); + + lcdDisplay(); + } + #ifdef SIMULATOR - delayms(50); + delayms(50); #else - delayms(3); + delayms(3); #endif + } } } @@ -130,8 +184,8 @@ static struct pos_s getFood(void) struct pos_s res; tryagain: - res.x = (getRandom() % (SIZE_X-1)) + 1; - res.y = (getRandom() % (SIZE_Y-3)) + 3; + res.x = (getRandom() % (SIZE_X+1)); + res.y = (getRandom() % (SIZE_Y+1)); for(i=0; i quitWhen) { + return 1; //indicate program should quit + } + return 0; + } + quitWhen = 0; + return 0; // program should continue running } static int hitWall() { - return ( (snake.tail[snake.t_start].x*3 <= MIN_X) - || (snake.tail[snake.t_start].x*3 >= MAX_X) - || (snake.tail[snake.t_start].y*3 <= MIN_Y) - || (snake.tail[snake.t_start].y*3 >= MAX_Y) ) ? - 1 : 0; + if (gametype == SNAKE_WRAPPING) return false; + + return ( (snake.tail[snake.t_start].x < 0) + || (snake.tail[snake.t_start].x > SIZE_X) + || (snake.tail[snake.t_start].y < 0) + || (snake.tail[snake.t_start].y > SIZE_Y) ); } @@ -300,9 +376,19 @@ static void death_anim() } +static char* highscore_filename() +{ + switch (gametype) { + case SNAKE_WRAPPING: + return "snake2.5cr"; + default: + return "snake.5cr"; + } +} + static bool highscore_set(uint32_t score) { - writeFile("snake.5cr", &score , sizeof(uint32_t)); + writeFile(highscore_filename(), &score , sizeof(uint32_t)); // old r0ket code to get highscore from the world #if 0 @@ -323,7 +409,7 @@ static bool highscore_set(uint32_t score) static uint32_t highscore_get() { uint32_t score = 0; - readFile("snake.5cr", &score, sizeof(score)); + readFile(highscore_filename(), &score, sizeof(score)); // old r0ket code to send highscore to the world #if 0 @@ -364,9 +450,9 @@ static void renderHighscore() setTextColor(0xff,0b00000011); DoString(RESX/2-4, RESY/2-2, IntToStr(highscore,6,0)); setTextColor(0xff,0x00); - DoString(0, RESY/2+18, " UP to play "); + DoString(0, RESY/2+18, " UP to play"); DoString(0, RESY/2+26, "RIGHT to reset HI "); - DoString(0, RESY/2+34, "DOWN to quit "); + DoString(0, RESY/2+34, " DOWN to quit "); lcdDisplay(); } diff --git a/l0dables/snake2.c b/l0dables/snake2.c deleted file mode 100644 index d11f0e55..00000000 --- a/l0dables/snake2.c +++ /dev/null @@ -1,340 +0,0 @@ -/* - * snake - ******** - * a snake clone for the r0ket - * created by Flori4n (DrivenHoliday) & MascH (CCCHB tent) - * snake II implementation by ElSjaako and OriginalSouth - ***************************************/ - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "invfont.c" - -#include "usetable.h" - -#define MAX_SNAKE_LEN (40) -#define SNAKE_DIM (3) -#define MIN_SPEED (25) -#define MAX_SPEED (3) -#define MIN_X 5 -#define MAX_X (RESX-4) -#define MIN_Y 8 -#define MAX_Y (RESY-4) -#define SIZE_X ((MAX_X-MIN_X)/SNAKE_DIM) -#define SIZE_Y ((MAX_Y-MIN_Y)/SNAKE_DIM) - -#define RIGHT 0 -#define LEFT 2 -#define UP 3 -#define DOWN 1 - -struct pos_s { - int x,y; -}; - -struct snake_s { - struct pos_s *tail; - int len, dir, speed, t_start; -}; - -static void reset(); -static void next_level(); -static void draw_block(); -static void handle_input(); -static void death_anim(); -static struct pos_s getFood(void); -static int hitWall(); -static int hitFood(); -static int hitSelf(); -static int showHighscore(); - -int points = 0; -struct snake_s snake = { NULL, 3, 0, MIN_SPEED, 2}; -struct pos_s food; - -void ram(void) -{ - int c=0, pos=0,del=0; - - struct pos_s tail[MAX_SNAKE_LEN]; - snake.tail = tail; - - // initially reset everything - reset(); - - while (1) - { - if(!(++c % snake.speed)) { - handle_input(); - - pos = (snake.t_start+1) % MAX_SNAKE_LEN; - snake.tail[pos].x = snake.tail[snake.t_start].x; - snake.tail[pos].y = snake.tail[snake.t_start].y; - - if(snake.dir == 0) - { - snake.tail[pos].x++; - if(snake.tail[pos].x*SNAKE_DIM>=MAX_X) snake.tail[pos].x=MIN_X/SNAKE_DIM+1; - } - else if(snake.dir == 1) - { - snake.tail[pos].y++; - if(snake.tail[pos].y*SNAKE_DIM>=MAX_Y) snake.tail[pos].y=MIN_Y/SNAKE_DIM+1; - } - else if(snake.dir == 2) - { - snake.tail[pos].x--; - if(snake.tail[pos].x*SNAKE_DIM<=MIN_X) snake.tail[pos].x=MAX_X/SNAKE_DIM-1; - } - else if(snake.dir == SNAKE_DIM) - { - snake.tail[pos].y--; - if(snake.tail[pos].y*SNAKE_DIM<=MIN_Y) snake.tail[pos].y=MAX_Y/SNAKE_DIM-1; - } - - snake.t_start = pos; - - del = pos - snake.len; - if (pos < snake.len) del += MAX_SNAKE_LEN; - - // remove last, add first line - draw_block(snake.tail[del].x, snake.tail[del].y, 0xFF); - draw_block(snake.tail[pos].x, snake.tail[pos].y, 0b00011000); - - // check for obstacle hit.. - if (hitSelf()) { - death_anim(); - if (showHighscore()) - break; - reset(); - } - else if (hitFood()) - next_level(); - - lcdDisplay(); - } - -#ifdef SIMULATOR - delayms(50); -#else - delayms(3); -#endif - } -} - -static struct pos_s getFood(void) -{ - int i,pos; - struct pos_s res; - -tryagain: - res.x = (getRandom() % (SIZE_X-1)) + 1; - res.y = (getRandom() % (SIZE_Y-3)) + 3; - - for(i=0; i= MAX_SPEED) - snake.speed--; - setTextColor(0xff,0b11100000); - DoString(0,0,IntToStr(++points,6,0)); -} - -static void handle_input() -{ - int key = getInputRaw(), dir_old = snake.dir; - - if (key&BTN_UP && dir_old != 1) - snake.dir = 3; - else if (key&BTN_DOWN && dir_old != 3) - snake.dir = 1; - else if (key&BTN_LEFT && dir_old != 0) - snake.dir = 2; - else if (key&BTN_RIGHT && dir_old !=2) - snake.dir = 0; -} - -static int hitSelf() -{ - int i, pos; - for (i=1; ipkt)>score) - return false; - - MO_TIME_set(mpkt->pkt,score); - strcpy((char*)MO_BODY(mpkt->pkt),nick); - if(GLOBAL(privacy)==0) { - uint32touint8p(GetUUID32(),mpkt->pkt+26); - mpkt->pkt[25]=0; - }; -#endif - return true; -} - -static uint32_t highscore_get(char nick[]){ -#if 0 - MPKT * mpkt= meshGetMessage('s'); - char * packet_nick = (char*)MO_BODY(mpkt->pkt); - // the packet crc end is already zeroed - if(MAXNICKpkt); -#endif - return 0; -} - -static int showHighscore() -{ - int key = getInputRaw(); //throw away pending keypress - char nick[20]; - uint32_t score = 0; - - highscore_set(points,GLOBAL(nickname)); - score = highscore_get(nick); - - lcdClear(); - setTextColor(0xff,0x00); - DoString(0,RESY/2-33, " Your Score"); - // Display own score in green color, if it's higher than high score, else red - if (points > score) { - setTextColor(0xff,0b00011100); - } else { - setTextColor(0xff,0b11100000); - } - DoString(RESX/2-4, RESY/2-25, IntToStr(points,6,0)); - setTextColor(0xff,0x00); - DoString(0,RESY/2-10, " Highscore"); - setTextColor(0xff,0b11100000); - DoString(RESX/2-4, RESY/2-2, IntToStr(score,6,0)); - setTextColor(0xff,0x00); - DoString(0, RESY/2+18, " UP to play "); - DoString(0, RESY/2+26, "DOWN to quit "); - - lcdDisplay(); - - while(1) { - key = getInputRaw(); - if (key&BTN_DOWN) { - return 1; - } else if (key&BTN_UP) { - return 0; - } - } -} - -static int hitFood() -{ - return ((snake.tail[snake.t_start].x == food.x) && (snake.tail[snake.t_start].y == food.y)) ? 1 : 0; -}