Simple fish eat program

Dependencies:   mbed mbed-rtos N5110 ShiftReg Tone

Committer:
el18a2k
Date:
Wed Mar 24 16:02:53 2021 +0000
Revision:
4:db929dab4f13
Parent:
2:532b0225519f
Child:
5:02e7b5197827
Menu choices added

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 //attach
el18a2k 1:7dfa61225fcf 8 BusOut RGB_led(p24,p23,p22); //RGB bus MSB - LSB, inverted logic 1 = low
el18a2k 4:db929dab4f13 9 DigitalIn button_A(p29);
el18a2k 4:db929dab4f13 10 DigitalIn button_B(p28);
el18a2k 4:db929dab4f13 11 DigitalIn button_C(p27);
el18a2k 4:db929dab4f13 12 DigitalIn button_D(p26);
el18a2k 1:7dfa61225fcf 13
el18a2k 2:532b0225519f 14 //objects
el18a2k 1:7dfa61225fcf 15 ShiftReg shift;
el18a2k 1:7dfa61225fcf 16 Graphics graphics;
el18a2k 1:7dfa61225fcf 17
el18a2k 2:532b0225519f 18 //initialise all of the board
el18a2k 2:532b0225519f 19 void FishEngine::system_init(N5110 &lcd, Tone &dac, Joystick &joystick){
el18a2k 1:7dfa61225fcf 20 lcd.init();
el18a2k 1:7dfa61225fcf 21 lcd.setBrightness(0.5);
el18a2k 2:532b0225519f 22 joystick.init();
el18a2k 1:7dfa61225fcf 23 dac.init();
el18a2k 1:7dfa61225fcf 24 shift.write(0x00);
el18a2k 1:7dfa61225fcf 25 RGB_led.write(0b111);
el18a2k 4:db929dab4f13 26 button_A.mode(PullNone);
el18a2k 4:db929dab4f13 27 button_B.mode(PullNone);
el18a2k 4:db929dab4f13 28 button_C.mode(PullNone);
el18a2k 4:db929dab4f13 29 button_D.mode(PullNone);
el18a2k 1:7dfa61225fcf 30 }
el18a2k 1:7dfa61225fcf 31
el18a2k 2:532b0225519f 32 // creates intro sequence
el18a2k 1:7dfa61225fcf 33 void FishEngine::titleSequence(N5110 &lcd){
el18a2k 1:7dfa61225fcf 34 lcd.clear();
el18a2k 2:532b0225519f 35 graphics.titleScreen(lcd); //prints titlescreen (held in graphics.h)
el18a2k 1:7dfa61225fcf 36 lcd.refresh();
el18a2k 2:532b0225519f 37 wait_ms(2000);
el18a2k 1:7dfa61225fcf 38
el18a2k 1:7dfa61225fcf 39 while(button_A.read() == 0){
el18a2k 1:7dfa61225fcf 40 lcd.clear();
el18a2k 2:532b0225519f 41 graphics.confirmationScreen(lcd); //calls confirmation function (held in graphics.h)
el18a2k 1:7dfa61225fcf 42 lcd.refresh();
el18a2k 1:7dfa61225fcf 43 wait_ms(1000/10);
el18a2k 1:7dfa61225fcf 44 }
el18a2k 1:7dfa61225fcf 45
el18a2k 2:532b0225519f 46 graphics.screenFlash(lcd); //calls screen flash function (held in graphics.h)
el18a2k 1:7dfa61225fcf 47 lcd.refresh();
el18a2k 1:7dfa61225fcf 48 }
el18a2k 1:7dfa61225fcf 49
el18a2k 2:532b0225519f 50 // creates the games main menu screen with scrolling ability through the use of a
el18a2k 2:532b0225519f 51 // state machine.
el18a2k 2:532b0225519f 52 void FishEngine::mainMenu(N5110 &lcd, Tone &dac, Joystick &joystick){
el18a2k 2:532b0225519f 53 const int Fish_1[4][9]= {
el18a2k 2:532b0225519f 54 { 1,0,0,1,1,1,1,1,0, },
el18a2k 2:532b0225519f 55 { 1,1,1,1,1,1,1,0,1, },
el18a2k 2:532b0225519f 56 { 1,1,1,1,1,1,1,1,1, },
el18a2k 2:532b0225519f 57 { 1,0,0,1,1,1,1,1,0, },
el18a2k 2:532b0225519f 58 };
el18a2k 2:532b0225519f 59
el18a2k 2:532b0225519f 60 // outputs for the state machine are used to draw Fish_1 beside the corrosponding
el18a2k 2:532b0225519f 61 // menu choice
el18a2k 2:532b0225519f 62 int g_menu_output[4] = {10, 18, 26, 34};
el18a2k 2:532b0225519f 63 int state = 0; //set inital state to 0
el18a2k 2:532b0225519f 64 int direction = CENTRE; //set inital direction to remain on that state until an input is received
el18a2k 2:532b0225519f 65 int y_pos;
el18a2k 2:532b0225519f 66
el18a2k 2:532b0225519f 67 while(1){
el18a2k 2:532b0225519f 68 lcd.clear();
el18a2k 2:532b0225519f 69 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
el18a2k 2:532b0225519f 70
el18a2k 2:532b0225519f 71 //prints players options
el18a2k 2:532b0225519f 72 lcd.printString("New Game", 15, 1);
el18a2k 2:532b0225519f 73 lcd.printString("High Score", 15, 2);
el18a2k 2:532b0225519f 74 lcd.printString("How to Play", 15, 3);
el18a2k 2:532b0225519f 75 lcd.printString("Settings", 15, 4);
el18a2k 2:532b0225519f 76
el18a2k 2:532b0225519f 77
el18a2k 2:532b0225519f 78 /*implementation of state machine allows the determination of the position of the fish marker
el18a2k 2:532b0225519f 79 based on the users joystick input:
el18a2k 2:532b0225519f 80
el18a2k 2:532b0225519f 81 inputs
el18a2k 2:532b0225519f 82 state 0 1 2 where 0 is UP and 1 is DOWN 2 is CENTRE
el18a2k 2:532b0225519f 83
el18a2k 2:532b0225519f 84 0 3 1 0
el18a2k 2:532b0225519f 85 1 0 2 1
el18a2k 2:532b0225519f 86 2 1 3 2
el18a2k 2:532b0225519f 87 3 2 0 3 */
el18a2k 2:532b0225519f 88
el18a2k 2:532b0225519f 89
el18a2k 2:532b0225519f 90 if(joystick.get_direction() == N){ //if the joystick is moved upward, then direction is UP
el18a2k 2:532b0225519f 91 direction = UP;
el18a2k 2:532b0225519f 92 }else if (joystick.get_direction() == S){//if the joystick is moved downward, then direction is DOWN
el18a2k 2:532b0225519f 93 direction = DOWN;
el18a2k 2:532b0225519f 94 }else{
el18a2k 2:532b0225519f 95 direction = CENTRE; //if the joystick is left in the centre position then direction is CENTRE
el18a2k 2:532b0225519f 96 };
el18a2k 2:532b0225519f 97
el18a2k 2:532b0225519f 98 y_pos = g_menu_output[state];//y_position is the array output at position 'state'
el18a2k 2:532b0225519f 99
el18a2k 2:532b0225519f 100
el18a2k 2:532b0225519f 101 //implement state machine
el18a2k 2:532b0225519f 102 switch(state) {
el18a2k 4:db929dab4f13 103
el18a2k 4:db929dab4f13 104 //case for new game
el18a2k 2:532b0225519f 105 case 0:
el18a2k 4:db929dab4f13 106 if(button_A.read()){
el18a2k 4:db929dab4f13 107 gamePlay(lcd,dac);
el18a2k 4:db929dab4f13 108 }
el18a2k 2:532b0225519f 109 switch(direction) {
el18a2k 2:532b0225519f 110 case UP:
el18a2k 2:532b0225519f 111 state = 3;
el18a2k 2:532b0225519f 112 break;
el18a2k 2:532b0225519f 113 case DOWN:
el18a2k 2:532b0225519f 114 state = 1;
el18a2k 2:532b0225519f 115 break;
el18a2k 2:532b0225519f 116 case CENTRE:
el18a2k 2:532b0225519f 117 state = 0;
el18a2k 2:532b0225519f 118 break;
el18a2k 2:532b0225519f 119 }
el18a2k 2:532b0225519f 120 break;
el18a2k 4:db929dab4f13 121
el18a2k 4:db929dab4f13 122 //case for HighScore
el18a2k 2:532b0225519f 123 case 1:
el18a2k 4:db929dab4f13 124 if(button_A.read()){
el18a2k 4:db929dab4f13 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 4:db929dab4f13 142 if(button_A.read()){
el18a2k 4:db929dab4f13 143 Instructions(lcd);
el18a2k 4:db929dab4f13 144 }
el18a2k 2:532b0225519f 145 switch(direction) {
el18a2k 2:532b0225519f 146 case UP:
el18a2k 2:532b0225519f 147 state = 1;
el18a2k 2:532b0225519f 148 break;
el18a2k 2:532b0225519f 149 case DOWN:
el18a2k 2:532b0225519f 150 state = 3;
el18a2k 2:532b0225519f 151 break;
el18a2k 2:532b0225519f 152 case CENTRE:
el18a2k 2:532b0225519f 153 state = 2;
el18a2k 2:532b0225519f 154 break;
el18a2k 2:532b0225519f 155 }
el18a2k 2:532b0225519f 156 break;
el18a2k 4:db929dab4f13 157
el18a2k 4:db929dab4f13 158 //case for settings
el18a2k 2:532b0225519f 159 case 3:
el18a2k 4:db929dab4f13 160 if(button_A.read()){
el18a2k 4:db929dab4f13 161 Settings(lcd, dac);
el18a2k 4:db929dab4f13 162 }
el18a2k 2:532b0225519f 163 switch(direction) {
el18a2k 2:532b0225519f 164 case UP:
el18a2k 2:532b0225519f 165 state = 2;
el18a2k 2:532b0225519f 166 break;
el18a2k 2:532b0225519f 167 case DOWN:
el18a2k 2:532b0225519f 168 state = 0;
el18a2k 2:532b0225519f 169 break;
el18a2k 2:532b0225519f 170 case CENTRE:
el18a2k 2:532b0225519f 171 state = 3;
el18a2k 2:532b0225519f 172 break;
el18a2k 4:db929dab4f13 173
el18a2k 2:532b0225519f 174 }
el18a2k 2:532b0225519f 175 break;
el18a2k 2:532b0225519f 176 default:
el18a2k 2:532b0225519f 177 state = 0;
el18a2k 2:532b0225519f 178 break;
el18a2k 2:532b0225519f 179 }
el18a2k 2:532b0225519f 180
el18a2k 2:532b0225519f 181 //draw sprite at position (3, y_pos)
el18a2k 2:532b0225519f 182 lcd.drawSprite(3, y_pos, 4, 9, (int*)Fish_1);
el18a2k 2:532b0225519f 183 lcd.refresh();
el18a2k 2:532b0225519f 184 wait_ms(500);
el18a2k 2:532b0225519f 185 }
el18a2k 2:532b0225519f 186 }
el18a2k 1:7dfa61225fcf 187
el18a2k 4:db929dab4f13 188 // function for game play
el18a2k 4:db929dab4f13 189 void FishEngine::gamePlay(N5110 &lcd, Tone &dac){
el18a2k 4:db929dab4f13 190 graphics.screenFlash(lcd);
el18a2k 4:db929dab4f13 191
el18a2k 4:db929dab4f13 192 do{
el18a2k 4:db929dab4f13 193 lcd.clear();
el18a2k 4:db929dab4f13 194 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
el18a2k 4:db929dab4f13 195 lcd.printString(" Play game", 10, 2);
el18a2k 4:db929dab4f13 196 lcd.refresh();
el18a2k 4:db929dab4f13 197 wait_ms(1000/10);
el18a2k 4:db929dab4f13 198 }while(button_B.read() == false);
el18a2k 4:db929dab4f13 199 }
el18a2k 4:db929dab4f13 200
el18a2k 4:db929dab4f13 201 //function for viewing high score
el18a2k 4:db929dab4f13 202 void FishEngine::highScore(N5110 &lcd){
el18a2k 4:db929dab4f13 203 graphics.screenFlash(lcd);
el18a2k 4:db929dab4f13 204
el18a2k 4:db929dab4f13 205 do{
el18a2k 4:db929dab4f13 206 lcd.clear();
el18a2k 4:db929dab4f13 207 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
el18a2k 4:db929dab4f13 208 lcd.printString(" High score", 10, 2);
el18a2k 4:db929dab4f13 209 lcd.refresh();
el18a2k 4:db929dab4f13 210 wait_ms(1000/10);
el18a2k 4:db929dab4f13 211 }while(button_B.read() == false);
el18a2k 4:db929dab4f13 212 }
el18a2k 4:db929dab4f13 213
el18a2k 4:db929dab4f13 214 //function for providing instruction on how to play
el18a2k 4:db929dab4f13 215 void FishEngine::Instructions(N5110 &lcd){
el18a2k 4:db929dab4f13 216 graphics.screenFlash(lcd);
el18a2k 4:db929dab4f13 217
el18a2k 4:db929dab4f13 218 do{
el18a2k 4:db929dab4f13 219 lcd.clear();
el18a2k 4:db929dab4f13 220 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
el18a2k 4:db929dab4f13 221 lcd.printString(" How to play", 10, 2);
el18a2k 4:db929dab4f13 222 lcd.refresh();
el18a2k 4:db929dab4f13 223 wait_ms(1000/10);
el18a2k 4:db929dab4f13 224 }while(button_B.read() == false);
el18a2k 4:db929dab4f13 225 }
el18a2k 4:db929dab4f13 226
el18a2k 4:db929dab4f13 227 //functions for changing settings
el18a2k 4:db929dab4f13 228 void FishEngine::Settings(N5110 &lcd, Tone &dac){
el18a2k 4:db929dab4f13 229 graphics.screenFlash(lcd);
el18a2k 4:db929dab4f13 230
el18a2k 4:db929dab4f13 231 do{
el18a2k 4:db929dab4f13 232 lcd.clear();
el18a2k 4:db929dab4f13 233 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
el18a2k 4:db929dab4f13 234 lcd.printString(" settings", 10, 2);
el18a2k 4:db929dab4f13 235 lcd.refresh();
el18a2k 4:db929dab4f13 236 wait_ms(1000/10);
el18a2k 4:db929dab4f13 237 }while(button_B.read() == false);
el18a2k 4:db929dab4f13 238 }