Simple fish eat program
Dependencies: mbed mbed-rtos N5110 ShiftReg Tone
classes/FishEngine.cpp@8:fe51fbf81dee, 2021-03-29 (annotated)
- Committer:
- el18a2k
- Date:
- Mon Mar 29 20:32:45 2021 +0000
- Revision:
- 8:fe51fbf81dee
- Parent:
- 6:7aafcaf7cbe2
- Child:
- 10:e221bd1ce3ec
Instructions and settings complete
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:7aafcaf7cbe2 | 8 | Settings settings; |
el18a2k | 1:7dfa61225fcf | 9 | Graphics graphics; |
el18a2k | 1:7dfa61225fcf | 10 | |
el18a2k | 6:7aafcaf7cbe2 | 11 | int ButtonA; |
el18a2k | 6:7aafcaf7cbe2 | 12 | int ButtonB; |
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 | 6:7aafcaf7cbe2 | 21 | while(settings.buttonA_state(ButtonA) == 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 | 8:fe51fbf81dee | 32 | void FishEngine::Instructions(N5110 &lcd){ |
el18a2k | 8:fe51fbf81dee | 33 | do{ |
el18a2k | 8:fe51fbf81dee | 34 | lcd.clear(); |
el18a2k | 8:fe51fbf81dee | 35 | lcd.printString(" -- Welcome --",0,0); |
el18a2k | 8:fe51fbf81dee | 36 | lcd.printString("Use the",0,2); |
el18a2k | 8:fe51fbf81dee | 37 | lcd.printString("joystick to ",0,3); |
el18a2k | 8:fe51fbf81dee | 38 | lcd.printString("move the fish",0,4); |
el18a2k | 8:fe51fbf81dee | 39 | lcd.refresh(); |
el18a2k | 8:fe51fbf81dee | 40 | |
el18a2k | 8:fe51fbf81dee | 41 | wait_ms(2000); |
el18a2k | 8:fe51fbf81dee | 42 | |
el18a2k | 8:fe51fbf81dee | 43 | lcd.clear(); |
el18a2k | 8:fe51fbf81dee | 44 | lcd.printString("remeber to",0,1); |
el18a2k | 8:fe51fbf81dee | 45 | lcd.printString("only eat fish",0,2); |
el18a2k | 8:fe51fbf81dee | 46 | lcd.printString("smaller than",0,3); |
el18a2k | 8:fe51fbf81dee | 47 | lcd.printString("you!!",0,4); |
el18a2k | 8:fe51fbf81dee | 48 | lcd.refresh(); |
el18a2k | 8:fe51fbf81dee | 49 | |
el18a2k | 8:fe51fbf81dee | 50 | wait_ms(2000); |
el18a2k | 8:fe51fbf81dee | 51 | |
el18a2k | 8:fe51fbf81dee | 52 | lcd.clear(); |
el18a2k | 8:fe51fbf81dee | 53 | lcd.printString("Hold B to Exit",0,2); |
el18a2k | 8:fe51fbf81dee | 54 | lcd.refresh(); |
el18a2k | 8:fe51fbf81dee | 55 | |
el18a2k | 8:fe51fbf81dee | 56 | wait_ms(2000); |
el18a2k | 8:fe51fbf81dee | 57 | |
el18a2k | 8:fe51fbf81dee | 58 | }while(settings.buttonB_state(ButtonB) == false); |
el18a2k | 8:fe51fbf81dee | 59 | } |
el18a2k | 8:fe51fbf81dee | 60 | |
el18a2k | 2:532b0225519f | 61 | // creates the games main menu screen with scrolling ability through the use of a |
el18a2k | 2:532b0225519f | 62 | // state machine. |
el18a2k | 2:532b0225519f | 63 | void FishEngine::mainMenu(N5110 &lcd, Tone &dac, Joystick &joystick){ |
el18a2k | 2:532b0225519f | 64 | const int Fish_1[4][9]= { |
el18a2k | 2:532b0225519f | 65 | { 1,0,0,1,1,1,1,1,0, }, |
el18a2k | 2:532b0225519f | 66 | { 1,1,1,1,1,1,1,0,1, }, |
el18a2k | 2:532b0225519f | 67 | { 1,1,1,1,1,1,1,1,1, }, |
el18a2k | 2:532b0225519f | 68 | { 1,0,0,1,1,1,1,1,0, }, |
el18a2k | 2:532b0225519f | 69 | }; |
el18a2k | 2:532b0225519f | 70 | |
el18a2k | 2:532b0225519f | 71 | // outputs for the state machine are used to draw Fish_1 beside the corrosponding |
el18a2k | 2:532b0225519f | 72 | // menu choice |
el18a2k | 2:532b0225519f | 73 | int g_menu_output[4] = {10, 18, 26, 34}; |
el18a2k | 2:532b0225519f | 74 | int state = 0; //set inital state to 0 |
el18a2k | 2:532b0225519f | 75 | int direction = CENTRE; //set inital direction to remain on that state until an input is received |
el18a2k | 2:532b0225519f | 76 | int y_pos; |
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 | 2:532b0225519f | 86 | lcd.printString("Settings", 15, 4); |
el18a2k | 2:532b0225519f | 87 | |
el18a2k | 2:532b0225519f | 88 | |
el18a2k | 2:532b0225519f | 89 | /*implementation of state machine allows the determination of the position of the fish marker |
el18a2k | 2:532b0225519f | 90 | based on the users joystick input: |
el18a2k | 2:532b0225519f | 91 | |
el18a2k | 2:532b0225519f | 92 | inputs |
el18a2k | 2:532b0225519f | 93 | state 0 1 2 where 0 is UP and 1 is DOWN 2 is CENTRE |
el18a2k | 2:532b0225519f | 94 | |
el18a2k | 2:532b0225519f | 95 | 0 3 1 0 |
el18a2k | 2:532b0225519f | 96 | 1 0 2 1 |
el18a2k | 2:532b0225519f | 97 | 2 1 3 2 |
el18a2k | 2:532b0225519f | 98 | 3 2 0 3 */ |
el18a2k | 2:532b0225519f | 99 | |
el18a2k | 2:532b0225519f | 100 | |
el18a2k | 2:532b0225519f | 101 | if(joystick.get_direction() == N){ //if the joystick is moved upward, then direction is UP |
el18a2k | 2:532b0225519f | 102 | direction = UP; |
el18a2k | 2:532b0225519f | 103 | }else if (joystick.get_direction() == S){//if the joystick is moved downward, then direction is DOWN |
el18a2k | 2:532b0225519f | 104 | direction = DOWN; |
el18a2k | 2:532b0225519f | 105 | }else{ |
el18a2k | 2:532b0225519f | 106 | direction = CENTRE; //if the joystick is left in the centre position then direction is CENTRE |
el18a2k | 2:532b0225519f | 107 | }; |
el18a2k | 2:532b0225519f | 108 | |
el18a2k | 2:532b0225519f | 109 | y_pos = g_menu_output[state];//y_position is the array output at position 'state' |
el18a2k | 2:532b0225519f | 110 | |
el18a2k | 2:532b0225519f | 111 | |
el18a2k | 2:532b0225519f | 112 | //implement state machine |
el18a2k | 2:532b0225519f | 113 | switch(state) { |
el18a2k | 4:db929dab4f13 | 114 | |
el18a2k | 4:db929dab4f13 | 115 | //case for new game |
el18a2k | 2:532b0225519f | 116 | case 0: |
el18a2k | 6:7aafcaf7cbe2 | 117 | if(settings.buttonA_state(ButtonA)){ |
el18a2k | 4:db929dab4f13 | 118 | } |
el18a2k | 2:532b0225519f | 119 | switch(direction) { |
el18a2k | 2:532b0225519f | 120 | case UP: |
el18a2k | 2:532b0225519f | 121 | state = 3; |
el18a2k | 2:532b0225519f | 122 | break; |
el18a2k | 2:532b0225519f | 123 | case DOWN: |
el18a2k | 2:532b0225519f | 124 | state = 1; |
el18a2k | 2:532b0225519f | 125 | break; |
el18a2k | 2:532b0225519f | 126 | case CENTRE: |
el18a2k | 2:532b0225519f | 127 | state = 0; |
el18a2k | 2:532b0225519f | 128 | break; |
el18a2k | 2:532b0225519f | 129 | } |
el18a2k | 2:532b0225519f | 130 | break; |
el18a2k | 4:db929dab4f13 | 131 | |
el18a2k | 4:db929dab4f13 | 132 | //case for HighScore |
el18a2k | 2:532b0225519f | 133 | case 1: |
el18a2k | 6:7aafcaf7cbe2 | 134 | if(settings.buttonA_state(ButtonA)){ |
el18a2k | 4:db929dab4f13 | 135 | } |
el18a2k | 2:532b0225519f | 136 | switch(direction) { |
el18a2k | 2:532b0225519f | 137 | case UP: |
el18a2k | 2:532b0225519f | 138 | state = 0; |
el18a2k | 2:532b0225519f | 139 | break; |
el18a2k | 2:532b0225519f | 140 | case DOWN: |
el18a2k | 2:532b0225519f | 141 | state = 2; |
el18a2k | 2:532b0225519f | 142 | break; |
el18a2k | 2:532b0225519f | 143 | case CENTRE: |
el18a2k | 2:532b0225519f | 144 | state = 1; |
el18a2k | 2:532b0225519f | 145 | break; |
el18a2k | 2:532b0225519f | 146 | } |
el18a2k | 2:532b0225519f | 147 | break; |
el18a2k | 4:db929dab4f13 | 148 | |
el18a2k | 4:db929dab4f13 | 149 | //case for how to play |
el18a2k | 2:532b0225519f | 150 | case 2: |
el18a2k | 6:7aafcaf7cbe2 | 151 | if(settings.buttonA_state(ButtonA)){ |
el18a2k | 8:fe51fbf81dee | 152 | graphics.screenFlash(lcd); |
el18a2k | 4:db929dab4f13 | 153 | Instructions(lcd); |
el18a2k | 4:db929dab4f13 | 154 | } |
el18a2k | 2:532b0225519f | 155 | switch(direction) { |
el18a2k | 2:532b0225519f | 156 | case UP: |
el18a2k | 2:532b0225519f | 157 | state = 1; |
el18a2k | 2:532b0225519f | 158 | break; |
el18a2k | 2:532b0225519f | 159 | case DOWN: |
el18a2k | 2:532b0225519f | 160 | state = 3; |
el18a2k | 2:532b0225519f | 161 | break; |
el18a2k | 2:532b0225519f | 162 | case CENTRE: |
el18a2k | 2:532b0225519f | 163 | state = 2; |
el18a2k | 2:532b0225519f | 164 | break; |
el18a2k | 2:532b0225519f | 165 | } |
el18a2k | 2:532b0225519f | 166 | break; |
el18a2k | 4:db929dab4f13 | 167 | |
el18a2k | 4:db929dab4f13 | 168 | //case for settings |
el18a2k | 2:532b0225519f | 169 | case 3: |
el18a2k | 6:7aafcaf7cbe2 | 170 | if(settings.buttonA_state(ButtonA)){ |
el18a2k | 6:7aafcaf7cbe2 | 171 | graphics.screenFlash(lcd); |
el18a2k | 6:7aafcaf7cbe2 | 172 | settings.settingsMenu(lcd, dac, joystick); |
el18a2k | 4:db929dab4f13 | 173 | } |
el18a2k | 2:532b0225519f | 174 | switch(direction) { |
el18a2k | 2:532b0225519f | 175 | case UP: |
el18a2k | 2:532b0225519f | 176 | state = 2; |
el18a2k | 2:532b0225519f | 177 | break; |
el18a2k | 2:532b0225519f | 178 | case DOWN: |
el18a2k | 2:532b0225519f | 179 | state = 0; |
el18a2k | 2:532b0225519f | 180 | break; |
el18a2k | 2:532b0225519f | 181 | case CENTRE: |
el18a2k | 2:532b0225519f | 182 | state = 3; |
el18a2k | 2:532b0225519f | 183 | break; |
el18a2k | 4:db929dab4f13 | 184 | |
el18a2k | 2:532b0225519f | 185 | } |
el18a2k | 2:532b0225519f | 186 | break; |
el18a2k | 2:532b0225519f | 187 | default: |
el18a2k | 2:532b0225519f | 188 | state = 0; |
el18a2k | 2:532b0225519f | 189 | break; |
el18a2k | 2:532b0225519f | 190 | } |
el18a2k | 2:532b0225519f | 191 | |
el18a2k | 2:532b0225519f | 192 | //draw sprite at position (3, y_pos) |
el18a2k | 2:532b0225519f | 193 | lcd.drawSprite(3, y_pos, 4, 9, (int*)Fish_1); |
el18a2k | 2:532b0225519f | 194 | lcd.refresh(); |
el18a2k | 2:532b0225519f | 195 | wait_ms(500); |
el18a2k | 2:532b0225519f | 196 | } |
el18a2k | 8:fe51fbf81dee | 197 | } |