submit
Dependencies: mbed Gamepad N5110
Engine/Engine.cpp@11:543c62bed764, 2019-04-21 (annotated)
- Committer:
- 694617778
- Date:
- Sun Apr 21 10:51:50 2019 +0000
- Revision:
- 11:543c62bed764
- Parent:
- 10:68076e3dcc33
- Child:
- 12:684f3ff512d8
t2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
694617778 | 1:b49c36604125 | 1 | #include "Engine.h" |
694617778 | 3:1358cbb05ad3 | 2 | #include "snake.h" |
694617778 | 1:b49c36604125 | 3 | |
694617778 | 1:b49c36604125 | 4 | |
694617778 | 1:b49c36604125 | 5 | Engine::Engine() |
694617778 | 1:b49c36604125 | 6 | { |
694617778 | 1:b49c36604125 | 7 | |
694617778 | 1:b49c36604125 | 8 | } |
694617778 | 1:b49c36604125 | 9 | |
694617778 | 1:b49c36604125 | 10 | Engine::~Engine() |
694617778 | 1:b49c36604125 | 11 | { |
694617778 | 1:b49c36604125 | 12 | |
694617778 | 1:b49c36604125 | 13 | } |
694617778 | 1:b49c36604125 | 14 | |
694617778 | 9:18b059e5abb9 | 15 | void Engine::init() { |
694617778 | 9:18b059e5abb9 | 16 | direction = 0; |
694617778 | 9:18b059e5abb9 | 17 | p = 1.0; |
694617778 | 11:543c62bed764 | 18 | s = 1; |
694617778 | 9:18b059e5abb9 | 19 | } |
694617778 | 9:18b059e5abb9 | 20 | |
694617778 | 8:b6ddc8e10da6 | 21 | int Engine::get_select(Gamepad &pad) { |
694617778 | 11:543c62bed764 | 22 | if ( pad.check_event(Gamepad::Y_PRESSED) == true && s >0) { |
694617778 | 11:543c62bed764 | 23 | s--; |
694617778 | 11:543c62bed764 | 24 | }else if ( pad.check_event(Gamepad::A_PRESSED) == true && s < 2) { |
694617778 | 11:543c62bed764 | 25 | s++; |
694617778 | 8:b6ddc8e10da6 | 26 | } |
694617778 | 8:b6ddc8e10da6 | 27 | return s; |
694617778 | 8:b6ddc8e10da6 | 28 | } |
694617778 | 8:b6ddc8e10da6 | 29 | |
694617778 | 10:68076e3dcc33 | 30 | void Engine::menu(Gamepad &pad, N5110 &lcd) { |
694617778 | 11:543c62bed764 | 31 | while(1){ |
694617778 | 10:68076e3dcc33 | 32 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 10:68076e3dcc33 | 33 | lcd.clear(); |
694617778 | 10:68076e3dcc33 | 34 | s = get_select(pad); |
694617778 | 10:68076e3dcc33 | 35 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 10:68076e3dcc33 | 36 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 37 | lcd.printString(" Press Y or A",2,1); |
694617778 | 10:68076e3dcc33 | 38 | lcd.printString(" Play",4,3); |
694617778 | 10:68076e3dcc33 | 39 | lcd.printString(" Setting",4,4); |
694617778 | 11:543c62bed764 | 40 | if ( s == 1 || s == 0) { |
694617778 | 10:68076e3dcc33 | 41 | lcd.printString("#",55,3); |
694617778 | 10:68076e3dcc33 | 42 | }else if ( s == 2) { |
694617778 | 10:68076e3dcc33 | 43 | lcd.printString("#",55,4); |
694617778 | 10:68076e3dcc33 | 44 | } |
694617778 | 10:68076e3dcc33 | 45 | lcd.refresh(); |
694617778 | 10:68076e3dcc33 | 46 | } |
694617778 | 11:543c62bed764 | 47 | if ( s == 1 || s == 0){ |
694617778 | 10:68076e3dcc33 | 48 | select(pad,lcd); |
694617778 | 11:543c62bed764 | 49 | break; |
694617778 | 10:68076e3dcc33 | 50 | }else if ( s == 2){ |
694617778 | 11:543c62bed764 | 51 | setting(pad,lcd); |
694617778 | 10:68076e3dcc33 | 52 | } |
694617778 | 11:543c62bed764 | 53 | } |
694617778 | 10:68076e3dcc33 | 54 | } |
694617778 | 10:68076e3dcc33 | 55 | |
694617778 | 11:543c62bed764 | 56 | void Engine::setting(Gamepad &pad, N5110 &lcd) { |
694617778 | 11:543c62bed764 | 57 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false ) { |
694617778 | 11:543c62bed764 | 58 | lcd.clear(); |
694617778 | 11:543c62bed764 | 59 | int s = get_select(pad); |
694617778 | 11:543c62bed764 | 60 | float pot = pad.read_pot(); |
694617778 | 11:543c62bed764 | 61 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 62 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 63 | //lcd.printString(" Press Y or A",2,1); |
694617778 | 11:543c62bed764 | 64 | lcd.printString(" Light",4,3); |
694617778 | 11:543c62bed764 | 65 | lcd.printString(" Contrast",4,4); |
694617778 | 11:543c62bed764 | 66 | if ( s == 1 || s == 0){ |
694617778 | 11:543c62bed764 | 67 | lcd.printString("#",65,3); |
694617778 | 11:543c62bed764 | 68 | lcd.drawRect(12,10,60,10,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 69 | lcd.drawRect(12,10,60*pot,10,FILL_BLACK); |
694617778 | 11:543c62bed764 | 70 | lcd.setBrightness(pot); |
694617778 | 11:543c62bed764 | 71 | }else if ( s == 2){ |
694617778 | 11:543c62bed764 | 72 | lcd.printString("#",65,4); |
694617778 | 11:543c62bed764 | 73 | lcd.drawRect(12,10,60,10,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 74 | lcd.drawRect(12,10,60*pot,10,FILL_BLACK); |
694617778 | 11:543c62bed764 | 75 | lcd.setContrast(pot); |
694617778 | 11:543c62bed764 | 76 | } |
694617778 | 11:543c62bed764 | 77 | lcd.refresh(); |
694617778 | 11:543c62bed764 | 78 | } |
694617778 | 11:543c62bed764 | 79 | } |
694617778 | 10:68076e3dcc33 | 80 | |
694617778 | 8:b6ddc8e10da6 | 81 | void Engine::select(Gamepad &pad, N5110 &lcd) { |
694617778 | 8:b6ddc8e10da6 | 82 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 8:b6ddc8e10da6 | 83 | lcd.clear(); |
694617778 | 8:b6ddc8e10da6 | 84 | int s = get_select(pad); |
694617778 | 8:b6ddc8e10da6 | 85 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 8:b6ddc8e10da6 | 86 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 11:543c62bed764 | 87 | lcd.printString(" Press Y or A",2,1); |
694617778 | 8:b6ddc8e10da6 | 88 | lcd.printString(" Easy ",4,2); |
694617778 | 8:b6ddc8e10da6 | 89 | lcd.printString(" Normal ",4,3); |
694617778 | 8:b6ddc8e10da6 | 90 | lcd.printString(" Hard ",4,4); |
694617778 | 8:b6ddc8e10da6 | 91 | if ( s == 0) { |
694617778 | 8:b6ddc8e10da6 | 92 | lcd.printString(" #",50,2); |
694617778 | 8:b6ddc8e10da6 | 93 | p = 1.5; |
694617778 | 8:b6ddc8e10da6 | 94 | }else if ( s == 1 ) { |
694617778 | 8:b6ddc8e10da6 | 95 | lcd.printString(" #",50,3); |
694617778 | 8:b6ddc8e10da6 | 96 | p = 1.0; |
694617778 | 8:b6ddc8e10da6 | 97 | }else if ( s == 2) { |
694617778 | 8:b6ddc8e10da6 | 98 | lcd.printString(" #",50,4); |
694617778 | 8:b6ddc8e10da6 | 99 | p = 0.5; |
694617778 | 8:b6ddc8e10da6 | 100 | } |
694617778 | 8:b6ddc8e10da6 | 101 | lcd.refresh(); |
694617778 | 8:b6ddc8e10da6 | 102 | } |
694617778 | 8:b6ddc8e10da6 | 103 | } |
694617778 | 9:18b059e5abb9 | 104 | |
694617778 | 6:feb6351e6f8e | 105 | |
694617778 | 1:b49c36604125 | 106 | int Engine::get_direction(Gamepad &pad) { |
694617778 | 1:b49c36604125 | 107 | if (pad.check_event(Gamepad::A_PRESSED) == true && direction != 3){ |
694617778 | 1:b49c36604125 | 108 | direction = 1; |
694617778 | 1:b49c36604125 | 109 | } else if (pad.check_event(Gamepad::B_PRESSED) == true && direction != 2){ |
694617778 | 1:b49c36604125 | 110 | direction = 0; |
694617778 | 1:b49c36604125 | 111 | } else if (pad.check_event(Gamepad::X_PRESSED) == true && direction != 0){ |
694617778 | 1:b49c36604125 | 112 | direction = 2; |
694617778 | 1:b49c36604125 | 113 | } else if (pad.check_event(Gamepad::Y_PRESSED) == true && direction != 1){ |
694617778 | 1:b49c36604125 | 114 | direction = 3; |
694617778 | 1:b49c36604125 | 115 | } |
694617778 | 9:18b059e5abb9 | 116 | if (pad.get_direction() == S && direction != 3){ |
694617778 | 9:18b059e5abb9 | 117 | direction = 1; |
694617778 | 9:18b059e5abb9 | 118 | } else if (pad.get_direction() == E && direction != 2){ |
694617778 | 9:18b059e5abb9 | 119 | direction = 0; |
694617778 | 9:18b059e5abb9 | 120 | } else if (pad.get_direction() == W && direction != 0){ |
694617778 | 9:18b059e5abb9 | 121 | direction = 2; |
694617778 | 9:18b059e5abb9 | 122 | } else if (pad.get_direction() == N && direction != 1){ |
694617778 | 9:18b059e5abb9 | 123 | direction = 3; |
694617778 | 9:18b059e5abb9 | 124 | } |
694617778 | 1:b49c36604125 | 125 | return direction; |
694617778 | 1:b49c36604125 | 126 | |
694617778 | 3:1358cbb05ad3 | 127 | } |
694617778 | 3:1358cbb05ad3 | 128 | |
694617778 | 3:1358cbb05ad3 | 129 | // simple splash screen displayed on start-up |
694617778 | 3:1358cbb05ad3 | 130 | void Engine::welcome(Gamepad &pad, N5110 &lcd) { |
694617778 | 5:82094591b4b4 | 131 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 5:82094591b4b4 | 132 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 4:323f42022d87 | 133 | lcd.printString(" ###SNAKE### ",4,1); |
694617778 | 4:323f42022d87 | 134 | lcd.printString(" Qi Minghong ",4,2); |
694617778 | 9:18b059e5abb9 | 135 | lcd.printString(" 201089396 ",10,3); |
694617778 | 5:82094591b4b4 | 136 | lcd.printString(" Press Start ",4,4); |
694617778 | 3:1358cbb05ad3 | 137 | lcd.refresh(); |
694617778 | 3:1358cbb05ad3 | 138 | |
694617778 | 3:1358cbb05ad3 | 139 | // wait flashing LEDs until start button is pressed |
694617778 | 8:b6ddc8e10da6 | 140 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 3:1358cbb05ad3 | 141 | pad.leds_on(); |
694617778 | 3:1358cbb05ad3 | 142 | wait(0.1); |
694617778 | 3:1358cbb05ad3 | 143 | pad.leds_off(); |
694617778 | 3:1358cbb05ad3 | 144 | wait(0.1); |
694617778 | 3:1358cbb05ad3 | 145 | } |
694617778 | 10:68076e3dcc33 | 146 | } |
694617778 | 10:68076e3dcc33 | 147 | // simple splash screen displayed on pasue |
694617778 | 10:68076e3dcc33 | 148 | void Engine::pause(Gamepad &pad, N5110 &lcd) { |
694617778 | 10:68076e3dcc33 | 149 | pad.tone(500.0,0.5); |
694617778 | 10:68076e3dcc33 | 150 | lcd.drawRect(22,13,40,14,FILL_TRANSPARENT); |
694617778 | 10:68076e3dcc33 | 151 | lcd.printString("PAUSE",28,2); |
694617778 | 10:68076e3dcc33 | 152 | lcd.refresh(); |
694617778 | 10:68076e3dcc33 | 153 | |
694617778 | 10:68076e3dcc33 | 154 | // wait flashing LEDs until start button is pressed |
694617778 | 10:68076e3dcc33 | 155 | while ( pad.check_event(Gamepad::BACK_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 10:68076e3dcc33 | 156 | pad.leds_on(); |
694617778 | 10:68076e3dcc33 | 157 | wait(0.1); |
694617778 | 10:68076e3dcc33 | 158 | pad.leds_off(); |
694617778 | 10:68076e3dcc33 | 159 | wait(0.1); |
694617778 | 10:68076e3dcc33 | 160 | } |
694617778 | 3:1358cbb05ad3 | 161 | } |
694617778 | 3:1358cbb05ad3 | 162 | |
694617778 | 3:1358cbb05ad3 | 163 | // simple splash screen of game over |
694617778 | 11:543c62bed764 | 164 | void Engine::gameover(Gamepad &pad, N5110 &lcd, int score,int hscore) { |
694617778 | 4:323f42022d87 | 165 | pad.tone(500.0,0.5); |
694617778 | 3:1358cbb05ad3 | 166 | lcd.init(); |
694617778 | 3:1358cbb05ad3 | 167 | pad.init(); |
694617778 | 3:1358cbb05ad3 | 168 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
694617778 | 3:1358cbb05ad3 | 169 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT); |
694617778 | 4:323f42022d87 | 170 | lcd.printString(" GAME OVER ",4,1); |
694617778 | 4:323f42022d87 | 171 | // we need an array of chars to store the message |
694617778 | 4:323f42022d87 | 172 | char buffer[14]; // max screen witdth is 14 |
694617778 | 4:323f42022d87 | 173 | // print message to buffer |
694617778 | 4:323f42022d87 | 174 | sprintf(buffer," SCORE : %d ",score); |
694617778 | 11:543c62bed764 | 175 | lcd.printString(buffer,2,2); |
694617778 | 11:543c62bed764 | 176 | char hbuffer[14]; // max screen witdth is 14 |
694617778 | 11:543c62bed764 | 177 | // print message to buffer |
694617778 | 11:543c62bed764 | 178 | sprintf(hbuffer,"Maximun : %d",hscore); |
694617778 | 11:543c62bed764 | 179 | lcd.printString(hbuffer,8,3); |
694617778 | 9:18b059e5abb9 | 180 | lcd.printString(" press start ",4,4); |
694617778 | 3:1358cbb05ad3 | 181 | lcd.refresh(); |
694617778 | 3:1358cbb05ad3 | 182 | |
694617778 | 3:1358cbb05ad3 | 183 | // wait flashing LEDs until start button is pressed |
694617778 | 8:b6ddc8e10da6 | 184 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) { |
694617778 | 3:1358cbb05ad3 | 185 | pad.leds_on(); |
694617778 | 3:1358cbb05ad3 | 186 | wait(0.1); |
694617778 | 3:1358cbb05ad3 | 187 | pad.leds_off(); |
694617778 | 3:1358cbb05ad3 | 188 | wait(0.1); |
694617778 | 3:1358cbb05ad3 | 189 | } |
694617778 | 3:1358cbb05ad3 | 190 | |
694617778 | 1:b49c36604125 | 191 | } |