From 63f5536ccf2f439036b4e130c75519d63ef0e77c Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sat, 28 Jan 2012 18:47:53 +0100 Subject: [PATCH] Add new/better snake.c from git://github.com/MascHman/r0ket.git --- firmware/applications/final/mesh.c | 6 + firmware/l0dable/snake.c | 322 +++++++++++++++++++++++++++++ firmware/l0dable/snake.c.disabled | 194 ----------------- 3 files changed, 328 insertions(+), 194 deletions(-) create mode 100644 firmware/l0dable/snake.c delete mode 100644 firmware/l0dable/snake.c.disabled diff --git a/firmware/applications/final/mesh.c b/firmware/applications/final/mesh.c index 63fb4c1..f2692f0 100644 --- a/firmware/applications/final/mesh.c +++ b/firmware/applications/final/mesh.c @@ -186,6 +186,9 @@ void m_choose(){ case('r'): strcpy(p,"r0type"); break; + case('s'): + strcpy(p,"Snake"); + break; #endif default: p[0]=*mm; @@ -233,6 +236,9 @@ void m_choose(){ case('r'): strcpy(p,"r0type"); break; + case('s'): + strcpy(p,"Snake"); + break; #endif }; if(tmm[i]>='a' && tmm[i]<='z'){ diff --git a/firmware/l0dable/snake.c b/firmware/l0dable/snake.c new file mode 100644 index 0000000..ce7a56c --- /dev/null +++ b/firmware/l0dable/snake.c @@ -0,0 +1,322 @@ +/* + * snake + ******** + * a snake clone for the r0ket + * created by Flori4n (DrivenHoliday) & MascH (CCCHB tent) + ***************************************/ + +#include +#include + +#include "basic/basic.h" +#include "basic/config.h" +#include "basic/random.h" +#include "lcd/render.h" +#include "lcd/display.h" +#include "lcd/fonts.h" +#include "lcd/fonts/invaders.h" +//#Include "lcd/lcd.h" +//#include "lcd/print.h" +#include "funk/mesh.h" +#include "usetable.h" + +#define MAX_SNAKE_LEN (40) +#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 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++; + 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; + + // remove last, add first line + draw_block(snake.tail[del].x, snake.tail[del].y, 0); + draw_block(snake.tail[pos].x, snake.tail[pos].y, 1); + + // check for obstacle hit.. + if (hitWall() || 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_X) + || (snake.tail[snake.t_start].y*3 <= MIN_Y) + || (snake.tail[snake.t_start].y*3 >= MAX_Y) ) ? + 1 : 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; + }; + return true; +} + +static uint32_t highscore_get(char nick[]){ + MPKT * mpkt= meshGetMessage('s'); + char * packet_nick = (char*)MO_BODY(mpkt->pkt); + // the packet crc end is already zeroed + if(MAXNICKpkt); +} + +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(); + DoString(0,RESY/2-33, " Your Score"); + DoInt(RESX/2-4, RESY/2-25, points); + DoString(0,RESY/2-10, " Highscore"); + DoInt(RESX/2-4, RESY/2-2, score); + 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; +} diff --git a/firmware/l0dable/snake.c.disabled b/firmware/l0dable/snake.c.disabled deleted file mode 100644 index 9422712..0000000 --- a/firmware/l0dable/snake.c.disabled +++ /dev/null @@ -1,194 +0,0 @@ -#include -#include "basic/basic.h" -#include "basic/config.h" - -#include "lcd/lcd.h" -#include "lcd/print.h" -#include "usetable.h" - -struct elem -{ - int x,y; -}; - -void reset(struct elem snake[],size_t *snake_size,int *dirc, -int*speed, int*points,int*point_s); -void o_rectangle (int x0, int y0, int width, int height); -struct elem rnd(void); - -#define MAX_SNAKE_LEN (40) -#define SNAKE_DEM (3) -#define MIN_SPEED (25) -#define MAX_SPEED (3) -#define SIZE_X (RESX) -#define SIZE_Y (RESY) - -void ram(void) -{ - int inpt,dirc,c,grows = 0,dx,dy,points,point_s=1; - size_t n = 0, snake_size = 5, speed=MIN_SPEED; - struct elem snake[MAX_SNAKE_LEN], food; - char test[512]; /* scratch space */ - o_init (test, sizeof(test)); - - reset(snake,&snake_size,&dirc,&speed,&points,&point_s); - - food = rnd(); - - while (1) - { - head: - if(!(++c % speed)) - { - - -inpt = getInputRaw(); - -dx=DoString(0,0,IntToStrX(points,2)); - dx=(SIZE_X-dx)/2; - if(dx<0) - dx=0; - dy=(SIZE_Y-getFontHeight())/2; - - lcdFill(255); - o_rectangle(1,0,SIZE_X-2,SIZE_Y-2); - o_set_gray (0); - o_fill (); - - //o_identity (); /* reset tranforms */ - - o_set_gray (50); - - setExtFont("UBUNTU29"); - - lcdSetPixel(1,1,1); - DoString(dx,dy,IntToStrX(points,2)); - - o_set_gray (255); - - - for(n=0;n SIZE_Y/SNAKE_DEM-1 || -t.x > SIZE_X/SNAKE_DEM) - { - reset(snake,&snake_size,&dirc,&speed,&points,&point_s); - goto head; - } - - for(n=0;n MAX_SPEED) --speed; - food = rnd(); - } - } - - if(!grows) - { - for(n=0;n