Racing Cars game using N5110 LCD and thumb Joystick

Dependencies:   N5110 PowerControl beep mbed

main.cpp

Committer:
el13gs
Date:
2015-05-08
Revision:
6:289f237b8d90
Parent:
5:243718c3cd8b
Child:
7:edf0f1fcb16b

File content as of revision 6:289f237b8d90:

/**
@file main.cpp
@brief Contains all functions implementations, and main program
*/
#include "main.h"

int main()

{
    /** Ethernet port power Down */
    //PHY_PowerDown();
    //Peripheral_PowerDown(0x40);

    char soundString[3]; // Create buffer String that stores the sound state String("YES","NO")
    sprintf(soundString,"%s","YES"); //print Initial String

    calibrateJoystick();  // get centred values of joystick
    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 30 times per second


    lcd.setXYAddress(0,0);//set the XY address to 0.0 as initialising condition
    initTable();//initialise the array table
    lcd.init();//initialise LCD screen
    lcd.refresh();
    start.rise(&startButtonPressed);
    reset.rise(&resetButtonPressed);


MainMenu:
    int menuPointer=1;
    startButtonFlag=0;
    clearCells(84,48);
    lcd.drawCircle(22,19,2,1); //draw pointer around Play submenu when selected
    lcd.drawCircle(53,19,2,1); //draw pointer around Play submenu when selected
    lcd.printString("MAIN MENU",15,0); //print title
    lcd.printString("Play",26,2); //print submenu title
    lcd.printString("Options",20,3); //print submenu title
    lcd.drawLine(15,9,65,9,1);//underline main menu title

    while(startButtonFlag==0) {

        if (joystick.direction==DOWN) {
            lcd.drawCircle(16,27,2,1); //draw pointer around Options submenu when selected
            lcd.drawCircle(64,27,2,1); //draw pointer around Options submenu when selected

            lcd.clearCircle(53,19,2,1);
            lcd.clearCircle(22,19,2,1);

            menuPointer=2;

        }
        wait(0.1);
        if(joystick.direction==UP) {
            lcd.drawCircle(22,19,2,1); //draw pointer around Play submenu when selected
            lcd.drawCircle(53,19,2,1); //draw pointer around Play submenu when selected

            lcd.clearCircle(16,27,2,1); //draw pointer around Options submenu when selected
            lcd.clearCircle(64,27,2,1);
            menuPointer=1;
        }



    }

    switch(menuPointer) {

        case 1:
            initTable();
            startButtonFlag=0;
            lives=5;
            round=1;
            coins=0;
            startButtonFlag=0;
            clearCells(84,48);
            wait(0.5);
            lcd.refresh();
            lcd.drawRect(x,v,w,h,0);
            timer.attach(&movePlayer,0.1);

            gameInitial();

            char buffer[1];
            sprintf(buffer,"%d",lives);
            lcd.printString(buffer,74,1);

            char buffer1[1];
            sprintf(buffer1,"%d",round);
            lcd.printString(buffer1,74,3);

            char buffer2[2];
            sprintf(buffer2,"%d",coins);
            lcd.printString(buffer2,72,5);

            while(gamePlays) {

                lcd.refresh();

                sprintf(buffer1,"%d",round);
                lcd.printString(buffer1,74,3);

                sprintf(buffer,"%d",lives);
                lcd.printString(buffer,74,1);

                sprintf(buffer2,"%d",coins);
                lcd.printString(buffer2,72,5);

                checkPlayerPos();
                enemy1MovesDown();
                enemy2MovesDown();
                checkPlayerPos();
                enemy3MovesDown();
                lcd.refresh();

                coinMoves();
                checkPlayerPos();
            }
            if (gamePlays==0){
                gamePlays=1;
                goto MainMenu;}
            break;

        case 2:

            int optionsMenu=1;

            startButtonFlag=0;

            clearCells(84,48);
            lcd.refresh();
            wait(0.02);
            lcd.printString("OPTIONS",18,0);
            lcd.printString("Exit",23,5);
            lcd.printString("Brightness",6,3);
            lcd.printString("Sound",6,2);
            lcd.drawLine(15,8,61,8,1);//underline Options Menu title

            char buffer3[2];
            sprintf(buffer3,"%d",brightnessDisplay);
            lcd.printString(buffer3,68,3);


            lcd.printString(soundString,40,2);

            while(optionsMenu) {

                if (joystick.direction==UP) {
                    optionsPointer--;
                    if(optionsPointer==0) {
                        optionsPointer=1;
                    }
                }
                wait(0.1);

                if(joystick.direction==DOWN) {
                    optionsPointer++;
                    if(optionsPointer>3) {
                        optionsPointer=3;
                    }

                }


                if (optionsPointer==1) {
                    lcd.drawCircle(2,19,2,1);
                    lcd.clearCircle(2,27,2,1);
                    lcd.clearCircle(19,44,2,1);
                    if(joystick.direction==LEFT) {
                        sounds=1;
                        lcd.refresh();
                        sprintf(soundString,"%s","YES");
                        lcd.printString(soundString,40,2);
                    }
                    if(joystick.direction==RIGHT) {
                        sounds=0;
                        lcd.refresh();
                        sprintf(soundString,"%s","NO ");
                        lcd.printString(soundString,40,2);
                    }

                }
                if (optionsPointer==2) {
                    lcd.drawCircle(2,27,2,1);
                    lcd.clearCircle(2,19,2,1);
                    lcd.clearCircle(19,44,2,1);

                    if(joystick.direction==LEFT) {
                        brightness=brightness-.1;
                        brightnessDisplay=brightnessDisplay-1;
                        if(brightness<0.1||brightnessDisplay<0) {
                            brightnessDisplay=1;
                            brightness=0.1;
                        }
                        lcd.setBrightness(brightness);

                        char buffer3[2];
                        sprintf(buffer3,"%d",brightnessDisplay);
                        lcd.printString(buffer3,68,3);
                    }

                    if(joystick.direction==RIGHT) {
                        brightness=brightness+.1;
                        brightnessDisplay=brightnessDisplay+1;
                        if(brightness>0.9||brightnessDisplay>9) {
                            brightnessDisplay=9;
                            brightness=0.9;
                        }
                        lcd.setBrightness(brightness);

                        char buffer4[2];
                        sprintf(buffer4,"%d",brightnessDisplay);
                        lcd.printString(buffer4,68,3);
                    }

                }
                if (optionsPointer==3) {
                    lcd.drawCircle(19,44,2,1);
                    lcd.clearCircle(2,19,2,1);
                    lcd.clearCircle(2,27,2,1);

                    if(startButtonFlag) {
                        wait(0.5);
                        startButtonFlag=0;
                        goto MainMenu;
                    }
                }
                wait(0.1);

            }
            break;
    }
}

