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:
- 5:257b4816ac6a
- Parent:
- 4:3ceebacef5f1
- Child:
- 6:9a5706a27240
--- a/main.cpp Wed Apr 06 12:42:02 2016 +0000 +++ b/main.cpp Wed Apr 06 17:49:36 2016 +0000 @@ -24,6 +24,8 @@ AnalogIn xPot(PTC11); AnalogIn yPot(PTC10); +PwmOut greenLed(PTA1); + // timer to regularly read the joystick Ticker pollJoystick; // Serial for debug @@ -78,20 +80,26 @@ int snakeTailY[100]; int snakeTailLength; +int score = 0; +int fruitValue = 10; + 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; +int prev_i = 41; +int prev_j = 23; +int prev2_i = 41; +int prev2_j = 23; // function prototypes void calibrateJoystick(); void updateJoystick(); void joystickDirection(); void generateFood(); -void newFruitValues(); +void newFruitXY(); void moveSnake(); +void greenLedFlash(); +void hardWall(); +void scoreCalculation(); int main() @@ -99,17 +107,12 @@ calibrateJoystick(); // get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second - - - - srand(time(NULL)); - lcd.init(); lcd.drawRect(i,j,1,1,1); // default snake position - + hardWall(); generateFood(); while(1) { @@ -117,7 +120,7 @@ if (printFlag) { // if flag set, clear flag and print joystick values to serial port printFlag = 0; - joystickDirection(); + //joystickDirection(); moveSnake(); } sleep(); // put the MCU to sleep until an interrupt wakes it up @@ -165,7 +168,7 @@ { 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 + randomX = rand() % 83 + 1; // randomX in the range 1 to 83 randomY = rand() % 47 + 1; // randomY in the range 1 to 47 // serial.printf("X = %i\n",randomX); // debug @@ -179,7 +182,7 @@ } -void newFruitValues() // new fruit coordinate values are given before it is passed to the generateFood function +void newFruitXY() // 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 @@ -233,6 +236,8 @@ } lcd.clear(); + snakeTailX[0] = i; + snakeTailY[0] = j 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 @@ -247,16 +252,47 @@ else if (currentDirection == down) { j += 2; } + hardWall(); 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 + greenLedFlash(); + scoreCalculation(); snakeTailLength++; - newFruitValues(); + newFruitXY(); generateFood(); } wait(1.0); - } \ No newline at end of file + } + +void greenLedFlash() { + + for(float p = 1.0f; p > 0.0f; p -= 0.1f) { // green led rapidly decreases from full brightness to off + greenLed = p; + wait(0.07); + } + greenLed = 0; + +} + +void hardWall() { + + lcd.drawRect(0,0,83,47,0); + lcd.refresh(); + +} + +void scoreCalculation() { + + score += fruitValue; // each time fruit is eaten score is calculated and fruit value will increase by 1 + + fruitValue++; + + serial.printf("score = %i\n",score); + serial.printf("fruitValue = %i\n\n",fruitValue); + +} \ No newline at end of file