Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed ll16j23s_test_docs
SnakeEngine/SnakeEngine.cpp@10:a2d643b3c782, 2020-05-26 (annotated)
- Committer:
- JoeShotton
- Date:
- Tue May 26 23:25:09 2020 +0000
- Revision:
- 10:a2d643b3c782
- Parent:
- 9:0571880085cc
Removed several bugs, started on thorough documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JoeShotton | 3:fcd6d70e9694 | 1 | #include "SnakeEngine.h" |
JoeShotton | 3:fcd6d70e9694 | 2 | |
JoeShotton | 7:dd84e0fab346 | 3 | SnakeEngine::SnakeEngine() { |
JoeShotton | 7:dd84e0fab346 | 4 | //constructor |
JoeShotton | 9:0571880085cc | 5 | _menu_select = 0; |
JoeShotton | 9:0571880085cc | 6 | _map_select = 0; |
JoeShotton | 8:bcc3403d7e79 | 7 | _pot2 = 0.5; |
JoeShotton | 9:0571880085cc | 8 | _death = false; |
JoeShotton | 10:a2d643b3c782 | 9 | score = 0; |
JoeShotton | 9:0571880085cc | 10 | |
JoeShotton | 3:fcd6d70e9694 | 11 | } |
JoeShotton | 3:fcd6d70e9694 | 12 | |
JoeShotton | 3:fcd6d70e9694 | 13 | SnakeEngine::~SnakeEngine() |
JoeShotton | 3:fcd6d70e9694 | 14 | { |
JoeShotton | 3:fcd6d70e9694 | 15 | //destructor |
JoeShotton | 3:fcd6d70e9694 | 16 | } |
JoeShotton | 3:fcd6d70e9694 | 17 | |
JoeShotton | 7:dd84e0fab346 | 18 | void SnakeEngine::game_init(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ |
JoeShotton | 10:a2d643b3c782 | 19 | game_reset(); //resets game |
JoeShotton | 10:a2d643b3c782 | 20 | transition_black(lcd); //transition animations |
JoeShotton | 9:0571880085cc | 21 | transition_white(lcd); |
JoeShotton | 10:a2d643b3c782 | 22 | map_run(lcd); //displays map |
JoeShotton | 10:a2d643b3c782 | 23 | lcd.refresh(); |
JoeShotton | 10:a2d643b3c782 | 24 | _food.init(pad, lcd, mag); //initialises food (including new seed for random function |
JoeShotton | 10:a2d643b3c782 | 25 | _body.init(); //initialises body |
JoeShotton | 10:a2d643b3c782 | 26 | menu_flash(pad, 3); //flashes LEDs |
JoeShotton | 10:a2d643b3c782 | 27 | game_state = 3; //sets game_state to the game itself, which changes the active while loop in main.cpp |
JoeShotton | 7:dd84e0fab346 | 28 | } |
JoeShotton | 7:dd84e0fab346 | 29 | |
JoeShotton | 4:ea3fa51c4386 | 30 | |
JoeShotton | 7:dd84e0fab346 | 31 | void SnakeEngine::game_run(Gamepad &pad, N5110 &lcd){ |
JoeShotton | 10:a2d643b3c782 | 32 | map_run(lcd); //displays the map and calculates relevant map collisions |
JoeShotton | 10:a2d643b3c782 | 33 | _body.run(pad, lcd, _death); |
JoeShotton | 10:a2d643b3c782 | 34 | _food.run(lcd); |
JoeShotton | 10:a2d643b3c782 | 35 | snake_food_collision(pad, lcd); |
JoeShotton | 10:a2d643b3c782 | 36 | if (_death == true) { //checks death (either snake-snake collision or snake-wall-collision) |
JoeShotton | 10:a2d643b3c782 | 37 | //printf("Dead!"); |
JoeShotton | 10:a2d643b3c782 | 38 | game_state = 4; //sets game_state to death, which changes the active while loop in main.cpp |
JoeShotton | 10:a2d643b3c782 | 39 | death_init(pad, lcd); //displays death menu |
JoeShotton | 8:bcc3403d7e79 | 40 | } |
JoeShotton | 7:dd84e0fab346 | 41 | //snake-wall collision |
JoeShotton | 7:dd84e0fab346 | 42 | } |
JoeShotton | 7:dd84e0fab346 | 43 | |
JoeShotton | 10:a2d643b3c782 | 44 | void SnakeEngine::snake_food_collision(Gamepad &pad, N5110 &lcd) { |
JoeShotton | 10:a2d643b3c782 | 45 | if (_food._x == _body._x_head && _food._y == _body._y_head){ //if coords of food and head match |
JoeShotton | 4:ea3fa51c4386 | 46 | //printf("FOOD!"); |
JoeShotton | 10:a2d643b3c782 | 47 | pad.led(3,0.9); //turns on green LEDs |
JoeShotton | 9:0571880085cc | 48 | pad.led(6,0.9); |
JoeShotton | 10:a2d643b3c782 | 49 | while (_food.rand_pos(pad, lcd) == false){ //while new food coordinates are in the snake/walls |
JoeShotton | 10:a2d643b3c782 | 50 | _food.rand_pos(pad, lcd); //choose new coords by rerunning the function |
JoeShotton | 10:a2d643b3c782 | 51 | //printf("Reselected food position\n"); |
JoeShotton | 9:0571880085cc | 52 | } |
JoeShotton | 10:a2d643b3c782 | 53 | _body.add_length(5); //sets length increase |
JoeShotton | 10:a2d643b3c782 | 54 | score++; |
JoeShotton | 10:a2d643b3c782 | 55 | pad.led(3,0.0); //turns off green LEDs |
JoeShotton | 9:0571880085cc | 56 | pad.led(6,0.0); |
JoeShotton | 4:ea3fa51c4386 | 57 | } |
JoeShotton | 4:ea3fa51c4386 | 58 | } |
JoeShotton | 4:ea3fa51c4386 | 59 | |
JoeShotton | 8:bcc3403d7e79 | 60 | void SnakeEngine::map_run(N5110 &lcd) { |
JoeShotton | 10:a2d643b3c782 | 61 | switch(_map_select+1) { //selects chosen map |
JoeShotton | 8:bcc3403d7e79 | 62 | case 2: |
JoeShotton | 8:bcc3403d7e79 | 63 | map2_draw(lcd); |
JoeShotton | 8:bcc3403d7e79 | 64 | snake_map2_collision(); |
JoeShotton | 8:bcc3403d7e79 | 65 | break; |
JoeShotton | 8:bcc3403d7e79 | 66 | case 3: |
JoeShotton | 8:bcc3403d7e79 | 67 | map3_draw(lcd); |
JoeShotton | 8:bcc3403d7e79 | 68 | snake_map3_collision(); |
JoeShotton | 8:bcc3403d7e79 | 69 | break; |
JoeShotton | 8:bcc3403d7e79 | 70 | case 4: |
JoeShotton | 8:bcc3403d7e79 | 71 | map4_draw(lcd); |
JoeShotton | 8:bcc3403d7e79 | 72 | snake_map4_collision(); |
JoeShotton | 8:bcc3403d7e79 | 73 | break; |
JoeShotton | 7:dd84e0fab346 | 74 | } |
JoeShotton | 7:dd84e0fab346 | 75 | } |
JoeShotton | 7:dd84e0fab346 | 76 | |
JoeShotton | 10:a2d643b3c782 | 77 | void SnakeEngine::snake_map2_collision() { //ring map |
JoeShotton | 9:0571880085cc | 78 | if (_body._x_head < 2 || _body._x_head > 81 || _body._y_head < 2 || _body._y_head > 45){ //if snakehead coords exceed the ring coords |
JoeShotton | 9:0571880085cc | 79 | _death = true; |
JoeShotton | 8:bcc3403d7e79 | 80 | } |
JoeShotton | 8:bcc3403d7e79 | 81 | } |
JoeShotton | 8:bcc3403d7e79 | 82 | |
JoeShotton | 10:a2d643b3c782 | 83 | void SnakeEngine::snake_map3_collision() { //cross map |
JoeShotton | 9:0571880085cc | 84 | if ((_body._x_head == 40 || _body._x_head == 42) && (_body._y_head < 16 || _body._y_head > 30)){ |
JoeShotton | 9:0571880085cc | 85 | //if head is at the either of the N/S walls' x coords, and above (y=18) or below (y=30), trigger death |
JoeShotton | 9:0571880085cc | 86 | _death = true; |
JoeShotton | 9:0571880085cc | 87 | } else if ((_body._x_head < 16 || _body._x_head > 66) && (_body._y_head == 22 || _body._y_head == 24)){ |
JoeShotton | 9:0571880085cc | 88 | //if head is at west of x=18 or east of x=66, and at either of the W/E wall's y coords, trigger death |
JoeShotton | 9:0571880085cc | 89 | _death = true; |
JoeShotton | 8:bcc3403d7e79 | 90 | } |
JoeShotton | 8:bcc3403d7e79 | 91 | } |
JoeShotton | 8:bcc3403d7e79 | 92 | |
JoeShotton | 10:a2d643b3c782 | 93 | void SnakeEngine::snake_map4_collision() { //lanes |
JoeShotton | 10:a2d643b3c782 | 94 | _map4_location = 0; |
JoeShotton | 10:a2d643b3c782 | 95 | if (_body._x_head == 8 || _body._x_head == 10 || _body._x_head == 40 || _body._x_head == 42 || _body._x_head == 72 || _body._x_head == 74){ |
JoeShotton | 10:a2d643b3c782 | 96 | _map4_location += 1; //adjust tracker if x matches upper/lower walls |
JoeShotton | 10:a2d643b3c782 | 97 | } //printf("x matches upper/lower walls"); |
JoeShotton | 10:a2d643b3c782 | 98 | if (_body._y_head < 12 || _body._y_head > 34){ |
JoeShotton | 10:a2d643b3c782 | 99 | _map4_location += 3; //adjust tracker if y matches upper/lower walls |
JoeShotton | 10:a2d643b3c782 | 100 | } //printf("y matches upper/lower walls"); |
JoeShotton | 10:a2d643b3c782 | 101 | if (_body._x_head == 24 || _body._x_head == 26 || _body._x_head == 56 || _body._x_head == 58){ |
JoeShotton | 10:a2d643b3c782 | 102 | _map4_location += 2; //adjust tracker if x matches mid walls |
JoeShotton | 10:a2d643b3c782 | 103 | } //printf("x matches mid walls"); |
JoeShotton | 10:a2d643b3c782 | 104 | if (_body._y_head > 14 && _body._y_head < 32){ |
JoeShotton | 10:a2d643b3c782 | 105 | _map4_location += 2; //adjust tracker if y matches mid walls |
JoeShotton | 10:a2d643b3c782 | 106 | } //printf("y matches mid walls"); |
JoeShotton | 10:a2d643b3c782 | 107 | if (_map4_location == 4){ //tracker will only be 4 if the correct combination of arguments is triggered - ie 1+3 (upper/lower walls) or 2+2 (mid walls) |
JoeShotton | 10:a2d643b3c782 | 108 | _death = true; |
JoeShotton | 10:a2d643b3c782 | 109 | } //printf("Wall collision"); |
JoeShotton | 7:dd84e0fab346 | 110 | } |
JoeShotton | 7:dd84e0fab346 | 111 | |
JoeShotton | 10:a2d643b3c782 | 112 | void SnakeEngine::map2_draw(N5110 &lcd){ //rings |
JoeShotton | 10:a2d643b3c782 | 113 | lcd.drawRect(0, 0, 84, 48,FILL_TRANSPARENT); //outer pixels of ring |
JoeShotton | 10:a2d643b3c782 | 114 | lcd.drawRect(1, 1, 82, 46,FILL_TRANSPARENT); //inner pixels |
JoeShotton | 10:a2d643b3c782 | 115 | } |
JoeShotton | 10:a2d643b3c782 | 116 | |
JoeShotton | 10:a2d643b3c782 | 117 | void SnakeEngine::map3_draw(N5110 &lcd){ //cross |
JoeShotton | 10:a2d643b3c782 | 118 | lcd.drawRect(40, 0, 4, 16,FILL_BLACK); //N wall |
JoeShotton | 10:a2d643b3c782 | 119 | lcd.drawRect(68, 22, 16, 4,FILL_BLACK);//E wall |
JoeShotton | 10:a2d643b3c782 | 120 | lcd.drawRect(40, 32, 4, 16,FILL_BLACK);//S wall |
JoeShotton | 10:a2d643b3c782 | 121 | lcd.drawRect(0, 22, 16, 4,FILL_BLACK); //W wall |
JoeShotton | 7:dd84e0fab346 | 122 | } |
JoeShotton | 7:dd84e0fab346 | 123 | |
JoeShotton | 10:a2d643b3c782 | 124 | void SnakeEngine::map4_draw(N5110 &lcd){ //lanes |
JoeShotton | 10:a2d643b3c782 | 125 | lcd.drawRect(8, 0, 4, 12,FILL_BLACK);//NW line |
JoeShotton | 10:a2d643b3c782 | 126 | lcd.drawRect(8, 36, 4, 12,FILL_BLACK);//SW line |
JoeShotton | 10:a2d643b3c782 | 127 | lcd.drawRect(40, 0, 4, 12,FILL_BLACK);//N-mid line |
JoeShotton | 10:a2d643b3c782 | 128 | lcd.drawRect(40, 36, 4, 12,FILL_BLACK);//S-mid line |
JoeShotton | 10:a2d643b3c782 | 129 | lcd.drawRect(72, 0, 4, 12,FILL_BLACK);//NE line |
JoeShotton | 10:a2d643b3c782 | 130 | lcd.drawRect(72, 36, 4, 12,FILL_BLACK);//SE line |
JoeShotton | 10:a2d643b3c782 | 131 | lcd.drawRect(24, 16, 4, 16,FILL_BLACK);//mid-W line |
JoeShotton | 10:a2d643b3c782 | 132 | lcd.drawRect(56, 16, 4, 16,FILL_BLACK);//mid-E line |
JoeShotton | 7:dd84e0fab346 | 133 | } |
JoeShotton | 7:dd84e0fab346 | 134 | |
JoeShotton | 6:6c9453397f4a | 135 | void SnakeEngine::transition_black(N5110 &lcd) { |
JoeShotton | 7:dd84e0fab346 | 136 | for (int j = 0; j < 21; j += 4) { //j iterator controls the size and location of current loop |
JoeShotton | 7:dd84e0fab346 | 137 | //printf("j: %d\n", j); |
JoeShotton | 7:dd84e0fab346 | 138 | for (int i = 1; i < 84 - (2*j); i += 2) { //i iterator controls length of rectangles |
JoeShotton | 10:a2d643b3c782 | 139 | lcd.drawRect(j, j, 1 + i, 4, FILL_BLACK); //top horizontal rectangle grows to the right by adding i iterator to width |
JoeShotton | 10:a2d643b3c782 | 140 | lcd.drawRect(83 - j - i, 44 - j, 1 + i, 4, FILL_BLACK); //bottom horizontal rectangle grows to the left by subtracting i iterator from x position, |
JoeShotton | 10:a2d643b3c782 | 141 | //and adding i iterator to width |
JoeShotton | 6:6c9453397f4a | 142 | wait_ms(5); |
JoeShotton | 10:a2d643b3c782 | 143 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops' rectangles |
JoeShotton | 6:6c9453397f4a | 144 | } |
JoeShotton | 7:dd84e0fab346 | 145 | for (int i = 1; i < 43 - (2*j); i += 2) { //i iterator controls length of rectangles |
JoeShotton | 10:a2d643b3c782 | 146 | lcd.drawRect(80 - j, 4 + j, 4, i,FILL_BLACK); //right vertical rectangle grows down by adding i iterator to height |
JoeShotton | 10:a2d643b3c782 | 147 | lcd.drawRect(j, 44 - j - i, 4, i,FILL_BLACK); //left vertical rectangle grows up by subtracting i iterator from y position, adds i to height |
JoeShotton | 6:6c9453397f4a | 148 | wait_ms(5); |
JoeShotton | 7:dd84e0fab346 | 149 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops |
JoeShotton | 6:6c9453397f4a | 150 | } |
JoeShotton | 6:6c9453397f4a | 151 | } |
JoeShotton | 6:6c9453397f4a | 152 | } |
JoeShotton | 6:6c9453397f4a | 153 | |
JoeShotton | 6:6c9453397f4a | 154 | void SnakeEngine::transition_white(N5110 &lcd) { |
JoeShotton | 10:a2d643b3c782 | 155 | //functionally same as black transition function |
JoeShotton | 10:a2d643b3c782 | 156 | for (int j = 0; j < 21; j += 4) { //j iterator controls the size and location of current loop |
JoeShotton | 10:a2d643b3c782 | 157 | //printf("j: %d\n", j); |
JoeShotton | 10:a2d643b3c782 | 158 | for (int i = 1; i < 84 - (2*j); i += 2) { //i iterator controls length of rectangles |
JoeShotton | 10:a2d643b3c782 | 159 | lcd.drawRect(j, j, 1 + i, 4, FILL_WHITE); //top horizontal rectangle grows to the right by adding i iterator to width |
JoeShotton | 10:a2d643b3c782 | 160 | lcd.drawRect(83 - j - i, 44 - j, 1 + i, 4, FILL_WHITE); //bottom horizontal rectangle grows to the left by subtracting i iterator from x position, |
JoeShotton | 10:a2d643b3c782 | 161 | //and adding i iterator to width |
JoeShotton | 6:6c9453397f4a | 162 | wait_ms(5); |
JoeShotton | 10:a2d643b3c782 | 163 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops' rectangles |
JoeShotton | 6:6c9453397f4a | 164 | } |
JoeShotton | 10:a2d643b3c782 | 165 | for (int i = 1; i < 43 - (2*j); i += 2) { //i iterator controls length of rectangles |
JoeShotton | 10:a2d643b3c782 | 166 | lcd.drawRect(80 - j, 4 + j, 4, i,FILL_WHITE); //right vertical rectangle grows down by adding i iterator to height |
JoeShotton | 10:a2d643b3c782 | 167 | lcd.drawRect(j, 44 - j - i, 4, i,FILL_WHITE); //left vertical rectangle grows up by subtracting i iterator from y position, adds i to height |
JoeShotton | 6:6c9453397f4a | 168 | wait_ms(5); |
JoeShotton | 10:a2d643b3c782 | 169 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops |
JoeShotton | 6:6c9453397f4a | 170 | } |
JoeShotton | 6:6c9453397f4a | 171 | } |
JoeShotton | 6:6c9453397f4a | 172 | } |
JoeShotton | 7:dd84e0fab346 | 173 | |
JoeShotton | 9:0571880085cc | 174 | void SnakeEngine::menu1_init(Gamepad &pad, N5110 &lcd){ |
JoeShotton | 10:a2d643b3c782 | 175 | contrast(pad, lcd); //adjusts contrast before transitions |
JoeShotton | 10:a2d643b3c782 | 176 | transition_black(lcd); |
JoeShotton | 7:dd84e0fab346 | 177 | transition_white(lcd); |
JoeShotton | 7:dd84e0fab346 | 178 | lcd.refresh(); |
JoeShotton | 7:dd84e0fab346 | 179 | //printf("Menu 2\n"); |
JoeShotton | 10:a2d643b3c782 | 180 | lcd.printString("SNAKE",27,0); //displays relevant text |
JoeShotton | 7:dd84e0fab346 | 181 | lcd.printString("Play",30,2); |
JoeShotton | 10:a2d643b3c782 | 182 | lcd.drawCircle(24,19,3,FILL_TRANSPARENT); //draws empty circles for option display |
JoeShotton | 7:dd84e0fab346 | 183 | lcd.printString("Maps",30,3); |
JoeShotton | 7:dd84e0fab346 | 184 | lcd.drawCircle(24,27,3,FILL_TRANSPARENT); |
JoeShotton | 7:dd84e0fab346 | 185 | lcd.refresh(); |
JoeShotton | 10:a2d643b3c782 | 186 | menu_flash(pad, 2); //flashes orange LEDs |
JoeShotton | 7:dd84e0fab346 | 187 | } |
JoeShotton | 7:dd84e0fab346 | 188 | |
JoeShotton | 10:a2d643b3c782 | 189 | void SnakeEngine::menu1_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ |
JoeShotton | 7:dd84e0fab346 | 190 | //printf("Menu 1\n"); |
JoeShotton | 10:a2d643b3c782 | 191 | if (pad.X_held() == true){ //detect if 'up' selection |
JoeShotton | 10:a2d643b3c782 | 192 | _menu_select--; |
JoeShotton | 10:a2d643b3c782 | 193 | } else if (pad.B_held() == true){ //detect if 'down' selection |
JoeShotton | 7:dd84e0fab346 | 194 | _menu_select++; |
JoeShotton | 7:dd84e0fab346 | 195 | } |
JoeShotton | 10:a2d643b3c782 | 196 | _menu_select = ((_menu_select % 2) + 2) % 2; //wrap around numbers, ie down on 1 goes to 0 and up on 0 goes to 1 |
JoeShotton | 9:0571880085cc | 197 | select_circles(lcd, _menu_select + 1); //draw black circle in selected option |
JoeShotton | 9:0571880085cc | 198 | //printf("Option: %d\n", _menu_select + 1); |
JoeShotton | 9:0571880085cc | 199 | |
JoeShotton | 10:a2d643b3c782 | 200 | if (pad.A_held() == true){ //if option 1 selected and 'A' held |
JoeShotton | 9:0571880085cc | 201 | if (_menu_select == 0){ |
JoeShotton | 9:0571880085cc | 202 | game_init(pad, lcd, mag); //Initialise game |
JoeShotton | 10:a2d643b3c782 | 203 | } else { //otherwise must be option 2 |
JoeShotton | 9:0571880085cc | 204 | menu2_init(pad, lcd); //Initialise menu 2 |
JoeShotton | 10:a2d643b3c782 | 205 | menu_flash(pad, 2); //flashes orange LEDs |
JoeShotton | 9:0571880085cc | 206 | } |
JoeShotton | 7:dd84e0fab346 | 207 | } |
JoeShotton | 7:dd84e0fab346 | 208 | } |
JoeShotton | 7:dd84e0fab346 | 209 | |
JoeShotton | 9:0571880085cc | 210 | void SnakeEngine::menu2_init(Gamepad &pad, N5110 &lcd){ |
JoeShotton | 10:a2d643b3c782 | 211 | //printf("Menu 2\n"); |
JoeShotton | 7:dd84e0fab346 | 212 | lcd.clear(); |
JoeShotton | 10:a2d643b3c782 | 213 | lcd.printString(" MAPS",0,0); //displays relevant text |
JoeShotton | 7:dd84e0fab346 | 214 | lcd.printString("Empty",30,2); |
JoeShotton | 10:a2d643b3c782 | 215 | lcd.drawCircle(24,19,3,FILL_TRANSPARENT); //draws empty circles for option display |
JoeShotton | 7:dd84e0fab346 | 216 | lcd.printString("Ring",30,3); |
JoeShotton | 7:dd84e0fab346 | 217 | lcd.drawCircle(24,27,3,FILL_TRANSPARENT); |
JoeShotton | 7:dd84e0fab346 | 218 | lcd.printString("Cross",30,4); |
JoeShotton | 7:dd84e0fab346 | 219 | lcd.drawCircle(24,35,3,FILL_TRANSPARENT); |
JoeShotton | 10:a2d643b3c782 | 220 | lcd.printString("Lanes",30,5); |
JoeShotton | 7:dd84e0fab346 | 221 | lcd.drawCircle(24,43,3,FILL_TRANSPARENT); |
JoeShotton | 7:dd84e0fab346 | 222 | lcd.refresh(); |
JoeShotton | 10:a2d643b3c782 | 223 | game_state = 2; //sets game_state to menu 2, which changes the active while loop in main.cpp |
JoeShotton | 7:dd84e0fab346 | 224 | } |
JoeShotton | 7:dd84e0fab346 | 225 | |
JoeShotton | 10:a2d643b3c782 | 226 | void SnakeEngine::menu2_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ |
JoeShotton | 10:a2d643b3c782 | 227 | |
JoeShotton | 10:a2d643b3c782 | 228 | select_circles(lcd, _map_select+1); |
JoeShotton | 7:dd84e0fab346 | 229 | |
JoeShotton | 7:dd84e0fab346 | 230 | //printf("Menu 2\n"); |
JoeShotton | 10:a2d643b3c782 | 231 | if (pad.X_held() == true){ |
JoeShotton | 9:0571880085cc | 232 | _map_select--; |
JoeShotton | 10:a2d643b3c782 | 233 | } else if (pad.B_held() == true){ |
JoeShotton | 10:a2d643b3c782 | 234 | _map_select++; |
JoeShotton | 10:a2d643b3c782 | 235 | } else if (pad.Y_held() == true){ |
JoeShotton | 10:a2d643b3c782 | 236 | preview(pad, lcd); //allows short preview of map to be displayed |
JoeShotton | 10:a2d643b3c782 | 237 | } else if (pad.A_held() == true){ |
JoeShotton | 10:a2d643b3c782 | 238 | game_init(pad, lcd, mag); //intialises game |
JoeShotton | 7:dd84e0fab346 | 239 | } |
JoeShotton | 9:0571880085cc | 240 | //printf("Map: %d\n", _map_select); |
JoeShotton | 10:a2d643b3c782 | 241 | _map_select = ((_map_select % 4) + 4) % 4; //wrap around numbers, ie down on 3 goes to 0 and up on 0 goes to 3 |
JoeShotton | 7:dd84e0fab346 | 242 | } |
JoeShotton | 7:dd84e0fab346 | 243 | |
JoeShotton | 7:dd84e0fab346 | 244 | |
JoeShotton | 9:0571880085cc | 245 | void SnakeEngine::death_init(Gamepad &pad, N5110 &lcd){ |
JoeShotton | 10:a2d643b3c782 | 246 | _death = 0; //resets death flag |
JoeShotton | 10:a2d643b3c782 | 247 | _menu_select = 0; //resets menu select so 'play again' is default |
JoeShotton | 7:dd84e0fab346 | 248 | transition_black(lcd); |
JoeShotton | 7:dd84e0fab346 | 249 | transition_white(lcd); |
JoeShotton | 7:dd84e0fab346 | 250 | //printf("Game over\n"); |
JoeShotton | 10:a2d643b3c782 | 251 | lcd.printString(" GAME OVER",3,0); //displays relevant text |
JoeShotton | 9:0571880085cc | 252 | lcd.printString(" Score:",0,1); |
JoeShotton | 10:a2d643b3c782 | 253 | char buffer[14]; //creates a buffer for the score |
JoeShotton | 9:0571880085cc | 254 | sprintf(buffer," %2d",score); |
JoeShotton | 7:dd84e0fab346 | 255 | lcd.printString(buffer,0,2); |
JoeShotton | 9:0571880085cc | 256 | lcd.printString("Again!",30,4); |
JoeShotton | 10:a2d643b3c782 | 257 | lcd.drawCircle(24,35,3,FILL_TRANSPARENT); //draws empty circles for option selection |
JoeShotton | 9:0571880085cc | 258 | lcd.printString("Maps",30,5); |
JoeShotton | 9:0571880085cc | 259 | lcd.drawCircle(24,43,3,FILL_TRANSPARENT); |
JoeShotton | 9:0571880085cc | 260 | lcd.refresh(); |
JoeShotton | 10:a2d643b3c782 | 261 | menu_flash(pad, 1); //flashes red LEDs |
JoeShotton | 7:dd84e0fab346 | 262 | } |
JoeShotton | 7:dd84e0fab346 | 263 | |
JoeShotton | 10:a2d643b3c782 | 264 | void SnakeEngine::death_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ |
JoeShotton | 9:0571880085cc | 265 | //printf("Menu 1\n"); |
JoeShotton | 10:a2d643b3c782 | 266 | if (pad.X_held() == true){ //detect if 'up' selection |
JoeShotton | 9:0571880085cc | 267 | _menu_select--; |
JoeShotton | 10:a2d643b3c782 | 268 | } else if (pad.B_held() == true){ //detect if 'down' selection |
JoeShotton | 9:0571880085cc | 269 | _menu_select++; |
JoeShotton | 9:0571880085cc | 270 | } |
JoeShotton | 9:0571880085cc | 271 | _menu_select = ((_menu_select % 2) + 2) % 2; //wrap around numbers, ie down on 2 goes to 1 and up on 1 goes to 2 |
JoeShotton | 9:0571880085cc | 272 | select_circles(lcd, _menu_select + 3); //draw black circle in selected option |
JoeShotton | 9:0571880085cc | 273 | //printf("Option: %d\n", _menu_select + 1); |
JoeShotton | 7:dd84e0fab346 | 274 | |
JoeShotton | 10:a2d643b3c782 | 275 | if (pad.A_held() == true){ //if option 1 selected and 'A' held |
JoeShotton | 9:0571880085cc | 276 | if (_menu_select == 0){ |
JoeShotton | 10:a2d643b3c782 | 277 | game_init(pad, lcd, mag); |
JoeShotton | 10:a2d643b3c782 | 278 | game_state = 3; |
JoeShotton | 9:0571880085cc | 279 | } else { |
JoeShotton | 10:a2d643b3c782 | 280 | menu2_init(pad, lcd); |
JoeShotton | 10:a2d643b3c782 | 281 | game_state = 2; |
JoeShotton | 9:0571880085cc | 282 | } |
JoeShotton | 9:0571880085cc | 283 | } |
JoeShotton | 9:0571880085cc | 284 | } |
JoeShotton | 7:dd84e0fab346 | 285 | |
JoeShotton | 7:dd84e0fab346 | 286 | void SnakeEngine::select_circles(N5110 &lcd, int line) { |
JoeShotton | 10:a2d643b3c782 | 287 | for(int i = 19; i < 52; i += 8) { //iterates over all the circle options' y values |
JoeShotton | 10:a2d643b3c782 | 288 | lcd.drawCircle(24,i,2,FILL_WHITE); //remove previous options' black circles |
JoeShotton | 7:dd84e0fab346 | 289 | } |
JoeShotton | 10:a2d643b3c782 | 290 | lcd.drawCircle(24, 11 + line * 8, 2, FILL_BLACK); //draws black circle in relevant option's blank circle |
JoeShotton | 7:dd84e0fab346 | 291 | lcd.refresh(); |
JoeShotton | 7:dd84e0fab346 | 292 | wait_ms(200); |
JoeShotton | 7:dd84e0fab346 | 293 | } |
JoeShotton | 7:dd84e0fab346 | 294 | |
JoeShotton | 10:a2d643b3c782 | 295 | void SnakeEngine::preview(Gamepad &pad, N5110 &lcd){ |
JoeShotton | 7:dd84e0fab346 | 296 | lcd.clear(); |
JoeShotton | 10:a2d643b3c782 | 297 | switch(_map_select + 1) { //selects relevant map |
JoeShotton | 7:dd84e0fab346 | 298 | case 1: |
JoeShotton | 7:dd84e0fab346 | 299 | lcd.clear(); |
JoeShotton | 8:bcc3403d7e79 | 300 | lcd.printString("(Empty)",21,2); |
JoeShotton | 7:dd84e0fab346 | 301 | break; |
JoeShotton | 7:dd84e0fab346 | 302 | case 2: |
JoeShotton | 8:bcc3403d7e79 | 303 | map2_draw(lcd); |
JoeShotton | 8:bcc3403d7e79 | 304 | //lcd.printString("Ring",30,2); |
JoeShotton | 7:dd84e0fab346 | 305 | break; |
JoeShotton | 7:dd84e0fab346 | 306 | case 3: |
JoeShotton | 8:bcc3403d7e79 | 307 | map3_draw(lcd); |
JoeShotton | 8:bcc3403d7e79 | 308 | //lcd.printString("Cross",27,2); |
JoeShotton | 7:dd84e0fab346 | 309 | break; |
JoeShotton | 7:dd84e0fab346 | 310 | case 4: |
JoeShotton | 8:bcc3403d7e79 | 311 | map4_draw(lcd); |
JoeShotton | 8:bcc3403d7e79 | 312 | //lcd.printString("Spots",27,2); |
JoeShotton | 7:dd84e0fab346 | 313 | break; |
JoeShotton | 7:dd84e0fab346 | 314 | } |
JoeShotton | 10:a2d643b3c782 | 315 | lcd.refresh(); |
JoeShotton | 10:a2d643b3c782 | 316 | pad.led(2, 1); |
JoeShotton | 10:a2d643b3c782 | 317 | pad.led(5, 1); |
JoeShotton | 10:a2d643b3c782 | 318 | wait_ms(1000); //displays for 1 second |
JoeShotton | 10:a2d643b3c782 | 319 | pad.led(2, 0); |
JoeShotton | 10:a2d643b3c782 | 320 | pad.led(5, 0); |
JoeShotton | 7:dd84e0fab346 | 321 | lcd.clear(); |
JoeShotton | 10:a2d643b3c782 | 322 | menu2_init(pad, lcd); //re-initialises menu 2 |
JoeShotton | 8:bcc3403d7e79 | 323 | } |
JoeShotton | 8:bcc3403d7e79 | 324 | |
JoeShotton | 8:bcc3403d7e79 | 325 | void SnakeEngine::contrast(Gamepad &pad, N5110 &lcd){ |
JoeShotton | 8:bcc3403d7e79 | 326 | _pot2 = pad.read_pot2(); |
JoeShotton | 10:a2d643b3c782 | 327 | lcd.setContrast(0.25 + _pot2 * 0.5); //maps pot value to range 0.25 - 0.75 |
JoeShotton | 8:bcc3403d7e79 | 328 | //printf("Contrast: %f\n", 0.25 + _pot2 * 0.5); |
JoeShotton | 9:0571880085cc | 329 | } |
JoeShotton | 9:0571880085cc | 330 | |
JoeShotton | 9:0571880085cc | 331 | void SnakeEngine::menu_flash(Gamepad &pad, int led){ |
JoeShotton | 9:0571880085cc | 332 | for(int i = 1; i < 7; i++){ |
JoeShotton | 10:a2d643b3c782 | 333 | pad.led(led, i % 2); //flash left side LEDs - odd numbers become 1 to turn on, even numbers become 0 to turn off |
JoeShotton | 10:a2d643b3c782 | 334 | pad.led(led + 3, i % 2); //flash right side LEDs |
JoeShotton | 9:0571880085cc | 335 | wait_ms(100); |
JoeShotton | 9:0571880085cc | 336 | } |
JoeShotton | 10:a2d643b3c782 | 337 | } |
JoeShotton | 10:a2d643b3c782 | 338 | |
JoeShotton | 10:a2d643b3c782 | 339 | void SnakeEngine::game_reset(){ |
JoeShotton | 10:a2d643b3c782 | 340 | score = 0; |
JoeShotton | 10:a2d643b3c782 | 341 | _body.reset(); |
JoeShotton | 10:a2d643b3c782 | 342 | } |
JoeShotton | 10:a2d643b3c782 | 343 |