My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
6:c11bb31a06b3
Parent:
5:ffa498d7071f
Child:
7:f31a4b4beb91
--- a/main.cpp	Sat Mar 26 14:41:40 2016 +0000
+++ b/main.cpp	Mon Mar 28 12:10:24 2016 +0000
@@ -9,6 +9,8 @@
 #include "stdlib.h"
 
 N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
+
+
 Serial pc(USBTX,USBRX);
 
 #define DIRECTION_TOLERANCE 0.05
@@ -44,23 +46,23 @@
 
 int main()
 {
-    lcd.init(); 
-    init_K64F(); 
-    init_serial(); 
-    button.rise(&button_isr); ///assign rise with ISR 
+    lcd.init();
+    init_K64F();
+    init_serial();
+    button.rise(&button_isr); ///assign rise with ISR
     init_game(); ///initialize game
     calibrateJoystick(); ///calibrate joystick
 
-    ticker.attach(&timer_isr, 0.1); ///attach ticker with ISR every 0.1 sec
+    ticker.attach(&timer_isr,0.1); ///attach ticker with ISR every 0.1 sec
     reset = level; ///set reset = level to check later if level has increased
 
     while (1) {
 
         if (g_timer_flag) {
 
-            g_timer_flag = 0; ///reset flag 
+            g_timer_flag = 0; ///reset flag
             lcd.clear();
-            guidance();  
+            guidance();
             hero();
             enemies();
             obstacles();
@@ -72,26 +74,21 @@
 
             if (reset < level) { ///if level has increased
 
-                reset = level; ///update reset 
+                reset = level; ///update reset
                 rectX = rand() % 84; ///set the position of rect enemy
                 rectY = 0;
                 circleX = 0; ///set position of circle enemy
-                circleY = rand() % 47; 
+                circleY = rand() % 47;
             }
-            for (int i=40+heroX; i<48+heroX; i++) { ///loop through pixels in x coordinate
-                for (int j=40+heroY; j<48+heroY; j++) { ///loop through pixels in y coordinate
-
-                    int n = intersection(i,j); ///get number of pixels overlapping
-                }
-            }
-
-            pc.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
+            pc.printf("x = %f y = %f button = %d \n",joystick.x,joystick.y,joystick.button);
+            pc.printf("heroY = %d , heroX = %d , n = %d \n",heroY, heroX, n);
         }
+        checkOverlap();
         updateJoystick();
         //callibrateJoystick();
         lcd.refresh();
         sleep();
-      //  break;
+        //  break;
     }
 }
 
@@ -118,7 +115,6 @@
 {
 
     g_timer_flag = 1;
-    // lcd.refresh();
 }
 
 void game_timer_isr()
@@ -150,16 +146,19 @@
 
 void enemyRect()
 {
+    ///generate rect shape enemy
     lcd.drawRect(rectX,rectY,5,4,1);
     rectX = rectX + rand()%4 - 2;
-    rectY++;
+    rectY++; ///enemy moving towards hero
 }
 
 void hero()
 {
-    heroX = heroX + 5*xPot;
-    heroY = heroY - 5*yPot;
+    ///draw hero
+    heroX = heroX + 5*xPot; /// control hero's x direction
+    heroY = heroY - 5*yPot; /// control hero's y direction
 
+    ///set x-axis boundaries so hero does not go out of screen
     if (heroX > 35) {
         heroX = 35;
     }
@@ -174,22 +173,24 @@
 
 void enemyCircle()
 {
+    ///generate circle shape enemy
     lcd.drawCircle(circleX,circleY,4,1);
     circleY = circleY + rand() %4 - 2;
 
-    circleX++;
+    circleX++; ///enemy moving towards hero
 }
 
 void init_game()
 {
+    ///initialise game
     //button.mode(PullNone);
-    srand(time(NULL));
-    rectY = 0;
+    srand(time(NULL)); /// generate random numbers
+    rectY = 0; /// init rectX, rectY
     rectX = rand() %40 + 20;
-    circleY = rand() %20 + 10;
+    circleY = rand() %20 + 10; /// init circleX, circleY
     circleX = 0;
 
-    if ( play == 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,2);
@@ -208,22 +209,20 @@
             g_game_timer_flag = 0;
             updateJoystick();
             lcd.clear();
-            menu();
+            menu(); ///get Joystick's value to select option in menu
             //option = menu();
 
-            if (option == 0) {
+            if (option == 0) { /// select Start Game
 
                 lcd.printString("Start Game <",0,0);
                 lcd.printString("Settings",0,2);
                 lcd.printString("Exit",0,4);
-            } 
-            else if (option == 1) {
+            } else if (option == 1) { /// select Settings
 
                 lcd.printString("Start Game",0,0);
                 lcd.printString("Settings <",0,2);
                 lcd.printString("Exit",0,4);
-            } 
-            else {
+            } else { /// select Exit
 
                 lcd.printString("Start Game",0,0);
                 lcd.printString("Settings",0,2);
@@ -233,18 +232,16 @@
         if (g_button_flag) {
 
             g_button_flag = 0;
-            
+
             if (option == 0) {
-                
+
                 game_ticker.detach();
                 break;
-            } 
-            else if (option == 1) {
+            } else if (option == 1) {
 
                 //settings_menu();
                 pc.printf("Modify Settings!");
-            } 
-            else {
+            } else {
 
                 lcd.turnOff();
                 deepsleep();
@@ -255,15 +252,15 @@
 }
 void guidance()
 {
-
-    if (level < 7) {
+    /// 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) {
 
-        lcd.printString("F",42,0);
+        lcd.printString("F",42,0); /// print the treasure icon
     } else if (level == 8) {
 
         ticker.detach();
@@ -290,7 +287,8 @@
 
 void obstacles()
 {
-
+    /// place obstacles in the screen
+    /// as level difficulty increases, more obstacles are added
     if (level == 1) {
 
         lcd.drawRect(10,15,2,2,1);
@@ -343,6 +341,7 @@
 
 void enemies()
 {
+    /// generate enemies in the screen depending on the level difficulty
 
     if (level == 0) {
 
@@ -399,20 +398,20 @@
 int menu()
 {
 
-    if (joystick.y <= 0.33) {
-        option = 0;
-    } else if (joystick.y <= 0.66) {
-        option = 1;
+    if (joystick.y <= 0.33) { /// if Joystick moves up
+        option = 0; /// the pointer moves upper in the menu display
+    } else if (joystick.y <= 0.66) { /// if joystick moves down
+        option = 1; /// else pointer moves down
     } else {
         option = 2;
     }
     return option;
 }
 
-int intersection(int i, int j) // here the neigbours are counted for each cell and we take the value of n
-// and apply the rules depending on the number of neighbours
+int intersection(int i, int j)
 {
-    int n=0; // set n (number of neigbours) as 0
+    /// check for overlap between enemies and hero
+    n=0;
 
     if (lcd.getPixel(i-1,j-1)!=0) //pixel to the top-left
         n++; // increase n by 1
@@ -430,14 +429,29 @@
         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 checkOverlap()
+{
+
+    for (int i=40+heroX; i<50+heroX; i++) {
+        for (int j=35+heroY; j<48+heroY; j++) {
+
+            lcd.setPixel(i,j);
+
+            
+        }
+    }
+}
+
+
+
 void error()
 {
+    /// display error message
     while (1) {
-        
+
         lcd.printString("Error!",0,0);
         r_led = 0;
         wait(0.2);