My take on the classic Pac man game. Using mbed LPC 1768 and Nokia 5110 LCD Analog joystick used to control direction

Dependencies:   N5110 PowerControl mbed

Revision:
6:af4d3c5a9d43
Parent:
5:61c3bbef19f0
Child:
7:9ecf9df1e95e
--- a/main.cpp	Tue Apr 21 15:35:18 2015 +0000
+++ b/main.cpp	Tue Apr 21 21:29:29 2015 +0000
@@ -14,13 +14,13 @@
 int j = 3;
 
 Ticker pollJoystick; // timer to regularly read the joystick
-int next[84][48]= {0};
+
 int coinflag = 1;
 int coinflag2 = 1;
 int coinflag3 = 1;
 int coinflag4 = 1;
 int coinflag5 = 1;
-bool canRight = 1;
+
 
 enum DirectionName { // create enumerated type (0,1,2,3 etc. for direction)
     UP,         // could be extended for diagonals etc.
@@ -75,8 +75,6 @@
         joystick.direction = UNKNOWN;
     }
 
-
-
 }
 
 void drawMap()
@@ -84,8 +82,6 @@
 
     lcd.drawRect(41,20,10,2,1);
     lcd.drawRect(41,0,2,10,1);
-
-
 }
 
 void checkRight()
@@ -116,119 +112,287 @@
     }
 }
 void checkUp()
-{ 
-    int u=0; 
+{
+    int u=0;
     if(lcd.getPixel (i,j-4))//Check 4 pixels above
         u++;
     if(lcd.getPixel (i,j-5))//Check 5 pixels above
         u++;
     if (u==2) { // Condition for solid obstacles
         j++;
-}
+    }
 }
 
-void checkDown(){ 
-    int d=0; 
+void checkDown()
+{
+    int d=0;
     if(lcd.getPixel (i,j+4))//Check 4 pixels below
         d++;
     if(lcd.getPixel (i,j+5))//Check 5 pixels below
         d++;
     if (d==2) { // Condition for solid obstacles
         j--;
+    }
 }
+
+void drawCoin()
+{
+
+    if (coinflag) {
+        lcd.drawCircle(10,3,2,0);
+    }
+    if (coinflag2) {
+        lcd.drawCircle(20,3,2,0);
+    }
+    if (coinflag3) {
+        lcd.drawCircle(30,3,2,0);
+    }
+    if (coinflag4) {
+        lcd.drawCircle(50,3,2,0);
+    }
+    if (coinflag5) {
+        lcd.drawCircle(60,3,2,0);
+    }
+}
+void checkCoin()
+{
+    if (i==10 &&j==3) {
+        coinflag=0;
+    }
+    if (i==20 &&j==3) {
+        coinflag2=0;
+    }
+    if (i==30 &&j==3) {
+        coinflag3=0;
+    }
+    if (i==50 &&j==3) {
+        coinflag4=0;
+    }
+    if (i==60 &&j==3) {
+        coinflag5=0;
+    }
+
 }
 
-void drawCoin() {
-
-        if (coinflag) {
-            lcd.drawCircle(10,3,2,0);
-        }
-        if (coinflag2) {
-            lcd.drawCircle(20,3,2,0);
-        }
-        if (coinflag3) {
-            lcd.drawCircle(30,3,2,0);
-        }
-        if (coinflag4) {
-            lcd.drawCircle(50,3,2,0);
-        }
-        if (coinflag5) {
-            lcd.drawCircle(60,3,2,0);
-        }
+void Joystickcheck()
+{
+    if (joystick.direction == UP) {
+        j--;
     }
-    void checkCoin() {
-        if (i==10 &&j==3) {
-            coinflag=0;
-        }
-        if (i==20 &&j==3) {
-            coinflag2=0;
-        }
-        if (i==30 &&j==3) {
-            coinflag3=0;
-        }
-        if (i==50 &&j==3) {
-            coinflag4=0;
-        }
-        if (i==60 &&j==3) {
-            coinflag5=0;
-        }
-
+    if (joystick.direction == DOWN) {
+        j++;
+    }
+    if (joystick.direction == LEFT) {
+        i--;
+    }
+    if (joystick.direction == RIGHT) {
+        i++;
     }
 
-    void Joystickcheck() {
-        if (joystick.direction == UP) {
-            j--;
-        }
-        if (joystick.direction == DOWN) {
-            j++;
-        }
-        if (joystick.direction == LEFT) {
-            i--;
-        }
-        if ((joystick.direction == RIGHT)&canRight) {
-            i++;
-        }
+}
+void drawPacman()
+{
+    if(joystick.direction == RIGHT) {
+        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);
+
 
-    }
+    }   else if(joystick.direction == LEFT) {
+        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 Boundarycheck() {
-        if (i<3) {
-            i=3;
-        }
-        if (j<3) {
-            j=3;
-        }
-        if (j>44) {
-            j=44;
-        }
-        if (i>80) {
-            i=80;
-        }
-    }
+
+
+    } else if(joystick.direction == UP) {
+        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);
 
 
 
 
 
 
-    int main() {
+
 
-        lcd.init();
-        calibrateJoystick();  // get centred values of joystick
-        pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
+    } else if (joystick.direction == DOWN) {
+        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);
 
-        while(1) {
-            lcd.clear();
-            lcd.drawCircle(i,j,3,1); //x-coordinate of centre,y-coordinate of centre,radius ;0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)
-            drawMap();
-            drawCoin();
-            checkRight();
-            checkLeft();
-            checkDown();
-            checkUp();
-            Joystickcheck();
-            Boundarycheck();
-            checkCoin();
-            lcd.refresh();
-        }
+    } 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 Boundarycheck()
+{
+    if (i<3) {
+        i=3;
+    }
+    if (j<3) {
+        j=3;
+    }
+    if (j>44) {
+        j=44;
+    }
+    if (i>80) {
+        i=80;
+    }
+}
+
+int main()
+{
+
+    lcd.init();
+    calibrateJoystick();  // get centred values of joystick
+    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
+
+    while(1) {
+        lcd.clear();
+        drawPacman();
+        drawMap();
+        drawCoin();
+        checkRight();
+        checkLeft();
+        checkDown();
+        checkUp();
+        Joystickcheck();
+        Boundarycheck();
+        checkCoin();
+        lcd.refresh();
+    }
+}