Skip to content
Closed
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
32 changes: 29 additions & 3 deletions l0dables/snake2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <r0ketlib/print.h>
#include <r0ketlib/keyin.h>
#include <r0ketlib/itoa.h>
#include <rad1olib/systick.h>

#include "invfont.c"

Expand All @@ -36,6 +37,9 @@
#define UP 3
#define DOWN 1

//define how long you have to hold down the button to quit
#define QUIT_DELAY 1000

struct pos_s {
int x,y;
};
Expand All @@ -48,7 +52,7 @@ struct snake_s {
static void reset();
static void next_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();
Expand All @@ -73,7 +77,9 @@ void ram(void)
while (1)
{
if(!(++c % snake.speed)) {
handle_input();
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;
Expand Down Expand Up @@ -145,6 +151,15 @@ static struct pos_s getFood(void)
goto tryagain;
}

// make sure new food is in playing field
// Quick and dirty, but it works.
if ((res.x*SNAKE_DIM <= MIN_X) ||
(res.x*SNAKE_DIM >= MAX_X) ||
(res.y*SNAKE_DIM <= MIN_Y) ||
(res.y*SNAKE_DIM >= MAX_Y)) {
goto tryagain;
}

return res;
}

Expand Down Expand Up @@ -218,9 +233,10 @@ static void next_level()
DoString(0,0,IntToStr(++points,6,0));
}

static void handle_input()
static int handle_input()
{
int key = getInputRaw(), dir_old = snake.dir;
static int quitWhen = 0;

if (key&BTN_UP && dir_old != 1)
snake.dir = 3;
Expand All @@ -230,6 +246,16 @@ static void handle_input()
snake.dir = 2;
else if (key&BTN_RIGHT && dir_old !=2)
snake.dir = 0;
else if (key&BTN_ENTER) {
if (quitWhen == 0){
quitWhen = _timectr + QUIT_DELAY/SYSTICKSPEED;
} else if (_timectr > quitWhen) {
return 1; //indicate program should quit
}
return 0;
}
quitWhen = 0;
return 0; // program should continue running
}

static int hitSelf()
Expand Down