Wall dodging game utilising a joystick and Nokia 5110 LCD display

Dependencies:   N5110 mbed

Revision:
2:602e9bb053a0
Parent:
1:26ebbb94cf36
Child:
3:8890b4605a10
--- a/main.cpp	Fri Apr 22 23:29:43 2016 +0000
+++ b/main.cpp	Sat Apr 23 12:41:46 2016 +0000
@@ -19,6 +19,9 @@
     - Game now keeps track of score and displays it at the end of the game
     - Intro screen added
 
+Week 23 Code - Brief invincibility added by pressing joybutton
+
+
 */
 
 #include "mbed.h"
@@ -39,10 +42,12 @@
 AnalogIn xPot(PTB11);       // joystick x direction object
 AnalogIn yPot(PTB10);       // joystick y direction object
 DigitalOut buzzer(PTA2);    // buzzer object
+InterruptIn flick (PTB3);   // interruptin instance of button
 
 Ticker pollJoystick;    // timer to regularly read the joystick
 Ticker game_timer;      // timer to regularly update the screen
 Timer noteTimer;       // timer for note tones
+Timer invun;
 
 Serial serial(USBTX,USBRX); // Serial for debug
 
@@ -96,21 +101,29 @@
 void game_timer_isr();      // sets flag for timer interrupt
 void moveBall();            // reads joystick direction and moves position of the player
 void moveWall();            // moves walls along the screen
-void checkCollision();      // checks for any collisions (i.e. player has hit a wall or side of the screen)
+void invincible();             // makes player briefly invincible
+void checkWallCollision();      // checks for any collisions with wall
+void checkBorderCollision();           // checks for any collisions with border
 void updateScreen();        // refreshes the screen, redraws player and walls
 void warning();             // flashes screen when a new wall is ready to appear
 void init_serial();         // sets baud rate for serial
 void debug();               // prints for debug purposes
+void button_isr();
 
 float refresh_rate = GAMESPEED; // how often to update display (Hz)
 float g_dt = 1.0F/refresh_rate; // global to store time step (F makes it a float, gets rid of compiler warning)
 volatile int g_timer_flag = 0;  // flag for timer interrupt
+volatile int game_over_flag = 0;         // flag to signal game over
+volatile int g_button_flag = 0;
 int printFlag = 0;              // flag for printing
-volatile int game_over_flag = 0;         // flag to signal game over
 int game_start_flag = 0;        // flag to start the game
 int i = 42;                     // x-coordinate value of player
 int j = 24;                     // y-coordinate value of player
 int counter = 0;                // number of times code has looped
+volatile bool mortal = 1;
+int invun_cool = 0;
+int saves = 5;
+
 
 
 
@@ -127,6 +140,7 @@
 
     game_timer.attach(&game_timer_isr,g_dt);
 
+
     while(1) {
 
         lcd.printString("Dodgemania",13,2); // Print game title on screen
@@ -157,14 +171,14 @@
         lcd.printString("Press button",6,2);
         lcd.printString("to start",18,3);
 
-        while(game_start_flag == 0) { // replace with interrupt?
-            if(button) {
-                game_start_flag = 1;
-                lcd.clear();
-                wait(0.5);
+        while(1) { // replace with interrupt?
+            if (button) {
+                break;
             }
         }
 
+        lcd.clear();
+        wait(0.5);
         lcd.inverseMode();
         wait(0.2);
         lcd.normalMode();
@@ -213,15 +227,20 @@
         upwall.x = rand() % 27+8;
         rightwall.cooldown = 61;
 
+        flick.rise(&button_isr);
+        flick.mode(PullDown);
+
         while (game_over_flag == 0) {
 
             if ( g_timer_flag ) {  // ticker interrupt
                 g_timer_flag = 0;  // clear flag
                 moveWall();
                 moveBall();
-                
-                checkCollision();
-                
+                invincible();
+                checkBorderCollision();
+                if (mortal) {
+                    checkWallCollision();
+                }
                 updateScreen();
                 warning();
                 debug();
@@ -271,7 +290,7 @@
 
 }
 
-void moveBall() // reads joystick direction and moves position of the player
+void moveBall()   // reads joystick direction and moves position of the player
 {
     if (joystick.direction == UP) {
         j-=1;
@@ -296,7 +315,7 @@
     }
 }
 
-void moveWall() // moves walls along the screen
+void moveWall()   // moves walls along the screen
 {
     leftwall.random = rand()%20;
     rightwall.random = rand()%20;
@@ -415,8 +434,7 @@
         }
     }
 }
-
-void checkCollision()   // checks for any collisions (i.e. player has hit a wall or side of the screen)
+void checkBorderCollision()
 {
     // if floor
     if ( j >= 47 - (PLAYERRADIUS+3)) {
@@ -437,6 +455,30 @@
     if ( i <= (PLAYERRADIUS+3)) {
         i = (PLAYERRADIUS+3);
     }
+}
+
+void invincible()
+{
+    if (g_button_flag) {
+        g_button_flag = 0;
+        if ((saves > 0) && (mortal == true)) { // saves are available and not currently used
+            invun_cool=0;
+            mortal = false;
+            invun_cool++;
+            saves--;
+        }
+    }
+    if (mortal == false) {
+        invun_cool++;
+        if (invun_cool > 20) {
+            mortal = true;
+            invun_cool=0;
+        }
+    }
+}
+
+void checkWallCollision()   // checks for any collisions (i.e. player has hit a wall or side of the screen)
+{
 
     // LEFT WALL
     if ((((i - PLAYERRADIUS) <= leftwall.x) && (leftwall.x <= (i + PLAYERRADIUS))) && (j > (leftwall.y+3) || j < (leftwall.y-3))) {
@@ -462,9 +504,16 @@
 void updateScreen()   // refreshes the screen, redraws player and walls
 {
     lcd.clear();
-    lcd.drawCircle(i,j,PLAYERRADIUS,1);
+    if (mortal) {
+        lcd.drawCircle(i,j,PLAYERRADIUS,1);
+    } else {
+        if (counter % 2 == 0) {
+            lcd.drawCircle(i,j,PLAYERRADIUS,1);
+        }
+    }
     lcd.refresh();  // update display
 
+
     // draw Border
     lcd.drawLine(2,2,81,2,1);
     lcd.drawLine(2,2,2,45,1);
@@ -528,6 +577,11 @@
     g_timer_flag = 1;
 }
 
+void button_isr()
+{
+    g_button_flag = 1;
+}
+
 void initDisplay()   // initialises the LCD display
 {
     lcd.init();
@@ -606,9 +660,9 @@
         if (joystick.direction == UNKNOWN)
             serial.printf(" Unsupported direction\n");
             */
-        serial.printf("Left-wall Cooldown = %d \n",leftwall.cooldown);
-        serial.printf("Right-wall Cooldown = %d \n",rightwall.cooldown);
-        serial.printf("counter = %d \n",counter);
+        serial.printf("saves = %d \n",saves);
+        //serial.printf("Right-wall Cooldown = %d \n",rightwall.cooldown);
+        //serial.printf("counter = %d \n",counter);
 
     }
 }