My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
16:a6ca6858af24
Parent:
15:c7af2ea5f164
Child:
17:2d424db3975f
--- a/main.h	Fri Apr 29 19:35:13 2016 +0000
+++ b/main.h	Mon May 02 08:56:08 2016 +0000
@@ -269,7 +269,6 @@
 
 //testing
 void checkOverlap();
-void userTouched();
 
 /**
 Check for intrersection
@@ -315,7 +314,7 @@
 
 volatile int n;/*!< number of pixels overlapping */
 
-volatile int game_speed;
+volatile float game_speed = 0.05; /*!< value is set to the ticker, allows user to select game speed */
 
 int reset = 0; /*!< used to save current level of difficulty */
 
@@ -329,10 +328,13 @@
 
 int state1 = 1; /*!< controls the direction of the objtacle */
 
-int menuY = 5;
+//int menuY = 5; /*!< controls the position of the circle selector */
+
+int speed = 1; /*!< controls the speed menu display  */
 
-bool normal=0;
-bool sound = 0;
+bool normal=0; /*!< controls the lcd mode selection on menu */
+
+bool sound = 0; /*!< determines if sound is on or off */
 
 void enemies()
 { /// generate enemies in the screen depending on the level difficulty
@@ -350,6 +352,7 @@
 
     }
     enemyCircle();
+    enemyRect();
 }
 
 void enemyRect()
@@ -357,6 +360,12 @@
     lcd.drawRect(rectX,rectY,5,4,1);
     rectX = rectX + rand()%4 - 2;
     rectY++; ///enemy moving towards hero
+    if (rectY == 50){
+    rectY = 0;
+    }
+    if (rectX == 75 || rectX == 25) {
+    rectX = 40+rand()%10 - 5;
+    }
 }
 
 void hero()
@@ -447,8 +456,9 @@
     lcd.printString("Welcome to",11,1);
     lcd.printString("Treasure Hunt!",1,3);
     lcd.refresh();
-    timeout.attach(&timeout_isr,3);
-    sleep();
+    //timeout.attach(&timeout_isr,3);
+    //sleep();
+    wait(3);
     lcd.clear();
 
 }
@@ -466,24 +476,29 @@
     } 
     else if (level == 8) {
 
-        ticker.detach();
+        //ticker.detach();
         lcd.clear();
         lcd.printString("Well done!",0,0);
         lcd.refresh();
         timeout.attach(&timeout_isr,2);
         sleep();
         lcd.clear();
-        lcd.printString("Play again <",0,0);
+        lcd.printString("Play again",0,0);
+        lcd.drawCircle(70,4,2,1);
         lcd.refresh();
-        sleep();
-        if  (g_button1_flag) {
+        g_button1_flag = 0;
+        while(1) {
+            if  (g_button1_flag) {
 
-            g_button1_flag = 0;
-            level = 0;
-           // play++;
-            lcd.clear();
-            main();
-            r_led = 0;
+                g_button1_flag = 0;
+                level = 0;
+                // play++;
+                lcd.clear();
+                main();
+                r_led = 0;
+                lcd.refresh();
+            }
+            sleep();
         }
     }
 }
