     head = new_ptr;
 }
 
-/* Remove the last item in the linked list */
-void remLast(){
+void 
+remLast(){
   coord *last;  
   temp = head;
   last = temp;

#define RESET  8

/* Pushbutton pins */
#define pbLeft 7
#define pbDown 6
#define pbUp 3
#define pbRight 2

/***********************************************************************
 * VARIABLES
 */

TFT myScreen = TFT(LCD_CS, DC, RESET);  /* x: 0-160  y: 0-128 */

int screenWidth = myScreen.width();
int screenHeight = myScreen.height();
int tileSize = 6;

long timestamp;
int growBy = 0;
int collision = 0;
int score = 0;
char scoreChars[5];


struct colour_struct{
  int r,g,b;
};
typedef struct colour_struct colour;

colour bgCol = {93,200,255};
colour snakeCol = {245, 78, 253};
colour foodCol = {253,230,62};

struct coord_struct{
  unsigned char x,y;
  struct coord_struct *next_ptr; 
};
typedef struct coord_struct coord;

coord *new_ptr;
coord *temp;
coord *head = NULL;

coord moveTo = {0,0};
coord foodTile = {5,4};

/***********************************************************************
 * FUNCTIONS
 */
void newFoodTile(){

  foodTile.x = int(5 + (random(0,24)*6));
  foodTile.y = int(4 + (random(0,19)*6));
}

void startScreen(){
  /* Reset variables/everything */
  foodTile.x = 5;
  foodTile.y = 4;
  collision = 0;
  score = 0;
  while(head){
    remLast();
  }
  moveTo.x = 0;
  moveTo.y = 0;
  int score = 0;
  int growBy = 0;
  
  /* Title */
//  PImage image;
/*  image = myScreen.loadImage("startscreen.bmp"); */
//  myScreen.image(image, 0, 0);
  
  myScreen.background(245, 78, 253);
  myScreen.setTextSize(4);
  myScreen.stroke(255,255,255);
  myScreen.text("Snake",10,10);
  myScreen.setTextSize(1);
  myScreen.text("press button to start",10, 80);
  
  while (moveTo.x == 0 && moveTo.y == 0){
    getInput();
    delay(5);
  }
  
  /* Background and framing */
  myScreen.stroke(0,0,0);
  myScreen.fill(bgCol.r,bgCol.g,bgCol.b);
  myScreen.rect(3,2,screenWidth-6,screenHeight-4);

  /* The snake and food data before the game starts */
  for (int i = 29; i<=53; i+=6){
    addFirst(i,52);
    drawSegment(head->x, head->y);
  }
  
  /* TODO: "random placement, not on forbidden tiles"-function */
//  foodTile.x = 5;
//  foodTile.y = 4;
  newFoodTile();
  drawFood(foodTile.x, foodTile.y);
}



/* Input the incremental move using the pushbuttons */
void getInput(){
    if (digitalRead(pbLeft) == HIGH){
      moveTo.x = -tileSize; 
      moveTo.y = 0;  
    }
    else if (digitalRead(pbDown) == HIGH){
      moveTo.x = 0; 
      moveTo.y = tileSize;  
    }
    else if (digitalRead(pbUp) == HIGH){
      moveTo.x = 0; 
      moveTo.y = -tileSize;  
    }
    else if (digitalRead(pbRight) == HIGH){
      moveTo.x = tileSize; 
      moveTo.y = 0;  
    }
}

/* Draw a segment of the snake */
void drawSegment(int x, int y){
  myScreen.noStroke();
  myScreen.fill(snakeCol.r, snakeCol.g, snakeCol.b);
  myScreen.rect(x,y,tileSize,tileSize);
}

/* Draw a food */
void drawFood(int x, int y){
  myScreen.noStroke();
  myScreen.fill(foodCol.r, foodCol.g, foodCol.b);
  myScreen.rect(x,y,tileSize,tileSize); 
}

/* "Erase" a segment of the snake */
void remSegment(int x, int y){
  myScreen.noStroke();
  myScreen.fill(bgCol.r, bgCol.g, bgCol.b);
  myScreen.rect(x,y,tileSize,tileSize);
}

/* Add x and y as a coord first in the linked list */
void addFirst(int x, int y){
    new_ptr = (coord *)malloc(sizeof(coord));
    new_ptr->x = x;
    new_ptr->y = y;

    free(temp);
    head = NULL;
  } else {
    drawRect(temp->x,temp->y,bgCol); /* draw */
    free(last->next_ptr);
    last->next_ptr = NULL;  
  }
}
  
void
startScreen(){
  reset();
  
  screen.background(245, 78, 253);
  screen.setTextSize(4);
  screen.stroke(0,255,0);
  screen.text("Snake",10,10);
  screen.setTextSize(1);
  screen.text("press button to start",10, 80);
  
  while (goX == 0 && goY == 0){
    getInput();
    delay(5);
  }
  
  screen.stroke(0,0,0);
  screen.fill(bgCol.r,bgCol.g,bgCol.b);
  screen.rect(3,2,screenWidth-6,screenHeight-4);
  
  for (int i = 29; i<=53; i+=6){
    addFirst(i,52);
    drawRect(head->x, head->y,snakeCol);
  }
  
  putFood();
}
 
void
reset(){
  goX = 0;
  goY = 0;
  collision = 0;  
  score = 0;
  while(head){
    remLast();
  }
}

void 
getInput(){
    if (digitalRead(PBLEFT) == HIGH){
      goX = -tileSize; 
      goY = 0;  
    }
    else if (digitalRead(PBDOWN) == HIGH){
      goX = 0; 
      goY = tileSize;  
    }
    else if (digitalRead(PBUP) == HIGH){
      goX = 0; 
      goY = -tileSize;  
    }
    else if (digitalRead(PBRIGHT) == HIGH){
      goX = tileSize; 
      goY = 0;  
    }
}

int 
isGrow(){
  if(head->x == food.x && head->y == food.y){
    return 1; 
  } else {
    return 0;
  }
}

int
isCollision(){
  temp = head->next_ptr;
  while(temp){
    if (temp->x == head->x && temp->y == head->y){
      return 1;
    }
    temp = temp->next_ptr;  
  }
  if(head->x < 5 || head->x > 149 || head->y < 4 || head->y > 118){
    return 1;
  }
  return 0;
}
 
void
putFood(){
  int validPlace = 0;
  while (!validPlace){
    food.x = 5 + (int)random(0,24)*6;
    food.y = 4 + (int)random(0,19)*6;
    if (food.x != head->x && food.y != head->y){
      validPlace = 1; 
    }
  }
  drawRect(food.x, food.y, foodCol);
}

void 
drawRect(int x, int y, colour c){
  screen.noStroke();
  screen.fill(c.r, c.g, c.b);
  screen.rect(x,y,tileSize,tileSize);
}

void
endScreen(){
  char scoreChars[5];
  screen.background(0, 255, 0);
  screen.stroke(255,255,255);
  String scorestring = String(score);
  scorestring.toCharArray(scoreChars,5);
  
  screen.text("Game over", 40, 40);
  screen.text("Score:", 40, 50);
  screen.text(scoreChars, 80, 50);  
  delay(1000);
}

void
setup(){
  screen.begin();
  randomSeed(analogRead(0));
}

void
loop(){
  startScreen();
  while (!collision){
    getInput();
    delay(5);
    if (millis()-timestamp > 200){
      addFirst((head->x + goX), (head->y + goY));
      drawRect(head->x,head->y,snakeCol);
      if (food.growBy){
        food.growBy--;
        score += 1;
      }
      else {
        remLast();
      }
      if (isGrow()){
        food.growBy++;
        putFood(); 
      }
      if (isCollision()){
        collision = 1;
      }
      timestamp = millis();
    }
  }
  endScreen();  
}
