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
main.cpp
- Committer:
- el13ks
- Date:
- 2015-04-27
- Revision:
- 12:3f4d9cb7dd8a
- Parent:
- 11:2fd81f59d7a7
- Child:
- 13:098319060433
File content as of revision 12:3f4d9cb7dd8a:
#include "mbed.h" #include "N5110.h" #define DIRECTION_TOLERANCE 0.1// Tolerance of Joystick BusOut myleds(p24,p23,p22); //External LEDS InterruptIn StartButton(p18);//Start button N5110 lcd(p7,p8,p9,p10,p11,p13,p26); //PWR, SCE, RST, DC, MOSI, SCLK, LED AnalogIn bright(p20);//Pot to adjust brightness Ticker timer; //Create ticker object DigitalIn Jbutton(p17);// Joystick Button AnalogIn xPot(p15);// Joystick x-direction AnalogIn yPot(p16);// Joystick y-direction int i=3; // Pacman's starting position int j = 3; int buttonflag=0; // flag for pause button InterruptIn button(p18);//Pause button Ticker pollJoystick; // timer to regularly read the joystick int coinflag = 1; int coinflag2 = 1; int coinflag3 = 1; int coinflag4 = 1; int coinflag5 = 1; int coinflag6 = 1; int coinflag7 = 1; int coinflag8 = 1; int coinflag9 = 1; int coinflag10 = 1; int coinflag11= 1; int coinflag12 = 1; int coinflag13 = 1; int coinflag14 = 1; int coinflag15 = 1; int coinflag16 = 1; int coinflag17 = 1; int coinflag18 = 1; int coinflag19 = 1; int coinflag20 = 1; int coinflag21 = 1; int coinflag22 = 1; 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); // must not move during calibration joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) joystick.y0 = yPot; } void updateJoystick() { joystick.x = xPot - joystick.x0; // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) joystick.y = yPot - joystick.y0; joystick.button = button; // read button state // calculate direction depending on x,y values // tolerance allows a little lee-way in case joystick not exactly in the stated direction if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = CENTRE; } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = UP; } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = DOWN; } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = LEFT; } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = RIGHT; } else { joystick.direction = UNKNOWN; } } void pressPause() { buttonflag=!buttonflag;// set button flag } void pause() { button.rise(&pressPause); // event generated on rising edge while (buttonflag){ lcd.clear(); lcd.printString("Pause", 25,21); } } 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() { 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() { if (coinflag) { lcd.drawCircle(10,3,2,0);//Coin 1.1 } if (coinflag2) { lcd.drawCircle(22,3,2,0);//Coin 1.2 } if (coinflag3) { lcd.drawCircle(35,3,2,0); //Coin 1.3 } if (coinflag4) { lcd.drawCircle(47,3,2,0);//Coin 1.4 } if (coinflag5) { lcd.drawCircle(65,3,2,0);//Coin 1.5 } if (coinflag6) { lcd.drawCircle(80,3,2,0);//Coin 1.6 } if (coinflag7) { lcd.drawCircle(41,14,2,0);//Coin 2.2 } if (coinflag8) { lcd.drawCircle(35,44,2,0);// Coin 5.3 } if (coinflag9) { lcd.drawCircle(47,44,2,0);//Coin 5.4 } if (coinflag10) { lcd.drawCircle(34,24,2,0); //Coin 3.3 } if (coinflag11) { lcd.drawCircle(47,24,2,0); //Coin 3.4 } if (coinflag12) { lcd.drawCircle(41,33,2,0);//Coin 4 } if (coinflag13) { lcd.drawCircle(7,44,2,0);//Coin 5.1 } if (coinflag14) { lcd.drawCircle(74,44,2,0);//Coin 5.6 } if (coinflag15) { lcd.drawCircle(21,44,2,0);//Coin 5.2 } if (coinflag16) { lcd.drawCircle(61,44,2,0);//Coin 5.5 } if (coinflag17) { lcd.drawCircle(7,24,2,0); //Coin 3.1 } if (coinflag18) { lcd.drawCircle(19,24,2,0); //Coin 3.2 } if (coinflag19) { lcd.drawCircle(62,24,2,0); //Coin 3.5 } if (coinflag20) { lcd.drawCircle(74,24,2,0); //Coin 3.6 } if (coinflag21) { lcd.drawCircle(15,15,2,0);//Coin 2.1 } if (coinflag22) { lcd.drawCircle(65,15,2,0);//Coin 2.3 } } void checkCoin() { if (i==10 &&j==3) { coinflag=0; } if (i==22 &&j==3) { coinflag2=0; } if (i==35 &&j==3) { coinflag3=0; } if (i==47 &&j==3) { coinflag4=0; } if (i==65 &&j==3) { coinflag5=0; } if (i==80 &&j==3) { coinflag6=0; } if (i== 41&&j==14) { coinflag7=0; } if (i==35 &&j==44) { coinflag8=0; } if (i==47&&j==44) { coinflag9=0; } if (i==34 &&j==24) { coinflag10=0; } if (i==47 &&j==24) { coinflag11=0; } if (i==41 &&j==33) { coinflag12=0; } if (i==7 &&j==44) { coinflag13=0; } if (i==74 &&j==44) { coinflag14=0; } if (i==21 &&j==44) { coinflag15=0; } if (i==61 &&j==44) { coinflag16=0; } if (i== 7&&j==24) { coinflag17=0; } if (i==19 &&j==24) { coinflag18=0; } if (i==62 &&j==24) { coinflag19=0; } if (i==74 &&j==24) { coinflag20=0; } if (i==15 &&j==15) { coinflag21=0; } if (i==65 &&j==15) { coinflag22=0; } } 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) { 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) } 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); } 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); } 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.setBrightness(bright); lcd.clear(); pause(); drawPacman(); drawMap(); drawCoin(); checkRight(); checkLeft(); checkDown(); checkUp(); Joystickcheck(); Boundarycheck(); checkCoin(); lcd.refresh(); wait_ms(35); } }