ELEC2645 (2015/16) / Mbed 2 deprecated Treasure_Hunt

Dependencies:   N5110 SoftPWM mbed

Files at this revision

API Documentation at this revision

Comitter:
Nikollao
Date:
Thu Mar 24 11:46:35 2016 +0000
Parent:
3:d2cc054e8605
Child:
5:ffa498d7071f
Commit message:
improvements, fixed the button, can press play again, need to improve obstacles, and enemies.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 21 11:36:51 2016 +0000
+++ b/main.cpp	Thu Mar 24 11:46:35 2016 +0000
@@ -1,17 +1,30 @@
+//Fadia
+
 #include "mbed.h"
 #include "N5110.h"
 #include "stdlib.h"
 #include "main.h"
 
-
 N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
 Serial pc(USBTX,USBRX);
 
+#define DIRECTION_TOLERANCE 0.05
 
 Ticker ticker;
 Ticker game_ticker;
 Timeout timeout;
 
+enum DirectionName {
+    UP,
+    DOWN,
+    LEFT,
+    RIGHT,
+    CENTRE,
+    UP_LEFT,
+    UP_RIGHT,
+    DOWN_LEFT,
+    DOWN_RIGHT
+};
 
 typedef struct JoyStick Joystick;
 struct JoyStick {
@@ -25,19 +38,13 @@
 // create struct variable
 Joystick joystick;
 
-// error function hangs flashing an LED
-
 
 int main()
 {
-
-
     lcd.init();
     init_K64F();
     init_serial();
-    sw2.fall(&sw2_isr);
     button.rise(&button_isr);
-    sw3.fall(&sw3_isr);
     init_game();
     calibrateJoystick();
 
@@ -54,9 +61,6 @@
             hero();
             enemies();
             obstacles();
-            count++;
-            //heroX++;
-            //heroY--;
 
             if (heroY < -45) {
                 heroY = 0;
@@ -66,17 +70,16 @@
             if (reset < level) {
 
                 reset = level;
-                count = 0;
                 rectX = rand() % 84;
                 rectY = 0;
                 circleX = 0;
                 circleY = rand() % 47;
             }
-            for (int i=0; i<84; i++) {
+            /*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);
+                    bool n = intersection(i,j);
 
                     if ( n > 0) {
 
@@ -85,7 +88,7 @@
                         break;
                     }
                 }
-            }
+            }*/
 
             pc.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
 
@@ -107,7 +110,7 @@
             if (joystick.direction == DOWN_RIGHT)
                 pc.printf(" DOWN - RIGHT\n");
             if (joystick.direction == DOWN_LEFT)
-                pc.printf(" DOWN - LEFT\n");        
+                pc.printf(" DOWN - LEFT\n");
         }
         updateJoystick();
         //callibrateJoystick();
@@ -144,39 +147,33 @@
 
 void game_timer_isr()
 {
-
     g_game_timer_flag = 1;
 }
 
 void sw2_isr()
 {
-
     g_sw2_flag = 1;
 }
 
-void sw3_isr() {
-    
+void sw3_isr()
+{
     g_sw3_flag = 1;
 }
 
 void button_isr()
 {
-
     g_button_flag =1;
 }
 
 void timeout_isr()
 {
-
     if (button) {
-
-        pc.printf("button is pressed!!!");
+        pc.printf("button is pressed! \n");
     }
 }
 
 void enemyRect()
 {
-
     lcd.drawRect(rectX,rectY,5,4,1);
     rectX = rectX + rand()%4 - 2;
     //rectY = rand()%4-2;
@@ -188,11 +185,13 @@
 {
     heroX = heroX + 5*xPot;
     heroY = heroY - 5*yPot;
-    
+
     if (heroX > 35) {
         heroX = 35;
-    } 
-    
+    }
+    if (heroX < 5) {
+        heroX = 5;
+    }
     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);
@@ -201,31 +200,32 @@
 
 void enemyCircle()
 {
-    if (count > 10) {
-        lcd.drawCircle(circleX,circleY,4,1);
-        circleY = circleY + rand() %4 - 2;
+    lcd.drawCircle(circleX,circleY,4,1);
+    circleY = circleY + rand() %4 - 2;
 
-        circleX++;
-    }
+    circleX++;
 }
 
 void init_game()
 {
-    button.mode(PullNone);
-    
+    //button.mode(PullNone);
     srand(time(NULL));
     rectY = 0;
-    rectX = rand() %84 + 1;
-    circleY = rand() %48 + 1;
+    rectX = rand() %40 + 20;
+    circleY = rand() %20 + 10;
     circleX = 0;
-    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();
-    game_ticker.attach(game_timer_isr,0.5);
+
+    if ( play == 0) {
+
+        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();
+    }
+    game_ticker.attach(game_timer_isr,0.2);
 
     while (1) {
 
@@ -242,36 +242,41 @@
                 lcd.printString("Start Game <",0,0);
                 lcd.printString("Settings",0,2);
                 lcd.printString("Exit",0,4);
-            } else if (option == 1) {
+            } 
+            else if (option == 1) {
 
                 lcd.printString("Start Game",0,0);
                 lcd.printString("Settings <",0,2);
                 lcd.printString("Exit",0,4);
-            } else {
+            } 
+            else {
 
                 lcd.printString("Start Game",0,0);
                 lcd.printString("Settings",0,2);
                 lcd.printString("Exit <",0,4);
             }
         }
-        if (g_sw2_flag) {
+        if (g_button_flag) {
+
+            g_button_flag = 0;
             
-            g_sw2_flag = 0;
             if (option == 0) {
                 
-            game_ticker.detach();
-            break;
-            } else if (option == 1) {
-                
+                game_ticker.detach();
+                break;
+            } 
+            else if (option == 1) {
+
                 //settings_menu();
                 pc.printf("Modify Settings!");
-            } else {
-                
+            } 
+            else {
+
                 lcd.turnOff();
                 deepsleep();
             }
-        } 
-      sleep();
+        }
+        sleep();
     }
 }
 void guidance()
@@ -284,20 +289,28 @@
         lcd.drawLine(42,0,44,2,1);
     } else if (level == 7) {
 
-        lcd.drawLine(42,0,42,6,1);
-        lcd.drawLine(40,3,44,3,1);
-        lcd.drawLine(40,0,44,6,1);
-        lcd.drawLine(40,6,44,0,1);
+        lcd.printString("F",42,0);
     } else if (level == 8) {
 
         ticker.detach();
         lcd.clear();
         lcd.printString("Well done!",0,0);
         lcd.refresh();
-        wait(2);
+        timeout.attach(&timeout_isr,2);
+        sleep();
         lcd.clear();
-        lcd.printString("Play again",0,0);
+        lcd.printString("Play again <",0,0);
         lcd.refresh();
+        sleep();
+
+        if  (g_button_flag) {
+
+            g_button_flag = 0;
+            level = 0;
+            play++;
+            lcd.clear();
+            main();
+        }
     }
 }
 
