My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
20:59aa6189a47d
Parent:
19:70d40dac5ae0
Child:
21:6b02ef341358
--- a/main.cpp	Mon May 02 15:08:00 2016 +0000
+++ b/main.cpp	Mon May 02 17:57:27 2016 +0000
@@ -6,9 +6,9 @@
 #include "main.h"
 #include "stdlib.h"
 
-int main()
+int main(void)
 {
-    lcd.init();
+    lcd.init(); ///initialise LED
     init_K64F();
     init_serial();
     init_game(); ///initialize game
@@ -17,42 +17,65 @@
     button.rise(&button_isr); ///assign rise with ISR
     button1.rise(&button1_isr);
 
-    game_ticker.attach(&game_timer_isr,0.2);
+    game_ticker.attach(&game_timer_isr,0.2); ///attach ticker for the menu different from the main ticker
     menu();
     game_ticker.detach();
+    //timer.start();
 
-    ticker.attach(&timer_isr,game_speed); ///attach ticker with ISR every 0.1 sec
+    ticker.attach(&timer_isr,game_speed); ///attach ticker with ISR. Speed is selected by the user
     reset = level; ///set reset = level to check later if level has increased
 
     while (1) {
 
-        if (g_timer_flag) {
+        if (g_timer_flag) {///if timer flag is UP
 
             g_timer_flag = 0; ///reset flag
+            //g_button_flag = 0;
             lcd.clear();
-            guidance();
-            hero();
-            enemies();
-            obstacles();
-
+            guidance(); ///get guidance
+            hero(); ///call hero
+            enemies(); ///generate enemies 
+            obstacles(); ///generate obstacles
+            if (g_button1_flag) { ///if heavy weapon button is pressed
+                
+                g_button1_flag = 0;
+                rectY = 60; ///remove enemies from field
+                rectX = -10;
+                circleX = -10;
+                circleY = 100;
+                //timer.reset();
+                //timer.start();
+            }
+            //float count_time = timer.read();
             if (reset < level) { ///if level has increased
-
                 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;
+                rectY = 0; ///remove enemies from field
+                circleX = 0;
             }
-            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();
+            if (g_button1_flag) { ///if heavy weapon button is pressed
+               
+                g_button1_flag = 0;
+                rectY = 60; ///remove enemies from field
+                rectX = -10;
+                circleX = -10;
+                circleY = 100;
+                //timer.reset();
+                //timer.start();
+            }
+            checkOverlap(); ///check is hero has overlapped with enemies or obstacles 
             updateJoystick();
             lcd.refresh();
         }
-        sleep();
+        sleep();///use sleep and timer triggered interrupt to save power 
     }
 }
 
+
+
+
+
+
+
 void init_serial()
 {
     // set to highest baud - ensure terminal software matches
@@ -76,6 +99,8 @@
     button1.mode(PullDown);
 }
 
+
+///ISR functions
 void timer_isr ()
 {
     g_timer_flag = 1;
@@ -114,9 +139,11 @@
     }
 }
 
+///game functions
+
 void enemies()
 {
-    /// generate enemies in the screen depending on the level difficulty
+    /// generate enemies in the screen depending on the level of difficulty
     if (level == 0) {
 
         enemyRect();
@@ -143,7 +170,8 @@
         enemyCircle();   
     }
     else if (level == 7) {
-           
+           enemyRocket();
+           enemyCircle();
     }
 }
 
@@ -151,13 +179,16 @@
 {
     ///generate rect shape enemy
     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()%6 - 3;
+    if (g_button1_flag == 0) {
+        rectX = rectX + rand()%4 - 2;
+        rectY++; ///enemy moving towards hero
+
+        if (rectY > 50) {
+            rectY = 0;
+        }
+        if (rectX > 75 || rectX < 25) {
+            rectX = 40+rand()%6 - 3;
+        }
     }
 }
 
@@ -168,14 +199,16 @@
     lcd.drawLine(rectX-2,rectY+2,rectX,rectY-1,1);
     lcd.drawLine(rectX+2,rectY+2,rectX,rectY-1,1);
     
-    rectX = rectX + rand()%4 - 2;
-    rectY++; ///enemy moving towards hero
-    
-    if (rectY == 50) {
-        rectY = 0;
-    }
-    if (rectX > 75 || rectX < 10) {
-        rectX = 40+rand()%6 - 3;
+    if (g_button1_flag == 0) {
+        rectX = rectX + rand()%4 - 2;
+        rectY++; ///enemy moving towards hero
+
+        if (rectY > 50) {
+            rectY = 0;
+        }
+        if (rectX > 75 || rectX < 10) {
+            rectX = 40+rand()%6 - 3;
+        }
     }
 }
 void hero()
@@ -239,16 +272,18 @@
     lcd.drawCircle(circleX,circleY,4,1);
     circleY = circleY + rand() %4 - 2;
 
-    if (circleY <= 10) {
-        circleY = 20;
+    if (g_button1_flag == 0) {
+        if (circleY <= 10) {
+            circleY = 20;
+        }
+        if (circleY >= 35) {
+            circleY = 35;
+        }
+        if (circleX > 84) {
+            circleX = 0;
+        }
+        circleX++; ///enemy moving towards hero
     }
-    if (circleY >= 35) {
-        circleY = 35;
-    }
-    if (circleX > 84) {
-        circleX = 0;
-    }
-    circleX++; ///enemy moving towards hero
 }
 
 void init_game()
@@ -290,6 +325,9 @@
         lcd.refresh();
         wait(2);
         lcd.clear();
+        lcd.printString("You 're a WINNER!",0,0);
+        wait(2);
+        lcd.clear();
         lcd.printString("Play again!",0,0);
         lcd.drawCircle(70,4,2,1);
         lcd.refresh();
@@ -300,7 +338,6 @@
 
                 g_button1_flag = 0;
                 level = 0;
-                // play++;
                 lcd.clear();
                 main();
                 r_led = 0;
@@ -416,12 +453,16 @@
         lcd.drawRect(80,30,2,2,1);
         lcd.drawRect(5,30,2,2,1);
     } else if (level == 7) {
+        lcd.printString("??",65,0);
 
-        /*
-        lcd.drawLine(50,15,30,15,1);
-        lcd.drawLine(30,15,30,35,1);
-        lcd.drawLine(30,25,45,25,1);
-        */
+       lcd.drawRect(1,23,82,2,1);
+       if (button && heroX <-30) {
+            g_button_flag = 0;
+            heroX = -30;
+            heroY = -30;  
+        }
+           
+        pc.printf("x = %d , y = %d \n",heroX,heroY);
     }
 }
 
@@ -463,7 +504,7 @@
     }
 }
 
-int menu()
+void menu()
 {
     while(1) {
         if (g_game_timer_flag) {
@@ -533,13 +574,13 @@
                             } 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("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("Lcd:Normal",0,1);
+                                    lcd.printString("Sound:OFF",0,2);
                                     lcd.printString("Back",0,3);
                                 }
                             }