Konlatee Sittichaivijit / Mbed 2 deprecated PacManII

Dependencies:   N5110 PowerControl mbed

Revision:
22:03acb7389497
Parent:
21:f2bcb2c399ab
Child:
23:e7f69bc42b29
--- a/main.cpp	Wed Apr 29 19:25:24 2015 +0000
+++ b/main.cpp	Wed Apr 29 21:35:36 2015 +0000
@@ -9,8 +9,8 @@
 float frequency[]= {494,494,988,988,740,622,988,587,622,440,1047,784,659,1047,784,659,494,988,740,622,988,587,622,659,698,698,740,784,784,831,880,988}; //frequency array
 
 float beat[]= {0.5,0.5,0.5,0.5,0.5,1,1,0.5,0.5,0.5,0.5,0.5,1,1,0.5,0.5,0.5,0.5,0.5,1,1,0.25,0.5,0.25,0.25,0.5,0.25,0.25,0.5,1}; //beat array
-
-
+Ticker ghost;
+int followflag=0;
 
 
 
@@ -35,27 +35,53 @@
 void  creditscreen();
 
 
-void tune()
-{ 
-   
-   for (int q=0; q<=31; q++) {
-    buzzer.period(1/(frequency[q])); // set PWM period
-    buzzer=0.5; // set duty cycle
-    wait(0.5*beat[q]); // hold for beat period
-    buzzer=0;
+void ghosttimerexpired()
+{
+    followflag=1;
+}
+void ghostfollow()
+{
+
+
+
+    if( y<j) { // I
+        y++;
+    }
+
+    if(y>j) { // If ghost is directly below pacman, move upwards
+        y--;
+
+    }
+    if ( x>i) {
+        x--;
+    }
+    if (x<i) {
+        x++;
+    }
 
 }
+
+void tune()
+{
+
+    for (int q=0; q<=31; q++) {
+        buzzer.period(1/(frequency[q])); // set PWM period
+        buzzer=0.5; // set duty cycle
+        wait(0.5*beat[q]); // hold for beat period
+        buzzer=0;
+
     }
+}
 
 
 void  creditscreen()
 {
-     
+
     lcd.printString("Thank You",15,1);
-    lcd.printString("For",14,2);
-    lcd.printString("Playing",8,3);
-    
-  
+    lcd.printString("For",14,20);
+    lcd.printString("Playing",10,3);
+
+
 }
 void calibrateJoystick() // read default positions of the joystick to calibrate later readings
 {
@@ -676,16 +702,14 @@
     calibrateJoystick();  // get centred values of joystick
     pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
     PHY_PowerDown();
-    tune();
-   
-    
-    
+    //tune();
+    ghost.attach(&ghosttimerexpired,0.2);
+
+
     while(win==false&& lose==false) {
-        
-     
-             
-    
-         lcd.setBrightness(bright);
+
+
+        lcd.setBrightness(bright);
         lcd.clear();
         pause();
         winningcondition();
@@ -700,6 +724,15 @@
         Boundarycheck();
         WastedCheck();
         checkCoin();
+        if (followflag){
+            followflag=0; 
+            
+        
+        
+        
+        ghostfollow();
+        
+        }
         lcd.refresh();
         wait_ms(35);
 
@@ -714,15 +747,16 @@
     if (lose==true) {
         lcd.setBrightness(bright);
         lcd.clear();
-        lcd.printString("Game Over", 15,10);
+        lcd.printString("Game Over", 10,10);
         tune();
-         creditscreen();
+        lcd.clear();
+        creditscreen();
 
     }
-  
-       
-    
-     
-} 
+
+
 
 
+}
+
+