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:
- 2:663b9aadf00c
- Parent:
- 1:97ac723959f2
- Child:
- 3:0e3179c452c5
--- a/main.cpp Tue Apr 05 11:09:41 2016 +0000 +++ b/main.cpp Tue Apr 05 19:39:22 2016 +0000 @@ -24,41 +24,12 @@ AnalogIn xPot(PTC11); AnalogIn yPot(PTC10); -// buttons to simulate joystick -InterruptIn upButton(PTB19); -InterruptIn downButton(PTC1); -InterruptIn leftButton(SW3); -InterruptIn rightButton(SW2); - -// flag - must be volatile as changes within ISR -// g_ prefix makes it easier to distinguish it as global -volatile int g_right_flag = 0; -volatile int g_left_flag = 0; -volatile int g_up_flag = 0; -volatile int g_down_flag = 0; - -// function prototypes -void left_isr(); -void right_isr(); -void up_isr(); -void down_isr(); - - // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default - // and fall to 0 V when pressed. We therefore need to look for a falling edge - // on the pin to fire the interrupt - sw2.fall(&sw2_isr); - sw3.fall(&sw3_isr); - - // since SW2 has an external pull-up, we should disable to internal pull-down - // resistor that is enabled by default using InterruptIn - sw2.mode(PullNone); - sw3.mode(PullNone); - - // timer to regularly read the joystick Ticker pollJoystick; // Serial for debug Serial serial(USBTX,USBRX); + + // create enumerated type (0,1,2,3 etc. for direction) // could be extended for diagonals etc. @@ -94,26 +65,37 @@ // create struct variable Joystick joystick; - - int printFlag = 0; +int pixels[84][48]; + +int randomX = rand() % 83 + 1; // randomX in the range 1 to 81 +int randomY = rand() % 47 + 1; // randomY in the range 1 to 47 +int randomXoddEven = randomX%2; // find out whether odd or even +int randomYoddEven = randomY%2; + // function prototypes void calibrateJoystick(); void updateJoystick(); +void generateFood(); int main() { + calibrateJoystick(); // get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second - upButton.mode(PullDown); - downButton.mode(PullDown); + - int i = 41; - int j = 23; + int i = 41; // snake head origin x + int j = 23; // snake head origin y + + srand(time(NULL)); + lcd.init(); - lcd.drawRect(i,j,2,2,1); + lcd.drawRect(i,j,1,1,1); // default snake position + + generateFood(); while(1) { @@ -135,70 +117,102 @@ if (joystick.direction == UNKNOWN) serial.printf(" Unsupported direction\n"); - if (leftButton == 0) { + 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(); - i -= 3; // shifts three pixels (snake thickness) - lcd.drawRect(i,j,2,2,1); + i -= 2; // shifts two pixels (snake thickness) + lcd.drawRect(i,j,1,1,1); // snake head + 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; + generateFood(); + } wait(1.0); - if (upButton == 1) { + if (joystick.direction == UP) { currentDirection = up; // direction enum updated, this while loop ends, up while loop starts } - if (downButton == 1) { + if (joystick.direction == DOWN) { currentDirection = down; // direction enum updated, this while loop ends, down while loop starts } } } - if (rightButton == 0) { + if (joystick.direction == RIGHT) { if (currentDirection != left) { currentDirection = right; } while (currentDirection == right) { lcd.clear(); - i += 3; - lcd.drawRect(i,j,2,2,1); + i += 2; + lcd.drawRect(i,j,1,1,1); // snake head + 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; + generateFood(); + } wait(1.0); - if (upButton == 1) { + if (joystick.direction == UP) { currentDirection = up; // direction enum updated, this while loop ends, up while loop starts } - if (downButton == 1) { + if (joystick.direction == DOWN) { currentDirection = down; // direction enum updated, this while loop ends, down while loop starts } } } - if (upButton == 1) { + if (joystick.direction == UP) { if (currentDirection != down) { currentDirection = up; } while (currentDirection == up) { lcd.clear(); - j -= 3; - lcd.drawRect(i,j,2,2,1); + j -= 2; + lcd.drawRect(i,j,1,1,1); // snake head + 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; + generateFood(); + } wait(1.0); - if (leftButton == 0) { + if (joystick.direction == LEFT) { currentDirection = left; // direction enum updated, this while loop ends, left while loop starts } - if (rightButton == 0) { + if (joystick.direction == RIGHT) { currentDirection = right; // direction enum updated, this while loop ends, right while loop starts } } } - if (downButton == 1) { + if (joystick.direction == DOWN) { if (currentDirection != up) { currentDirection = down; } while (currentDirection == down) { lcd.clear(); - j += 3; - lcd.drawRect(i,j,2,2,1); + j += 2; + lcd.drawRect(i,j,1,1,1); // snake head + 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; + generateFood(); + } wait(1.0); - if (leftButton == 0) { + if (joystick.direction == LEFT) { currentDirection = left; // direction enum updated, this while loop ends, left while loop starts } - if (rightButton == 0) { + if (joystick.direction == RIGHT) { currentDirection = right; // direction enum updated, this while loop ends, right while loop starts } } @@ -207,7 +221,8 @@ } - + // put the MCU to sleep until an interrupt wakes it up + sleep(); } } @@ -247,24 +262,22 @@ printFlag = 1; } -void left_isr() -{ - g_left_flag = 1; // set flag in ISR -} - -void right_isr() +void generateFood() { - g_right_flag = 1; // set flag in ISR -} + while (randomXoddEven ==0 || randomYoddEven ==0 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on + + 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); + + randomXoddEven = randomX%2; // find out whether odd or even + randomYoddEven = randomY%2; + } + + lcd.drawRect(randomX,randomY,1,1,1); -void up_isr() -{ - g_up_flag = 1; // set flag in ISR -} - -void down_isr() -{ - g_down_flag = 1; // set flag in ISR }