My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
3:d2cc054e8605
Parent:
2:af5b5d42e835
Child:
4:f31bdc212241
--- a/main.cpp	Mon Mar 21 09:29:46 2016 +0000
+++ b/main.cpp	Mon Mar 21 11:36:51 2016 +0000
@@ -36,12 +36,12 @@
     init_K64F();
     init_serial();
     sw2.fall(&sw2_isr);
-    button.fall(&button_isr);
+    button.rise(&button_isr);
     sw3.fall(&sw3_isr);
     init_game();
     calibrateJoystick();
 
-    ticker.attach(&timer_isr,0.1);
+    ticker.attach(&timer_isr, 0.1);
     reset = level;
 
     while (1) {
@@ -52,24 +52,11 @@
             lcd.clear();
             guidance();
             hero();
-            if (level == 0) {
-
-                enemyRect();
-                enemyCircle();
-            } else if (level == 1) {
-
-                enemyRect();
-                enemyCircle();
-                enemyRect();
-            } else if (level == 2) {
-
-            } else if (level == 3) {
-
-            }
+            enemies();
             obstacles();
             count++;
             //heroX++;
-            heroY--;
+            //heroY--;
 
             if (heroY < -45) {
                 heroY = 0;
@@ -85,6 +72,20 @@
                 circleX = 0;
                 circleY = rand() % 47;
             }
+            for (int i=0; i<84; i++) {
+                for (int j=0; j<48; j++) {
+                    // loop through the cells on the grid
+
+                    int n = intersection(i,j);
+
+                    if ( n > 0) {
+
+                        lcd.clear();
+                        lcd.printString("enemy touched",0,0);
+                        break;
+                    }
+                }
+            }
 
             pc.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
 
@@ -185,11 +186,17 @@
 
 void hero()
 {
-
-    lcd.drawLine(40,47+heroY,48,43+heroY,1);
-    lcd.drawLine(40,43+heroY,48,47+heroY,1);
-    lcd.drawLine(44,45+heroY,44,41+heroY,1);
-    lcd.drawCircle(44,39+heroY,2,0);
+    heroX = heroX + 5*xPot;
+    heroY = heroY - 5*yPot;
+    
+    if (heroX > 35) {
+        heroX = 35;
+    } 
+    
+    lcd.drawLine(40+heroX, 47+heroY, 48+heroX, 43+heroY,1);
+    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()
@@ -204,7 +211,8 @@
 
 void init_game()
 {
-
+    button.mode(PullNone);
+    
     srand(time(NULL));
     rectY = 0;
     rectX = rand() %84 + 1;
@@ -227,7 +235,7 @@
             updateJoystick();
             lcd.clear();
             menu();
-            int option = menu();
+            //option = menu();
 
             if (option == 0) {
 
@@ -245,28 +253,24 @@
                 lcd.printString("Settings",0,2);
                 lcd.printString("Exit <",0,4);
             }
-
-            if (g_sw3_flag) {
-
-                g_sw3_flag = 0;
-
-                if (option == 0) {
-
-                    pc.printf("continue!");
-                } else if (option == 1) {
-
-                    pc.printf("display Settings!");
-                } else if (option == 2) {
-
-                    lcd.turnOff();
-                }
-            }
         }
         if (g_sw2_flag) {
-
+            
+            g_sw2_flag = 0;
+            if (option == 0) {
+                
             game_ticker.detach();
             break;
-        }
+            } else if (option == 1) {
+                
+                //settings_menu();
+                pc.printf("Modify Settings!");
+            } else {
+                
+                lcd.turnOff();
+                deepsleep();
+            }
+        } 
       sleep();
     }
 }
@@ -342,9 +346,26 @@
     }
 }
 
+void enemies() {
+    
+    if (level == 0) {
+
+                enemyRect();
+                enemyCircle();
+            } else if (level == 1) {
+
+                enemyRect();
+                enemyCircle();
+                enemyRect();
+            } else if (level == 2) {
+
+            } else if (level == 3) {
+
+            }
+       
+}
 void calibrateJoystick()
 {
-    button.mode(PullDown);
     // must not move during calibration
     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
     joystick.y0 = yPot;
@@ -383,8 +404,6 @@
 int menu()
 {
 
-    int option;
-
     if (joystick.y <= 0.33) {
         option = 0;
     } else if (joystick.y <= 0.66) {
@@ -394,6 +413,31 @@
     }
     return option;
 }
+
+int intersection (int i, int j) {
+
+    int n=0; // set n (number of neigbours) as 0
+
+    if (lcd.getPixel(i-1,j-1)!=0) //pixel to the top-left
+        n++; // increase n by 1
+    if (lcd.getPixel(i-1,j)!=0) //pixel to the left
+        n++; // increase n by 1
+    if (lcd.getPixel(i-1,j+1)!=0) //pixel to the bottom-left
+        n++; // increase n by 1
+    if (lcd.getPixel(i,j-1)!=0) // pixel to the top
+        n++; // increase n by 1
+    if (lcd.getPixel(i,j+1)!=0) //pixel to the bottom
+        n++; // increase n by 1
+    if (lcd.getPixel(i+1,j-1)!=0) //pixel to the top-right
+        n++; // increase n by 1
+    if (lcd.getPixel(i+1,j)!=0) // pixel to the right
+        n++; // increase n by 1
+    if (lcd.getPixel(i+1,j+1)!=0) //pixel to the bottom right
+        n++; // increase n by 1
+
+    return n;
+}
+
 void error()
 {