submit
Dependencies: mbed Gamepad N5110
Engine/Engine.cpp@23:fd0339ec480c, 2019-05-06 (annotated)
- Committer:
- 694617778
- Date:
- Mon May 06 05:26:28 2019 +0000
- Revision:
- 23:fd0339ec480c
- Parent:
- 22:51fb86b48d9d
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
694617778 | 1:b49c36604125 | 1 | #include "Engine.h" |
694617778 | 13:32772a08841e | 2 | |
694617778 | 20:6ef06ba15666 | 3 | // nothing doing in the constructor and destructor |
694617778 | 20:6ef06ba15666 | 4 | Engine::Engine() |
694617778 | 20:6ef06ba15666 | 5 | { |
694617778 | 20:6ef06ba15666 | 6 | |
694617778 | 20:6ef06ba15666 | 7 | } |
694617778 | 20:6ef06ba15666 | 8 | |
694617778 | 20:6ef06ba15666 | 9 | Engine::~Engine() |
694617778 | 20:6ef06ba15666 | 10 | { |
694617778 | 20:6ef06ba15666 | 11 | |
694617778 | 20:6ef06ba15666 | 12 | } |
694617778 | 20:6ef06ba15666 | 13 | // draw the pointer |
694617778 | 12:684f3ff512d8 | 14 | int sprite[8][8] = { |
694617778 | 12:684f3ff512d8 | 15 | {0,0,0,1,1,0,0,0}, |
694617778 | 12:684f3ff512d8 | 16 | {0,0,1,1,0,0,0,0}, |
694617778 | 12:684f3ff512d8 | 17 | {0,1,1,0,0,0,0,0}, |
694617778 | 12:684f3ff512d8 | 18 | {1,1,1,1,1,1,1,0}, |
694617778 | 12:684f3ff512d8 | 19 | {1,1,1,1,1,1,1,0}, |
694617778 | 12:684f3ff512d8 | 20 | {0,1,1,0,0,0,0,0}, |
694617778 | 12:684f3ff512d8 | 21 | {0,0,1,1,0,0,0,0}, |
694617778 | 12:684f3ff512d8 | 22 | {0,0,0,1,1,0,0,0}, |
694617778 | 12:684f3ff512d8 | 23 | }; |
694617778 | 20:6ef06ba15666 | 24 | // initialise the components of engine |
694617778 | 20:6ef06ba15666 | 25 | void Engine::init() { |
694617778 | 20:6ef06ba15666 | 26 | direction = 0; //direction |
694617778 | 20:6ef06ba15666 | 27 | game = 0; // game number |
694617778 | 20:6ef06ba15666 | 28 | p = 1.0; // speed |
694617778 | 20:6ef06ba15666 | 29 | s = 1; // selection |
694617778 | 1:b49c36604125 | 30 | } |
694617778 | 20:6ef06ba15666 | 31 | // use Y and A make selection |
694617778 | 8:b6ddc8e10da6 | 32 | int Engine::get_select(Gamepad &pad) { |
694617778 | 11:543c62bed764 | 33 | if ( pad.check_event(Gamepad::Y_PRESSED) == true && s >0) { |
694617778 | 11:543c62bed764 | 34 | s--; |
694617778 | 11:543c62bed764 | 35 | }else if ( pad.check_event(Gamepad::A_PRESSED) == true && s < 2) { |
694617778 | 11:543c62bed764 | 36 | s++; |
694617778 | 8:b6ddc8e10da6 | 37 | } |
694617778 | 8:b6ddc8e10da6 | 38 | return s; |
694617778 | 8:b6ddc8e10da6 | 39 | } |
694617778 | 20:6ef06ba15666 | 40 | // display of menu and wait user to select game, setting and game |
694617778 | 10:68076e3dcc33 | 41 | void Engine::menu(Gamepad &pad, N5110 &lcd) { |
694617778 | 11:543c62bed764 | 42 | while(1){ |
694617778 | 10:68076e3dcc33 | 43 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 10:68076e3dcc33 | 44 | lcd.clear(); |
694617778 | 22:51fb86b48d9d | 45 | s = get_select(pad); // get the selection |
694617778 | 10:68076e3dcc33 | 46 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 10:68076e3dcc33 | 47 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 22:51fb86b48d9d | 48 | lcd.printString(" Press Y or A",2,1); // print the instruction |
694617778 | 14:e1f7b4be1cf2 | 49 | lcd.printString(" Guess",4,2); |
694617778 | 14:e1f7b4be1cf2 | 50 | lcd.printString(" Snake",4,3); |
694617778 | 10:68076e3dcc33 | 51 | lcd.printString(" Setting",4,4); |
694617778 | 14:e1f7b4be1cf2 | 52 | if ( s == 1 ) { |
694617778 | 14:e1f7b4be1cf2 | 53 | lcd.drawSprite(60,24,8,8,(int *)sprite); |
694617778 | 10:68076e3dcc33 | 54 | }else if ( s == 2) { |
694617778 | 12:684f3ff512d8 | 55 | lcd.drawSprite(60,32,8,8,(int *)sprite); |
694617778 | 14:e1f7b4be1cf2 | 56 | }else if( s == 0) { |
694617778 | 14:e1f7b4be1cf2 | 57 | lcd.drawSprite(60,16,8,8,(int *)sprite); |
694617778 | 14:e1f7b4be1cf2 | 58 | } |
694617778 | 10:68076e3dcc33 | 59 | lcd.refresh(); |
694617778 | 10:68076e3dcc33 | 60 | } |
694617778 | 22:51fb86b48d9d | 61 | if ( s == 1 ){ // select the game |
694617778 | 10:68076e3dcc33 | 62 | select(pad,lcd); |
694617778 | 14:e1f7b4be1cf2 | 63 | game = 0; |
694617778 | 11:543c62bed764 | 64 | break; |
694617778 | 10:68076e3dcc33 | 65 | }else if ( s == 2){ |
694617778 | 11:543c62bed764 | 66 | setting(pad,lcd); |
694617778 | 14:e1f7b4be1cf2 | 67 | }else if (s == 0){ |
694617778 | 14:e1f7b4be1cf2 | 68 | game = 1; |
694617778 | 14:e1f7b4be1cf2 | 69 | break; |
694617778 | 14:e1f7b4be1cf2 | 70 | } |
694617778 | 11:543c62bed764 | 71 | } |
694617778 | 10:68076e3dcc33 | 72 | } |
694617778 | 20:6ef06ba15666 | 73 | // use to change the brightness and contrast of LED |
694617778 | 11:543c62bed764 | 74 | void Engine::setting(Gamepad &pad, N5110 &lcd) { |
694617778 | 11:543c62bed764 | 75 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false ) { |
694617778 | 11:543c62bed764 | 76 | lcd.clear(); |
694617778 | 11:543c62bed764 | 77 | int s = get_select(pad); |
694617778 | 11:543c62bed764 | 78 | float pot = pad.read_pot(); |
694617778 | 11:543c62bed764 | 79 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 80 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 81 | lcd.printString(" Light",4,3); |
694617778 | 11:543c62bed764 | 82 | lcd.printString(" Contrast",4,4); |
694617778 | 11:543c62bed764 | 83 | if ( s == 1 || s == 0){ |
694617778 | 12:684f3ff512d8 | 84 | lcd.drawSprite(60,23,8,8,(int *)sprite); |
694617778 | 11:543c62bed764 | 85 | lcd.drawRect(12,10,60,10,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 86 | lcd.drawRect(12,10,60*pot,10,FILL_BLACK); |
694617778 | 11:543c62bed764 | 87 | lcd.setBrightness(pot); |
694617778 | 11:543c62bed764 | 88 | }else if ( s == 2){ |
694617778 | 12:684f3ff512d8 | 89 | lcd.drawSprite(60,32,8,8,(int *)sprite); |
694617778 | 11:543c62bed764 | 90 | lcd.drawRect(12,10,60,10,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 91 | lcd.drawRect(12,10,60*pot,10,FILL_BLACK); |
694617778 | 11:543c62bed764 | 92 | lcd.setContrast(pot); |
694617778 | 11:543c62bed764 | 93 | } |
694617778 | 11:543c62bed764 | 94 | lcd.refresh(); |
694617778 | 11:543c62bed764 | 95 | } |
694617778 | 11:543c62bed764 | 96 | } |
694617778 | 10:68076e3dcc33 | 97 | |
694617778 | 20:6ef06ba15666 | 98 | // simple splash screen of diffculty selection |
694617778 | 8:b6ddc8e10da6 | 99 | void Engine::select(Gamepad &pad, N5110 &lcd) { |
694617778 | 8:b6ddc8e10da6 | 100 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 8:b6ddc8e10da6 | 101 | lcd.clear(); |
694617778 | 8:b6ddc8e10da6 | 102 | int s = get_select(pad); |
694617778 | 8:b6ddc8e10da6 | 103 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 8:b6ddc8e10da6 | 104 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 105 | lcd.printString(" Press Y or A",2,1); |
694617778 | 12:684f3ff512d8 | 106 | lcd.printString(" Easy",4,2); |
694617778 | 12:684f3ff512d8 | 107 | lcd.printString(" Normal",4,3); |
694617778 | 12:684f3ff512d8 | 108 | lcd.printString(" Hard",4,4); |
694617778 | 8:b6ddc8e10da6 | 109 | if ( s == 0) { |
694617778 | 12:684f3ff512d8 | 110 | lcd.drawSprite(63,15,8,8,(int *)sprite); |
694617778 | 20:6ef06ba15666 | 111 | p = 1.5; // change the speed |
694617778 | 8:b6ddc8e10da6 | 112 | }else if ( s == 1 ) { |
694617778 | 12:684f3ff512d8 | 113 | lcd.drawSprite(63,24,8,8,(int *)sprite); |
694617778 | 8:b6ddc8e10da6 | 114 | p = 1.0; |
694617778 | 8:b6ddc8e10da6 | 115 | }else if ( s == 2) { |
694617778 | 12:684f3ff512d8 | 116 | lcd.drawSprite(63,32,8,8,(int *)sprite); |
694617778 | 8:b6ddc8e10da6 | 117 | p = 0.5; |
694617778 | 8:b6ddc8e10da6 | 118 | } |
694617778 | 8:b6ddc8e10da6 | 119 | lcd.refresh(); |
694617778 | 8:b6ddc8e10da6 | 120 | } |
694617778 | 8:b6ddc8e10da6 | 121 | } |
694617778 | 9:18b059e5abb9 | 122 | |
694617778 | 20:6ef06ba15666 | 123 | // check the direction of the snake |
694617778 | 1:b49c36604125 | 124 | int Engine::get_direction(Gamepad &pad) { |
694617778 | 1:b49c36604125 | 125 | if (pad.check_event(Gamepad::A_PRESSED) == true && direction != 3){ |
694617778 | 1:b49c36604125 | 126 | direction = 1; |
694617778 | 1:b49c36604125 | 127 | } else if (pad.check_event(Gamepad::B_PRESSED) == true && direction != 2){ |
694617778 | 1:b49c36604125 | 128 | direction = 0; |
694617778 | 1:b49c36604125 | 129 | } else if (pad.check_event(Gamepad::X_PRESSED) == true && direction != 0){ |
694617778 | 1:b49c36604125 | 130 | direction = 2; |
694617778 | 1:b49c36604125 | 131 | } else if (pad.check_event(Gamepad::Y_PRESSED) == true && direction != 1){ |
694617778 | 1:b49c36604125 | 132 | direction = 3; |
694617778 | 1:b49c36604125 | 133 | } |
694617778 | 20:6ef06ba15666 | 134 | // use the joystick can also control the snake |
694617778 | 9:18b059e5abb9 | 135 | if (pad.get_direction() == S && direction != 3){ |
694617778 | 9:18b059e5abb9 | 136 | direction = 1; |
694617778 | 9:18b059e5abb9 | 137 | } else if (pad.get_direction() == E && direction != 2){ |
694617778 | 9:18b059e5abb9 | 138 | direction = 0; |
694617778 | 9:18b059e5abb9 | 139 | } else if (pad.get_direction() == W && direction != 0){ |
694617778 | 9:18b059e5abb9 | 140 | direction = 2; |
694617778 | 9:18b059e5abb9 | 141 | } else if (pad.get_direction() == N && direction != 1){ |
694617778 | 9:18b059e5abb9 | 142 | direction = 3; |
694617778 | 9:18b059e5abb9 | 143 | } |
694617778 | 1:b49c36604125 | 144 | return direction; |
694617778 | 1:b49c36604125 | 145 | |
694617778 | 3:1358cbb05ad3 | 146 | } |
694617778 | 3:1358cbb05ad3 | 147 | |
694617778 | 3:1358cbb05ad3 | 148 | // simple splash screen displayed on start-up |
694617778 | 3:1358cbb05ad3 | 149 | void Engine::welcome(Gamepad &pad, N5110 &lcd) { |
694617778 | 22:51fb86b48d9d | 150 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw the area of game |
694617778 | 5:82094591b4b4 | 151 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 22:51fb86b48d9d | 152 | lcd.printString("###ULTRA###",10,1); // print the game name |
694617778 | 22:51fb86b48d9d | 153 | lcd.printString("#TWO IN ONE#",7,2); |
694617778 | 22:51fb86b48d9d | 154 | lcd.printString(" Qi Minghong ",4,3); |
694617778 | 5:82094591b4b4 | 155 | lcd.printString(" Press Start ",4,4); |
694617778 | 3:1358cbb05ad3 | 156 | lcd.refresh(); |
694617778 | 3:1358cbb05ad3 | 157 | |
694617778 | 21:5861b5baf4b0 | 158 | // wait flashing LEDs with music until start button is pressed |
694617778 | 8:b6ddc8e10da6 | 159 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 3:1358cbb05ad3 | 160 | pad.leds_on(); |
694617778 | 22:51fb86b48d9d | 161 | pad.tone(659.0,0.15); // play some simple music |
694617778 | 21:5861b5baf4b0 | 162 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 163 | pad.leds_off(); |
694617778 | 21:5861b5baf4b0 | 164 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 165 | pad.tone(659.0,0.15); |
694617778 | 21:5861b5baf4b0 | 166 | pad.leds_on(); |
694617778 | 21:5861b5baf4b0 | 167 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 168 | pad.leds_off(); |
694617778 | 21:5861b5baf4b0 | 169 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 170 | pad.tone(587.0,0.15); |
694617778 | 21:5861b5baf4b0 | 171 | pad.leds_on(); |
694617778 | 21:5861b5baf4b0 | 172 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 173 | pad.leds_off(); |
694617778 | 21:5861b5baf4b0 | 174 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 175 | pad.tone(659.0,0.15); |
694617778 | 21:5861b5baf4b0 | 176 | pad.leds_on(); |
694617778 | 21:5861b5baf4b0 | 177 | wait(0.15); |
694617778 | 3:1358cbb05ad3 | 178 | pad.leds_off(); |
694617778 | 21:5861b5baf4b0 | 179 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 180 | pad.tone(784.0,0.15); |
694617778 | 21:5861b5baf4b0 | 181 | pad.leds_on(); |
694617778 | 21:5861b5baf4b0 | 182 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 183 | pad.leds_off(); |
694617778 | 21:5861b5baf4b0 | 184 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 185 | pad.leds_on(); |
694617778 | 21:5861b5baf4b0 | 186 | pad.tone(659.0,0.15); |
694617778 | 21:5861b5baf4b0 | 187 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 188 | pad.leds_off(); |
694617778 | 21:5861b5baf4b0 | 189 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 190 | pad.tone(659.0,0.15); |
694617778 | 21:5861b5baf4b0 | 191 | pad.leds_on(); |
694617778 | 21:5861b5baf4b0 | 192 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 193 | pad.leds_off(); |
694617778 | 21:5861b5baf4b0 | 194 | wait(0.15); |
694617778 | 21:5861b5baf4b0 | 195 | |
694617778 | 3:1358cbb05ad3 | 196 | } |
694617778 | 10:68076e3dcc33 | 197 | } |
694617778 | 21:5861b5baf4b0 | 198 | |
694617778 | 21:5861b5baf4b0 | 199 | |
694617778 | 10:68076e3dcc33 | 200 | // simple splash screen displayed on pasue |
694617778 | 10:68076e3dcc33 | 201 | void Engine::pause(Gamepad &pad, N5110 &lcd) { |
694617778 | 10:68076e3dcc33 | 202 | pad.tone(500.0,0.5); |
694617778 | 10:68076e3dcc33 | 203 | lcd.drawRect(22,13,40,14,FILL_TRANSPARENT); |
694617778 | 10:68076e3dcc33 | 204 | lcd.printString("PAUSE",28,2); |
694617778 | 10:68076e3dcc33 | 205 | lcd.refresh(); |
694617778 | 10:68076e3dcc33 | 206 | |
694617778 | 10:68076e3dcc33 | 207 | // wait flashing LEDs until start button is pressed |
694617778 | 10:68076e3dcc33 | 208 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 10:68076e3dcc33 | 209 | pad.leds_on(); |
694617778 | 10:68076e3dcc33 | 210 | wait(0.1); |
694617778 | 10:68076e3dcc33 | 211 | pad.leds_off(); |
694617778 | 10:68076e3dcc33 | 212 | wait(0.1); |
694617778 | 10:68076e3dcc33 | 213 | } |
694617778 | 3:1358cbb05ad3 | 214 | } |
694617778 | 3:1358cbb05ad3 | 215 | |
694617778 | 3:1358cbb05ad3 | 216 | // simple splash screen of game over |
694617778 | 11:543c62bed764 | 217 | void Engine::gameover(Gamepad &pad, N5110 &lcd, int score,int hscore) { |
694617778 | 21:5861b5baf4b0 | 218 | pad.tone(1000.0,0.5); |
694617778 | 21:5861b5baf4b0 | 219 | wait(0.1); |
694617778 | 4:323f42022d87 | 220 | pad.tone(500.0,0.5); |
694617778 | 21:5861b5baf4b0 | 221 | wait(0.1); |
694617778 | 21:5861b5baf4b0 | 222 | pad.tone(300.0,0.5); |
694617778 | 21:5861b5baf4b0 | 223 | wait(0.1); |
694617778 | 21:5861b5baf4b0 | 224 | pad.tone(100.0,0.5); |
694617778 | 21:5861b5baf4b0 | 225 | wait(0.1); |
694617778 | 12:684f3ff512d8 | 226 | lcd.clear(); |
694617778 | 3:1358cbb05ad3 | 227 | pad.init(); |
694617778 | 3:1358cbb05ad3 | 228 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 3:1358cbb05ad3 | 229 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 4:323f42022d87 | 230 | lcd.printString(" GAME OVER ",4,1); |
694617778 | 4:323f42022d87 | 231 | // we need an array of chars to store the message |
694617778 | 4:323f42022d87 | 232 | char buffer[14]; // max screen witdth is 14 |
694617778 | 4:323f42022d87 | 233 | // print message to buffer |
694617778 | 12:684f3ff512d8 | 234 | sprintf(buffer," Score : %d ",score); |
694617778 | 11:543c62bed764 | 235 | lcd.printString(buffer,2,2); |
694617778 | 20:6ef06ba15666 | 236 | char hbuffer[14]; |
694617778 | 18:2ca7bd135b68 | 237 | sprintf(hbuffer,"Record : %d",hscore); |
694617778 | 11:543c62bed764 | 238 | lcd.printString(hbuffer,8,3); |
694617778 | 12:684f3ff512d8 | 239 | lcd.printString(" press back",8,4); |
694617778 | 3:1358cbb05ad3 | 240 | lcd.refresh(); |
694617778 | 3:1358cbb05ad3 | 241 | |
694617778 | 3:1358cbb05ad3 | 242 | // wait flashing LEDs until start button is pressed |
694617778 | 12:684f3ff512d8 | 243 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 3:1358cbb05ad3 | 244 | pad.leds_on(); |
694617778 | 3:1358cbb05ad3 | 245 | wait(0.1); |
694617778 | 3:1358cbb05ad3 | 246 | pad.leds_off(); |
694617778 | 3:1358cbb05ad3 | 247 | wait(0.1); |
694617778 | 3:1358cbb05ad3 | 248 | } |
694617778 | 3:1358cbb05ad3 | 249 | |
694617778 | 1:b49c36604125 | 250 | } |