My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
5:ffa498d7071f
Parent:
4:f31bdc212241
Child:
6:c11bb31a06b3
--- a/main.cpp	Thu Mar 24 11:46:35 2016 +0000
+++ b/main.cpp	Sat Mar 26 14:41:40 2016 +0000
@@ -1,9 +1,12 @@
 //Fadia
+/**
+@file main.cpp
+@brief Game implementation
 
-#include "mbed.h"
+*/
+#include "main.h"
 #include "N5110.h"
 #include "stdlib.h"
-#include "main.h"
 
 N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
 Serial pc(USBTX,USBRX);
@@ -28,94 +31,67 @@
 
 typedef struct JoyStick Joystick;
 struct JoyStick {
-    double x;    // current x value
-    double x0;   // 'centred' x value
-    double y;    // current y value
-    double y0;   // 'centred' y value
-    int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
+    double x;    /// current x value
+    double x0;   /// 'centred' x value
+    double y;    /// current y value
+    double y0;   /// 'centred' y value
+    int button; /// button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
     DirectionName direction;  // current direction
 };
-// create struct variable
+/// create struct variable
 Joystick joystick;
 
 
 int main()
 {
-    lcd.init();
-    init_K64F();
-    init_serial();
-    button.rise(&button_isr);
-    init_game();
-    calibrateJoystick();
+    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);
-    reset = level;
+    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;
+            g_timer_flag = 0; ///reset flag 
             lcd.clear();
-            guidance();
+            guidance();  
             hero();
             enemies();
             obstacles();
 
-            if (heroY < -45) {
-                heroY = 0;
-                level++;
+            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
             }
 
-            if (reset < level) {
+            if (reset < level) { ///if level has increased
 
-                reset = level;
-                rectX = rand() % 84;
+                reset = level; ///update reset 
+                rectX = rand() % 84; ///set the position of rect enemy
                 rectY = 0;
-                circleX = 0;
-                circleY = rand() % 47;
+                circleX = 0; ///set position of circle enemy
+                circleY = rand() % 47; 
             }
-            /*for (int i=0; i<84; i++) {
-                for (int j=0; j<48; j++) {
-                    // loop through the cells on the grid
-
-                    bool n = intersection(i,j);
+            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
 
-                    if ( n > 0) {
-
-                        lcd.clear();
-                        lcd.printString("enemy touched",0,0);
-                        break;
-                    }
+                    int n = intersection(i,j); ///get number of pixels overlapping
                 }
-            }*/
+            }
 
             pc.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
-
-            // check joystick direction
-            if (joystick.direction == UP)
-                pc.printf(" UP\n");
-            if (joystick.direction == DOWN)
-                pc.printf(" DOWN\n");
-            if (joystick.direction == LEFT)
-                pc.printf(" LEFT\n");
-            if (joystick.direction == RIGHT)
-                pc.printf(" RIGHT\n");
-            if (joystick.direction == CENTRE)
-                pc.printf(" CENTRE\n");
-            if (joystick.direction == UP_RIGHT)
-                pc.printf(" UP - RIGHT\n");
-            if (joystick.direction == UP_LEFT)
-                pc.printf(" UP - LEFT\n");
-            if (joystick.direction == DOWN_RIGHT)
-                pc.printf(" DOWN - RIGHT\n");
-            if (joystick.direction == DOWN_LEFT)
-                pc.printf(" DOWN - LEFT\n");
         }
         updateJoystick();
         //callibrateJoystick();
         lcd.refresh();
         sleep();
+      //  break;
     }
 }
 
@@ -176,8 +152,6 @@
 {
     lcd.drawRect(rectX,rectY,5,4,1);
     rectX = rectX + rand()%4 - 2;
-    //rectY = rand()%4-2;
-    //rectX++;
     rectY++;
 }
 
@@ -435,28 +409,28 @@
     return option;
 }
 
-bool intersection (int i, int j)
+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
 {
-
-    bool n=0; // set n (number of neigbours) as 0
+    int n=0; // set n (number of neigbours) as 0
 
-    if (lcd.getPixel(i-1,j-1)) //pixel to the top-left
-        n = 1; // increase n by 1
-    if (lcd.getPixel(i-1,j)) //pixel to the left
-        n = 1; // increase n by 1
-    if (lcd.getPixel(i-1,j+1)) //pixel to the bottom-left
-        n = 1; // increase n by 1
-    if (lcd.getPixel(i,j-1)) // pixel to the top
-        n = 1; // increase n by 1
-    if (lcd.getPixel(i,j+1)) //pixel to the bottom
-        n = 1; // increase n by 1
-    if (lcd.getPixel(i+1,j-1)) //pixel to the top-right
-        n = 1; // increase n by 1
-    if (lcd.getPixel(i+1,j)) // pixel to the right
-        n = 1; // increase n by 1
-    if (lcd.getPixel(i+1,j+1)) //pixel to the bottom right
-        n = 1; // increase n by 1
-    //pc.printf("Number of neighbours = %d \n",n);
+    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;
 }