Racing Cars game using N5110 LCD and thumb Joystick
Dependencies: N5110 PowerControl beep mbed
Diff: main.cpp
- Revision:
- 5:243718c3cd8b
- Parent:
- 4:f9a899c6ac50
- Child:
- 6:289f237b8d90
--- a/main.cpp Sat May 02 17:32:04 2015 +0000 +++ b/main.cpp Wed May 06 00:16:31 2015 +0000 @@ -1,141 +1,32 @@ -#include "N5110.h" -#include "mbed.h" -#include "PowerControl.h" -#include "beep.h" - - -// change this to alter tolerance of joystick direction -#define DIRECTION_TOLERANCE 0.05 - -// connections for joystick -DigitalIn button(p17); -DigitalIn select(p25); -InterruptIn start(p24); - -AnalogIn yPot(p18); -AnalogIn xPot(p19); -Beep buzzer(p21); - - -//nokia display connections -N5110 lcd(p7,p8,p9,p10,p11,p13,p26); - -// timer to regularly read the joystick -Ticker pollJoystick; -Ticker timer; -Ticker randomize; - - -// create enumerated type (0,1,2,3 etc. for direction) -// could be extended for diagonals etc. -enum DirectionName { - UP, - DOWN, - LEFT, - RIGHT, - CENTRE, - UNKNOWN -}; - -// struct for Joystick -typedef struct JoyStick 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 -}; -// create struct variable -Joystick joystick; - -//flag pointers when interrupt comes in -int printFlag = 0; -int buttonFlag=0; - -//initial enemies Y positions (p,q,c) -int p=-20; -int q=-100; -int c=0; -int j=0; - -//enemies X positions (p,q,c) -int enemy1x=46; -int enemy2x=6; -int enemy3x=26; - - -//initial player's Car position and its dimensions(w,h) -int x=26; //x-position -int v=30; //y-position -int w=8; // width -int h=12; // height - -//initial Coin Xposition -int xPos=6; - -int lives=5; -int coins=0; -int round=1; - -float brightness=0.9; -int brightnessDisplay=9; - -int table[84][48]; - -// function prototypes -void calibrateJoystick(); -void updateJoystick(); -void ClearCar(int x,int v,int w,int h); -void gameInitial(); -void enemy1MovesDown(); -void enemy2MovesDown(); -void enemy3MovesDown(); - -void movePlayer(); -void initTable(); -void loose(); -void resetPlayer(); -void checkPlayerPos(); -void coinMoves(); -void drawCoin(int c); -void clearCoin(int c); -void randomizeCoin(); -void setCircleCells(int x0,int y0,int radius); -void clearCircleCells(int x0,int y0,int radius); -void buttonPressed(); -void clearCells(int x,int y); -void setCells(int x,int v,int w,int h); - -int gamePlays=1; -int coinAppear=1; -int enemiesMoving=1; -int optionsPointer=1; -int pointerSelect; -int sounds=1; - -float joystickCheckTime=1.0/30.0; - - +/** +@file main.cpp +@brief Contains all functions implementations, and main program +*/ +#include "main.h" int main() { - char soundString[3]; - sprintf(soundString,"%s","YES"); + /** 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,joystickCheckTime); // read joystick 0 times per second - lcd.setXYAddress(0,0); - initTable(); - lcd.init(); + 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(&buttonPressed); MainMenu: buttonFlag=0; - pointerSelect=1; 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 @@ -153,7 +44,7 @@ lcd.clearCircle(53,19,2,1); lcd.clearCircle(22,19,2,1); - pointerSelect=2; + menuPointer=2; } wait(0.1); @@ -163,25 +54,29 @@ lcd.clearCircle(16,27,2,1); //draw pointer around Options submenu when selected lcd.clearCircle(64,27,2,1); - pointerSelect=1; + menuPointer=1; } } - switch(pointerSelect) { + switch(menuPointer) { case 1: - //GameBegins: - + //GameBegins: + lives=5; + round=1; + coins=0; buttonFlag=0; clearCells(84,48); - wait(2); + 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); @@ -195,17 +90,10 @@ sprintf(buffer2,"%d",coins); lcd.printString(buffer2,72,5); - - //int pointerSelect; + while(gamePlays) { - while(gamePlays) { - - int a=select.read(); - if(a){ - gamePlays=0; - goto MainMenu;} + lcd.refresh(); - enemiesMoving=1; sprintf(buffer,"%d",lives); lcd.printString(buffer,74,1); @@ -213,14 +101,14 @@ lcd.printString(buffer2,72,5); checkPlayerPos(); - lcd.refresh(); enemy1MovesDown(); - lcd.refresh(); enemy2MovesDown(); - lcd.refresh(); + checkPlayerPos(); enemy3MovesDown(); lcd.refresh(); + coinMoves(); + checkPlayerPos(); } break; @@ -247,9 +135,6 @@ lcd.printString(soundString,40,2); while(optionsMenu) { - char pointerbuffer[2]; - sprintf(pointerbuffer,"%d",optionsPointer); - lcd.printString(pointerbuffer,0,0); if (joystick.direction==UP) { optionsPointer--; @@ -329,7 +214,7 @@ goto MainMenu; } } - wait(0.1); + wait(0.1); } break; @@ -347,55 +232,42 @@ //lcd.refresh(); if (joystick.direction==RIGHT&&x+w<58) { - ClearCar(x,v,w,h); + clearRect(x,v,w,h); + lcd.refresh(); x=x+4; lcd.drawRect(x,v,w,h,0); lcd.refresh(); - if (table[x+w][v+h]==1||table[x+w][v]==1) { - loose(); - } - - } if (joystick.direction==LEFT&&x+w>10) { - ClearCar(x,v,w,h); + clearRect(x,v,w,h); + lcd.refresh(); x=x-4; - lcd.refresh(); + lcd.drawRect(x,v,w,h,0); - if (table[x][v]==1||table[x][v+h]==1) { - loose(); - } - + lcd.refresh(); } if (joystick.direction==UP && v>2) { - ClearCar(x,v,w,h); + clearRect(x,v,w,h); + lcd.refresh(); v=v-4; - lcd.refresh(); lcd.drawRect(x,v,w,h,0); - if (table[x][v]==1||table[x+w][v]==1) { - loose(); - } - + lcd.refresh(); } if (joystick.direction==DOWN && v<32) { - ClearCar(x,v,w,h); + clearRect(x,v,w,h); + lcd.refresh(); v=v+4; - lcd.refresh(); lcd.drawRect(x,v,w,h,0); - if (table[x][v+h]==1||table[x+w][v+h]==1) { - loose(); - } - + lcd.refresh(); } } } -// read default positions of the joystick to calibrate later readings void calibrateJoystick() { button.mode(PullDown); @@ -405,7 +277,6 @@ } void updateJoystick() { - // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) joystick.x = xPot - joystick.x0; joystick.y = yPot - joystick.y0; // read button state @@ -432,7 +303,7 @@ } -void ClearCar(int x,int v,int w,int h) +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++) { @@ -451,20 +322,18 @@ lcd.drawLine(40,0,40,48,2); } - - - void enemy1MovesDown() { - ClearCar(enemy1x,(j-4),w,h); + clearRect(enemy1x,(j-4),w,h); + lcd.refresh(); lcd.drawRect(enemy1x,j,w,h,1); - setCells(enemy1x,j,w,h); + lcd.refresh(); + setRectCells(enemy1x,j,w,h); + lcd.refresh(); wait(0.05); - lcd.refresh(); - j=j+4; - lcd.refresh(); + //lcd.refresh(); if(j>60) { j=-(rand()%100+80); @@ -472,17 +341,17 @@ } - - void enemy2MovesDown() { - ClearCar(enemy2x,(p-4),w,h); + clearRect(enemy2x,(p-4),w,h); + lcd.refresh(); lcd.drawRect(enemy2x,p,w,h,1); - setCells(enemy2x,p,w,h); + lcd.refresh(); + setRectCells(enemy2x,p,w,h); + lcd.refresh(); wait(0.05); - lcd.refresh(); p=p+4; - lcd.refresh(); + //lcd.refresh(); if(p>60) { p=-(rand()%40+20); @@ -495,20 +364,22 @@ void enemy3MovesDown() { - ClearCar(enemy3x,(q-4),w,h); + clearRect(enemy3x,(q-4),w,h); + lcd.refresh(); lcd.drawRect(enemy3x,q,w,h,1); - setCells(enemy3x,q,w,h); + lcd.refresh(); + setRectCells(enemy3x,q,w,h); + lcd.refresh(); wait(0.05); lcd.refresh(); q=q+4; - lcd.refresh(); if (q>70) { q=-(rand()%10+1); } } -void setCells(int x,int v,int w,int h) +void setRectCells(int x,int v,int w,int h) { for(int i=x; i<=x+w; i++) { @@ -522,8 +393,8 @@ void initTable() { - for (int i=0; i<=84; i++) { - for(int j=0; j<=48; j++) { + for (int i=0; i<=83; i++) { + for(int j=0; j<=47; j++) { table[i][j]=0; } } @@ -536,12 +407,14 @@ } if (table[x+w][v+h]==2||table[x][v+h]==2||table[x][v]==2||table[x+w][v]==2) { - coins=coins+10; - coinAppear=0; + coins=coins+5; + clearCoin(c); if(sounds) { buzzer.beep(2200,0.4); buzzer.beep(1000,0.2); } + coinAppear=0; + } } @@ -552,66 +425,63 @@ clearCoin(c); c=c+2; drawCoin(c); - wait(0.05); - if(c>70) { - randomizeCoin(); + if(c>80) { + c=-(rand()%80+0); + xPos=rand()%46+6; coinAppear=1; - c=-100; } } void drawCoin(int c) { - if (table[xPos][c+4]==0&&table[xPos][c-4]==0&&coinAppear) { + if (table[xPos][c+4]==0&&table[xPos][c-4]==0&&coinAppear&&c>0&&c<48) { lcd.drawCircle(xPos,c,4,0); setCircleCells(xPos,c,4); lcd.setPixel(xPos,c-1); lcd.setPixel(xPos,c+2); + table[xPos][c-1]=2; + table[xPos][c+2]=2; lcd.setPixel(xPos+1,c-1); lcd.setPixel(xPos+1,c+2); + table[xPos+1][c-1]=2; + table[xPos+1][c+2]=2; lcd.setPixel(xPos-1,c); lcd.setPixel(xPos-1,c+1); + table[xPos-1][c]=2; + table[xPos-1][c+1]=2; + lcd.refresh(); } } void clearCoin(int c) { + if(coinAppear){ lcd.clearCircle(xPos,c,4,0); clearCircleCells(xPos,c,4); lcd.clearPixel(xPos,c-1); lcd.clearPixel(xPos,c+2); + table[xPos][c-1]=0; + table[xPos][c+2]=0; lcd.clearPixel(xPos+1,c-1); lcd.clearPixel(xPos+1,c+2); + table[xPos+1][c-1]=0; + table[xPos+1][c+2]=0; lcd.clearPixel(xPos-1,c); lcd.clearPixel(xPos-1,c+1); + table[xPos-1][c]=0; + table[xPos-1][c+1]=0; lcd.refresh(); - +} } -void randomizeCoin() -{ -int randX; - - randX=rand()%3+1; - - if (randX==1) { - xPos=6; - } - if (randX==2) { - xPos=26; - } - if (randX==3) { - xPos=46; - } -} void setCircleCells(int x0,int y0,int radius) { @@ -665,31 +535,47 @@ table[i][j]=0; } } - + lcd.refresh(); } void loose() { if (sounds) { - buzzer.beep(500,0.1); + buzzer.beep(500,0.2); } + timer.detach(); lives=lives-1; - gamePlays=0; - wait(0.5); - ClearCar(x,v,w,h); - lcd.refresh(); - clearCells(60,48); - lcd.setXYAddress(0,0); + + if(lives==0) { + gamePlays=0; + clearCells(84,48); + lcd.refresh(); + lcd.clear(); + + wait(2); + lcd.printString("Game Over!!!",9,3); + } - wait(1); - lcd.refresh(); - x=26; - v=30; - lcd.drawRect(x,v,w,h,0); - wait(0.5); - gamePlays=1; - j=-100; - p=-50; - q=-40; + else { + clearRect(x,v,w,h); + lcd.refresh(); + clearCells(60,48); + lcd.refresh(); + initTable(); + //lcd.setXYAddress(0,0); + + //lcd.refresh(); + x=26; + v=30; + lcd.drawRect(x,v,w,h,0); + lcd.refresh(); + //lcd.setXYAddress(0,0); + j=-70; + q=-10; + p=-40; + wait(2); + + timer.attach(&movePlayer,0.1); + } }