My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
14:91ae0afe9037
Parent:
13:359d3aa66352
Child:
15:c7af2ea5f164
--- a/main.h	Thu Apr 28 22:04:15 2016 +0000
+++ b/main.h	Fri Apr 29 14:19:57 2016 +0000
@@ -306,12 +306,11 @@
 
 int menuY = 5;
 
+bool normal;
+
 
 void enemies()
-
-{
-    /// generate enemies in the screen depending on the level difficulty
-
+{ /// generate enemies in the screen depending on the level difficulty
     if (level == 0) {
 
         enemyRect();
@@ -329,34 +328,49 @@
 }
 
 void enemyRect()
-{
-    ///generate rect shape enemy
+{ ///generate rect shape enemy
     lcd.drawRect(rectX,rectY,5,4,1);
     rectX = rectX + rand()%4 - 2;
     rectY++; ///enemy moving towards hero
 }
 
 void hero()
-{
-    ///cotrol hero
-    
-    if (xPot > 0.60) { 
+{///cotrol hero
+    if (joystick.direction == RIGHT) { 
         heroX--;
     }
-     else if (xPot < 0.40){
+     else if (joystick.direction == LEFT){
         heroX++;
     } else {
         heroX = heroX; 
     }
     
-    if (yPot > 0.60) {
+    if (joystick.direction == UP) {
         heroY--;
     } 
-    else if (yPot < 0.40) {
+    else if (joystick.direction == DOWN) {
         heroY++;
     } else {
         heroY = heroY;
     }
+    
+    if (joystick.direction == UP_LEFT) {
+        heroY--;
+        heroX++;   
+    }
+    else if (joystick.direction == UP_RIGHT) {
+            heroY--;
+            heroX--;    
+    }
+    else if (joystick.direction == DOWN_RIGHT) {
+            heroY++;
+            heroX--;    
+    }
+    else if (joystick.direction == DOWN_LEFT) {
+            heroY++;
+            heroX++;    
+    }
+    
     ///set x-axis boundaries so hero does not go out of screen
     if (heroX > 35) {
         heroX = 35;
@@ -372,8 +386,7 @@
 }
 
 void enemyCircle()
-{
-    ///generate circle shape enemy
+{///generate circle shape enemy
     lcd.drawCircle(circleX,circleY,4,1);
     circleY = circleY + rand() %4 - 2;
     
@@ -390,36 +403,34 @@
 void init_game()
 {
     ///initialise game
-    //button.mode(PullNone);
     srand(time(NULL)); /// generate random numbers
     rectY = 0; /// init rectX, rectY
     rectX = rand() %40 + 20;
     circleY = rand() %20 + 10; /// init circleX, circleY
     circleX = 0;
 
-    if ( play == 0) { ///if its first play print welcome message, otherwise don't
+    lcd.setBrightness(0.5); // put LED backlight on 50%
+    timeout.attach(&timeout_isr,3);
+    lcd.printString("Welcome to",11,1);
+    lcd.printString("Treasure Hunt!",1,3);
+    lcd.refresh();
+    sleep();
+    lcd.clear();
 
-        lcd.setBrightness(0.5); // put LED backlight on 50%
-        timeout.attach(&timeout_isr,2);
-        lcd.printString("Welcome to",11,1);
-        lcd.printString("Treasure Hunt!",1,3);
-        lcd.refresh();
-        sleep();
-        lcd.clear();
-    }
 }
 void guidance()
-{
-    /// show arrow to act as guidance towards the treasure
+{ /// show arrow to act as guidance towards the treasure
     if (level < 7) { ///check level of difficulty
 
         lcd.drawLine(42,0,42,4,1);
         lcd.drawLine(42,0,40,2,1);
         lcd.drawLine(42,0,44,2,1);
-    } else if (level == 7) {
+    } 
+    else if (level == 7) {
 
         lcd.printString("F",42,0); /// print the treasure icon
-    } else if (level == 8) {
+    } 
+    else if (level == 8) {
 
         ticker.detach();
         lcd.clear();
@@ -596,7 +607,7 @@
         updateJoystick();
         lcd.clear();
 
-        lcd.printString("Start Game",0,0); ///print the main menu
+        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);
@@ -641,8 +652,8 @@
                     updateJoystick(); ///update joystick position
                     lcd.clear();
                     lcd.printString("Game speed",0,0);  ///print settings menu
-                    lcd.printString("Lcd Mode",0,1);
-                    lcd.printString("Sound",0,2);
+                    lcd.printString("Lcd: Normal",0,1);
+                    lcd.printString("Sound: ON",0,2);
                     lcd.printString("Back",0,3);
                     switch (joystick.direction) {
                         case UP:
@@ -662,8 +673,9 @@
                         option = 0;
                     }
 
+                    /// menu selection depends on the position of the Joystick
                     if (option == 0) {
-                        lcd.drawCircle(70,4,2,1); /// menu selection depends on the position of the Joystick
+                        lcd.drawCircle(70,4,2,1);
                     } else if (option == 1) {
                         lcd.drawCircle(55,12,2,1);
                     } else if (option == 2) {
@@ -674,7 +686,7 @@
                     if (g_button_flag) { ///if button is pressed
                         g_button_flag = 0; ///reset button
 
-                        if (option == 0) {
+                        if (option == 0) { /// if user selects to modify speed of the game
 
                             while(1) { ///set speed of the game
 
@@ -726,10 +738,16 @@
                                     }
                                 }
                                 lcd.refresh();
-                                sleep();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     sleep();
+                                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
                             buzzer.write(0.5);
                         } else if(option == 3) { ///go back to main menu
@@ -779,9 +797,26 @@
 
     for (int i=40+heroX; i<50+heroX; i++) {
         for (int j=35+heroY; j<48+heroY; j++) {
+        
+            int check = intersection(i,j);
+            //lcd.setPixel(i,j);
+            char bit[50];
+            sprintf(bit,"Pixels: %d",check);
+            
+            if (check > 7) {
+                for(int i = 40; i < 45; i++) {
+                    for (int j = -20; i < 10; j++) {
 
-            int count = intersection(i,j);
-            lcd.setPixel(i,j);
+                        if (lcd.getPixel(i,j)!=0) {
+                            check = 6;
+                        }
+                    }
+                }
+
+                lcd.printString(bit,0,0);
+                lcd.refresh();
+                wait(3);
+            }
         }
     }
 }