Simple fish eat program

Dependencies:   mbed mbed-rtos N5110 ShiftReg Tone

Committer:
el18a2k
Date:
Wed Apr 21 15:24:31 2021 +0000
Revision:
12:20ac766b3175
Parent:
11:7c1e2a9303d3
Child:
13:183bd19f3d7d
lots of bugs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el18a2k 1:7dfa61225fcf 1 #include "FishEngine.h"
el18a2k 1:7dfa61225fcf 2
el18a2k 12:20ac766b3175 3 //definitions
el18a2k 2:532b0225519f 4 #define UP 0
el18a2k 2:532b0225519f 5 #define DOWN 1
el18a2k 2:532b0225519f 6 #define CENTRE 2
el18a2k 2:532b0225519f 7
el18a2k 2:532b0225519f 8 //objects
el18a2k 10:e221bd1ce3ec 9 Game game;
el18a2k 6:7aafcaf7cbe2 10 Settings settings;
el18a2k 1:7dfa61225fcf 11 Graphics graphics;
el18a2k 11:7c1e2a9303d3 12 HighScore highscore;
el18a2k 1:7dfa61225fcf 13
el18a2k 2:532b0225519f 14 // creates intro sequence
el18a2k 1:7dfa61225fcf 15 void FishEngine::titleSequence(N5110 &lcd){
el18a2k 1:7dfa61225fcf 16 lcd.clear();
el18a2k 2:532b0225519f 17 graphics.titleScreen(lcd); //prints titlescreen (held in graphics.h)
el18a2k 1:7dfa61225fcf 18 lcd.refresh();
el18a2k 2:532b0225519f 19 wait_ms(2000);
el18a2k 1:7dfa61225fcf 20
el18a2k 12:20ac766b3175 21 while(settings.buttonB_state(ButtonB) == 0){
el18a2k 1:7dfa61225fcf 22 lcd.clear();
el18a2k 2:532b0225519f 23 graphics.confirmationScreen(lcd); //calls confirmation function (held in graphics.h)
el18a2k 1:7dfa61225fcf 24 lcd.refresh();
el18a2k 1:7dfa61225fcf 25 wait_ms(1000/10);
el18a2k 1:7dfa61225fcf 26 }
el18a2k 1:7dfa61225fcf 27
el18a2k 2:532b0225519f 28 graphics.screenFlash(lcd); //calls screen flash function (held in graphics.h)
el18a2k 1:7dfa61225fcf 29 lcd.refresh();
el18a2k 1:7dfa61225fcf 30 }
el18a2k 1:7dfa61225fcf 31
el18a2k 12:20ac766b3175 32 //plays the game
el18a2k 10:e221bd1ce3ec 33 void FishEngine::gamePlay(N5110 &lcd, Tone &dac, Joystick &joystick){
el18a2k 12:20ac766b3175 34 game.init(lcd, 20, 20, 1); //initalise game conditions
el18a2k 10:e221bd1ce3ec 35 do{
el18a2k 10:e221bd1ce3ec 36 DIRECTION direction = {joystick.get_direction()};
el18a2k 12:20ac766b3175 37 game.gamePlay(lcd, dac, direction);
el18a2k 12:20ac766b3175 38 playerScore = game.Score(lcd); //updates playerScore in real time
el18a2k 10:e221bd1ce3ec 39 }while(game.Lives() != 0);
el18a2k 11:7c1e2a9303d3 40
el18a2k 12:20ac766b3175 41 graphics.gameOver(lcd); //display game over screen
el18a2k 12:20ac766b3175 42 fileValue = highscore.readFile(); //reads current value stored in file, saves it to fileValue
el18a2k 11:7c1e2a9303d3 43
el18a2k 12:20ac766b3175 44 //if player score is higher that current file value save new highscore
el18a2k 11:7c1e2a9303d3 45 if(playerScore > fileValue){
el18a2k 12:20ac766b3175 46 highscore.writeFile(playerScore);
el18a2k 12:20ac766b3175 47 graphics.newHighscore(lcd, playerScore);
el18a2k 11:7c1e2a9303d3 48 }
el18a2k 11:7c1e2a9303d3 49 }
el18a2k 11:7c1e2a9303d3 50
el18a2k 12:20ac766b3175 51 //displays current highscore
el18a2k 11:7c1e2a9303d3 52 void FishEngine::highScore(N5110 &lcd){
el18a2k 11:7c1e2a9303d3 53 do{
el18a2k 12:20ac766b3175 54 highscore.displayFile(lcd);
el18a2k 11:7c1e2a9303d3 55 }while(settings.buttonB_state(ButtonB) == false);
el18a2k 10:e221bd1ce3ec 56 }
el18a2k 10:e221bd1ce3ec 57
el18a2k 12:20ac766b3175 58 //displays player instructions
el18a2k 8:fe51fbf81dee 59 void FishEngine::Instructions(N5110 &lcd){
el18a2k 12:20ac766b3175 60 graphics.playerInstruc(lcd);
el18a2k 8:fe51fbf81dee 61 }
el18a2k 8:fe51fbf81dee 62
el18a2k 12:20ac766b3175 63 // creates the games main menu screen with scrolling ability through the use of a state machine.
el18a2k 2:532b0225519f 64 void FishEngine::mainMenu(N5110 &lcd, Tone &dac, Joystick &joystick){
el18a2k 2:532b0225519f 65 const int Fish_1[4][9]= {
el18a2k 2:532b0225519f 66 { 1,0,0,1,1,1,1,1,0, },
el18a2k 2:532b0225519f 67 { 1,1,1,1,1,1,1,0,1, },
el18a2k 2:532b0225519f 68 { 1,1,1,1,1,1,1,1,1, },
el18a2k 2:532b0225519f 69 { 1,0,0,1,1,1,1,1,0, },
el18a2k 2:532b0225519f 70 };
el18a2k 2:532b0225519f 71
el18a2k 2:532b0225519f 72 // outputs for the state machine are used to draw Fish_1 beside the corrosponding
el18a2k 2:532b0225519f 73 // menu choice
el18a2k 2:532b0225519f 74 int g_menu_output[4] = {10, 18, 26, 34};
el18a2k 12:20ac766b3175 75 state = 0; //set inital state to 0
el18a2k 12:20ac766b3175 76 direction = CENTRE; //set inital direction to remain on that state until an input is received
el18a2k 2:532b0225519f 77
el18a2k 2:532b0225519f 78 while(1){
el18a2k 2:532b0225519f 79 lcd.clear();
el18a2k 2:532b0225519f 80 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
el18a2k 2:532b0225519f 81
el18a2k 2:532b0225519f 82 //prints players options
el18a2k 2:532b0225519f 83 lcd.printString("New Game", 15, 1);
el18a2k 2:532b0225519f 84 lcd.printString("High Score", 15, 2);
el18a2k 2:532b0225519f 85 lcd.printString("How to Play", 15, 3);
el18a2k 12:20ac766b3175 86 lcd.printString("Settings", 15, 4);
el18a2k 2:532b0225519f 87
el18a2k 2:532b0225519f 88 if(joystick.get_direction() == N){ //if the joystick is moved upward, then direction is UP
el18a2k 2:532b0225519f 89 direction = UP;
el18a2k 2:532b0225519f 90 }else if (joystick.get_direction() == S){//if the joystick is moved downward, then direction is DOWN
el18a2k 2:532b0225519f 91 direction = DOWN;
el18a2k 2:532b0225519f 92 }else{
el18a2k 2:532b0225519f 93 direction = CENTRE; //if the joystick is left in the centre position then direction is CENTRE
el18a2k 2:532b0225519f 94 };
el18a2k 2:532b0225519f 95
el18a2k 2:532b0225519f 96 y_pos = g_menu_output[state];//y_position is the array output at position 'state'
el18a2k 2:532b0225519f 97
el18a2k 2:532b0225519f 98
el18a2k 2:532b0225519f 99 //implement state machine
el18a2k 2:532b0225519f 100 switch(state) {
el18a2k 4:db929dab4f13 101
el18a2k 4:db929dab4f13 102 //case for new game
el18a2k 2:532b0225519f 103 case 0:
el18a2k 6:7aafcaf7cbe2 104 if(settings.buttonA_state(ButtonA)){
el18a2k 10:e221bd1ce3ec 105 graphics.screenFlash(lcd);
el18a2k 10:e221bd1ce3ec 106 gamePlay(lcd, dac, joystick);
el18a2k 4:db929dab4f13 107 }
el18a2k 2:532b0225519f 108 switch(direction) {
el18a2k 2:532b0225519f 109 case UP:
el18a2k 2:532b0225519f 110 state = 3;
el18a2k 2:532b0225519f 111 break;
el18a2k 2:532b0225519f 112 case DOWN:
el18a2k 2:532b0225519f 113 state = 1;
el18a2k 2:532b0225519f 114 break;
el18a2k 2:532b0225519f 115 case CENTRE:
el18a2k 2:532b0225519f 116 state = 0;
el18a2k 2:532b0225519f 117 break;
el18a2k 2:532b0225519f 118 }
el18a2k 2:532b0225519f 119 break;
el18a2k 4:db929dab4f13 120
el18a2k 12:20ac766b3175 121 //case to view HighScore
el18a2k 2:532b0225519f 122 case 1:
el18a2k 6:7aafcaf7cbe2 123 if(settings.buttonA_state(ButtonA)){
el18a2k 11:7c1e2a9303d3 124 graphics.screenFlash(lcd);
el18a2k 11:7c1e2a9303d3 125 highScore(lcd);
el18a2k 4:db929dab4f13 126 }
el18a2k 2:532b0225519f 127 switch(direction) {
el18a2k 2:532b0225519f 128 case UP:
el18a2k 2:532b0225519f 129 state = 0;
el18a2k 2:532b0225519f 130 break;
el18a2k 2:532b0225519f 131 case DOWN:
el18a2k 2:532b0225519f 132 state = 2;
el18a2k 2:532b0225519f 133 break;
el18a2k 2:532b0225519f 134 case CENTRE:
el18a2k 2:532b0225519f 135 state = 1;
el18a2k 2:532b0225519f 136 break;
el18a2k 2:532b0225519f 137 }
el18a2k 2:532b0225519f 138 break;
el18a2k 4:db929dab4f13 139
el18a2k 4:db929dab4f13 140 //case for how to play
el18a2k 2:532b0225519f 141 case 2:
el18a2k 6:7aafcaf7cbe2 142 if(settings.buttonA_state(ButtonA)){
el18a2k 8:fe51fbf81dee 143 graphics.screenFlash(lcd);
el18a2k 4:db929dab4f13 144 Instructions(lcd);
el18a2k 4:db929dab4f13 145 }
el18a2k 2:532b0225519f 146 switch(direction) {
el18a2k 2:532b0225519f 147 case UP:
el18a2k 2:532b0225519f 148 state = 1;
el18a2k 2:532b0225519f 149 break;
el18a2k 2:532b0225519f 150 case DOWN:
el18a2k 2:532b0225519f 151 state = 3;
el18a2k 2:532b0225519f 152 break;
el18a2k 2:532b0225519f 153 case CENTRE:
el18a2k 2:532b0225519f 154 state = 2;
el18a2k 2:532b0225519f 155 break;
el18a2k 2:532b0225519f 156 }
el18a2k 2:532b0225519f 157 break;
el18a2k 4:db929dab4f13 158
el18a2k 4:db929dab4f13 159 //case for settings
el18a2k 2:532b0225519f 160 case 3:
el18a2k 6:7aafcaf7cbe2 161 if(settings.buttonA_state(ButtonA)){
el18a2k 6:7aafcaf7cbe2 162 graphics.screenFlash(lcd);
el18a2k 6:7aafcaf7cbe2 163 settings.settingsMenu(lcd, dac, joystick);
el18a2k 4:db929dab4f13 164 }
el18a2k 2:532b0225519f 165 switch(direction) {
el18a2k 2:532b0225519f 166 case UP:
el18a2k 2:532b0225519f 167 state = 2;
el18a2k 2:532b0225519f 168 break;
el18a2k 2:532b0225519f 169 case DOWN:
el18a2k 2:532b0225519f 170 state = 0;
el18a2k 2:532b0225519f 171 break;
el18a2k 2:532b0225519f 172 case CENTRE:
el18a2k 2:532b0225519f 173 state = 3;
el18a2k 2:532b0225519f 174 break;
el18a2k 4:db929dab4f13 175
el18a2k 2:532b0225519f 176 }
el18a2k 2:532b0225519f 177 break;
el18a2k 2:532b0225519f 178 default:
el18a2k 2:532b0225519f 179 state = 0;
el18a2k 2:532b0225519f 180 break;
el18a2k 2:532b0225519f 181 }
el18a2k 2:532b0225519f 182
el18a2k 2:532b0225519f 183 //draw sprite at position (3, y_pos)
el18a2k 2:532b0225519f 184 lcd.drawSprite(3, y_pos, 4, 9, (int*)Fish_1);
el18a2k 2:532b0225519f 185 lcd.refresh();
el18a2k 2:532b0225519f 186 wait_ms(500);
el18a2k 2:532b0225519f 187 }
el18a2k 8:fe51fbf81dee 188 }