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-29
Revision:
21:f2bcb2c399ab
Parent:
20:874b009fea11
Child:
22:03acb7389497

File content as of revision 21:f2bcb2c399ab:

/**
@file main.cpp

@brief Program implementation

*/
#include "main.h"
PwmOut buzzer(p25);
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







// Function Prototypes
void pressWasted();

void  pause();
void  winningcondition();
void  losingcondition();
void  drawMap();
void  drawCoin();
void  drawGhost();
void  checkRight();
void  checkLeft();
void  checkDown();
void  checkUp();
void  Boundarycheck();
void  WastedCheck();
void  checkCoin();
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  creditscreen()
{
     
    lcd.printString("Thank You",15,1);
    lcd.printString("For",14,2);
    lcd.printString("Playing",8,3);
    
  
}
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 timerExpired() // ISR for timeout function
{
    timerflag=1;

}


void pressPause()// Interrupt Service Routine (ISR) for pause button
{
    buttonflag=!buttonflag;// set button flag
}


void pause()
{
    timer.attach(&timerExpired,15.0);// Call timeout after 15 seconds
    Jbutton.rise(&pressPause); // event generated on rising edge
    while (buttonflag) {
        lcd.clear();
        lcd.printString("Pause", 25,21);
        while (timerflag) {// Device goes into sleep and lcd is powered down after 15 seconds in pause menu
            timer.detach();
            lcd.turnOff();
            Sleep();
            if (!buttonflag) { // Press button again to wake the device up
                timerflag=0;
                lcd.init();
                buttonflag=0;
            }
        }
    }

}
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);


    }   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);


    } 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);

    } 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)



    } 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 drawGhost()
{
    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 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 pressWasted()  // ISR to swap direction of joystick
{

    buttonflag1=!buttonflag1;
}

void WastedMode()
{

    if (joystick.direction == UP) {
        i++;
    }
    if (joystick.direction == DOWN) {
        i--;
    }
    if (joystick.direction == LEFT) {
        j++;
    }
    if (joystick.direction == RIGHT) {
        j--;
    }
}

void WastedCheck()
{
    button.rise(&pressWasted); // event generated on rising edge
    if(buttonflag1) {
        WastedMode();
        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()
{


    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) {
        i=3;
    }
    if (j<3) {
        j=3;
    }
    if (j>44) {
        j=44;
    }
    if (i>80) {
        i=80;
    }
}


void winningcondition()// Player wins if all coins are eaten
{
    if(w==0) {
        win=true;
    }
}

void losingcondition()//Player loses if pacman is eaten by ghost
{
    if (i==x && j==y) {
        win = false;
        lose = true;
    }
}


int main()
{

    lcd.init();
    calibrateJoystick();  // get centred values of joystick
    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
    PHY_PowerDown();
    tune();
   
    
    
    while(win==false&& lose==false) {
        
     
             
    
         lcd.setBrightness(bright);
        lcd.clear();
        pause();
        winningcondition();
        losingcondition();
        drawMap();
        drawCoin();
        drawGhost();
        checkRight();
        checkLeft();
        checkDown();
        checkUp();
        Boundarycheck();
        WastedCheck();
        checkCoin();
        lcd.refresh();
        wait_ms(35);

    }
    if (win==true) {
        lcd.setBrightness(bright);
        lcd.clear();
        lcd.printString("Yay!", 25,21);
        tune();
        creditscreen();
    }
    if (lose==true) {
        lcd.setBrightness(bright);
        lcd.clear();
        lcd.printString("Game Over", 15,10);
        tune();
         creditscreen();

    }
  
       
    
     
}