void startButtonPressed()
{
    startButtonFlag=!startButtonFlag;
}

void resetButtonPressed()
{
    resetButtonFlag=!resetButtonFlag;
}


void movePlayer()
{
    if (gamePlays) {
        //lcd.refresh();
        if (joystick.direction==RIGHT&&x+w<58) {

            clearRect(x,v,w,h);
            lcd.refresh();
            x=x+4;
            lcd.drawRect(x,v,w,h,0);
            lcd.refresh();
        }

        if (joystick.direction==LEFT&&x+w>10) {

            clearRect(x,v,w,h);
            lcd.refresh();
            x=x-4;

            lcd.drawRect(x,v,w,h,0);
            lcd.refresh();
        }

        if (joystick.direction==UP && v>2) {

            clearRect(x,v,w,h);
            lcd.refresh();
            v=v-4;
            lcd.drawRect(x,v,w,h,0);
            lcd.refresh();
        }
        if (joystick.direction==DOWN && v<32) {
            clearRect(x,v,w,h);
            lcd.refresh();
            v=v+4;
            lcd.drawRect(x,v,w,h,0);
            lcd.refresh();
        }

    }
}

void calibrateJoystick()
{
    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;
    joystick.y = yPot - joystick.y0;
    // read button state
    joystick.button = button;

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

    // set flag for printing
    printFlag = 1;
}


void clearRect(int x,int v,int w,int h)
{
    for(int i=x; i<=x+w; i++) {
        for(int j=v; j<=v+h; j++) {
            lcd.clearPixel(i,j);
            table[i][j]=0;
        }
    }
}

void gameInitial()
{
    lcd.drawLine(60,0,60,48,1);
    lcd.drawLine(60,16,84,16,1);
    lcd.drawLine(60,32,84,32,1);
    lcd.drawLine(20,0,20,48,2);
    lcd.drawLine(40,0,40,48,2);
}

void enemy1MovesDown()
{
    clearRect(enemy1x,(j-a),w,h);
    lcd.refresh();
    lcd.drawRect(enemy1x,j,w,h,1);
    lcd.refresh();
    setRectCells(enemy1x,j,w,h);
    lcd.refresh();
    wait(0.05);

    j=j+a;
    //lcd.refresh();

    if(j>60) {
        j=-(rand()%100+80);
    }

}

void enemy2MovesDown()
{
    clearRect(enemy2x,(p-a),w,h);
    lcd.refresh();
    lcd.drawRect(enemy2x,p,w,h,1);
    lcd.refresh();
    setRectCells(enemy2x,p,w,h);
    lcd.refresh();
    wait(0.05);
    p=p+a;
    //lcd.refresh();

    if(p>60) {
        p=-(rand()%40+20);
    }
    while(p==q) {
        p=-(rand()%40+20);
    }

}