@@ -637,195 +652,221 @@
 int menu()
 {
     while(1) {
-
-        updateJoystick();
-        lcd.clear();
+        if (g_game_timer_flag) {
+            g_game_timer_flag = 0;
 
-        lcd.printString("Start Game",0,0); ///print the main 
-        lcd.printString("Settings",0,2);
-        lcd.printString("Exit",0,4);
-        //lcd.drawCircle(70,4,2,1);
+            updateJoystick();
+            lcd.clear();
+
+            lcd.printString("Start Game",0,0); ///print the main
+            lcd.printString("Settings",0,2);
+            lcd.printString("Exit",0,4);
+            //lcd.drawCircle(70,4,2,1);
 
-        switch (joystick.direction) { ///check the direction of joystick
-            case UP:
-                option--;
-                break;
-            case DOWN:
-                option++;
-                break;
-        }
-        if (option < 0) {
-            option = 2;
-        }
-        if (option > 2) {
-            option = 0;
-        }
+            switch (joystick.direction) { ///check the direction of joystick
+                case UP:
+                    option--;
+                    break;
+                case DOWN:
+                    option++;
+                    break;
+            }
+            if (option < 0) {
+                option = 2;
+            }
+            if (option > 2) {
+                option = 0;
+            }
 
-        if (option == 0) { ///selection in menu depends on the value of int option
-            lcd.drawCircle(70,4,2,1);
-        } else if (option == 1) {
-            lcd.drawCircle(55,20,2,1);
-        } else if (option == 2) {
-            lcd.drawCircle(35,35,2,1);
-        }
+            if (option == 0) { ///selection in menu depends on the value of int option
+                lcd.drawCircle(70,4,2,1);
+            } else if (option == 1) {
+                lcd.drawCircle(55,20,2,1);
+            } else if (option == 2) {
+                lcd.drawCircle(35,35,2,1);
+            }
 
-        if(g_button_flag) { /// if button is pressed
+            if(g_button_flag) { /// if button is pressed
+
+                g_button_flag = 0; ///reset flag
 
-            g_button_flag = 0; ///reset flag
+                if (option == 0) { ///break the while loop, start game
+                    break;
+                }
+
+                else if (option == 1) { ///enter settings menu
 
-            if (option == 0) { ///break the while loop, start game
-                break;
-            } 
-            
-            else if (option == 1) { ///enter settings menu
+                    option = 0;
 
-                option = 0;
+                    while(1) {
+                        if (g_game_timer_flag) {
+                            g_game_timer_flag = 0;
 
-                while(1) {
+                            updateJoystick(); ///update joystick position
+                            lcd.clear();
+                            if (normal) {
+                                if (sound) {
+                                    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);
+                                } 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);
+                                }
+                            } 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);
+                                } 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);
+                                }
+                            }
+                            switch (joystick.direction) {
+                                case UP:
+                                    option--;
+                                    break;
+                                case DOWN:
+                                    option++;
+                                    break;
+                                case CENTRE:
+                                    option = option;
+                                    break;
+                            }
+                            if (option < 0) { ///if selector is on the top of the screen and UP is pressed, selector goes to the bottom
+                                option = 3;
+                            }
+                            if (option > 3) { ///if selector is on the bottom of the screen and DOWN is pressed, selector goes to the top
+                                option = 0;
+                            }
 
-                    updateJoystick(); ///update joystick position
-                    lcd.clear();
-                    if (normal) {
-                        if (sound) {
-                            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);
-                        } 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);
-                        }
-                    } 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);
-                        } 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);
-                        }
-                    }
-                    switch (joystick.direction) {
-                        case UP:
-                            option--;
-                            break;
-                        case DOWN:
-                            option++;
-                            break;
-                        case CENTRE:
-                            option = option;
-                            break;
-                    }
-                    if (option < 0) { ///if selector is on the top of the screen and UP is pressed, selector goes to the bottom
-                        option = 3;
-                    }
-                    if (option > 3) { ///if selector is on the bottom of the screen and DOWN is pressed, selector goes to the top
-                        option = 0;
-                    }
+                            /// menu selection depends on the position of the Joystick
+                            if (option == 0) {
+                                lcd.drawCircle(70,4,2,1);
+                            } else if (option == 1) {
+                                lcd.drawCircle(70,12,2,1);
+                            } else if (option == 2) {
+                                lcd.drawCircle(70,20,2,1);
+                            } else {
+                                lcd.drawCircle(35,27,2,1);
+                            }
+                            if (g_button_flag) { ///if button is pressed
+                                g_button_flag = 0; ///reset button
 
-                    /// menu selection depends on the position of the Joystick
-                    if (option == 0) {
-                        lcd.drawCircle(70,4,2,1);
-                    } else if (option == 1) {
-                        lcd.drawCircle(70,12,2,1);
-                    } else if (option == 2) {
-                        lcd.drawCircle(70,20,2,1);
-                    } else {
-                        lcd.drawCircle(35,27,2,1);
-                    }
-                    if (g_button_flag) { ///if button is pressed
-                        g_button_flag = 0; ///reset button
+                                if (option == 0) { /// if user selects to modify speed of the game
+
+                                    while(1) { ///set speed of the game
 
-                        if (option == 0) { /// if user selects to modify speed of the game
+                                        if (g_game_timer_flag) {
+                                            g_game_timer_flag = 0;
+                                            updateJoystick();
+                                            lcd.clear();
+                                            if (speed == 0) {
+                                                lcd.printString("Slow!",0,0); ///print game speed menu
+                                                lcd.printString("Normal",0,1);
+                                                lcd.printString("Fast",0,2);
+                                                lcd.printString("Back",0,3);
+                                            } else if (speed == 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);
+                                            } else if (speed == 2) {
+                                                lcd.printString("Slow",0,0); ///print game speed menu
+                                                lcd.printString("Normal",0,1);
+                                                lcd.printString("Fast!",0,2);
+                                                lcd.printString("Back",0,3);
+                                            }
 
-                            while(1) { ///set speed of the game
-                                updateJoystick();
-                                lcd.clear();
-                                lcd.printString("Slow",0,0); ///print game speed menu
-                                lcd.printString("Normal",0,1);
-                                lcd.printString("Fast",0,2);
-                                lcd.printString("Back",0,3);
+                                            switch (joystick.direction) {
+                                                case UP:
+                                                    option--;
+                                                    break;
+                                                case DOWN:
+                                                    option++;
+                                                    break;
+                                                case CENTRE:
+                                                    option = option;
+                                                    break;
+                                            }
+                                            if (option < 0) {
+                                                option = 3;
+                                            }
+                                            if (option > 3) {
+                                                option = 0;
+                                            }
 
-                                switch (joystick.direction) {
-                                    case UP:
-                                        option--;
-                                        break;
-                                    case DOWN:
-                                        option++;
-                                        break;
-                                    case CENTRE:
-                                        option = option;
-                                        break;
-                                }
-                                if (option < 0) {
-                                    option = 3;
-                                }
-                                if (option > 3) {
-                                    option = 0;
-                                }
+                                            if (option == 0) {
+                                                lcd.drawCircle(70,4,2,1);
+                                            } else if (option == 1) {
+                                                lcd.drawCircle(55,12,2,1);
+                                            } else if (option == 2) {
+                                                lcd.drawCircle(35,20,2,1);
+                                            } else {
+                                                lcd.drawCircle(35,27,2,1);
+                                            }
 
-                                if (option == 0) {
-                                    lcd.drawCircle(70,4,2,1);
-                                } else if (option == 1) {
-                                    lcd.drawCircle(55,12,2,1);
-                                } else if (option == 2) {
-                                    lcd.drawCircle(35,20,2,1);
-                                } else {
-                                    lcd.drawCircle(35,27,2,1);
-                                }
-
-                                if (g_button_flag) {
-                                    g_button_flag = 0;
+                                            if (g_button_flag) {
+                                                g_button_flag = 0;
 
-                                    if (option == 0) {
-                                        game_speed = 0.1;
-                                    } else if (option == 1) {
-                                        game_speed = 0.05;
-                                    } else if (option == 2) {
-                                        game_speed = 0.04;
-                                    } else if (option == 3) {
-                                        break;
+                                                if (option == 0) {
+                                                    game_speed = 0.1;
+                                                    speed = 0;
+                                                } else if (option == 1) {
+                                                    game_speed = 0.05;
+                                                    speed = 1;
+                                                } else if (option == 2) {
+                                                    game_speed = 0.04;
+                                                    speed = 2;
+                                                } else if (option == 3) {
+                                                    break;
+                                                }
+                                            }
+                                            lcd.refresh();
+                                        }
+                                        sleep();
                                     }
+                                } else if (option == 1) { /// Lcd inverse mode
+                                    normal =! normal;
+                                    if (normal) {
+                                        lcd.inverseMode();
+                                    } else {
+                                        lcd.normalMode();
+                                    }
+                                } else if (option == 2) { ///select  sound or not
+
+                                    sound =! sound;
+
+                                    if (sound)
+                                        buzzer.write(0.5);
+                                    else
+                                        buzzer.write(0.0);
+                                    // buzzer.period(1/400);
+                                } else if(option == 3) { ///go back to main menu
+                                    break;
                                 }
-                                lcd.refresh();
-                                sleep();
-                                sleep();
                             }
-                        } else if (option == 1) { /// Lcd inverse mode
-                            normal =! normal;
-                            if (normal) {
-                                lcd.inverseMode();
-                            } else {
-                                lcd.normalMode();
-                            }
-                        } else if (option == 2) { ///select  sound or not
-                            
-                            sound =! sound;
-                            
-                            if (sound)
-                            buzzer.write(0.5);
-                            else
-                            buzzer.write(0.0);
-                           // buzzer.period(1/400);
-                        } else if(option == 3) { ///go back to main menu
-                            break;
+                            lcd.refresh();
                         }
+                        sleep();
                     }
-                    lcd.refresh();
-                    sleep();
+                } else if (option == 2) { ///turn off LED
+                    lcd.clear();
+                    lcd.turnOff();
+                    while(1) {
+                        deepsleep();
+                    }
                 }
-            } else if (option == 2) { ///turn off LED
-                lcd.clear();
-                lcd.turnOff();
-                deepsleep();
             }
+            lcd.refresh();
         }
-        lcd.refresh();
         sleep();
     }
 }