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:
- 4:3ceebacef5f1
- Parent:
- 3:0e3179c452c5
- Child:
- 5:257b4816ac6a
--- a/main.cpp Wed Apr 06 11:52:25 2016 +0000 +++ b/main.cpp Wed Apr 06 12:42:02 2016 +0000 @@ -78,12 +78,20 @@ int snakeTailY[100]; int snakeTailLength; +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; + // function prototypes void calibrateJoystick(); void updateJoystick(); void joystickDirection(); void generateFood(); void newFruitValues(); +void moveSnake(); int main() @@ -93,12 +101,7 @@ pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second - 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)); @@ -115,143 +118,21 @@ printFlag = 0; joystickDirection(); - - if (joystick.direction == LEFT) { - if (currentDirection != right) { // user can't turn opposite way - currentDirection = left; // direct enum updated - } - 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) { // if fruit is eaten - snakeTailLength++; - newFruitValues(); - generateFood(); - } - wait(1.0); - if (joystick.direction == UP) { - currentDirection = up; // direction enum updated, this while loop ends, up while loop starts - } - if (joystick.direction == DOWN) { - currentDirection = down; // direction enum updated, this while loop ends, down while loop starts - } - } - } - if (joystick.direction == RIGHT) { - if (currentDirection != left) { - currentDirection = right; - } - 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) { // if fruit is eaten - snakeTailLength++; - newFruitValues(); - generateFood(); - } - wait(1.0); - if (joystick.direction == UP) { - currentDirection = up; // direction enum updated, this while loop ends, up while loop starts - } - if (joystick.direction == DOWN) { - currentDirection = down; // direction enum updated, this while loop ends, down while loop starts - } - } - } - if (joystick.direction == UP) { - if (currentDirection != down) { - currentDirection = up; - } - 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) { // if fruit is eaten - snakeTailLength++; - newFruitValues(); - generateFood(); - } - wait(1.0); - if (joystick.direction == LEFT) { - currentDirection = left; // direction enum updated, this while loop ends, left while loop starts - } - if (joystick.direction == RIGHT) { - currentDirection = right; // direction enum updated, this while loop ends, right while loop starts - } - } - } - if (joystick.direction == DOWN) { - if (currentDirection != up) { - currentDirection = down; - } - 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) { // if fruit is eaten - snakeTailLength++; - newFruitValues(); - generateFood(); - } - wait(1.0); - if (joystick.direction == LEFT) { - currentDirection = left; // direction enum updated, this while loop ends, left while loop starts - } - if (joystick.direction == RIGHT) { - currentDirection = right; // direction enum updated, this while loop ends, right while loop starts - } - } - } - - - - } - // put the MCU to sleep until an interrupt wakes it up - sleep(); + moveSnake(); + } + sleep(); // put the MCU to sleep until an interrupt wakes it up } } -// read default positions of the joystick to calibrate later readings -void calibrateJoystick() + +void calibrateJoystick() // read default positions of the joystick to calibrate later readings { button.mode(PullDown); // must not move during calibration joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) joystick.y0 = yPot; } + void updateJoystick() { // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) @@ -298,10 +179,9 @@ } -void newFruitValues() +void newFruitValues() // new fruit coordinate values are given before it is passed to the generateFood function { - // 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 @@ -309,7 +189,7 @@ } -void joystickDirection() { +void joystickDirection() { // stcik direction for debug serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button); @@ -328,4 +208,55 @@ serial.printf(" Unsupported direction\n"); } - \ No newline at end of file + +void moveSnake() { + + if (joystick.direction == LEFT) { + if (currentDirection != right) { // change the currentDirection according to joystick input, providing + currentDirection = left; // it's not the opposite direction to the current direction + } + } + else if (joystick.direction == RIGHT) { + if (currentDirection != left) { // change the currentDirection according to joystick input, providing + currentDirection = right; // it's not the opposite direction to the current direction + } + } + else if (joystick.direction == UP) { + if (currentDirection != down) { // change the currentDirection according to joystick input, providing + currentDirection = up; // it's not the opposite direction to the current direction + } + } + else if (joystick.direction == DOWN) { + if (currentDirection != up) { // change the currentDirection according to joystick input, providing + currentDirection = down; // it's not the opposite direction to the current direction + } + } + + lcd.clear(); + prev2_i = prev_i; // previous coordinates of the previous snake head coordinates + prev2_j = prev_j; + prev_i = i; // previous coordinates of the snake head + prev_j = j; + + if (currentDirection == left) { // shift snake head coordinates according to current direction + i -= 2; } + else if (currentDirection == right) { + i += 2; } + else if (currentDirection == up) { + j -= 2; } + else if (currentDirection == down) { + j += 2; } + + lcd.drawRect(i,j,1,1,1); // snake head + lcd.drawRect(prev_i,prev_j,1,1,1); // seg 1 + lcd.drawRect(prev2_i,prev2_j,1,1,1); // seg 2 + lcd.drawRect(randomX,randomY,1,1,1); // food + + if (i == randomX && j == randomY) { // if fruit is eaten + snakeTailLength++; + newFruitValues(); + generateFood(); + } + wait(1.0); + + } \ No newline at end of file