My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
21:6b02ef341358
Parent:
20:59aa6189a47d
--- a/main.cpp	Mon May 02 17:57:27 2016 +0000
+++ b/main.cpp	Wed May 04 09:19:50 2016 +0000
@@ -9,13 +9,11 @@
 int main(void)
 {
     lcd.init(); ///initialise LED
-    init_K64F();
-    init_serial();
+    init_K64F(); ///init K64F 
+    init_serial(); ///initiate connection with PC port
     init_game(); ///initialize game
-    led_output = fsm[fsm_state];
+    led_output = fsm[fsm_state]; ///set FSM to turn on LEDs
     calibrateJoystick(); ///calibrate joystick
-    button.rise(&button_isr); ///assign rise with ISR
-    button1.rise(&button1_isr);
 
     game_ticker.attach(&game_timer_isr,0.2); ///attach ticker for the menu different from the main ticker
     menu();
@@ -24,46 +22,34 @@
 
     ticker.attach(&timer_isr,game_speed); ///attach ticker with ISR. Speed is selected by the user
     reset = level; ///set reset = level to check later if level has increased
-
+    g_button1_flag = 0;
+    timer.start();
     while (1) {
-
+        
         if (g_timer_flag) {///if timer flag is UP
 
             g_timer_flag = 0; ///reset flag
-            //g_button_flag = 0;
             lcd.clear();
+            if (reset < level) { ///if level has increased
+                g_button1_flag = 0; ///reset weapon button
+                reset = level; ///update reset
+                rectY = 0; ///set enemies to default position
+                circleX = 0;
+            }
             guidance(); ///get guidance
             hero(); ///call hero
             enemies(); ///generate enemies 
             obstacles(); ///generate obstacles
-            if (g_button1_flag) { ///if heavy weapon button is pressed
-                
-                g_button1_flag = 0;
-                rectY = 60; ///remove enemies from field
-                rectX = -10;
-                circleX = -10;
-                circleY = 100;
-                //timer.reset();
-                //timer.start();
+            //float count_time = timer.read();
+            
+            if (g_button1_flag) { ///the blue LED indicates if the weapon is ready to use or not
+                blue_led = 0;    
+            } else {
+                blue_led = 1;
             }
-            //float count_time = timer.read();
-            if (reset < level) { ///if level has increased
-                reset = level; ///update reset
-                rectY = 0; ///remove enemies from field
-                circleX = 0;
-            }
-            if (g_button1_flag) { ///if heavy weapon button is pressed
-               
-                g_button1_flag = 0;
-                rectY = 60; ///remove enemies from field
-                rectX = -10;
-                circleX = -10;
-                circleY = 100;
-                //timer.reset();
-                //timer.start();
-            }
+                
             checkOverlap(); ///check is hero has overlapped with enemies or obstacles 
-            updateJoystick();
+            updateJoystick(); ///update joystick
             lcd.refresh();
         }
         sleep();///use sleep and timer triggered interrupt to save power 
@@ -71,9 +57,7 @@
 }
 
 
-
-
-
+//mbed functions
 
 
 void init_serial()
@@ -95,7 +79,10 @@
     // resistors that are enabled by default using InterruptIn
     sw2.mode(PullNone);
     sw3.mode(PullNone);
-    button.mode(PullDown);
+    
+    button.rise(&button_isr); ///assign rise with ISR
+    button1.rise(&button1_isr);
+    button.mode(PullDown); ///use PullDown mode
     button1.mode(PullDown);
 }
 
@@ -144,102 +131,141 @@
 void enemies()
 {
     /// generate enemies in the screen depending on the level of difficulty
-    if (level == 0) {
+    if (level == 0) { ///generate enemies at level 0
+
+        enemyRect();
+        if (g_button1_flag == 0) {
+            enemyCircle();
+        }
+    } else if (level == 1) {///generate enemies at level 1
 
         enemyRect();
-        enemyCircle();
-    } else if (level == 1) {
+        if (g_button1_flag == 0) {
+            enemyCircle();
+        }
+    } else if (level == 2) {///generate enemies at level 2
+
+        enemyRocket();
+        if (g_button1_flag == 0) {
+            enemyCircle();
+        }
+    } else if (level == 3) {///generate enemies at level 3
+
+        enemyRect();
+        if (g_button1_flag == 0) {
+            enemyCircle();
+        }
+    } else if (level == 4) {///generate enemies at level 4
 
         enemyRect();
-        enemyCircle();
-    } else if (level == 2) {
-        
+        if (g_button1_flag == 0) {
+            enemyCircle();
+        }
+    } else if (level == 5) {///generate enemies at level 5
         enemyRocket();
-    } else if (level == 3) {
-        
-        enemyRect();
+        if (g_button1_flag == 0) {
+            enemyCircle();
+        }
+    } else if (level == 6) {///generate enemies at level 6
+        enemyRocket();
+        if (g_button1_flag == 0) {
+            enemyCircle();
+        }
+    } else if (level == 7) {///generate enemies at level 7
+        enemyRocket();
         enemyCircle();
-    } else if (level == 4) {
-        
-         enemyRect();
-        enemyCircle();  
-    } else if (level == 5) {
-        enemyRocket();
-    } else if (level == 6) {
-        enemyRocket();
-        enemyCircle();   
+        if (g_button1_flag == 1) {///if button1 is pressed don't allow to use at level 7
+            g_button1_flag = 0;
+             lcd.clear();
+            lcd.printString("Can't use now!",0,0);
+            lcd.refresh();
+            wait(1);
+            lcd.clear();
+        }
     }
-    else if (level == 7) {
-           enemyRocket();
-           enemyCircle();
-    }
+
 }
 
 void enemyRect()
 {
-    ///generate rect shape enemy
+    ///generate rectangular shape enemy
     lcd.drawRect(rectX,rectY,5,4,1);
-    if (g_button1_flag == 0) {
-        rectX = rectX + rand()%4 - 2;
-        rectY++; ///enemy moving towards hero
+
+    rectX = rectX + rand()%4 - 2;
+    rectY++; ///enemy moving towards hero
 
-        if (rectY > 50) {
-            rectY = 0;
-        }
-        if (rectX > 75 || rectX < 25) {
-            rectX = 40+rand()%6 - 3;
-        }
+    if (rectY > 50) {///set boundaries for enemy position
+        rectY = 0;
+    }
+    if (rectX > 75 || rectX < 25) {
+        rectX = 40+rand()%6 - 3;
     }
 }
 
-void enemyRocket() {
+void enemyRocket() { ///generate a "rocket-like" enemy
     
     lcd.drawRect(rectX,rectY,2,7,1);
     lcd.drawCircle(rectX+1,rectY+1,2,1);
     lcd.drawLine(rectX-2,rectY+2,rectX,rectY-1,1);
     lcd.drawLine(rectX+2,rectY+2,rectX,rectY-1,1);
-    
-    if (g_button1_flag == 0) {
-        rectX = rectX + rand()%4 - 2;
-        rectY++; ///enemy moving towards hero
+
+    rectX = rectX + rand()%4 - 2;
+    rectY++; ///enemy moving towards hero
+
+    if (rectY > 50) {///set boundaries for position of enemy
+        rectY = 0;
+    }
+    if (rectX > 75 || rectX < 10) {
+        rectX = 40+rand()%6 - 3;
+    }
+}
 
-        if (rectY > 50) {
-            rectY = 0;
-        }
-        if (rectX > 75 || rectX < 10) {
-            rectX = 40+rand()%6 - 3;
-        }
+void enemyCircle()
+{
+    ///generate circle shape enemy
+    lcd.drawCircle(circleX,circleY,4,1);
+    circleY = circleY + rand() %4 - 2;
+
+    if (circleY <= 10) { ///set the boundaries of the enemy
+        circleY = 20;
     }
+    if (circleY >= 35) {
+        circleY = 35;
+    }
+    if (circleX > 84) {
+        circleX = 0;
+    }
+    circleX++; ///enemy moving towards hero
 }
 void hero()
 {
     ///cotrol hero
-    if (joystick.direction == RIGHT) {
+    if (joystick.direction == RIGHT) { ///joystick direction points to the right
         heroX--;
-    } else if (joystick.direction == LEFT) {
+    } else if (joystick.direction == LEFT) {///joystick direction points to the left
         heroX++;
     } else {
         heroX = heroX;
     }
 
-    if (joystick.direction == UP) {
+    if (joystick.direction == UP) {///joystick direction points to the top
         heroY--;
-    } else if (joystick.direction == DOWN) {
+    } else if (joystick.direction == DOWN) {///joystick direction points down
         heroY++;
     } else {
-        heroY = heroY;
+        heroY = heroY; ///joystick is centred
     }
 
-    if (joystick.direction == UP_LEFT) {
+    if (joystick.direction == UP_LEFT) { ///joystick direction points up and left
         heroY--;
         heroX++;
-    } else if (joystick.direction == UP_RIGHT) {
+    } else if (joystick.direction == UP_RIGHT) { ///joystick direction points up and right
         heroY--;
         heroX--;
-    } else if (joystick.direction == DOWN_RIGHT) {
+    } else if (joystick.direction == DOWN_RIGHT) {///joystick direction points down and right
         heroY++;
         heroX--;
-    } else if (joystick.direction == DOWN_LEFT) {
+    } else if (joystick.direction == DOWN_LEFT) {///joystick direction points down and left
         heroY++;
         heroX++;
     }
@@ -253,39 +279,20 @@
     }
     if (heroY < -45) { ///if hero has reached the top of the screen
         heroY = 0; ///hero goes back to the bottom of the screen
-        level++; ///level increases by 1
+        g_button1_flag = 0;
+        level++; ///increase level of the game
     }
 
     if (heroY >= 0) {/// hero cannot go to previous level
         heroY = 0;
     }
     ///draw hero
-    lcd.drawLine(40+heroX, 47+heroY, 48+heroX, 43+heroY,1);
+    lcd.drawLine(40+heroX, 47+heroY, 48+heroX, 43+heroY,1); ///variables heroX and heroY are used to control the direction of the hero
     lcd.drawLine(40+heroX, 43+heroY,48+heroX, 47+heroY,1);
     lcd.drawLine(44+heroX, 45+heroY,44+heroX, 41+heroY,1);
     lcd.drawCircle(44+heroX, 39+heroY,2,0);
 }
 
-void enemyCircle()
-{
-    ///generate circle shape enemy
-    lcd.drawCircle(circleX,circleY,4,1);
-    circleY = circleY + rand() %4 - 2;
-
-    if (g_button1_flag == 0) {
-        if (circleY <= 10) {
-            circleY = 20;
-        }
-        if (circleY >= 35) {
-            circleY = 35;
-        }
-        if (circleX > 84) {
-            circleX = 0;
-        }
-        circleX++; ///enemy moving towards hero
-    }
-}
-
 void init_game()
 {
     ///initialise game
@@ -300,42 +307,47 @@
     lcd.printString("Treasure Hunt!",1,3);
     lcd.refresh();
     //timeout.attach(&timeout_isr,3);
-    //sleep();
-    wait(3);
+   // sleep();
+    wait(3); ///small delay prints welcome message
     lcd.clear();
 
 }
 void guidance()
 {
-    /// show arrow to act as guidance towards the treasure
-    if (level < 7) { ///check level of difficulty
-
-        lcd.drawLine(42,0,42,4,1);
+    if (level < 7) {
         lcd.drawLine(42,0,40,2,1);
         lcd.drawLine(42,0,44,2,1);
-    } else if (level == 7) {
+        lcd.drawLine(42,0,42,3,1);   
+    }
+    /// function acts as guidance towards the treasure
+     else if (level == 7) {
 
         lcd.printString("F",42,0); /// print the treasure icon
     } else if (level == 8) {
-
+        
+        timer.stop(); ///stop timer
+        timer.reset();
+        float win_time = timer.read();///value reads the time taken to win the game
+        char win[80]; ///create an array of chars
+        sprintf(win,"Time:%.2f sec",win_time); ///create string
         ticker.detach();
-        game_ticker.attach(&game_timer_isr,0.2);
+        game_ticker.attach(&game_timer_isr,0.2); 
         lcd.clear();
         lcd.printString("Well done!",0,0);
         lcd.refresh();
         wait(2);
         lcd.clear();
-        lcd.printString("You 're a WINNER!",0,0);
-        wait(2);
+        lcd.printString(win,0,0); ///print the time taken to win the game on screen
+        wait(3);
         lcd.clear();
-        lcd.printString("Play again!",0,0);
+        lcd.printString("Play again!",0,0); ///provide play again option
         lcd.drawCircle(70,4,2,1);
         lcd.refresh();
         g_button_flag = 0;
 
         while(1) {
-            if  (g_button1_flag) {
-
+            if  (g_button1_flag) { ///if button1 is pressed
+                ///reset
                 g_button1_flag = 0;
                 level = 0;
                 lcd.clear();
@@ -352,7 +364,8 @@
 {
     /// place obstacles in the screen
     /// as level difficulty increases, more obstacles are added
-    if (level == 0) {
+    
+    if (level == 0) { ///obstacles are located in the ground and they change from one level to another
 
         lcd.drawRect(10,15,2,2,1);
         lcd.drawRect(74,15,2,2,1);
@@ -364,11 +377,11 @@
         lcd.drawRect(74,28,2,2,1);
     } else if (level == 2) {
 
-        lcd.drawCircle(37,25,5,0);
-        lcd.drawCircle(47,25,5,0);
-        lcd.drawCircle(42,11,3,1);
-        lcd.drawCircle(37,25,3,1);
-        lcd.drawCircle(47,25,3,1);
+        lcd.drawCircle(25,25,5,0);
+        lcd.drawCircle(65,25,5,0);
+        lcd.drawCircle(45,11,3,1);
+        lcd.drawCircle(25,25,3,1);
+        lcd.drawCircle(65,25,3,1);
         lcd.drawRect(80,2,2,42,1);
         lcd.drawRect(2,2,2,42,1);
 
@@ -384,7 +397,7 @@
         lcd.drawRect(52,30,2,2,1);
     } else if (level == 4) {
 
-        lcd.drawRect(5+rand() %4 - 2 ,12+rand() %4 - 2,2,2,1);
+        lcd.drawRect(5+rand() %4 - 2 ,12+rand() %4 - 2,2,2,1); ///make obstacles moveable by adding a small random variance in position
         lcd.drawRect(79 + rand() %4 - 2,12 + rand() %4 - 2,2,2,1);
         lcd.drawRect(5+ rand() %4 - 2,30+rand() %4 - 2 ,2,2,1);
         lcd.drawRect(79 + rand() %4 - 2,30+rand() %4 - 2,2,2,1);
@@ -526,10 +539,10 @@
                     option++;
                     break;
             }
-            if (option < 0) {
+            if (option < 0) { /// if the last (down) option is set for selection and user presses joystick down, selector moves at the top
                 option = 2;
             }
-            if (option > 2) {
+            if (option > 2) { /// if the first (up) option is set for selection and user presses joystick up, selector moves at the bottom
                 option = 0;
             }
 
@@ -564,24 +577,24 @@
                                     lcd.printString("Game speed",0,0);  ///print settings menu
                                     lcd.printString("Lcd:Inverse",0,1);
                                     lcd.printString("Sound:ON",0,2);
-                                    lcd.printString("Back",0,3);
+                                    lcd.printString("Back",0,4);
                                 } else {
                                     lcd.printString("Game speed",0,0);  ///print settings menu
                                     lcd.printString("Lcd:Inverse",0,1);
                                     lcd.printString("Sound:OFF",0,2);
-                                    lcd.printString("Back",0,3);
+                                    lcd.printString("Back",0,4);
                                 }
                             } else {
                                 if (sound) {
                                     lcd.printString("Game speed",0,0);  ///print settings menu
                                     lcd.printString("Lcd:Normal",0,1);
                                     lcd.printString("Sound:ON",0,2);
-                                    lcd.printString("Back",0,3);
+                                    lcd.printString("Back",0,4);
                                 } else {
                                     lcd.printString("Game speed",0,0);  ///print settings menu
                                     lcd.printString("Lcd:Normal",0,1);
                                     lcd.printString("Sound:OFF",0,2);
-                                    lcd.printString("Back",0,3);
+                                    lcd.printString("Back",0,4);
                                 }
                             }
                             switch (joystick.direction) {
@@ -610,7 +623,7 @@
                             } else if (option == 2) {
                                 lcd.drawCircle(70,20,2,1);
                             } else {
-                                lcd.drawCircle(35,27,2,1);
+                                lcd.drawCircle(35,36,2,1);
                             }
                             if (g_button_flag) { ///if button is pressed
                                 g_button_flag = 0; ///reset button
@@ -624,20 +637,20 @@
                                             updateJoystick();
                                             lcd.clear();
                                             if (speed == 0) {
-                                                lcd.printString("Slow!",0,0); ///print game speed menu
+                                                lcd.printString("Slow<",0,0); ///print game speed menu
                                                 lcd.printString("Normal",0,1);
                                                 lcd.printString("Fast",0,2);
-                                                lcd.printString("Back",0,3);
+                                                lcd.printString("Back",0,4);
                                             } else if (speed == 1) {
-                                                lcd.printString("Slow",0,0); ///print game speed menu
-                                                lcd.printString("Normal!",0,1);
+                                                lcd.printString("Slow",0,0); //print game speed menu
+                                                lcd.printString("Normal<",0,1);
                                                 lcd.printString("Fast",0,2);
-                                                lcd.printString("Back",0,3);
+                                                lcd.printString("Back",0,4);
                                             } else if (speed == 2) {
-                                                lcd.printString("Slow",0,0); ///print game speed menu
+                                                lcd.printString("Slow",0,0); //print game speed menu
                                                 lcd.printString("Normal",0,1);
-                                                lcd.printString("Fast!",0,2);
-                                                lcd.printString("Back",0,3);
+                                                lcd.printString("Fast<",0,2);
+                                                lcd.printString("Back",0,4);
                                             }
 
                                             switch (joystick.direction) {
@@ -665,20 +678,20 @@
                                             } else if (option == 2) {
                                                 lcd.drawCircle(35,20,2,1);
                                             } else {
-                                                lcd.drawCircle(35,27,2,1);
+                                                lcd.drawCircle(35,36,2,1);
                                             }
 
                                             if (g_button_flag) {
                                                 g_button_flag = 0;
 
                                                 if (option == 0) {
-                                                    game_speed = 0.1;
+                                                    game_speed = 0.1; ///slow
                                                     speed = 0;
                                                 } else if (option == 1) {
-                                                    game_speed = 0.05;
+                                                    game_speed = 0.05; ///normal
                                                     speed = 1;
                                                 } else if (option == 2) {
-                                                    game_speed = 0.04;
+                                                    game_speed = 0.04; ///fast
                                                     speed = 2;
                                                 } else if (option == 3) {
                                                     break;
@@ -699,10 +712,13 @@
 
                                     sound =! sound;
 
-                                    if (sound)
-                                        buzzer.write(0.5);
-                                    else
+                                    if (sound) {
+                                        //buzzer.write(0.5);
+                                       // buzzer.period(1/4000);
+                                    }
+                                    else {
                                         buzzer.write(0.0);
+                                    }
                                     // buzzer.period(1/400);
                                 } else if(option == 3) { ///go back to main menu
                                     break;
@@ -753,19 +769,19 @@
 void checkOverlap()
 {
 
-    for (int i=40+heroX; i<50+heroX; i++) {
+    for (int i=40+heroX; i<50+heroX; i++) { ///loop follows the hero and checks for overlap around him
         for (int j=35+heroY; j<48+heroY; j++) {
 
-            int check = intersection(i,j);
+            int check = intersection(i,j); ///int gets number of pixels around the hero
             //lcd.setPixel(i,j);
             char bit[50];
             sprintf(bit,"Pixels: %d",check);
 
 
-            for(int i = 40; i < 50; i++) {
+            for(int i = 40; i < 50; i++) { ///provides safety if hero is in default position he doesn't get killed by enemies
                 for (int j = 35; j < 47; j++) {
 
-                    if (lcd.getPixel(i,j) && check > 6) {
+                    if (lcd.getPixel(i,j) && check > 6) { ///6 is the offset of the neigbours, as hero has his own neighbours pixels 
                         check = 6;
                     }
                 }
@@ -773,14 +789,20 @@
             if (check > 7) {
                 tries--;
                 //if tries
-
-                lcd.printString("BANG!",0,0);
+                lcd.printString("BANG!",0,0); ///Bang message is displayed when overlap has occured
+                if (sound) {
+                    buzzer.write(0.5);
+                    //buzzer.pulsewidth(0.9);
+                } else {
+                    buzzer.write(0.0);
+                }
                 lcd.refresh();
                 wait(2);
                 lcd.clear();
 
                 if (tries == 2) {
-
+                    
+                    g_button1_flag = 0;
                     lcd.printString("Ready.",20,1);
                     lcd.refresh();
                     wait(0.5);
@@ -790,29 +812,33 @@
                     lcd.printString("Ready...",20,1);
                     lcd.refresh();
                     wait(1);
-                    lcd.printString("GO!",31,2);
+                    lcd.printString("GO!",31,3);
                     lcd.refresh();
                     wait(1);
                     lcd.clear();
                     fsm_state = 1; ///2 lives left
                 } else if (tries == 1) {
-
+                    
+                    g_button1_flag = 0;
                     lcd.clear();
                     lcd.printString("Last Chance!",10,2);
                     lcd.refresh();
                     wait(1);
                     fsm_state = 2; //1 life left
                 } else if (tries == 0) {
-
+                    
+                    g_button1_flag = 0;
                     fsm_state = 3;
                     led_output=fsm[fsm_state];
                     lcd.clear();
                     lcd.printString("Game Over!",10,2);
+                    timer.stop();
+                    timer.reset();
                     lcd.refresh();
                     wait(2);
 
                     ticker.detach();
-                    game_ticker.attach(&game_timer_isr,0.2);
+                    game_ticker.attach(&game_timer_isr,0.2); ///attach a slower ticker to save power while not playing
                     lcd.clear();
                     lcd.printString("Play again!",0,0);
                     lcd.drawCircle(70,4,2,1);
@@ -820,19 +846,21 @@
                     g_button_flag = 0;
 
                     while(1) {
-                        if  (g_button1_flag) {
+                        if  (g_button1_flag) { ///if button1 is pressed
 
-                            g_button1_flag = 0;
-                            level = 0;
+                            g_button1_flag = 0; ///reset button1 flag
+                            level = 0; ///reset parameters
                             tries = 3;
                             fsm_state = 0;
                             led_output=fsm[fsm_state];
+                            buzzer.write(0.0);
                             lcd.clear();
+                            ///reset values
                             heroX = 0;
                             heroY = 0;
                             rectY = 0;
                             circleX = 0;
-                            main();
+                            main(); ///call main
                         }
                         sleep();
                     }
@@ -843,25 +871,15 @@
                 rectY = 0;
                 circleX = 0;
                 led_output=fsm[fsm_state];
+                buzzer.write(0.0);
             }
 
         }
     }
 }
 
-void error()
-{
-    /// display error message
-    while (1) {
-
-        lcd.printString("Error!",0,0);
-        r_led = 0;
-        wait(0.2);
-        r_led = 1;
-        wait(0.2);
-    }
-}
 
 
 
 
+