void enemy3MovesDown()
{
    clearRect(enemy3x,(q-a),w,h);
    lcd.refresh();
    lcd.drawRect(enemy3x,q,w,h,1);
    lcd.refresh();
    setRectCells(enemy3x,q,w,h);
    lcd.refresh();
    wait(0.05);
    lcd.refresh();
    q=q+a;
    if (q>70) {
        q=-(rand()%10+1);
    }

}

void setRectCells(int x,int v,int w,int h)
{

    for(int i=x; i<=x+w; i++) {
        for(int j=v; j<=v+h; j++) {
            if(j>0&&j<48) {
                table[i][j]=1;
            }
        }
    }
}

void initTable()
{
    for (int i=0; i<=83; i++) {
        for(int j=0; j<=47; j++) {
            table[i][j]=0;
        }
    }
}

void checkPlayerPos()
{
    if (table[x+w][v+h]==1||table[x][v+h]==1||table[x][v]==1||table[x+w][v]==1) {
        loose();
    }
    if (table[x+w][v+h]==2||table[x][v+h]==2||table[x][v]==2||table[x+w][v]==2) {
        coins=coins+5;
        clearCoin(c);
        if(sounds) {
            buzzer.beep(2200,0.4);
            buzzer.beep(1000,0.2);
        }
        coinAppear=0;

        if(coins==10) {
            round=2;
            gamePlays=0;
            clearCells(84,48);
            lcd.refresh();
            lcd.printString("ROUND 2",10,2);
            wait(1);
            clearCells(84,48);
            lcd.refresh();
            gamePlays=1;
            gameInitialise();
            a=6;
        }
        if(coins==70) {
            round=3;
            gamePlays=0;
            clearCells(84,48);
            lcd.refresh();
            lcd.printString("ROUND 3",10,2);
            wait(1);
            clearCells(84,48);
            lcd.refresh();
            gamePlays=1;
            gameInitialise();
            a=8;
        }
        if(coins==90) {
            lcd.printString("YOU WIN!",4,2);
            wait(3);
            gamePlays=0;
        }
    }
}


void coinMoves()
{
    if(table[xPos][c]==1) { //prevent coin from appearing above enemies
        coinAppear=0;
    }
    clearCoin(c);
    c=c+4;
    drawCoin(c);
    if(c>80) {
        c=-(rand()%10);
        xPos=rand()%52+4;
        coinAppear=1;
    }
}

void drawCoin(int c)
{
    if (c>4&&c<48&&coinAppear==1) {
        lcd.drawCircle(xPos,c,4,0); //Draw Coin Circle Pixels
        setCircleCells(xPos,c,4);// Set Coin array's cells values to 2

        // "c" character Print on coin
        lcd.setPixel(xPos,c-1);
        lcd.setPixel(xPos,c+2);
        lcd.setPixel(xPos+1,c-1);
        lcd.setPixel(xPos+1,c+2);
        lcd.setPixel(xPos-1,c);
        lcd.setPixel(xPos-1,c+1);

        lcd.refresh();
    }
}

void clearCoin(int c)
{
    if(c>4&&c<48&&coinAppear==1) {
        lcd.clearCircle(xPos,c,4,1); //Clear the Coin 
        clearCircleCells(xPos,c,4);// Set Coin array's cells values to 0
        lcd.refresh();
    }
}


void setCircleCells(int x0,int y0,int radius)
{
    //function from N5110 Library
    int x = radius;
    int y = 0;

    while(x >= y) {

        setLineCells(x+x0,y+y0,-x+x0,y+y0,0); //set line's Cells values to 2
        setLineCells(y+x0,x+y0,-y+x0,x+y0,0);//set line's Cells values to 2
        setLineCells(y+x0,-x+y0,-y+x0,-x+y0,0);//set line's Cells values to 2
        setLineCells(x+x0,-y+y0,-x+x0,-y+y0,0);//set line's Cells values to 2

        y++;
    }
}

void clearCircleCells(int x0,int y0,int radius)
{
    //function from N5110 Library
    int x = radius;
    int y = 0;

    while(x >= y) {
        clearLineCells(x+x0,y+y0,-x+x0,y+y0,0);
        clearLineCells(y+x0,x+y0,-y+x0,x+y0,0);
        clearLineCells(y+x0,-x+y0,-y+x0,-x+y0,0);
        clearLineCells(x+x0,-y+y0,-x+x0,-y+y0,0);
        y++;
    }
}

