Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 mbed PowerControl
Diff: main.cpp
- Revision:
- 14:f5760f76fe83
- Parent:
- 13:7071a14b2fab
- Child:
- 17:7c926de79e02
--- a/main.cpp Thu Apr 30 00:15:51 2015 +0000 +++ b/main.cpp Fri May 01 16:17:27 2015 +0000 @@ -1,159 +1,129 @@ -#include "mbed.h" -#include "N5110.h" -#include "GameScreen.h" - -Serial serial(USBTX,USBRX); -GameScreen lcd(p7,p8,p9,p10,p11,p13,p21); -BusOut leds(LED4,LED3,LED2,LED1); -AnalogIn joystickX(p15); -AnalogIn joystickY(p16); -DigitalIn joystickButton(p17); -DigitalOut buzzer(p5); -LocalFileSystem fs("fs"); +/** +@file main.cpp +@brief Source file for main game containing function defintions and main loop. +@autor Thomas Davies +@date May 2015 +*/ -Ticker gameClock; //the master clock of game operation. each animation uses some multiple of this clock to trigger. -int clockCount = 0; -bool isFirstCheck = true; -bool isFirstHit = true; +#include "main.h" -void advancePlatforms(); -bool isBallFalling(int bY); -bool right(); -bool left(); -bool up(); -bool down(); -void clockCounter (); -int gameLevel = 0; - -extern "C" void mbed_reset(); //enables calls to mbed_reset() --> soft reset! -bool isBallDirClear(int dir); -void endScreenController(); -const char* recordEntryController(); - -FILE *fp = fopen("/fs/scores.csv","a+"); //open file for edit, int main() -{ +{ lcd.Initialize(); - - // - lcd.displayStartScreen(); - while(!right()){} //wait until right button pressed - + while(!isRight()) {} //wait until right button pressed + wait(0.5); //just to ignore layover from first button. lcd.displayInstructionScreen(); - while(!right()){} //wait until right button pressed. + while(!isRight()) {} //wait until right button pressed. - //lcd.displayCountdown(); + lcd.displayCountdown(); lcd.clear(); - - leds = 1; + + leds = 1; bool refresh = false; - - lcd.drawAllPlatforms(); + + lcd.drawAllPlatforms(); lcd.drawBall(); lcd.refresh(); - - gameClock.attach(&clockCounter,0.01); - + + gameClock.attach(&clockCounter,0.003); + //MAIN GAME LOOP //multiple runs on a single loop. stop with a bool - while(false) - { - if (isFirstCheck) - { - if(clockCount%(20-gameLevel) == 0) - { - advancePlatforms(); + char buffer[10]; + while(true) { + if (isFirstCheck) { + if(clockCount %(gameSpeed) == 0) { + + if (gameLevel > 60) + advancePlatforms(2); + else + advancePlatforms(1); refresh = true; } + + if(clockCount%10 == 0) { + //check if moving left will move you into platform. + if (isLeft() && isBallDirClear(1)) { + lcd.eraseBall(); + lcd.setBallPos(lcd.getBallX()+2,lcd.getBallY()); + refresh = true; + + } else if (isRight() && isBallDirClear(0)) { + lcd.eraseBall(); + lcd.setBallPos(lcd.getBallX()-2,lcd.getBallY()); + refresh = true; + } + + if(isBallFalling(lcd.getBallY())) { + lcd.eraseBall(); + lcd.setBallPos(lcd.getBallX(),lcd.getBallY() + 1); + refresh = true; + isFirstHit = true; + } + } - if(clockCount%4 == 0) + if (clockCount %500 == 0) { - //check if moving left will move you into platform. - if (left() && isBallDirClear(1)) - { - lcd.eraseBall(); - lcd.setBallPos(lcd.getBallX()+1,lcd.getBallY()); - refresh = true; - - } else if (right() && isBallDirClear(0)) - { - lcd.eraseBall(); - lcd.setBallPos(lcd.getBallX()-1,lcd.getBallY()); - refresh = true; - } - - } - if (clockCount %10 == 0) - { - if(isBallFalling(lcd.getBallY())) { - lcd.eraseBall(); - lcd.setBallPos(lcd.getBallX(),lcd.getBallY() + 1); - refresh = true; - isFirstHit = true; - } + gameLevel++; + if (gameLevel%4 == 0) + if (gameLevel > 60) + gameSpeed -=1; + else + gameSpeed -= 2; } - if (clockCount %400 == 0) - gameLevel++; - - if (refresh) - { + if (refresh) { lcd.drawBall(); lcd.drawAllPlatforms(); - char buffer[10]; - sprintf(buffer,"lvl: %d",gameLevel); + + sprintf(buffer,"lvl: %d",gameLevel); lcd.printString(buffer,lcd.getMaxX() - 2,-1); lcd.refresh(); - refresh = false; - } - - //check if ball hit roof - if (lcd.getBallY() == 7) - { - //GAME OVER FUCKER - break; + refresh = false; } - isFirstCheck = false; - } - } + //check if ball hit roof + if (lcd.getBallY() == 7) { + break; + } + isFirstCheck = false; + } + } - lcd.clear(); + gameClock.detach(); - //endScreenController(); - - char *winners[3] = {"TRD","BOB","TOM"}; - int lvls[3] = {11,9,8}; - lcd.displayHighScores(winners,lvls); - + lcd.clear(); + + endScreenController(); return 1; } -void advancePlatforms() -{ + +//#############FUNCTION DEFINITIONS#################// +void advancePlatforms(int n) +{ //ADVANCE lcd.eraseAllPlatforms(); - lcd.shiftAllPlatforms(); - lcd.drawAllPlatforms(); - - if(!isBallFalling(lcd.getBallY()-1)) { + lcd.shiftAllPlatforms(n); + lcd.drawAllPlatforms(); + + if(!isBallFalling(lcd.getBallY()-1)) { //move ball up with platform - if (isFirstHit) - { + if (isFirstHit) { //beep(); isFirstHit = false; - } - + } + lcd.eraseBall(); lcd.setBallPos(lcd.getBallX(),lcd.getBallY() -1); - } - + } + } @@ -166,25 +136,19 @@ //check Y pos to see if ball is on platform, return false if on plat //check X pos to see if ball is over gap, return true if over gap Platform closestPlatform = lcd.nextClosestPlatform(bY - bR); - - + + //if bottom of ball on level of platform - if ((bY+bR) == (closestPlatform.y)) - { + if ((bY+bR) == (closestPlatform.y)) { //serial.printf ("On level of plat \n"); - if (bX > closestPlatform.x && (bX+bR-1) < (closestPlatform.x + lcd.getPlatGapSize())) //OVER GAP - { - return true; - } - else { - return false; + if (bX > closestPlatform.x && (bX+bR-1) < (closestPlatform.x + lcd.getPlatGapSize())) { //OVER GAP + return true; + } else { + return false; } //On platform! - } - else if ((bY+bR) == lcd.getMaxY()) - { - return false; - } - else //FREE SPACE + } else if ((bY+bR) >= (lcd.getMaxY()-1)) { + return false; + } else //FREE SPACE return true; } @@ -203,74 +167,70 @@ return false; } -bool right() +bool isRight() { double count = 0.0; double total = 0.0; - while (count < 10) - { + while (count < 10) { count ++; total += joystickX.read(); } if ((total/count)> 0.8) return true; - - return false; + + return false; } -bool left() +bool isLeft() { double count = 0.0; double total = 0.0; - - while (count < 10) - { + + while (count < 10) { count ++; total += joystickX.read(); } if ((total/count) < 0.2) return true; - - return false; + + return false; } -bool up() +bool isUp() { double count = 0.0; double total = 0.0; - - while (count < 10) - { + + while (count < 10) { count ++; total += joystickY.read(); } - + if ((total/count) < 0.2) return true; - - return false; + + return false; } -bool down() +bool isDown() { double count = 0.0; double total = 0.0; - - while (count < 10) - { + + while (count < 10) { count ++; total += joystickY.read(); } if ((total/count) > 0.8) return true; - - return false; + + return false; } void clockCounter () { - clockCount ++; + clockCount ++; isFirstCheck = true; - //serial.printf("%d mod 12 = %d \n",clockCount,clockCount%12); + //serial.printf("%d mod 12 = %d \n",clockCount,clockCount%12); } void endScreenController() @@ -281,26 +241,27 @@ {0,1,0}, {0,0,1} }; - - lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc]); + + bool record = isRecord(); + lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc],record); bool change = false; while (true) { - if (up()) { + if (isUp()) { cursorLoc ++; change = true; - } else if (down()) { + } else if (isDown()) { cursorLoc --; change = true; } if (cursorLoc == 3) - cursorLoc = 0; - else if (cursorLoc == -1) + cursorLoc = 1-record; + else if (cursorLoc == (-record)) cursorLoc = 2; if (change) { - lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc]); + lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc],record); wait(0.5); change = false; } @@ -309,20 +270,20 @@ switch(cursorLoc) { case 0: //take to initial entry page! (only allow if score > top 3) - recordEntryController(); - //write lvl and initials to csv + wait(0.1); + highScoreEditor(recordEntryController(),record); - //take to high score page! - lcd.displayHighScores(); + wait(1); //reset once button pressed! - while(!joystickButton.read()){} + while(!joystickButton.read()) {} mbed_reset(); break; case 1: //take to high score page! - lcd.displayHighScores(); + highScoreEditor("",0); + wait(1); //reset once button pressed. - while(!joystickButton.read()){} + while(!joystickButton.read()) {} mbed_reset(); break; case 2: @@ -334,53 +295,53 @@ } } -const char* recordEntryController() +char* recordEntryController() { char *letters = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; int ptr[3] = {0,0,0}; - + int cursorLoc = 0; int cursorArray[3][3] = { {1,0,0}, {0,1,0}, {0,0,1} }; - - lcd.displayRecordScreen(cursorArray[cursorLoc],letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]); - + + lcd.displayRecordScreen(cursorArray[cursorLoc],letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]); + bool change = false; while (true) { - if (up()) { + if (isUp()) { cursorLoc ++; change = true; - } else if (down()) { + } else if (isDown()) { cursorLoc --; change = true; - } else if (left()) { + } else if (isLeft()) { if (ptr[cursorLoc] == 0) ptr[cursorLoc] = 25; else ptr[cursorLoc] --; - change = true; - } else if (right()) { + change = true; + } else if (isRight()) { if (ptr[cursorLoc] == 25) ptr[cursorLoc] = 0; else ptr[cursorLoc] ++; change = true; } - + //cyclical mouse management if (cursorLoc == 3) cursorLoc = 0; else if (cursorLoc == -1) - cursorLoc = 2; + cursorLoc = 2; if (change) { - lcd.displayRecordScreen(cursorArray[cursorLoc],letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]); - wait(0.5); + lcd.displayRecordScreen(cursorArray[cursorLoc],letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]); + wait(0.1); change = false; } @@ -390,12 +351,74 @@ sprintf(initials,"%c%c%c",letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]); return initials; } - } + } } - - +void highScoreEditor(char *playerInitials,bool isRecord) +{ + //check if lvl is in top 3 + FILE *fp = fopen("/local/scores.csv","r"); //open for reading. + + char s_hs0[3],s_hs1[3],s_hs2[3]; + int d_hs[3]; + char buffer[2]; - + fscanf(fp,"%3s,%s",s_hs0,buffer); + d_hs[0] = atoi(buffer); + fscanf(fp,"%3s,%s",s_hs1,buffer); + d_hs[1] = atoi(buffer); + fscanf(fp,"%3s,%s",s_hs2,buffer); + d_hs[2] = atoi(buffer); + + fclose(fp); + + if (isRecord) { + + if (gameLevel >= d_hs[2]) { + d_hs[2] = gameLevel; + strncpy(s_hs2,playerInitials,3); + } + if (gameLevel >= d_hs[1]) { + d_hs[2] = d_hs[1]; + d_hs[1] = gameLevel; + strncpy(s_hs2,s_hs1,3); + strncpy(s_hs1,playerInitials,3); + } + if (gameLevel >= d_hs[0]) { + d_hs[1] = d_hs[0]; + d_hs[0] = gameLevel; + strncpy(s_hs1,s_hs0,3); + strncpy(s_hs0,playerInitials,3); + } + + char buffer[40]; + sprintf(buffer,"%s,%d\n%s,%d\n%s,%d",s_hs0,d_hs[0],s_hs1,d_hs[1],s_hs2,d_hs[2]); + fp = fopen("/local/scores.csv","w"); + fprintf(fp,buffer); + fclose(fp); + } + + lcd.displayHighScores(s_hs0,s_hs1,s_hs2,d_hs); +} - +bool isRecord () +{ + //check if lvl is in top 3 + FILE *fp = fopen("/local/scores.csv","r"); //open for reading. + + int highScore; + char buffer[2]; + char temp[3]; + + for (int i = 0;i < 3;i++) + { + fscanf(fp,"%3s,%s",temp,buffer); + highScore = atoi(buffer); + if (gameLevel > highScore) { + fclose (fp); + return 1; + } + } + fclose(fp); + return 0; +}