Simple (basic) snake game

Dependencies:   N5110 mbed

Revision:
1:ab8ceac59e71
Parent:
0:9eb93eff2469
Child:
2:dbfe6019b558
diff -r 9eb93eff2469 -r ab8ceac59e71 main.cpp
--- a/main.cpp	Sun May 08 20:10:54 2016 +0000
+++ b/main.cpp	Sun May 08 21:42:25 2016 +0000
@@ -7,19 +7,21 @@
 #include "main.h"
 
 int main() {
-
     lcd.init(); // Initialise LCD
+    g_lcd_bright_level = 0.5; // Set the default brightness
     init_K64F(); // Initialise connection with buttons
     welcomeScreen(); // Display welcome screen
     calibrateJoystick(); // Calibrate the joystick 
     
     // Read joystick values every 20 ms (50 Hz)
-    joystick_update_ticker.attach(&updateJoystick,0.02);
+    joystick_update_ticker.attach(&updateJoystick, 0.02);
+    // Read potentiometer value every 300 ms
+    brightness_update_ticker.attach(&updateBrightness, 0.3);
     
     // Initialising random number generator
     srand(time(NULL));
             
-    snake_game();
+    snakeGame();
 }
 
 void init_K64F() {
@@ -71,7 +73,7 @@
 
 /* Snake Game Logic */
 
-void snake_game() {
+void snakeGame() {
     
     // Game Variables
     int snake_x[220]; // Snake parts X
@@ -79,12 +81,14 @@
     
     AppleType apple; // Creating an apple
         
-    int snake_size = 5; // Initial size of snake
-    int x_direction = 0;
-    int y_direction = 0;
+    // Game Variables
+    int snake_size;
+    int x_direction;
+    int y_direction;
     int next_x;
     int next_y;
-    int score = 0;
+    int score;
+    int snake_speed;
 
     while(1) {
         switch(game_state) {
@@ -92,6 +96,8 @@
                 x_direction = 0;
                 y_direction = 0;
                 score = 0;
+                snake_size = 5; // Initial size of snake
+                snake_speed = 0; // 0 - 100
                 // Clear Screen
                 lcd.clear();
                 // Draw border                        
@@ -169,6 +175,7 @@
                             // Increment snake size and score
                             snake_size++;
                             score++;
+                            snake_speed = ((snake_speed + 10) < 150) ? (snake_speed + 10) : snake_speed;
                             // Place new apple
                             apple = placeRandomApple();
                             // Don't remove tail
@@ -194,20 +201,23 @@
                         // Redraw things on LCD
                         lcd.refresh();
                                 
-                        wait_ms(200);
+                        wait_ms(200 - snake_speed);
                     }
                 }
                 break;
             case GAME_OVER:
-                g_pcb_button_flag = 0;
+                char score_string[14];
+                sprintf(score_string, "Score: %d", score);
                 // Clear Screen
                 lcd.clear();
-                lcd.printString("GAME OVER!",2,0); // prints name in fifth line
-                
-                while(!g_pcb_button_flag) {
-                    sleep();
-                }
-                
+                // Print score
+                lcd.printString("GAME OVER!",0,0);
+                lcd.printString(score_string,0,1);
+                lcd.printString("REPLAY",48,5);
+                // Blink LED and Buzz until user presses the
+                // PCB Button
+                blinkLEDandBuzz();
+                // When blinkLEDandBuzz finished go to Init state
                 game_state = INIT;
                 break;              
             default:
@@ -267,4 +277,52 @@
 // This function is called when button is pressed
 void pcb_button_isr() {
     g_pcb_button_flag = 1;
+}
+
+void blinkLEDandBuzz() {
+    // Reset button interrupt flag
+    // in case if it was pressed during the game
+    g_pcb_button_flag = 0;
+                
+    // Set the buzzer period to 0.002 s
+    pcb_buzzer.period(0.002);
+    // Flag is needed to switch between
+    // on and off states repeatedly    
+    int flag = 0;
+    
+    // Wait for the button to be pressed
+    while(!g_pcb_button_flag) {
+        
+        if (flag == 0) {
+            // enable buzzer and led
+            pcb_led.write(1);
+            pcb_buzzer.write(0.5);
+            flag = 1;
+        
+        } else if (flag == 1) {
+            // disable buzzer and led
+            pcb_led.write(0);
+            pcb_buzzer.write(0);
+            flag = 0;
+        }                
+        // Wait before switching
+        wait(0.1);
+    }
+    
+    // Disable LED and Buzzer 
+    pcb_led.write(0);
+    pcb_buzzer.write(0);
+}
+
+// updateBrightness function called
+// by the ticker every 10 ms
+
+void updateBrightness() {
+    
+    float pot_value = pot.read();
+    
+    if (abs(g_lcd_bright_level - pot_value) > 0.1) {
+        lcd.setBrightness(current_pot_value);
+    }
+    
 }
\ No newline at end of file