void clearCells(int x,int y)
{
    for(int i=0; i<=x; i++) {
        for (int j=0; j<=y; j++) {
            lcd.clearPixel(i,j);
            table[i][j]=0;
        }
    }
    lcd.refresh();
}

void loose()
{
    if (sounds) {
        buzzer.beep(500,0.2);
    }
    gamePlays=0;
    lives=lives-1;

    if(lives==0) {
        
        clearCells(84,48);
        lcd.refresh();
        lcd.clear();

        wait(2);
        while(startButtonFlag==0&&resetButtonFlag==0) {
            lcd.printString("Game Over!!!",0,0);
            lcd.printString("Press START",0,2);
            lcd.printString("to try again",0,3);
            lcd.printString("or RESET",0,4);
            lcd.printString("to exit :)",0,5);
        }
        if(startButtonFlag) {
            a=4;
            gamePlays=1;
            lives=5;
            coins=0;
            round=1;
            clearCells(84,48);
            gameInitialise();
            startButtonFlag=0;
            wait(1);
            timer.attach(&movePlayer,0.1);
        }
        if (resetButtonFlag) {
            gamePlays=0;
            resetButtonFlag=0;
        }
    }

    else {
        clearCells(60,48);
        lcd.refresh();
        gameInitialise();
        wait(2);
        gamePlays=1;
        //timer.attach(&movePlayer,0.1);
    }
}

void gameInitialise()
{
    initTable();
    x=26;
    v=30;
    lcd.drawRect(x,v,w,h,0);
    lcd.refresh();
    j=-70;
    q=-10;
    p=-40;
}


void setLineCells(int x0,int y0,int x1,int y1,int type)
{
    //function from N5110 Library
    int y_range = y1-y0;  // calc range of y and x
    int x_range = x1-x0;
    int start,stop,step;

    // if dotted line, set step to 2, else step is 1
    step = (type==2) ? 2:1;

    // make sure we loop over the largest range to get the most pixels on the display
    // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
    // or else we'll only end up with 1 pixel in the x column
    if ( abs(x_range) > abs(y_range) ) {

        // ensure we loop from smallest to largest or else for-loop won't run as expected
        start = x1>x0 ? x0:x1;
        stop =  x1>x0 ? x1:x0;

        // loop between x pixels
        for (int x = start; x<= stop ; x+=step) {
            // do linear interpolation
            int y = y0 + (y1-y0)*(x-x0)/(x1-x0);

            if (type == 0)   // if 'white' line, turn off pixel
                table[x][y]=2;
            else
                table[x][y]=2;  // else if 'black' or 'dotted' turn on pixel

        }
    } else {

        // ensure we loop from smallest to largest or else for-loop won't run as expected
        start = y1>y0 ? y0:y1;
        stop =  y1>y0 ? y1:y0;

        for (int y = start; y<= stop ; y+=step) {
            // do linear interpolation
            int x = x0 + (x1-x0)*(y-y0)/(y1-y0);

            if (type == 0)   // if 'white' line, turn off pixel
                table[x][y]=2;
            else
                table[x][y]=2;  // else if 'black' or 'dotted' turn on pixel

        }
    }

}

void clearLineCells(int x0,int y0,int x1,int y1,int type)
{
    //function from N5110 Library
    int y_range = y1-y0;  // calc range of y and x
    int x_range = x1-x0;
    int start,stop,step;

    // if dotted line, set step to 2, else step is 1
    step = (type==2) ? 2:1;

    // make sure we loop over the largest range to get the most pixels on the display
    // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
    // or else we'll only end up with 1 pixel in the x column
    if ( abs(x_range) > abs(y_range) ) {

        // ensure we loop from smallest to largest or else for-loop won't run as expected
        start = x1>x0 ? x0:x1;
        stop =  x1>x0 ? x1:x0;

        // loop between x pixels
        for (int x = start; x<= stop ; x+=step) {
            // do linear interpolation
            int y = y0 + (y1-y0)*(x-x0)/(x1-x0);

            if (type == 0)   // if 'white' line, turn off pixel
                table[x][y]=0;
            else
                table[x][y]=0;  // else if 'black' or 'dotted' turn on pixel

        }
    } else {

        // ensure we loop from smallest to largest or else for-loop won't run as expected
        start = y1>y0 ? y0:y1;
        stop =  y1>y0 ? y1:y0;

        for (int y = start; y<= stop ; y+=step) {
            // do linear interpolation
            int x = x0 + (x1-x0)*(y-y0)/(y1-y0);

            if (type == 0)   // if 'white' line, turn off pixel
                table[x][y]=0;
            else
                table[x][y]=0;  // else if 'black' or 'dotted' turn on pixel

        }
    }
}