Simple fish eat program

Dependencies:   mbed mbed-rtos N5110 ShiftReg Tone

Committer:
el18a2k
Date:
Tue Apr 20 15:35:00 2021 +0000
Revision:
10:e221bd1ce3ec
Parent:
8:fe51fbf81dee
Child:
11:7c1e2a9303d3
gameplay implemented

Who changed what in which revision?

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