Joe Shotton / Mbed 2 deprecated ELEC2645_Project_ll16j23s

Dependencies:   mbed ll16j23s_test_docs

Committer:
JoeShotton
Date:
Sun May 24 22:18:55 2020 +0000
Revision:
7:dd84e0fab346
Parent:
6:6c9453397f4a
Child:
8:bcc3403d7e79
Menu system functions implemented, although not fully working together. Map draw functions finished

Who changed what in which revision?

UserRevisionLine numberNew 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 4:ea3fa51c4386 5 score = 0;
JoeShotton 7:dd84e0fab346 6 _menu_select = 1;
JoeShotton 7:dd84e0fab346 7 _map = 1;
JoeShotton 7:dd84e0fab346 8 _game_state = 1;
JoeShotton 3:fcd6d70e9694 9 }
JoeShotton 3:fcd6d70e9694 10
JoeShotton 3:fcd6d70e9694 11 SnakeEngine::~SnakeEngine()
JoeShotton 3:fcd6d70e9694 12 {
JoeShotton 3:fcd6d70e9694 13 //destructor
JoeShotton 3:fcd6d70e9694 14 }
JoeShotton 3:fcd6d70e9694 15
JoeShotton 3:fcd6d70e9694 16 /* bool collision(int state, int x, int y,) {
JoeShotton 3:fcd6d70e9694 17 if ((lcd.getPixel(x, y) == 1 && ((state == 1) || (state == 4))) || (lcd.getPixel(x+1, y+1) == 1 && ((state == 2) || (state == 3)))) {
JoeShotton 3:fcd6d70e9694 18 // checks infront of head to see if pixel is set
JoeShotton 3:fcd6d70e9694 19 // due to the size of the head, there is an offset for the check for North and Eastward directions
JoeShotton 3:fcd6d70e9694 20 pad.led(1,0.9);
JoeShotton 3:fcd6d70e9694 21 return true;
JoeShotton 3:fcd6d70e9694 22 } else {
JoeShotton 3:fcd6d70e9694 23 pad.led(1,0.0);
JoeShotton 3:fcd6d70e9694 24 return = false;
JoeShotton 3:fcd6d70e9694 25 }
JoeShotton 3:fcd6d70e9694 26 }
JoeShotton 4:ea3fa51c4386 27 */
JoeShotton 7:dd84e0fab346 28 void SnakeEngine::game_init(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){
JoeShotton 5:06fa7674622a 29 _food.init(mag);
JoeShotton 6:6c9453397f4a 30 _body.init();
JoeShotton 7:dd84e0fab346 31 _food.rand_pos(pad);
JoeShotton 7:dd84e0fab346 32 transition_black(lcd);
JoeShotton 7:dd84e0fab346 33 transition_white(lcd);
JoeShotton 7:dd84e0fab346 34 }
JoeShotton 7:dd84e0fab346 35
JoeShotton 4:ea3fa51c4386 36
JoeShotton 7:dd84e0fab346 37 void SnakeEngine::game_run(Gamepad &pad, N5110 &lcd){
JoeShotton 7:dd84e0fab346 38 //input???
JoeShotton 7:dd84e0fab346 39 move_body(pad, lcd);
JoeShotton 7:dd84e0fab346 40 map();
JoeShotton 7:dd84e0fab346 41 //snake-snake collision
JoeShotton 7:dd84e0fab346 42 //snake-wall collision
JoeShotton 7:dd84e0fab346 43 snake_food_collision(pad, _body._length);
JoeShotton 7:dd84e0fab346 44 }
JoeShotton 7:dd84e0fab346 45
JoeShotton 7:dd84e0fab346 46
JoeShotton 7:dd84e0fab346 47 void SnakeEngine::move_body(Gamepad &pad, N5110 &lcd){
JoeShotton 4:ea3fa51c4386 48 _body.snake_movement(pad);
JoeShotton 4:ea3fa51c4386 49 _body.draw_body(lcd);
JoeShotton 4:ea3fa51c4386 50 //printf("Moving!");
JoeShotton 4:ea3fa51c4386 51 _food.do_food(lcd);
JoeShotton 4:ea3fa51c4386 52 snake_food_collision(pad, _body._length);
JoeShotton 4:ea3fa51c4386 53 if (_body._state > 0){
JoeShotton 7:dd84e0fab346 54 _body.snake_snake_collision(pad);
JoeShotton 4:ea3fa51c4386 55 }
JoeShotton 7:dd84e0fab346 56 }
JoeShotton 4:ea3fa51c4386 57
JoeShotton 4:ea3fa51c4386 58 void SnakeEngine::snake_food_collision(Gamepad &pad, int &_length) {
JoeShotton 4:ea3fa51c4386 59 if (_food._x == _body._x_head && _food._y == _body._y_head){
JoeShotton 4:ea3fa51c4386 60 //printf("FOOD!");
JoeShotton 7:dd84e0fab346 61 _food.rand_pos(pad);
JoeShotton 4:ea3fa51c4386 62 pad.led(3,0.9);
JoeShotton 4:ea3fa51c4386 63 _length += 5;
JoeShotton 4:ea3fa51c4386 64 score++;
JoeShotton 4:ea3fa51c4386 65 }
JoeShotton 4:ea3fa51c4386 66 }
JoeShotton 4:ea3fa51c4386 67
JoeShotton 7:dd84e0fab346 68 void SnakeEngine::map() {
JoeShotton 7:dd84e0fab346 69
JoeShotton 7:dd84e0fab346 70 switch(_map) {
JoeShotton 7:dd84e0fab346 71 case 2:
JoeShotton 7:dd84e0fab346 72 break;
JoeShotton 7:dd84e0fab346 73 case 3:
JoeShotton 7:dd84e0fab346 74 break;
JoeShotton 7:dd84e0fab346 75 case 4:
JoeShotton 7:dd84e0fab346 76 break;
JoeShotton 7:dd84e0fab346 77 }
JoeShotton 7:dd84e0fab346 78 }
JoeShotton 7:dd84e0fab346 79
JoeShotton 7:dd84e0fab346 80 void SnakeEngine::draw_map2(N5110 &lcd){
JoeShotton 7:dd84e0fab346 81 lcd.drawRect(0, 0, 84, 48,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 82 lcd.drawRect(1, 1, 82, 46,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 83 }
JoeShotton 7:dd84e0fab346 84
JoeShotton 7:dd84e0fab346 85 void SnakeEngine::draw_map3(N5110 &lcd){
JoeShotton 7:dd84e0fab346 86 lcd.drawRect(40, 0, 4, 16,FILL_BLACK);
JoeShotton 7:dd84e0fab346 87 lcd.drawRect(40, 32, 4, 16,FILL_BLACK);
JoeShotton 7:dd84e0fab346 88 lcd.drawRect(0, 21, 16, 4,FILL_BLACK);
JoeShotton 7:dd84e0fab346 89 lcd.drawRect(68, 21, 16, 4,FILL_BLACK);
JoeShotton 7:dd84e0fab346 90 }
JoeShotton 7:dd84e0fab346 91
JoeShotton 7:dd84e0fab346 92 void SnakeEngine::draw_map4(N5110 &lcd){
JoeShotton 7:dd84e0fab346 93 lcd.drawRect(12, 8, 4, 4,FILL_BLACK); //NW square
JoeShotton 7:dd84e0fab346 94 lcd.drawRect(12, 36, 4, 4,FILL_BLACK);//SW square
JoeShotton 7:dd84e0fab346 95 lcd.drawRect(68, 8, 4, 4,FILL_BLACK); //NE square
JoeShotton 7:dd84e0fab346 96 lcd.drawRect(68, 36, 4, 4,FILL_BLACK);//SE square
JoeShotton 7:dd84e0fab346 97
JoeShotton 7:dd84e0fab346 98 lcd.drawRect(40, 6, 4, 8,FILL_BLACK); //N spot, vertical
JoeShotton 7:dd84e0fab346 99 lcd.drawRect(38, 8, 8, 4,FILL_BLACK); //N spot, horizontal
JoeShotton 7:dd84e0fab346 100
JoeShotton 7:dd84e0fab346 101 lcd.drawRect(26, 20, 4, 8,FILL_BLACK);//E spot, vertical
JoeShotton 7:dd84e0fab346 102 lcd.drawRect(24, 22, 8, 4,FILL_BLACK);//E spot, horizontal
JoeShotton 7:dd84e0fab346 103
JoeShotton 7:dd84e0fab346 104 lcd.drawRect(40, 34, 4, 8,FILL_BLACK);//S spot, vertical
JoeShotton 7:dd84e0fab346 105 lcd.drawRect(38, 36, 8, 4,FILL_BLACK);//S spot, horizontal
JoeShotton 7:dd84e0fab346 106
JoeShotton 7:dd84e0fab346 107 lcd.drawRect(54, 20, 4, 8,FILL_BLACK);//W spot, vertical
JoeShotton 7:dd84e0fab346 108 lcd.drawRect(52, 22, 8, 4,FILL_BLACK);//W spot, horizontal
JoeShotton 7:dd84e0fab346 109 }
JoeShotton 7:dd84e0fab346 110
JoeShotton 7:dd84e0fab346 111
JoeShotton 6:6c9453397f4a 112 void SnakeEngine::transition_black(N5110 &lcd) {
JoeShotton 6:6c9453397f4a 113 //lcd.clear();
JoeShotton 7:dd84e0fab346 114 for (int j = 0; j < 21; j += 4) { //j iterator controls the size and location of current loop
JoeShotton 7:dd84e0fab346 115 //printf("j: %d\n", j);
JoeShotton 7:dd84e0fab346 116 for (int i = 1; i < 84 - (2*j); i += 2) { //i iterator controls length of rectangles
JoeShotton 7:dd84e0fab346 117 lcd.drawRect(j, j, i + 1, 4, FILL_BLACK); //top horizontal rectangle
JoeShotton 7:dd84e0fab346 118 lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_BLACK); //bottom horizontal rectangle
JoeShotton 6:6c9453397f4a 119 wait_ms(5);
JoeShotton 7:dd84e0fab346 120 lcd.refresh(); //refreshes screen without clearing it to maintain previous loops
JoeShotton 6:6c9453397f4a 121 }
JoeShotton 7:dd84e0fab346 122 for (int i = 1; i < 43 - (2*j); i += 2) { //i iterator controls length of rectangles
JoeShotton 7:dd84e0fab346 123 lcd.drawRect(80 - j, 4 + j, 4, i,FILL_BLACK); //right vertical rectangle
JoeShotton 7:dd84e0fab346 124 lcd.drawRect(j, 44 - i - j, 4, i,FILL_BLACK); //left vertical rectangle
JoeShotton 6:6c9453397f4a 125 wait_ms(5);
JoeShotton 7:dd84e0fab346 126 lcd.refresh(); //refreshes screen without clearing it to maintain previous loops
JoeShotton 6:6c9453397f4a 127 }
JoeShotton 6:6c9453397f4a 128 }
JoeShotton 7:dd84e0fab346 129 //wait_ms(500);
JoeShotton 6:6c9453397f4a 130 }
JoeShotton 6:6c9453397f4a 131
JoeShotton 6:6c9453397f4a 132 void SnakeEngine::transition_white(N5110 &lcd) {
JoeShotton 6:6c9453397f4a 133 //lcd.clear();
JoeShotton 6:6c9453397f4a 134 for (int j = 0; j < 21; j += 4) {
JoeShotton 7:dd84e0fab346 135 //printf("j: %d\n", j);
JoeShotton 6:6c9453397f4a 136 for (int i = 1; i < 84 - (2*j); i += 2) {
JoeShotton 6:6c9453397f4a 137 lcd.drawRect(j, j, i + 1, 4, FILL_WHITE);
JoeShotton 6:6c9453397f4a 138 lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_WHITE);
JoeShotton 6:6c9453397f4a 139 wait_ms(5);
JoeShotton 6:6c9453397f4a 140 lcd.refresh();
JoeShotton 6:6c9453397f4a 141 }
JoeShotton 6:6c9453397f4a 142 for (int i = 1; i < 43 - (2*j); i += 2) {
JoeShotton 6:6c9453397f4a 143 lcd.drawRect(80 - j, 4 + j, 4, i,FILL_WHITE);
JoeShotton 6:6c9453397f4a 144 lcd.drawRect(j, 44 - i - j, 4, i,FILL_WHITE);
JoeShotton 6:6c9453397f4a 145 wait_ms(5);
JoeShotton 6:6c9453397f4a 146 lcd.refresh();
JoeShotton 6:6c9453397f4a 147 }
JoeShotton 6:6c9453397f4a 148 }
JoeShotton 6:6c9453397f4a 149 //wait_ms(500);
JoeShotton 6:6c9453397f4a 150 }
JoeShotton 7:dd84e0fab346 151
JoeShotton 7:dd84e0fab346 152 void SnakeEngine::menu1_init(N5110 &lcd){
JoeShotton 7:dd84e0fab346 153 transition_black(lcd);
JoeShotton 7:dd84e0fab346 154 transition_white(lcd);
JoeShotton 7:dd84e0fab346 155 lcd.clear();
JoeShotton 7:dd84e0fab346 156 lcd.refresh();
JoeShotton 7:dd84e0fab346 157 //printf("Menu 2\n");
JoeShotton 7:dd84e0fab346 158 lcd.printString(" SNAKE",3,0);
JoeShotton 7:dd84e0fab346 159 lcd.printString("Play",30,2);
JoeShotton 7:dd84e0fab346 160 lcd.drawCircle(24,19,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 161 lcd.printString("Maps",30,3);
JoeShotton 7:dd84e0fab346 162 lcd.drawCircle(24,27,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 163 lcd.refresh();
JoeShotton 7:dd84e0fab346 164 }
JoeShotton 7:dd84e0fab346 165
JoeShotton 7:dd84e0fab346 166 void SnakeEngine::menu1_select(N5110 &lcd, Gamepad &pad){
JoeShotton 7:dd84e0fab346 167 //printf("Menu 1\n");
JoeShotton 7:dd84e0fab346 168 if (pad.X_pressed() == true){ //detect if 'up' selection
JoeShotton 7:dd84e0fab346 169 _menu_select--;
JoeShotton 7:dd84e0fab346 170 } else if (pad.B_pressed() == true){ //detect if 'down' selection
JoeShotton 7:dd84e0fab346 171 _menu_select++;
JoeShotton 7:dd84e0fab346 172 }
JoeShotton 7:dd84e0fab346 173 //printf("Option: %d\n", _menu_select);
JoeShotton 7:dd84e0fab346 174 if (pad.A_pressed() == true && _menu_select == 1){ //if option 1 selected and 'A' pressed
JoeShotton 7:dd84e0fab346 175 //game_init(); //Initialise game
JoeShotton 7:dd84e0fab346 176 _game_state = 3; //switch state
JoeShotton 7:dd84e0fab346 177 } else if (pad.A_pressed() == true && _menu_select == 2){ //if option 2 selected and 'A' pressed
JoeShotton 7:dd84e0fab346 178 menu2_init(lcd); //Initialise map menu
JoeShotton 7:dd84e0fab346 179 _game_state = 2; //switch state
JoeShotton 7:dd84e0fab346 180 }
JoeShotton 7:dd84e0fab346 181 if (_menu_select > 2) { //bottom - top wrap around
JoeShotton 7:dd84e0fab346 182 _menu_select = 1;
JoeShotton 7:dd84e0fab346 183 } else if (_menu_select < 1) { ////top - bottom wrap around
JoeShotton 7:dd84e0fab346 184 _menu_select = 2;
JoeShotton 7:dd84e0fab346 185 }
JoeShotton 7:dd84e0fab346 186 select_circles(lcd, _menu_select); //draw black circle in selected option
JoeShotton 7:dd84e0fab346 187 }
JoeShotton 7:dd84e0fab346 188
JoeShotton 7:dd84e0fab346 189 void SnakeEngine::menu2_init(N5110 &lcd){
JoeShotton 7:dd84e0fab346 190 //transition_black(lcd);
JoeShotton 7:dd84e0fab346 191 //transition_white(lcd);
JoeShotton 7:dd84e0fab346 192 //printf("Menu 1\n");
JoeShotton 7:dd84e0fab346 193 lcd.clear();
JoeShotton 7:dd84e0fab346 194 lcd.printString(" MAPS",0,0);
JoeShotton 7:dd84e0fab346 195 lcd.printString("Empty",30,2);
JoeShotton 7:dd84e0fab346 196 lcd.drawCircle(24,19,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 197 lcd.printString("Ring",30,3);
JoeShotton 7:dd84e0fab346 198 lcd.drawCircle(24,27,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 199 lcd.printString("Cross",30,4);
JoeShotton 7:dd84e0fab346 200 lcd.drawCircle(24,35,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 201 lcd.printString("Spots",30,5);
JoeShotton 7:dd84e0fab346 202 lcd.drawCircle(24,43,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 203 lcd.refresh();
JoeShotton 7:dd84e0fab346 204 }
JoeShotton 7:dd84e0fab346 205
JoeShotton 7:dd84e0fab346 206 void SnakeEngine::menu2_select(N5110 &lcd, Gamepad &pad){
JoeShotton 7:dd84e0fab346 207
JoeShotton 7:dd84e0fab346 208 //printf("Menu 2\n");
JoeShotton 7:dd84e0fab346 209 if (pad.X_pressed() == true){
JoeShotton 7:dd84e0fab346 210 _map--;
JoeShotton 7:dd84e0fab346 211 } else if (pad.B_pressed() == true){
JoeShotton 7:dd84e0fab346 212 _map++;
JoeShotton 7:dd84e0fab346 213 } else if (pad.Y_pressed() == true){
JoeShotton 7:dd84e0fab346 214 preview(lcd, _map);
JoeShotton 7:dd84e0fab346 215 } else if (pad.A_pressed() == true){
JoeShotton 7:dd84e0fab346 216 //game_init();
JoeShotton 7:dd84e0fab346 217 _game_state = 3;
JoeShotton 7:dd84e0fab346 218 }
JoeShotton 7:dd84e0fab346 219
JoeShotton 7:dd84e0fab346 220 //printf("Map: %d\n", _map);
JoeShotton 7:dd84e0fab346 221 if (_map > 4) {
JoeShotton 7:dd84e0fab346 222 _map = 1;
JoeShotton 7:dd84e0fab346 223 } else if (_map < 1) {
JoeShotton 7:dd84e0fab346 224 _map = 4;
JoeShotton 7:dd84e0fab346 225 }
JoeShotton 7:dd84e0fab346 226 select_circles(lcd, _map);
JoeShotton 7:dd84e0fab346 227 }
JoeShotton 7:dd84e0fab346 228
JoeShotton 7:dd84e0fab346 229
JoeShotton 7:dd84e0fab346 230 void SnakeEngine::death_init(N5110 &lcd){
JoeShotton 7:dd84e0fab346 231 wait_ms(500);
JoeShotton 7:dd84e0fab346 232 transition_black(lcd);
JoeShotton 7:dd84e0fab346 233 transition_white(lcd);
JoeShotton 7:dd84e0fab346 234 //printf("Game over\n");
JoeShotton 7:dd84e0fab346 235 lcd.printString(" GAME OVER ",0,0);
JoeShotton 7:dd84e0fab346 236 lcd.printString(" Score =",3,1);
JoeShotton 7:dd84e0fab346 237 char buffer[14];
JoeShotton 7:dd84e0fab346 238 if (score < 10){
JoeShotton 7:dd84e0fab346 239 sprintf(buffer," 0%2d",score);
JoeShotton 7:dd84e0fab346 240 } else {
JoeShotton 7:dd84e0fab346 241 sprintf(buffer," %2d",score);
JoeShotton 7:dd84e0fab346 242 }
JoeShotton 7:dd84e0fab346 243 lcd.printString(buffer,0,2);
JoeShotton 7:dd84e0fab346 244 lcd.printString("Again!",24,4);
JoeShotton 7:dd84e0fab346 245 lcd.drawCircle(24,35,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 246 lcd.printString("Menu",24,5);
JoeShotton 7:dd84e0fab346 247 lcd.drawCircle(24,44,3,FILL_TRANSPARENT);
JoeShotton 7:dd84e0fab346 248 lcd.refresh();
JoeShotton 7:dd84e0fab346 249 }
JoeShotton 7:dd84e0fab346 250
JoeShotton 7:dd84e0fab346 251 void SnakeEngine::death_select(N5110 &lcd, Gamepad &pad){
JoeShotton 7:dd84e0fab346 252
JoeShotton 7:dd84e0fab346 253 }
JoeShotton 7:dd84e0fab346 254
JoeShotton 7:dd84e0fab346 255 void SnakeEngine::select_circles(N5110 &lcd, int line) {
JoeShotton 7:dd84e0fab346 256 for(int i = 19; i < 52; i +=8) {
JoeShotton 7:dd84e0fab346 257 lcd.drawCircle(24,i,2,FILL_WHITE);
JoeShotton 7:dd84e0fab346 258 }
JoeShotton 7:dd84e0fab346 259 lcd.drawCircle(24,11+line*8,2,FILL_BLACK);
JoeShotton 7:dd84e0fab346 260 lcd.refresh();
JoeShotton 7:dd84e0fab346 261 wait_ms(200);
JoeShotton 7:dd84e0fab346 262 //snake_select1.render(lcd, 0, 8);
JoeShotton 7:dd84e0fab346 263 //wait_ms(200);
JoeShotton 7:dd84e0fab346 264 //snake_select2.render(lcd, 0, 8);
JoeShotton 7:dd84e0fab346 265 //wait_ms(200);
JoeShotton 7:dd84e0fab346 266 //snake_select3.render(lcd, 0, 8);
JoeShotton 7:dd84e0fab346 267 //wait_ms(200);
JoeShotton 7:dd84e0fab346 268 }
JoeShotton 7:dd84e0fab346 269
JoeShotton 7:dd84e0fab346 270 void SnakeEngine::preview(N5110 &lcd, int _map){
JoeShotton 7:dd84e0fab346 271 lcd.clear();
JoeShotton 7:dd84e0fab346 272 switch(_map) {
JoeShotton 7:dd84e0fab346 273 case 1:
JoeShotton 7:dd84e0fab346 274 lcd.clear();
JoeShotton 7:dd84e0fab346 275 lcd.printString("Empty",27,2);
JoeShotton 7:dd84e0fab346 276 break;
JoeShotton 7:dd84e0fab346 277 case 2:
JoeShotton 7:dd84e0fab346 278 draw_map2(lcd);
JoeShotton 7:dd84e0fab346 279 lcd.printString("Ring",30,2);
JoeShotton 7:dd84e0fab346 280 break;
JoeShotton 7:dd84e0fab346 281 case 3:
JoeShotton 7:dd84e0fab346 282 draw_map3(lcd);
JoeShotton 7:dd84e0fab346 283 lcd.printString("Cross",27,2);
JoeShotton 7:dd84e0fab346 284 break;
JoeShotton 7:dd84e0fab346 285 case 4:
JoeShotton 7:dd84e0fab346 286 draw_map4(lcd);
JoeShotton 7:dd84e0fab346 287 lcd.printString("Spots",27,2);
JoeShotton 7:dd84e0fab346 288 break;
JoeShotton 7:dd84e0fab346 289 }
JoeShotton 7:dd84e0fab346 290 lcd.refresh();
JoeShotton 7:dd84e0fab346 291 wait_ms(1000);
JoeShotton 7:dd84e0fab346 292 lcd.clear();
JoeShotton 7:dd84e0fab346 293 menu2_init(lcd);
JoeShotton 7:dd84e0fab346 294 }