
Ball drop game with menus and highscore tracking developed for ELEC2645 at the University of Leeds.
Dependencies: N5110 mbed PowerControl
Game developed for ELEC 2645.
Extremely detailed report outlining all aspects of project found below. /media/uploads/AppleJuice/projectreport.pdf
Revision 12:9ba4b21f8220, committed 2015-04-29
- Comitter:
- AppleJuice
- Date:
- Wed Apr 29 23:45:08 2015 +0000
- Parent:
- 11:c6c88617f7e7
- Child:
- 13:7071a14b2fab
- Commit message:
- record entry screen complete;
Changed in this revision
--- a/GameScreen.cpp Mon Apr 27 23:49:19 2015 +0000 +++ b/GameScreen.cpp Wed Apr 29 23:45:08 2015 +0000 @@ -172,6 +172,7 @@ //then it is printed! void GameScreen::printString(const char * str,int x,int y,bool invert) { + //set all pixels in rows to black if (invert){ for (int a = 0; a < 48; a ++) { @@ -279,9 +280,32 @@ printString(buffer,maxX_ - 2,23); printString("Reached!",maxX_ - 2,32); + //Options. printString("Record",maxX_ - 8,55,cursor[0]); printString("View",maxX_ - 15,63,cursor[1]); - printString("Quit",maxX_ - 15,71,cursor[2]); + printString("Restart",maxX_ - 5,71,cursor[2]); refresh(); } +void GameScreen::displayRecordScreen(int *cursor,char ltr1,char ltr2,char ltr3) +{ + clear(); + printString("Enter",maxX_ - 2,-1); + printString("Initials",maxX_ - 2,7); + + //letter entry + char buffer[2]; + sprintf(buffer,"%c",ltr1); + printString(buffer,maxX_ - 23,20,cursor[0]); + sprintf(buffer,"%c",ltr2); + printString(buffer,maxX_ - 23,32,cursor[1]); + sprintf(buffer,"%c",ltr3); + printString(buffer,maxX_ - 23,44,cursor[2]); + + printString("Click",maxX_ - 2,59); + printString("when",maxX_ - 2,67); + printString("finished",maxX_ - 2,75); + refresh(); + +} +
--- a/GameScreen.h Mon Apr 27 23:49:19 2015 +0000 +++ b/GameScreen.h Wed Apr 29 23:45:08 2015 +0000 @@ -41,6 +41,7 @@ void displayInstructionScreen(); void displayCountdown(); void displayEndScreen(int lvl,int *cursor); + void displayRecordScreen(int *cursor, char ltr1, char ltr2,char ltr3); //Write Access, set void setBallPos(int x, int y) { playerBall.x = x; playerBall.y = y; }
--- a/main.cpp Mon Apr 27 23:49:19 2015 +0000 +++ b/main.cpp Wed Apr 29 23:45:08 2015 +0000 @@ -5,8 +5,11 @@ Serial serial(USBTX,USBRX); GameScreen lcd(p7,p8,p9,p10,p11,p13,p21); BusOut leds(LED4,LED3,LED2,LED1); -AnalogIn joystick(p15); +AnalogIn joystickX(p15); +AnalogIn joystickY(p16); +DigitalIn joystickButton(p17); DigitalOut buzzer(p5); +LocalFileSystem fs("fs"); Ticker gameClock; //the master clock of game operation. each animation uses some multiple of this clock to trigger. int clockCount = 0; @@ -17,24 +20,31 @@ 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(); -bool isBallDirClear(int dir); +FILE *fp = fopen("/fs/scores.csv","a+"); //open file for edit, int main() { lcd.Initialize(); - int cursor[3] = {1,0,0}; - lcd.displayEndScreen(5,cursor); - //lcd.displayStartScreen(); + + // + + lcd.displayStartScreen(); while(!right()){} //wait until right button pressed wait(0.5); //just to ignore layover from first button. - //lcd.displayInstructionScreen(); - //while(!right()){} //wait until right button pressed. + lcd.displayInstructionScreen(); + while(!right()){} //wait until right button pressed. //lcd.displayCountdown(); lcd.clear(); @@ -47,21 +57,22 @@ lcd.drawBall(); lcd.refresh(); - gameClock.attach(&clockCounter,0.05); + gameClock.attach(&clockCounter,0.01); //MAIN GAME LOOP //multiple runs on a single loop. stop with a bool - while(true) + + while(false) { if (isFirstCheck) { - if(clockCount%(10-gameLevel) == 0) + if(clockCount%(20-gameLevel) == 0) { advancePlatforms(); refresh = true; } - if(clockCount%2 == 0) + if(clockCount%4 == 0) { //check if moving left will move you into platform. if (left() && isBallDirClear(1)) @@ -76,18 +87,21 @@ 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; } - - } + } + if (clockCount %400 == 0) gameLevel++; - + if (refresh) { lcd.drawBall(); @@ -109,8 +123,15 @@ } } + lcd.clear(); - + + //endScreenController(); + + + + serial.printf("%s \n",recordEntryController()); + return 1; } @@ -189,9 +210,9 @@ while (count < 10) { count ++; - total += joystick.read(); + total += joystickX.read(); } - if ((total/count)> 0.95) + if ((total/count)> 0.8) return true; return false; @@ -205,14 +226,46 @@ while (count < 10) { count ++; - total += joystick.read(); + total += joystickX.read(); } - if ((total/count) < 0.1) + if ((total/count) < 0.2) return true; return false; } +bool up() +{ + double count = 0.0; + double total = 0.0; + + while (count < 10) + { + count ++; + total += joystickY.read(); + } + + if ((total/count) < 0.2) + return true; + + return false; +} +bool down() +{ + double count = 0.0; + double total = 0.0; + + while (count < 10) + { + count ++; + total += joystickY.read(); + } + if ((total/count) > 0.8) + return true; + + return false; +} + void clockCounter () { clockCount ++; @@ -220,7 +273,117 @@ //serial.printf("%d mod 12 = %d \n",clockCount,clockCount%12); } +void endScreenController() +{ + int cursorLoc = 0; + int cursorArray[3][3] = { + {1,0,0}, + {0,1,0}, + {0,0,1} + }; + + lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc]); + bool change = false; + + while (true) { + if (up()) { + cursorLoc ++; + change = true; + } else if (down()) { + cursorLoc --; + change = true; + } + + if (cursorLoc == 3) + cursorLoc = 0; + else if (cursorLoc == -1) + cursorLoc = 2; + + if (change) { + lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc]); + wait(0.5); + change = false; + } + + if (joystickButton.read()) { + switch(cursorLoc) { + case 0: + //take to initial entry page! + + break; + case 1: + //take to high score page! + + break; + case 2: + //restart + mbed_reset(); + break; + } + } + } +} + +const 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]]); + + bool change = false; + + while (true) { + if (up()) { + cursorLoc ++; + change = true; + } else if (down()) { + cursorLoc --; + change = true; + } else if (left()) { + if (ptr[cursorLoc] == 0) + ptr[cursorLoc] = 25; + else + ptr[cursorLoc] --; + change = true; + } else if (right()) { + 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; + + if (change) { + lcd.displayRecordScreen(cursorArray[cursorLoc],letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]); + wait(0.5); + change = false; + } + + if (joystickButton.read()) { + //go to high score screen + static char initials[10]; + sprintf(initials,"%c%c%c",letters[ptr[0]],letters[ptr[1]],letters[ptr[2]]); + return initials; + } + } +} +