@@ -334,35 +347,43 @@
         lcd.drawRect(54,15 + rand() %4 - 2,2,2,1);
         lcd.drawRect(30,28 + rand() %4 - 2,2,2,1);
         lcd.drawRect(54 + rand() %4 - 2,28,2,2,1);
-    }else if ( level == 5) {
-       
+    } else if ( level == 5) {
+
         lcd.drawLine(50,35,65,35,1);
         lcd.drawLine(38,30,28,30,1);
         lcd.drawLine(60,25,70,25,1);
         lcd.drawLine(28,25,18,25,1);
         lcd.drawLine(70,20,80,20,1);
         lcd.drawLine(18,20,8,20,1);
-           
+    } else if (level == 6) {
+
+    } else if (level == 7) {
+
+        /*
+        lcd.drawLine(50,15,30,15,1);
+        lcd.drawLine(30,15,30,35,1);
+        lcd.drawLine(30,25,45,25,1);
+        */
     }
 }
 
-void enemies() {
-    
+void enemies()
+{
+
     if (level == 0) {
 
-                enemyRect();
-                enemyCircle();
-            } else if (level == 1) {
+        enemyRect();
+        //enemyCircle();
+    } else if (level == 1) {
 
-                enemyRect();
-                enemyCircle();
-                enemyRect();
-            } else if (level == 2) {
+        enemyRect();
+        //enemyCircle();
+    } else if (level == 2) {
 
-            } else if (level == 3) {
+    } else if (level == 3) {
 
-            }
-       
+    }
+    enemyCircle();
 }
 void calibrateJoystick()
 {
@@ -414,37 +435,36 @@
     return option;
 }
 
-int intersection (int i, int j) {
+bool intersection (int i, int j)
+{
 
-    int n=0; // set n (number of neigbours) as 0
+    bool 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
-
+    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);
     return n;
 }
 
 void error()
 {
-
     while (1) {
-
+        
         lcd.printString("Error!",0,0);
-
         r_led = 0;
         wait(0.2);
         r_led = 1;
--- a/main.h	Mon Mar 21 11:36:51 2016 +0000
+++ b/main.h	Thu Mar 24 11:46:35 2016 +0000
@@ -6,31 +6,11 @@
 InterruptIn sw2(SW2);
 InterruptIn sw3(SW3);
 
-#define DIRECTION_TOLERANCE 0.05
-
 InterruptIn button(PTB2);
 AnalogIn xPot(PTB3);
 AnalogIn yPot(PTB10);
 PwmOut speaker(PTC11);
 
-
-volatile int rectY;
-volatile int rectX ;
-volatile int circleX;
-volatile int circleY;
-volatile int heroX;
-volatile int heroY;
-
-volatile int level = 0;
-volatile int count = 0;
-volatile int reset = 0;
-volatile int g_timer_flag = 0;
-volatile int g_game_timer_flag = 0;
-volatile int g_sw2_flag = 0;
-volatile int g_sw3_flag = 0;
-volatile int g_button_flag = 0;
-volatile int option = 0;
-
 void error();
 // setup serial port
 void init_serial();
@@ -54,17 +34,25 @@
 void obstacles();
 void enemies();
 int menu();
-int intersection(int i, int j);
+bool intersection(int i, int j);
+
+volatile int rectY;
+volatile int rectX ;
+volatile int circleX;
+volatile int circleY;
+volatile int heroX;
+volatile int heroY;
 
-enum DirectionName {
-    UP,
-    DOWN,
-    LEFT,
-    RIGHT,
-    CENTRE,
-    UP_LEFT,
-    UP_RIGHT,
-    DOWN_LEFT,
-    DOWN_RIGHT
-};
+volatile int level = 0;
+volatile int g_timer_flag = 0;
+volatile int g_game_timer_flag = 0;
+volatile int g_sw2_flag = 0;
+volatile int g_sw3_flag = 0;
+volatile int g_button_flag = 0;
+volatile int option = 0;
+volatile int play = 0;
 
+int reset = 0;
+
+
+