Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Joystick N5110 SDFileSystem beep fsmMenu mbed
Fork of SnakeProjectRev1 by
Diff: main.cpp
- Revision:
- 3:0e3179c452c5
- Parent:
- 2:663b9aadf00c
- Child:
- 4:3ceebacef5f1
--- a/main.cpp Tue Apr 05 19:39:22 2016 +0000
+++ b/main.cpp Wed Apr 06 11:52:25 2016 +0000
@@ -74,10 +74,17 @@
int randomXoddEven = randomX%2; // find out whether odd or even
int randomYoddEven = randomY%2;
+int snakeTailX[100];
+int snakeTailY[100];
+int snakeTailLength;
+
// function prototypes
void calibrateJoystick();
void updateJoystick();
+void joystickDirection();
void generateFood();
+void newFruitValues();
+
int main()
{
@@ -88,6 +95,11 @@
int i = 41; // snake head origin x
int j = 23; // snake head origin y
+ int prev_i;
+ int prev_j;
+ int prev2_i;
+ int prev2_j;
+
srand(time(NULL));
@@ -101,21 +113,8 @@
if (printFlag) { // if flag set, clear flag and print joystick values to serial port
printFlag = 0;
- serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
-
- // check joystick direction
- if (joystick.direction == UP)
- serial.printf(" UP\n");
- if (joystick.direction == DOWN)
- serial.printf(" DOWN\n");
- if (joystick.direction == LEFT)
- serial.printf(" LEFT\n");
- if (joystick.direction == RIGHT)
- serial.printf(" RIGHT\n");
- if (joystick.direction == CENTRE)
- serial.printf(" CENTRE\n");
- if (joystick.direction == UNKNOWN)
- serial.printf(" Unsupported direction\n");
+
+ joystickDirection();
if (joystick.direction == LEFT) {
if (currentDirection != right) { // user can't turn opposite way
@@ -123,14 +122,18 @@
}
while (currentDirection == left) { // enters left movement loop
lcd.clear();
+ prev2_i = prev_i;
+ prev2_j = prev_j;
+ prev_i = i;
+ prev_j = j;
i -= 2; // shifts two pixels (snake thickness)
lcd.drawRect(i,j,1,1,1); // snake head
+ lcd.drawRect(prev_i,prev_j,1,1,1);
+ lcd.drawRect(prev2_i,prev2_j,1,1,1);
lcd.drawRect(randomX,randomY,1,1,1); // food
- if (i == randomX && j == randomY) {
- randomX = rand() % 83 + 1; // randomX in the range 1 to 81
- randomY = rand() % 47 + 1; // randomY in the range 1 to 47
- randomXoddEven = randomX%2; // find out whether odd or even
- randomYoddEven = randomY%2;
+ if (i == randomX && j == randomY) { // if fruit is eaten
+ snakeTailLength++;
+ newFruitValues();
generateFood();
}
wait(1.0);
@@ -148,14 +151,18 @@
}
while (currentDirection == right) {
lcd.clear();
+ prev2_i = prev_i;
+ prev2_j = prev_j;
+ prev_i = i;
+ prev_j = j;
i += 2;
lcd.drawRect(i,j,1,1,1); // snake head
+ lcd.drawRect(prev_i,prev_j,1,1,1);
+ lcd.drawRect(prev2_i,prev2_j,1,1,1);
lcd.drawRect(randomX,randomY,1,1,1); // food
- if (i == randomX && j == randomY) {
- randomX = rand() % 83 + 1; // randomX in the range 1 to 81
- randomY = rand() % 47 + 1; // randomY in the range 1 to 47
- randomXoddEven = randomX%2; // find out whether odd or even
- randomYoddEven = randomY%2;
+ if (i == randomX && j == randomY) { // if fruit is eaten
+ snakeTailLength++;
+ newFruitValues();
generateFood();
}
wait(1.0);
@@ -173,14 +180,18 @@
}
while (currentDirection == up) {
lcd.clear();
+ prev2_i = prev_i;
+ prev2_j = prev_j;
+ prev_i = i;
+ prev_j = j;
j -= 2;
lcd.drawRect(i,j,1,1,1); // snake head
+ lcd.drawRect(prev_i,prev_j,1,1,1);
+ lcd.drawRect(prev2_i,prev2_j,1,1,1);
lcd.drawRect(randomX,randomY,1,1,1); // food
- if (i == randomX && j == randomY) {
- randomX = rand() % 83 + 1; // randomX in the range 1 to 81
- randomY = rand() % 47 + 1; // randomY in the range 1 to 47
- randomXoddEven = randomX%2; // find out whether odd or even
- randomYoddEven = randomY%2;
+ if (i == randomX && j == randomY) { // if fruit is eaten
+ snakeTailLength++;
+ newFruitValues();
generateFood();
}
wait(1.0);
@@ -198,14 +209,21 @@
}
while (currentDirection == down) {
lcd.clear();
+ prev2_i = snakeTailX[];
+ prev2_j = snakeTailY[];
+ snakeTailX[] = prev_i;
+ snakeTailY[] = prev_j;
+ prev_i = i;
+ prev_j = j;
j += 2;
lcd.drawRect(i,j,1,1,1); // snake head
+ for (k=0; k<snakeTailLength; k++) {
+ lcd.drawRect(prev_i,prev_j,1,1,1);
+ }
lcd.drawRect(randomX,randomY,1,1,1); // food
- if (i == randomX && j == randomY) {
- randomX = rand() % 83 + 1; // randomX in the range 1 to 81
- randomY = rand() % 47 + 1; // randomY in the range 1 to 47
- randomXoddEven = randomX%2; // find out whether odd or even
- randomYoddEven = randomY%2;
+ if (i == randomX && j == randomY) { // if fruit is eaten
+ snakeTailLength++;
+ newFruitValues();
generateFood();
}
wait(1.0);
@@ -269,8 +287,8 @@
randomX = rand() % 83 + 1; // randomX in the range 1 to 81
randomY = rand() % 47 + 1; // randomY in the range 1 to 47
- serial.printf("X = %i\n",randomX); // debug
- serial.printf("Y = %i\n\n",randomY);
+ // serial.printf("X = %i\n",randomX); // debug
+ // serial.printf("Y = %i\n\n",randomY);
randomXoddEven = randomX%2; // find out whether odd or even
randomYoddEven = randomY%2;
@@ -279,6 +297,35 @@
lcd.drawRect(randomX,randomY,1,1,1);
}
+
+void newFruitValues()
+{
+ // new fruit coordinate values are given before it is passed to the generateFood function
+
+ randomX = rand() % 83 + 1; // randomX in the range 1 to 81
+ randomY = rand() % 47 + 1; // randomY in the range 1 to 47
+ randomXoddEven = randomX%2; // find out whether odd or even
+ randomYoddEven = randomY%2;
+}
+
+void joystickDirection() {
+
+ serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
-
\ No newline at end of file
+ // check joystick direction
+ if (joystick.direction == UP)
+ serial.printf(" UP\n");
+ if (joystick.direction == DOWN)
+ serial.printf(" DOWN\n");
+ if (joystick.direction == LEFT)
+ serial.printf(" LEFT\n");
+ if (joystick.direction == RIGHT)
+ serial.printf(" RIGHT\n");
+ if (joystick.direction == CENTRE)
+ serial.printf(" CENTRE\n");
+ if (joystick.direction == UNKNOWN)
+ serial.printf(" Unsupported direction\n");
+
+ }
+
\ No newline at end of file
