Simple fish eat program
Dependencies: mbed mbed-rtos N5110 ShiftReg Tone
Diff: classes/FishEngine.cpp
- Revision:
- 12:20ac766b3175
- Parent:
- 11:7c1e2a9303d3
- Child:
- 13:183bd19f3d7d
diff -r 7c1e2a9303d3 -r 20ac766b3175 classes/FishEngine.cpp --- a/classes/FishEngine.cpp Wed Apr 21 11:08:02 2021 +0000 +++ b/classes/FishEngine.cpp Wed Apr 21 15:24:31 2021 +0000 @@ -1,5 +1,6 @@ #include "FishEngine.h" +//definitions #define UP 0 #define DOWN 1 #define CENTRE 2 @@ -10,9 +11,6 @@ Graphics graphics; HighScore highscore; -int ButtonA; -int ButtonB; - // creates intro sequence void FishEngine::titleSequence(N5110 &lcd){ lcd.clear(); @@ -20,7 +18,7 @@ lcd.refresh(); wait_ms(2000); - while(settings.buttonA_state(ButtonA) == 0){ + while(settings.buttonB_state(ButtonB) == 0){ lcd.clear(); graphics.confirmationScreen(lcd); //calls confirmation function (held in graphics.h) lcd.refresh(); @@ -31,64 +29,38 @@ lcd.refresh(); } +//plays the game void FishEngine::gamePlay(N5110 &lcd, Tone &dac, Joystick &joystick){ - game.init(lcd, 20, 20, 1); - + game.init(lcd, 20, 20, 1); //initalise game conditions do{ DIRECTION direction = {joystick.get_direction()}; - game.gamePlay(lcd, direction); - playerScore = game.Score(lcd); - + game.gamePlay(lcd, dac, direction); + playerScore = game.Score(lcd); //updates playerScore in real time }while(game.Lives() != 0); - fileValue = highscore.read_File(); + graphics.gameOver(lcd); //display game over screen + fileValue = highscore.readFile(); //reads current value stored in file, saves it to fileValue + //if player score is higher that current file value save new highscore if(playerScore > fileValue){ - highscore.write_File(playerScore); - lcd.clear(); - lcd.printString("New Score", 5, 1); - lcd.refresh(); - wait_ms(1000); + highscore.writeFile(playerScore); + graphics.newHighscore(lcd, playerScore); } } +//displays current highscore void FishEngine::highScore(N5110 &lcd){ do{ - highscore.display_File(lcd); + highscore.displayFile(lcd); }while(settings.buttonB_state(ButtonB) == false); } +//displays player instructions void FishEngine::Instructions(N5110 &lcd){ - do{ - lcd.clear(); - lcd.printString(" -- Welcome --",0,0); - lcd.printString("Use the",0,2); - lcd.printString("joystick to ",0,3); - lcd.printString("move the fish",0,4); - lcd.refresh(); - - wait_ms(2000); - - lcd.clear(); - lcd.printString("remeber to",0,1); - lcd.printString("only eat fish",0,2); - lcd.printString("smaller than",0,3); - lcd.printString("you!!",0,4); - lcd.refresh(); - - wait_ms(2000); - - lcd.clear(); - lcd.printString("Hold B to Exit",0,2); - lcd.refresh(); - - wait_ms(2000); - - }while(settings.buttonB_state(ButtonB) == false); + graphics.playerInstruc(lcd); } -// creates the games main menu screen with scrolling ability through the use of a -// state machine. +// creates the games main menu screen with scrolling ability through the use of a state machine. void FishEngine::mainMenu(N5110 &lcd, Tone &dac, Joystick &joystick){ const int Fish_1[4][9]= { { 1,0,0,1,1,1,1,1,0, }, @@ -100,9 +72,8 @@ // outputs for the state machine are used to draw Fish_1 beside the corrosponding // menu choice int g_menu_output[4] = {10, 18, 26, 34}; - int state = 0; //set inital state to 0 - int direction = CENTRE; //set inital direction to remain on that state until an input is received - int y_pos; + state = 0; //set inital state to 0 + direction = CENTRE; //set inital direction to remain on that state until an input is received while(1){ lcd.clear(); @@ -112,20 +83,7 @@ lcd.printString("New Game", 15, 1); lcd.printString("High Score", 15, 2); lcd.printString("How to Play", 15, 3); - lcd.printString("Settings", 15, 4); - - - /*implementation of state machine allows the determination of the position of the fish marker - based on the users joystick input: - - inputs - state 0 1 2 where 0 is UP and 1 is DOWN 2 is CENTRE - - 0 3 1 0 - 1 0 2 1 - 2 1 3 2 - 3 2 0 3 */ - + lcd.printString("Settings", 15, 4); if(joystick.get_direction() == N){ //if the joystick is moved upward, then direction is UP direction = UP; @@ -160,7 +118,7 @@ } break; - //case for HighScore + //case to view HighScore case 1: if(settings.buttonA_state(ButtonA)){ graphics.screenFlash(lcd);