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
Revision 13:08159ea3d556, committed 2016-05-01
- Comitter:
- meurigp
- Date:
- Sun May 01 14:43:01 2016 +0000
- Parent:
- 12:825a402d230f
- Child:
- 14:56e355c5cfc9
- Commit message:
- moveSnake() cleaned up
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun May 01 13:46:08 2016 +0000
+++ b/main.cpp Sun May 01 14:43:01 2016 +0000
@@ -44,7 +44,7 @@
while(gamePlaying == true) {
- //displayGame();
+ gameLogic();
if (printFlag) { // if flag set, clear flag and print joystick values to serial port
printFlag = 0;
@@ -105,67 +105,65 @@
currentDirection = down; // it's not the opposite direction to the current direction
}
}
-
- lcd.clear();
- greenLed = 1;
- if (snakeTailLength > 4) {
- for (int q=1; q<snakeTailLength; q++) {
- if (snakeTailX[q] == i && snakeTailY[q] == j) {
- gameOver();
- }
+}
+
+void gameLogic() {
+
+ lcd.clear();
+ greenLed = 1;
+ if (snakeTailLength > 4) {
+ for (int q=1; q<snakeTailLength; q++) {
+ if (snakeTailX[q] == i && snakeTailY[q] == j) {
+ gameOver();
}
}
-
- if (gamePlaying == true) {
-
- prev_i = snakeTailX[0];
- prev_j = snakeTailY[0];
- snakeTailX[0] = i;
- snakeTailY[0] = j;
+ }
+
+ prev_i = snakeTailX[0];
+ prev_j = snakeTailY[0];
+ snakeTailX[0] = i;
+ snakeTailY[0] = j;
- for (int a=1; a<snakeTailLength; a++) {
- prev2_i = snakeTailX[a];
- prev2_j = snakeTailY[a];
- snakeTailX[a] = prev_i;
- snakeTailY[a] = prev_j;
- prev_i = prev2_i;
- prev_j = prev2_j;
- }
-
-
- if (currentDirection == left) { // shift snake head coordinates according to current direction
- i -= 2; } // move left
- else if (currentDirection == right) {
- i += 2; } // move right
- else if (currentDirection == up) {
- j -= 2; } // move up
- else if (currentDirection == down) {
- j += 2; } // move down
-
-
- lcd.drawRect(i,j,1,1,1); // snake head
- for (int b=0; b<snakeTailLength; b++) {
- lcd.drawRect(snakeTailX[b],snakeTailY[b],1,1,1);
- }
-
- lcd.refresh();
-
- lcd.drawRect(randomX,randomY,1,1,1); // food
- hardWall();
-
- if (i == randomX && j == randomY) { // if fruit is eaten
- greenLed = 0;
- //buzzer.beep(2000,0.2);
- scoreCalculation();
- snakeTailLength++;
- newFruitXY();
- generateFood();
- }
- wait(0.1);
- }
- }
-
+ for (int a=1; a<snakeTailLength; a++) {
+ prev2_i = snakeTailX[a];
+ prev2_j = snakeTailY[a];
+ snakeTailX[a] = prev_i;
+ snakeTailY[a] = prev_j;
+ prev_i = prev2_i;
+ prev_j = prev2_j;
+ }
+
+
+ if (currentDirection == left) { // shift snake head coordinates according to current direction
+ i -= 2; } // move left
+ else if (currentDirection == right) {
+ i += 2; } // move right
+ else if (currentDirection == up) {
+ j -= 2; } // move up
+ else if (currentDirection == down) {
+ j += 2; } // move down
+
+
+ lcd.drawRect(i,j,1,1,1); // snake head
+ for (int b=0; b<snakeTailLength; b++) {
+ lcd.drawRect(snakeTailX[b],snakeTailY[b],1,1,1);
+ }
+
+ lcd.refresh();
+ lcd.drawRect(randomX,randomY,1,1,1); // food
+ hardWall();
+
+ if (i == randomX && j == randomY) { // if fruit is eaten
+ greenLed = 0;
+ //buzzer.beep(2000,0.2);
+ scoreCalculation();
+ snakeTailLength++;
+ newFruitXY();
+ generateFood();
+ }
+ wait(0.1);
+}
void hardWall() {
--- a/main.h Sun May 01 13:46:08 2016 +0000 +++ b/main.h Sun May 01 14:43:01 2016 +0000 @@ -64,7 +64,7 @@ */ void newFruitXY(); /** -Main in game function, involves the input, logic and display +Receives input form joystick and updates the current direction */ void moveSnake(); /** @@ -99,6 +99,10 @@ Game paused function */ void gamePaused(); +/** +Contains all logic for the snake game and displays accordingly +*/ +void gameLogic(); #endif \ No newline at end of file
