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@14:2dfe04ced21c, 2020-05-27 (annotated)
- Committer:
- JoeShotton
- Date:
- Wed May 27 03:18:16 2020 +0000
- Revision:
- 14:2dfe04ced21c
- Parent:
- 13:7b7ec5db56b2
Final Version
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 | 14:2dfe04ced21c | 40 | } |
| JoeShotton | 7:dd84e0fab346 | 41 | } |
| JoeShotton | 7:dd84e0fab346 | 42 | |
| JoeShotton | 10:a2d643b3c782 | 43 | void SnakeEngine::snake_food_collision(Gamepad &pad, N5110 &lcd) { |
| JoeShotton | 13:7b7ec5db56b2 | 44 | if (_food.x == _body.head_x && _food.y == _body.head_y){ //if coords of food and head match |
| JoeShotton | 4:ea3fa51c4386 | 45 | //printf("FOOD!"); |
| JoeShotton | 10:a2d643b3c782 | 46 | pad.led(3,0.9); //turns on green LEDs |
| JoeShotton | 9:0571880085cc | 47 | pad.led(6,0.9); |
| JoeShotton | 10:a2d643b3c782 | 48 | while (_food.rand_pos(pad, lcd) == false){ //while new food coordinates are in the snake/walls |
| JoeShotton | 10:a2d643b3c782 | 49 | _food.rand_pos(pad, lcd); //choose new coords by rerunning the function |
| JoeShotton | 10:a2d643b3c782 | 50 | //printf("Reselected food position\n"); |
| JoeShotton | 9:0571880085cc | 51 | } |
| JoeShotton | 10:a2d643b3c782 | 52 | _body.add_length(5); //sets length increase |
| JoeShotton | 10:a2d643b3c782 | 53 | score++; |
| JoeShotton | 10:a2d643b3c782 | 54 | pad.led(3,0.0); //turns off green LEDs |
| JoeShotton | 9:0571880085cc | 55 | pad.led(6,0.0); |
| JoeShotton | 4:ea3fa51c4386 | 56 | } |
| JoeShotton | 4:ea3fa51c4386 | 57 | } |
| JoeShotton | 4:ea3fa51c4386 | 58 | |
| JoeShotton | 8:bcc3403d7e79 | 59 | void SnakeEngine::map_run(N5110 &lcd) { |
| JoeShotton | 14:2dfe04ced21c | 60 | switch(_map_select + 1) { //selects chosen map |
| JoeShotton | 8:bcc3403d7e79 | 61 | case 2: |
| JoeShotton | 8:bcc3403d7e79 | 62 | map2_draw(lcd); |
| JoeShotton | 8:bcc3403d7e79 | 63 | snake_map2_collision(); |
| JoeShotton | 8:bcc3403d7e79 | 64 | break; |
| JoeShotton | 8:bcc3403d7e79 | 65 | case 3: |
| JoeShotton | 8:bcc3403d7e79 | 66 | map3_draw(lcd); |
| JoeShotton | 8:bcc3403d7e79 | 67 | snake_map3_collision(); |
| JoeShotton | 8:bcc3403d7e79 | 68 | break; |
| JoeShotton | 8:bcc3403d7e79 | 69 | case 4: |
| JoeShotton | 8:bcc3403d7e79 | 70 | map4_draw(lcd); |
| JoeShotton | 8:bcc3403d7e79 | 71 | snake_map4_collision(); |
| JoeShotton | 8:bcc3403d7e79 | 72 | break; |
| JoeShotton | 7:dd84e0fab346 | 73 | } |
| JoeShotton | 7:dd84e0fab346 | 74 | } |
| JoeShotton | 7:dd84e0fab346 | 75 | |
| JoeShotton | 10:a2d643b3c782 | 76 | void SnakeEngine::snake_map2_collision() { //ring map |
| JoeShotton | 13:7b7ec5db56b2 | 77 | if (_body.head_x < 2 || _body.head_x > 81 || _body.head_y < 2 || _body.head_y > 45){ //if snakehead coords exceed the ring coords |
| JoeShotton | 9:0571880085cc | 78 | _death = true; |
| JoeShotton | 8:bcc3403d7e79 | 79 | } |
| JoeShotton | 8:bcc3403d7e79 | 80 | } |
| JoeShotton | 8:bcc3403d7e79 | 81 | |
| JoeShotton | 10:a2d643b3c782 | 82 | void SnakeEngine::snake_map3_collision() { //cross map |
| JoeShotton | 13:7b7ec5db56b2 | 83 | if ((_body.head_x == 40 || _body.head_x == 42) && (_body.head_y < 16 || _body.head_y > 30)){ |
| JoeShotton | 9:0571880085cc | 84 | //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 | 85 | _death = true; |
| JoeShotton | 13:7b7ec5db56b2 | 86 | } else if ((_body.head_x < 16 || _body.head_x > 66) && (_body.head_y == 22 || _body.head_y == 24)){ |
| JoeShotton | 9:0571880085cc | 87 | //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 | 88 | _death = true; |
| JoeShotton | 8:bcc3403d7e79 | 89 | } |
| JoeShotton | 8:bcc3403d7e79 | 90 | } |
| JoeShotton | 8:bcc3403d7e79 | 91 | |
| JoeShotton | 10:a2d643b3c782 | 92 | void SnakeEngine::snake_map4_collision() { //lanes |
| JoeShotton | 10:a2d643b3c782 | 93 | _map4_location = 0; |
| JoeShotton | 13:7b7ec5db56b2 | 94 | if (_body.head_x == 8 || _body.head_x == 10 || _body.head_x == 40 || _body.head_x == 42 || _body.head_x == 72 || _body.head_x == 74){ |
| JoeShotton | 10:a2d643b3c782 | 95 | _map4_location += 1; //adjust tracker if x matches upper/lower walls |
| JoeShotton | 10:a2d643b3c782 | 96 | } //printf("x matches upper/lower walls"); |
| JoeShotton | 13:7b7ec5db56b2 | 97 | if (_body.head_y < 12 || _body.head_y > 34){ |
| JoeShotton | 10:a2d643b3c782 | 98 | _map4_location += 3; //adjust tracker if y matches upper/lower walls |
| JoeShotton | 10:a2d643b3c782 | 99 | } //printf("y matches upper/lower walls"); |
| JoeShotton | 13:7b7ec5db56b2 | 100 | if (_body.head_x == 24 || _body.head_x == 26 || _body.head_x == 56 || _body.head_x == 58){ |
| JoeShotton | 10:a2d643b3c782 | 101 | _map4_location += 2; //adjust tracker if x matches mid walls |
| JoeShotton | 10:a2d643b3c782 | 102 | } //printf("x matches mid walls"); |
| JoeShotton | 13:7b7ec5db56b2 | 103 | if (_body.head_y > 14 && _body.head_y < 32){ |
| JoeShotton | 10:a2d643b3c782 | 104 | _map4_location += 2; //adjust tracker if y matches mid walls |
| JoeShotton | 10:a2d643b3c782 | 105 | } //printf("y matches mid walls"); |
| JoeShotton | 10:a2d643b3c782 | 106 | 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 | 107 | _death = true; |
| JoeShotton | 10:a2d643b3c782 | 108 | } //printf("Wall collision"); |
| JoeShotton | 7:dd84e0fab346 | 109 | } |
| JoeShotton | 7:dd84e0fab346 | 110 | |
| JoeShotton | 10:a2d643b3c782 | 111 | void SnakeEngine::map2_draw(N5110 &lcd){ //rings |
| JoeShotton | 10:a2d643b3c782 | 112 | lcd.drawRect(0, 0, 84, 48,FILL_TRANSPARENT); //outer pixels of ring |
| JoeShotton | 10:a2d643b3c782 | 113 | lcd.drawRect(1, 1, 82, 46,FILL_TRANSPARENT); //inner pixels |
| JoeShotton | 10:a2d643b3c782 | 114 | } |
| JoeShotton | 10:a2d643b3c782 | 115 | |
| JoeShotton | 10:a2d643b3c782 | 116 | void SnakeEngine::map3_draw(N5110 &lcd){ //cross |
| JoeShotton | 10:a2d643b3c782 | 117 | lcd.drawRect(40, 0, 4, 16,FILL_BLACK); //N wall |
| JoeShotton | 10:a2d643b3c782 | 118 | lcd.drawRect(68, 22, 16, 4,FILL_BLACK);//E wall |
| JoeShotton | 10:a2d643b3c782 | 119 | lcd.drawRect(40, 32, 4, 16,FILL_BLACK);//S wall |
| JoeShotton | 10:a2d643b3c782 | 120 | lcd.drawRect(0, 22, 16, 4,FILL_BLACK); //W wall |
| JoeShotton | 7:dd84e0fab346 | 121 | } |
| JoeShotton | 7:dd84e0fab346 | 122 | |
| JoeShotton | 10:a2d643b3c782 | 123 | void SnakeEngine::map4_draw(N5110 &lcd){ //lanes |
| JoeShotton | 10:a2d643b3c782 | 124 | lcd.drawRect(8, 0, 4, 12,FILL_BLACK);//NW line |
| JoeShotton | 10:a2d643b3c782 | 125 | lcd.drawRect(8, 36, 4, 12,FILL_BLACK);//SW line |
| JoeShotton | 10:a2d643b3c782 | 126 | lcd.drawRect(40, 0, 4, 12,FILL_BLACK);//N-mid line |
| JoeShotton | 10:a2d643b3c782 | 127 | lcd.drawRect(40, 36, 4, 12,FILL_BLACK);//S-mid line |
| JoeShotton | 10:a2d643b3c782 | 128 | lcd.drawRect(72, 0, 4, 12,FILL_BLACK);//NE line |
| JoeShotton | 10:a2d643b3c782 | 129 | lcd.drawRect(72, 36, 4, 12,FILL_BLACK);//SE line |
| JoeShotton | 10:a2d643b3c782 | 130 | lcd.drawRect(24, 16, 4, 16,FILL_BLACK);//mid-W line |
| JoeShotton | 10:a2d643b3c782 | 131 | lcd.drawRect(56, 16, 4, 16,FILL_BLACK);//mid-E line |
| JoeShotton | 7:dd84e0fab346 | 132 | } |
| JoeShotton | 7:dd84e0fab346 | 133 | |
| JoeShotton | 6:6c9453397f4a | 134 | void SnakeEngine::transition_black(N5110 &lcd) { |
| JoeShotton | 7:dd84e0fab346 | 135 | for (int j = 0; j < 21; j += 4) { //j iterator controls the size and location of current loop |
| JoeShotton | 7:dd84e0fab346 | 136 | //printf("j: %d\n", j); |
| JoeShotton | 7:dd84e0fab346 | 137 | for (int i = 1; i < 84 - (2*j); i += 2) { //i iterator controls length of rectangles |
| JoeShotton | 10:a2d643b3c782 | 138 | 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 | 139 | 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 | 140 | //and adding i iterator to width |
| JoeShotton | 6:6c9453397f4a | 141 | wait_ms(5); |
| JoeShotton | 10:a2d643b3c782 | 142 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops' rectangles |
| JoeShotton | 6:6c9453397f4a | 143 | } |
| JoeShotton | 7:dd84e0fab346 | 144 | for (int i = 1; i < 43 - (2*j); i += 2) { //i iterator controls length of rectangles |
| JoeShotton | 10:a2d643b3c782 | 145 | lcd.drawRect(80 - j, 4 + j, 4, i,FILL_BLACK); //right vertical rectangle grows down by adding i iterator to height |
| JoeShotton | 10:a2d643b3c782 | 146 | 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 | 147 | wait_ms(5); |
| JoeShotton | 7:dd84e0fab346 | 148 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops |
| JoeShotton | 6:6c9453397f4a | 149 | } |
| JoeShotton | 6:6c9453397f4a | 150 | } |
| JoeShotton | 6:6c9453397f4a | 151 | } |
| JoeShotton | 6:6c9453397f4a | 152 | |
| JoeShotton | 6:6c9453397f4a | 153 | void SnakeEngine::transition_white(N5110 &lcd) { |
| JoeShotton | 10:a2d643b3c782 | 154 | //functionally same as black transition function |
| JoeShotton | 10:a2d643b3c782 | 155 | for (int j = 0; j < 21; j += 4) { //j iterator controls the size and location of current loop |
| JoeShotton | 10:a2d643b3c782 | 156 | //printf("j: %d\n", j); |
| JoeShotton | 10:a2d643b3c782 | 157 | for (int i = 1; i < 84 - (2*j); i += 2) { //i iterator controls length of rectangles |
| JoeShotton | 10:a2d643b3c782 | 158 | 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 | 159 | 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 | 160 | //and adding i iterator to width |
| JoeShotton | 6:6c9453397f4a | 161 | wait_ms(5); |
| JoeShotton | 10:a2d643b3c782 | 162 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops' rectangles |
| JoeShotton | 6:6c9453397f4a | 163 | } |
| JoeShotton | 10:a2d643b3c782 | 164 | for (int i = 1; i < 43 - (2*j); i += 2) { //i iterator controls length of rectangles |
| JoeShotton | 10:a2d643b3c782 | 165 | lcd.drawRect(80 - j, 4 + j, 4, i,FILL_WHITE); //right vertical rectangle grows down by adding i iterator to height |
| JoeShotton | 10:a2d643b3c782 | 166 | 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 | 167 | wait_ms(5); |
| JoeShotton | 10:a2d643b3c782 | 168 | lcd.refresh(); //refreshes screen without clearing it to maintain previous loops |
| JoeShotton | 6:6c9453397f4a | 169 | } |
| JoeShotton | 6:6c9453397f4a | 170 | } |
| JoeShotton | 6:6c9453397f4a | 171 | } |
| JoeShotton | 7:dd84e0fab346 | 172 | |
| JoeShotton | 9:0571880085cc | 173 | void SnakeEngine::menu1_init(Gamepad &pad, N5110 &lcd){ |
| JoeShotton | 10:a2d643b3c782 | 174 | contrast(pad, lcd); //adjusts contrast before transitions |
| JoeShotton | 10:a2d643b3c782 | 175 | transition_black(lcd); |
| JoeShotton | 7:dd84e0fab346 | 176 | transition_white(lcd); |
| JoeShotton | 7:dd84e0fab346 | 177 | lcd.refresh(); |
| JoeShotton | 14:2dfe04ced21c | 178 | //printf("Menu 1\n"); |
| JoeShotton | 10:a2d643b3c782 | 179 | lcd.printString("SNAKE",27,0); //displays relevant text |
| JoeShotton | 7:dd84e0fab346 | 180 | lcd.printString("Play",30,2); |
| JoeShotton | 10:a2d643b3c782 | 181 | lcd.drawCircle(24,19,3,FILL_TRANSPARENT); //draws empty circles for option display |
| JoeShotton | 7:dd84e0fab346 | 182 | lcd.printString("Maps",30,3); |
| JoeShotton | 7:dd84e0fab346 | 183 | lcd.drawCircle(24,27,3,FILL_TRANSPARENT); |
| JoeShotton | 7:dd84e0fab346 | 184 | lcd.refresh(); |
| JoeShotton | 10:a2d643b3c782 | 185 | menu_flash(pad, 2); //flashes orange LEDs |
| JoeShotton | 7:dd84e0fab346 | 186 | } |
| JoeShotton | 7:dd84e0fab346 | 187 | |
| JoeShotton | 10:a2d643b3c782 | 188 | void SnakeEngine::menu1_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ |
| JoeShotton | 7:dd84e0fab346 | 189 | //printf("Menu 1\n"); |
| JoeShotton | 10:a2d643b3c782 | 190 | if (pad.X_held() == true){ //detect if 'up' selection |
| JoeShotton | 10:a2d643b3c782 | 191 | _menu_select--; |
| JoeShotton | 10:a2d643b3c782 | 192 | } else if (pad.B_held() == true){ //detect if 'down' selection |
| JoeShotton | 7:dd84e0fab346 | 193 | _menu_select++; |
| JoeShotton | 7:dd84e0fab346 | 194 | } |
| JoeShotton | 10:a2d643b3c782 | 195 | _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 | 196 | select_circles(lcd, _menu_select + 1); //draw black circle in selected option |
| JoeShotton | 9:0571880085cc | 197 | //printf("Option: %d\n", _menu_select + 1); |
| JoeShotton | 9:0571880085cc | 198 | |
| JoeShotton | 10:a2d643b3c782 | 199 | if (pad.A_held() == true){ //if option 1 selected and 'A' held |
| JoeShotton | 9:0571880085cc | 200 | if (_menu_select == 0){ |
| JoeShotton | 9:0571880085cc | 201 | game_init(pad, lcd, mag); //Initialise game |
| JoeShotton | 10:a2d643b3c782 | 202 | } else { //otherwise must be option 2 |
| JoeShotton | 9:0571880085cc | 203 | menu2_init(pad, lcd); //Initialise menu 2 |
| JoeShotton | 10:a2d643b3c782 | 204 | menu_flash(pad, 2); //flashes orange LEDs |
| JoeShotton | 9:0571880085cc | 205 | } |
| JoeShotton | 7:dd84e0fab346 | 206 | } |
| JoeShotton | 7:dd84e0fab346 | 207 | } |
| JoeShotton | 7:dd84e0fab346 | 208 | |
| JoeShotton | 9:0571880085cc | 209 | void SnakeEngine::menu2_init(Gamepad &pad, N5110 &lcd){ |
| JoeShotton | 10:a2d643b3c782 | 210 | //printf("Menu 2\n"); |
| JoeShotton | 7:dd84e0fab346 | 211 | lcd.clear(); |
| JoeShotton | 10:a2d643b3c782 | 212 | lcd.printString(" MAPS",0,0); //displays relevant text |
| JoeShotton | 7:dd84e0fab346 | 213 | lcd.printString("Empty",30,2); |
| JoeShotton | 10:a2d643b3c782 | 214 | lcd.drawCircle(24,19,3,FILL_TRANSPARENT); //draws empty circles for option display |
| JoeShotton | 7:dd84e0fab346 | 215 | lcd.printString("Ring",30,3); |
| JoeShotton | 7:dd84e0fab346 | 216 | lcd.drawCircle(24,27,3,FILL_TRANSPARENT); |
| JoeShotton | 7:dd84e0fab346 | 217 | lcd.printString("Cross",30,4); |
| JoeShotton | 7:dd84e0fab346 | 218 | lcd.drawCircle(24,35,3,FILL_TRANSPARENT); |
| JoeShotton | 10:a2d643b3c782 | 219 | lcd.printString("Lanes",30,5); |
| JoeShotton | 7:dd84e0fab346 | 220 | lcd.drawCircle(24,43,3,FILL_TRANSPARENT); |
| JoeShotton | 7:dd84e0fab346 | 221 | lcd.refresh(); |
| JoeShotton | 10:a2d643b3c782 | 222 | game_state = 2; //sets game_state to menu 2, which changes the active while loop in main.cpp |
| JoeShotton | 7:dd84e0fab346 | 223 | } |
| JoeShotton | 7:dd84e0fab346 | 224 | |
| JoeShotton | 10:a2d643b3c782 | 225 | void SnakeEngine::menu2_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ |
| JoeShotton | 10:a2d643b3c782 | 226 | |
| JoeShotton | 10:a2d643b3c782 | 227 | select_circles(lcd, _map_select+1); |
| JoeShotton | 7:dd84e0fab346 | 228 | |
| JoeShotton | 7:dd84e0fab346 | 229 | //printf("Menu 2\n"); |
| JoeShotton | 10:a2d643b3c782 | 230 | if (pad.X_held() == true){ |
| JoeShotton | 9:0571880085cc | 231 | _map_select--; |
| JoeShotton | 10:a2d643b3c782 | 232 | } else if (pad.B_held() == true){ |
| JoeShotton | 10:a2d643b3c782 | 233 | _map_select++; |
| JoeShotton | 10:a2d643b3c782 | 234 | } else if (pad.Y_held() == true){ |
| JoeShotton | 10:a2d643b3c782 | 235 | preview(pad, lcd); //allows short preview of map to be displayed |
| JoeShotton | 10:a2d643b3c782 | 236 | } else if (pad.A_held() == true){ |
| JoeShotton | 10:a2d643b3c782 | 237 | game_init(pad, lcd, mag); //intialises game |
| JoeShotton | 7:dd84e0fab346 | 238 | } |
| JoeShotton | 9:0571880085cc | 239 | //printf("Map: %d\n", _map_select); |
| JoeShotton | 10:a2d643b3c782 | 240 | _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 | 241 | } |
| JoeShotton | 7:dd84e0fab346 | 242 | |
| JoeShotton | 7:dd84e0fab346 | 243 | |
| JoeShotton | 9:0571880085cc | 244 | void SnakeEngine::death_init(Gamepad &pad, N5110 &lcd){ |
| JoeShotton | 10:a2d643b3c782 | 245 | _death = 0; //resets death flag |
| JoeShotton | 10:a2d643b3c782 | 246 | _menu_select = 0; //resets menu select so 'play again' is default |
| JoeShotton | 7:dd84e0fab346 | 247 | transition_black(lcd); |
| JoeShotton | 7:dd84e0fab346 | 248 | transition_white(lcd); |
| JoeShotton | 7:dd84e0fab346 | 249 | //printf("Game over\n"); |
| JoeShotton | 10:a2d643b3c782 | 250 | lcd.printString(" GAME OVER",3,0); //displays relevant text |
| JoeShotton | 9:0571880085cc | 251 | lcd.printString(" Score:",0,1); |
| JoeShotton | 10:a2d643b3c782 | 252 | char buffer[14]; //creates a buffer for the score |
| JoeShotton | 9:0571880085cc | 253 | sprintf(buffer," %2d",score); |
| JoeShotton | 7:dd84e0fab346 | 254 | lcd.printString(buffer,0,2); |
| JoeShotton | 9:0571880085cc | 255 | lcd.printString("Again!",30,4); |
| JoeShotton | 10:a2d643b3c782 | 256 | lcd.drawCircle(24,35,3,FILL_TRANSPARENT); //draws empty circles for option selection |
| JoeShotton | 9:0571880085cc | 257 | lcd.printString("Maps",30,5); |
| JoeShotton | 9:0571880085cc | 258 | lcd.drawCircle(24,43,3,FILL_TRANSPARENT); |
| JoeShotton | 9:0571880085cc | 259 | lcd.refresh(); |
| JoeShotton | 10:a2d643b3c782 | 260 | menu_flash(pad, 1); //flashes red LEDs |
| JoeShotton | 7:dd84e0fab346 | 261 | } |
| JoeShotton | 7:dd84e0fab346 | 262 | |
| JoeShotton | 10:a2d643b3c782 | 263 | void SnakeEngine::death_select(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ |
| JoeShotton | 9:0571880085cc | 264 | //printf("Menu 1\n"); |
| JoeShotton | 10:a2d643b3c782 | 265 | if (pad.X_held() == true){ //detect if 'up' selection |
| JoeShotton | 9:0571880085cc | 266 | _menu_select--; |
| JoeShotton | 10:a2d643b3c782 | 267 | } else if (pad.B_held() == true){ //detect if 'down' selection |
| JoeShotton | 9:0571880085cc | 268 | _menu_select++; |
| JoeShotton | 9:0571880085cc | 269 | } |
| JoeShotton | 9:0571880085cc | 270 | _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 | 271 | select_circles(lcd, _menu_select + 3); //draw black circle in selected option |
| JoeShotton | 9:0571880085cc | 272 | //printf("Option: %d\n", _menu_select + 1); |
| JoeShotton | 7:dd84e0fab346 | 273 | |
| JoeShotton | 10:a2d643b3c782 | 274 | if (pad.A_held() == true){ //if option 1 selected and 'A' held |
| JoeShotton | 9:0571880085cc | 275 | if (_menu_select == 0){ |
| JoeShotton | 10:a2d643b3c782 | 276 | game_init(pad, lcd, mag); |
| JoeShotton | 10:a2d643b3c782 | 277 | game_state = 3; |
| JoeShotton | 9:0571880085cc | 278 | } else { |
| JoeShotton | 14:2dfe04ced21c | 279 | menu2_init(pad, lcd); |
| JoeShotton | 14:2dfe04ced21c | 280 | menu_flash(pad, 2); |
| 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 |