Konlatee Sittichaivijit / Mbed 2 deprecated PacManII

Dependencies:   N5110 PowerControl mbed

Revision:
19:e2659a0b91fa
Parent:
18:d0900678656a
Child:
20:874b009fea11
--- a/main.cpp	Tue Apr 28 14:18:07 2015 +0000
+++ b/main.cpp	Tue Apr 28 16:07:28 2015 +0000
@@ -6,63 +6,6 @@
 */
 #include "main.h"
 
-BusOut myleds(p24,p23,p22); //External LEDS
-volatile bool win= false;
-volatile bool lose= false;
-N5110 lcd(p7,p8,p9,p10,p11,p13,p26); //PWR, SCE, RST, DC, MOSI, SCLK, LED
-AnalogIn bright(p20);//Pot to adjust brightness
-Timeout timer; //Create timeout object
-DigitalIn Jbutton(p17);// Joystick Button
-AnalogIn xPot(p15);// Joystick x-direction
-AnalogIn yPot(p16);// Joystick y-direction
-InterruptIn button(p18);//Pause button
-Ticker pollJoystick; // timer to regularly read the joystick
-//int i=3; // Pacman's starting position
-//int j = 3;
-//int buttonflag=0; // flag for pause button
-int x=67; // Ghost starting position
-int y = 44;
-int timerflag=0;// flag for timeout function
-int w=28;// winning condition variable
-
-
-
-int coins[28][2] = { // array for location of coins
-    {10,3},{22,3},{35,3},{47,3},{65,3},{80,3},
-    {41,14},
-    {3,15},{15,15},{65,15},{80,15},
-    {7,24},{19,24},{34,24},{47,24},{62,24},{74,24},
-    {7,33},{23,33},{41,33},{61,33},{74,33},
-    {7,44},{21,44},{35,44},{47,44},{61,44},{74,44}
-};
-
-
-
-
-
-
-enum DirectionName { // create enumerated type (0,1,2,3 etc. for direction)
-    UP,         // could be extended for diagonals etc.
-    DOWN,
-    LEFT,
-    RIGHT,
-    CENTRE,
-    UNKNOWN
-};
-
-typedef struct JoyStick Joystick; // struct for Joystick
-struct JoyStick {
-    float x;    // current x value
-    float x0;   // 'centred' x value
-    float y;    // current y value
-    float y0;   // 'centred' y value
-    int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
-    DirectionName direction;  // current direction
-};
-
-Joystick joystick; // create struct variable
-
-
 void calibrateJoystick() // read default positions of the joystick to calibrate later readings
 {
     button.mode(PullDown);
@@ -112,7 +55,7 @@
 void pause()
 {
     timer.attach(&timerExpired,15.0);// Call timeout after 15 seconds
-    button.rise(&pressPause); // event generated on rising edge
+    Jbutton.rise(&pressPause); // event generated on rising edge
     while (buttonflag) {
         lcd.clear();
         lcd.printString("Pause", 25,21);
@@ -129,133 +72,233 @@
     }
 
 }
+void drawPacman2()
+{
+    if(joystick.direction == RIGHT) {
+        lcd.setPixel(i,j);
+        lcd.setPixel(i+1,j);
+        lcd.setPixel(i+2,j);
+        lcd.setPixel(i+3,j);
+        lcd.setPixel(i-1,j);
+        lcd.setPixel(i-2,j);
+        lcd.setPixel(i-3,j);
+        lcd.setPixel(i,j+1);//Direction dependent(Up and Down)
+        lcd.setPixel(i+1,j+1);//Direction dependent(Up and Down)
+        lcd.setPixel(i+2,j+1);
+        lcd.setPixel(i+3,j+1);
+        lcd.setPixel(i-1,j+1);//Direction dependent(Up and Down)
+        lcd.setPixel(i-2,j+1);
+        lcd.setPixel(i-3,j+1);
+        lcd.setPixel(i,j+2);//Direction dependent(Up and Down)
+        lcd.setPixel(i+1,j+2);//Direction dependent(Up and Down)
+        lcd.setPixel(i+2,j+2);
+        lcd.setPixel(i-1,j+2);//Direction dependent(Up and Down)
+        lcd.setPixel(i-2,j+2);
+        lcd.setPixel(i-1,j+3);//Direction dependent(Up and Down)
+        lcd.setPixel(i,j+3);//Direction dependent(Up and Down)
+        lcd.setPixel(i+1,j+3);//Direction dependent(Up and Down)
+        lcd.setPixel(i-2,j-1);
+        lcd.setPixel(i-3,j-1);
+        lcd.setPixel(i+3,j-1);
+        lcd.setPixel(i+2,j-1);
+        lcd.setPixel(i+2,j-2);
+        lcd.setPixel(i-2,j-2);
 
-void drawMap()
-{
+
+
+
+
+
+
+
 
-    lcd.drawRect(39,0,3,10,1);//x-coordinate (top-left),y-coordinate(top-left), width, height, fill - 0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)
-    lcd.drawRect(40,40,2,7,1);// T-shaped obstacle Vertical
-    lcd.drawRect(7,8,23,2,1);// Left hand "Hook" shaped obstacle horizontal
-    lcd.drawRect(51,8,24,2,1);//Right hand "Hook" shaped obstacle horizontal
-    lcd.drawRect(0,18,3,29,1);// left hand border
-    lcd.drawRect(78,18,5,29,1);// Right hand border
-    lcd.drawRect(11,18,3,21,1);//Left hand C shape obstacle vertical
-    lcd.drawRect(27,11,3,18,1);// Left hand "Hook" shaped obstacle vertical
-    lcd.drawRect(35,37,12,2,1);//T-shaped obstacle Horizontal
-    lcd.drawRect(67,18,3,21,1);// Right hand C shaped obstacle vertical
-    lcd.drawRect(51,11,3,18,1);//Right hand "Hook" shaped obstacle vertical
-    lcd.drawRect(38,18,5,11,1);// Central block
-    lcd.drawRect(15,18,4,2,1);//Left hand C shape obstacle horizontal
-    lcd.drawRect(22,27,5,2,1);//Left hand "Hook" shaped obstacle
-    lcd.drawRect(15,37,4,2,1);//Left hand C shape obstacle
-    lcd.drawRect(55,27,3,2,1);// Right hand "Hook" shaped obstacle
-    lcd.drawRect(63,18,4,2,1);//Right hand C shape obstacle horizontal
-    lcd.drawRect(63,37,4,2,1);//Right hand C shape obstacle
+
+
+
+
 
-}
+    }   else if(joystick.direction == LEFT) {
+        lcd.setPixel(i,j);
+        lcd.setPixel(i+1,j);
+        lcd.setPixel(i+2,j);
+        lcd.setPixel(i+3,j);
+        lcd.setPixel(i-1,j);
+        lcd.setPixel(i-2,j);
+        lcd.setPixel(i-3,j);
+        lcd.setPixel(i,j-1);//Direction dependent(Up and Down)
+        lcd.setPixel(i+1,j-1);//Direction dependent(Up and Down)
+        lcd.setPixel(i+2,j+1);
+        lcd.setPixel(i+3,j+1);
+        lcd.setPixel(i-1,j-1);//Direction dependent(Up and Down)
+        lcd.setPixel(i-2,j+1);
+        lcd.setPixel(i-3,j+1);
+        lcd.setPixel(i,j-2);//Direction dependent(Up and Down)
+        lcd.setPixel(i+1,j-2);//Direction dependent(Up and Down)
+        lcd.setPixel(i+2,j+2);
+        lcd.setPixel(i-1,j-2);//Direction dependent(Up and Down)
+        lcd.setPixel(i-2,j+2);
+        lcd.setPixel(i-1,j-3);//Direction dependent(Up and Down)
+        lcd.setPixel(i,j-3);//Direction dependent(Up and Down)
+        lcd.setPixel(i+1,j-3);//Direction dependent(Up and Down)
+        lcd.setPixel(i-2,j-1);
+        lcd.setPixel(i-3,j-1);
+        lcd.setPixel(i+3,j-1);
+        lcd.setPixel(i+2,j-1);
+        lcd.setPixel(i+2,j-2);
+        lcd.setPixel(i-2,j-2);
 
-void checkRight()
-{
+
+
+
+
+
 
 
-    int r = 0;//Variable for right movement check
-    if(lcd.getPixel (i+3,j))//Check 4 pixels to the right
-        r++;
-    if(lcd.getPixel (i+4,j))//Check 5 pixels to the right
-        r++;
-    if (r==2) { // Condition for solid obstacles
-        i--;
+    } else if(joystick.direction == UP) {
+        lcd.setPixel(i,j);
+        lcd.setPixel(i-1,j);//Direction dependent (left and right)
+        lcd.setPixel(i-2,j);//Direction dependent (left and right)
+        lcd.setPixel(i-3,j);//Direction dependent (left and right)
+        lcd.setPixel(i,j+1);
+        lcd.setPixel(i,j+2);
+        lcd.setPixel(i,j+3);
+        lcd.setPixel(i,j-1);
+        lcd.setPixel(i,j-2);
+        lcd.setPixel(i,j-3);
+        lcd.setPixel(i-1,j+3);
+        lcd.setPixel(i+1,j+3);
+        lcd.setPixel(i,j-1);
+        lcd.setPixel(i-2,j+2);
+        lcd.setPixel(i-1,j+2);
+        lcd.setPixel(i+2,j+2);
+        lcd.setPixel(i+1,j+2);
+        lcd.setPixel(i-1,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i-2,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i-3,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i-1,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i-2,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i-3,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i-1,j-2);
+        lcd.setPixel(i-2,j-2);
+        lcd.setPixel(i+1,j-2);
+        lcd.setPixel(i+2,j-2);
+        lcd.setPixel(i+1,j-3);
+        lcd.setPixel(i-1,j-3);
 
-    }
-}
-void checkLeft()
-{
-    int l = 0 ; // Variable for left movement check
-    if(lcd.getPixel (i-3,j))//Check 4 pixels to the left
-        l++;
-    if(lcd.getPixel (i-4,j))//Check 5 pixels to the left
-        l++;
-    if (l==2) { // Condition for solid obstacles
-        i++;
+
+
+
+
+
 
 
-    }
-}
-void checkUp()
-{
-    int u=0;
-    if(lcd.getPixel (i,j-3))//Check 4 pixels above
-        u++;
-    if(lcd.getPixel (i,j-4))//Check 5 pixels above
-        u++;
-    if(lcd.getPixel (i+1,j-4))
-        u++;
-    if(lcd.getPixel (i-1,j-4))
-        u++;
-    if(lcd.getPixel (i-2,j-4))
-        u++;
-    if (u>=4) { // Condition for solid obstacles
-        j++;
-    }
-}
+    } else if (joystick.direction == DOWN) {
+        lcd.setPixel(i,j);
+        lcd.setPixel(i,j+1);
+        lcd.setPixel(i,j+2);
+        lcd.setPixel(i,j+3);
+        lcd.setPixel(i,j-1);
+        lcd.setPixel(i,j-2);
+        lcd.setPixel(i,j-3);
+        lcd.setPixel(i-1,j+3);
+        lcd.setPixel(i+1,j+3);
+        lcd.setPixel(i,j-1);
+        lcd.setPixel(i-2,j+2);
+        lcd.setPixel(i-1,j+2);
+        lcd.setPixel(i+2,j+2);
+        lcd.setPixel(i+1,j+2);
+        lcd.setPixel(i-1,j-2);
+        lcd.setPixel(i-2,j-2);
+        lcd.setPixel(i+1,j-2);
+        lcd.setPixel(i+2,j-2);
+        lcd.setPixel(i+1,j-3);
+        lcd.setPixel(i-1,j-3);
+        lcd.setPixel(i+1,j);//Direction dependent(left and right)
+        lcd.setPixel(i+2,j);//Direction dependent(left and right)
+        lcd.setPixel(i+3,j);//Direction dependent (left and right)
+        lcd.setPixel(i+1,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i+2,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i+3,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i+1,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i+2,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i+3,j-1);//Direction dependent (left and right)
 
-void checkDown()
-{
-    int d=0;
-    if(lcd.getPixel (i,j+3))//Check 4 pixels below
-        d++;
-    if(lcd.getPixel (i,j+4))//Check 5 pixels below
-        d++;
-    if(lcd.getPixel (i+1,j+4))
-        d++;
-    if(lcd.getPixel (i-1,j+4))
-        d++;
-    if(lcd.getPixel (i-2,j+4))
-        d++;
-    if (d>=4) { // Condition for solid obstacles
-        j--;
-    }
-}
+
 
 
-void drawCoin(void)
-{
-    for (int k=0; k<28; k++) {
-        lcd.drawCircle(coins[k][0], coins[k][1],2,0);
+
+
+
+
+    } else {
+        lcd.setPixel(i,j);
+        lcd.setPixel(i-1,j);//Direction dependent (left and right)
+        lcd.setPixel(i-2,j);//Direction dependent (left and right)
+        lcd.setPixel(i-3,j);//Direction dependent (left and right)
+        lcd.setPixel(i,j+1);
+        lcd.setPixel(i,j+2);
+        lcd.setPixel(i,j+3);
+        lcd.setPixel(i,j-1);
+        lcd.setPixel(i,j-2);
+        lcd.setPixel(i,j-3);
+        lcd.setPixel(i-1,j+3);
+        lcd.setPixel(i+1,j+3);
+        lcd.setPixel(i,j-1);
+        lcd.setPixel(i-2,j+2);
+        lcd.setPixel(i-1,j+2);
+        lcd.setPixel(i+2,j+2);
+        lcd.setPixel(i+1,j+2);
+        lcd.setPixel(i-1,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i-2,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i-3,j+1);//Direction dependent (left and right)
+        lcd.setPixel(i-1,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i-2,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i-3,j-1);//Direction dependent (left and right)
+        lcd.setPixel(i-1,j-2);
+        lcd.setPixel(i-2,j-2);
+        lcd.setPixel(i+1,j-2);
+        lcd.setPixel(i+2,j-2);
+        lcd.setPixel(i+1,j-3);
+        lcd.setPixel(i-1,j-3);
     }
 }
 
 
 
-void checkCoin()
+void drawGhost()
 {
-    for (int c=0; c<28; c++) {
-        if(i==coins[c][0]&& j==coins[c][1]) {
-            coins[c][0]=90;
-            coins[c][1]=50;
-            w--;
-        }
-    }
+    lcd.setPixel(x,y);
+    lcd.setPixel(x+2,y);
+    lcd.setPixel(x-2,y);
+    lcd.setPixel(x,y+1);
+    lcd.setPixel(x,y-1);
+    lcd.setPixel(x-1,y-1);
+    lcd.setPixel(x-2,y-1);
+    lcd.setPixel(x+1,y-1);
+    lcd.setPixel(x+2,y-1);
+    lcd.setPixel(x+2,y+1);
+    lcd.setPixel(x+1,y+1);
+    lcd.setPixel(x-1,y+1);
+    lcd.setPixel(x-2,y+1);
+    lcd.setPixel(x+2,y+2);
+    lcd.setPixel(x+2,y+2);
+    lcd.setPixel(x-1,y+2);
+    lcd.setPixel(x+1,y+2);
+    lcd.setPixel(x,y-2);
+    lcd.setPixel(x+2,y-2);
+    lcd.setPixel(x-1,y-2);
+    lcd.setPixel(x+1,y-2);
+    lcd.setPixel(x-2,y-2);
+    lcd.setPixel(x-2,y+2);
 }
 
 
 
-void Joystickcheck()
-{
-    if (joystick.direction == UP) {
-        j--;
-    }
-    if (joystick.direction == DOWN) {
-        j++;
-    }
-    if (joystick.direction == LEFT) {
-        i--;
-    }
-    if (joystick.direction == RIGHT) {
-        i++;
-    }
+
+
 
-}
+
+
 void drawPacman()
 {
     if(joystick.direction == RIGHT) {
@@ -417,32 +460,199 @@
 
 
 
-void drawGhost()
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+void pressSwap()  // ISR to swap direction of joystick
+{
+
+
+    buttonflag1=!buttonflag1;
+}
+
+
+
+
+void directionSwap()
+{
+
+    if (joystick.direction == UP) {
+        i++;
+    }
+    if (joystick.direction == DOWN) {
+        i--;
+    }
+    if (joystick.direction == LEFT) {
+        j++;
+    }
+    if (joystick.direction == RIGHT) {
+        j--;
+    }
+}
+
+void swapCheck()
+{
+    button.rise(&pressSwap); // event generated on rising edge
+    if(buttonflag1) {
+        directionSwap();
+        drawPacman2();
+    } else {
+        Joystickcheck();
+        drawPacman();
+    }
+
+
+
+}
+
+
+void drawMap()
+{
+
+    lcd.drawRect(39,0,3,10,1);//x-coordinate (top-left),y-coordinate(top-left), width, height, fill - 0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)
+    lcd.drawRect(40,40,2,7,1);// T-shaped obstacle Vertical
+    lcd.drawRect(7,8,23,2,1);// Left hand "Hook" shaped obstacle horizontal
+    lcd.drawRect(51,8,24,2,1);//Right hand "Hook" shaped obstacle horizontal
+    lcd.drawRect(0,18,3,29,1);// left hand border
+    lcd.drawRect(78,18,5,29,1);// Right hand border
+    lcd.drawRect(11,18,3,21,1);//Left hand C shape obstacle vertical
+    lcd.drawRect(27,11,3,18,1);// Left hand "Hook" shaped obstacle vertical
+    lcd.drawRect(35,37,12,2,1);//T-shaped obstacle Horizontal
+    lcd.drawRect(67,18,3,21,1);// Right hand C shaped obstacle vertical
+    lcd.drawRect(51,11,3,18,1);//Right hand "Hook" shaped obstacle vertical
+    lcd.drawRect(38,18,5,11,1);// Central block
+    lcd.drawRect(15,18,4,2,1);//Left hand C shape obstacle horizontal
+    lcd.drawRect(22,27,5,2,1);//Left hand "Hook" shaped obstacle
+    lcd.drawRect(15,37,4,2,1);//Left hand C shape obstacle
+    lcd.drawRect(55,27,3,2,1);// Right hand "Hook" shaped obstacle
+    lcd.drawRect(63,18,4,2,1);//Right hand C shape obstacle horizontal
+    lcd.drawRect(63,37,4,2,1);//Right hand C shape obstacle
+
+}
+
+void checkRight()
 {
-    lcd.setPixel(x,y);
-    lcd.setPixel(x+2,y);
-    lcd.setPixel(x-2,y);
-    lcd.setPixel(x,y+1);
-    lcd.setPixel(x,y-1);
-    lcd.setPixel(x-1,y-1);
-    lcd.setPixel(x-2,y-1);
-    lcd.setPixel(x+1,y-1);
-    lcd.setPixel(x+2,y-1);
-    lcd.setPixel(x+2,y+1);
-    lcd.setPixel(x+1,y+1);
-    lcd.setPixel(x-1,y+1);
-    lcd.setPixel(x-2,y+1);
-    lcd.setPixel(x+2,y+2);
-    lcd.setPixel(x+2,y+2);
-    lcd.setPixel(x-1,y+2);
-    lcd.setPixel(x+1,y+2);
-    lcd.setPixel(x,y-2);
-    lcd.setPixel(x+2,y-2);
-    lcd.setPixel(x-1,y-2);
-    lcd.setPixel(x+1,y-2);
-    lcd.setPixel(x-2,y-2);
-    lcd.setPixel(x-2,y+2);
+
+
+    int r = 0;//Variable for right movement check
+    if(lcd.getPixel (i+3,j))//Check 4 pixels to the right
+        r++;
+    if(lcd.getPixel (i+4,j))//Check 5 pixels to the right
+        r++;
+    if (r==2) { // Condition for solid obstacles
+        i--;
+
+    }
+}
+void checkLeft()
+{
+    int l = 0 ; // Variable for left movement check
+    if(lcd.getPixel (i-3,j))//Check 4 pixels to the left
+        l++;
+    if(lcd.getPixel (i-4,j))//Check 5 pixels to the left
+        l++;
+    if (l==2) { // Condition for solid obstacles
+        i++;
+
+
+    }
+}
+void checkUp()
+{
+    int u=0;
+    if(lcd.getPixel (i,j-3))//Check 4 pixels above
+        u++;
+    if(lcd.getPixel (i,j-4))//Check 5 pixels above
+        u++;
+    if(lcd.getPixel (i+1,j-4))
+        u++;
+    if(lcd.getPixel (i-1,j-4))
+        u++;
+    if(lcd.getPixel (i-2,j-4))
+        u++;
+    if (u>=4) { // Condition for solid obstacles
+        j++;
+    }
 }
+
+void checkDown()
+{
+    int d=0;
+    if(lcd.getPixel (i,j+3))//Check 4 pixels below
+        d++;
+    if(lcd.getPixel (i,j+4))//Check 5 pixels below
+        d++;
+    if(lcd.getPixel (i+1,j+4))
+        d++;
+    if(lcd.getPixel (i-1,j+4))
+        d++;
+    if(lcd.getPixel (i-2,j+4))
+        d++;
+    if (d>=4) { // Condition for solid obstacles
+        j--;
+    }
+}
+
+
+void drawCoin(void)
+{
+    for (int k=0; k<28; k++) {
+        lcd.drawCircle(coins[k][0], coins[k][1],2,0);
+    }
+}
+
+
+
+void checkCoin()
+{
+    for (int c=0; c<28; c++) {
+        if(i==coins[c][0]&& j==coins[c][1]) {
+            coins[c][0]=90;
+            coins[c][1]=50;
+            w--;
+        }
+    }
+}
+
+
+
+void Joystickcheck()
+{
+    if (joystick.direction == UP) {
+        j--;
+    }
+    if (joystick.direction == DOWN) {
+        j++;
+    }
+    if (joystick.direction == LEFT) {
+        i--;
+    }
+    if (joystick.direction == RIGHT) {
+        i++;
+    }
+
+}
+
 void Boundarycheck()// Set boundary conditions so that ghost and pacman cannot move off screen
 {
     if (i<3) {
@@ -490,7 +700,7 @@
         pause();
         winningcondition();
         losingcondition();
-        drawPacman();
+
         drawMap();
         drawCoin();
         drawGhost();
@@ -498,8 +708,9 @@
         checkLeft();
         checkDown();
         checkUp();
-        Joystickcheck();
+
         Boundarycheck();
+        swapCheck();
         checkCoin();
         lcd.refresh();
         wait_ms(35);