I wrote a game of Snake (random pixel lights up, you guide the snake up, down, left, and right to eat the pixel. Snake grows longer, don't hit sides or body of snake). I am having problems with it, and I can't exactly figure it out. Here is my code (it should be self-explanatory) and any suggestions would be appreciated.
#include <Figure.h>
#include <Bounce.h>
#include <Charliplexing.h>
#include <Font.h>
#include <WProgram.h>
char test[] = "PRESS RESET";
int testLength = sizeof(test);
int x = 0, x2 = 0;
//Shadow array to keep track of the pixels that are on/off
bool shadow[13][8] = {
false};
//Lists for coords of the pixels of the snake
int xSnake[99];
int ySnake[99];
//Position in the lists of head and tail
int posHead = 0;
int posTail = 0;
//Coords of head
int headX = 6;
int headY = 3;
//Length variable
int snakeLength = 3;
//Coords for random pixel
int randX;
int randY;
//Directional control
int dirCurr = 3;
int dirPrev = 3;
int up = 0;
int down = 3;
int left = 1;
int right = 2;
//Buttons for controlling the snake
int buttonUp = 14; //analog pin 0 used as digital input
int buttonDown = 15; //analog pin 1
int buttonLeft = 16; // analog pin 2
int buttonRight = 17; //analog pin 3
//Debouncing of buttons
Bounce bounceUp = Bounce(buttonUp, 25);
Bounce bounceDown = Bounce(buttonDown, 25);
Bounce bounceLeft = Bounce(buttonLeft, 25);
Bounce bounceRight = Bounce(buttonRight, 25);
void setup()
{
LedSign::Init();
pinMode(buttonUp, INPUT);
digitalWrite(buttonUp, LOW);
pinMode(buttonDown, INPUT);
digitalWrite(buttonDown, LOW);
pinMode(buttonLeft, INPUT);
digitalWrite(buttonLeft, LOW);
pinMode(buttonRight, INPUT);
digitalWrite(buttonRight, LOW);
pinMode(18, OUTPUT);
digitalWrite(18, HIGH);
randomSeed(analogRead(5));
randX = random(0,14);
randY = random(0,9);
LedSign::Set(randX, randY, 1);
}
void loop()
{
LedSign::Set(headX,headY,1);
shadow[headX][headY] = true;
dirCurr = dirPrev;
bounceUp.update();
int valueUp = bounceUp.risingEdge();
if (valueUp == HIGH)
{
dirCurr = up;
}
bounceDown.update();
int valueDown = bounceDown.risingEdge();
if (valueDown == HIGH)
{
dirCurr = down;
}
bounceLeft.update();
int valueLeft = bounceDown.risingEdge();
if (valueLeft == HIGH)
{
dirCurr = left;
}
bounceRight.update();
int valueRight = bounceRight.risingEdge();
if (valueRight == HIGH)
{
dirCurr = right;
}
if (dirCurr + dirPrev == 3)
{
dirCurr = dirPrev;
}
headX = (headX - (dirCurr == left) + (dirCurr == right));
headY = (headY - (dirCurr == up) + (dirCurr == down));
if (headX == randX && headY == randY)
{
LedSign::Set(randX,randY,0);
snakeLength++;
while(shadow[randX][randY] != true)
{
randX = random(0,14);
randY = random(0,9);
}
LedSign::Set(randX, randY, 1);
}
if (shadow[headX][headY] == true)
{
LedSign::Init();
for (int k = 0; k < 5; k++)
{
Figure::Scroll90(snakeLength-3);
delay(250);
}
for (int j = 13; j > -(6 * testLength); j--)
{
x = j;
LedSign::Clear();
for (int i = 0; i < testLength; i++)
{
x2 = Font::Draw(test[i], x, 0);
x += x2;
if (x >= 13) break;
}
delay(80);
}
}
if ((headX < 0) || (headY < 0) || (headX > 13) || (headY > 8))
{
LedSign::Init();
bool shadow[13][8] = {false};
for (int k = 0; k < 5; k++)
{
Figure::Scroll90(snakeLength-3);
delay(250);
}
for (int j = 13; j > -(6 * testLength); j--)
{
x = j;
LedSign::Clear();
for (int i = 0; i < testLength; i++)
{
x2 = Font::Draw(test[i], x, 0);
x += x2;
if (x >= 13) break;
}
delay(80);
}
}
posHead++;
if (posHead > 99)
{
posHead = 0;
}
xSnake[posHead] = headX;
ySnake[posHead] = headY;
if ( abs(posHead - posTail) >= snakeLength)
{
LedSign::Set(xSnake[posTail], ySnake[posTail], 0);
int xTail = xSnake[posTail];
int yTail = ySnake[posTail];
shadow[xTail][yTail] = false;
posTail++;
if (posTail > 99)
{
posTail = 0;
}
}
delay(100);
}