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
- Committer:
- JoeShotton
- Date:
- 2020-05-24
- Revision:
- 7:dd84e0fab346
- Parent:
- 6:6c9453397f4a
- Child:
- 8:bcc3403d7e79
File content as of revision 7:dd84e0fab346:
#include "SnakeEngine.h" SnakeEngine::SnakeEngine() { //constructor score = 0; _menu_select = 1; _map = 1; _game_state = 1; } SnakeEngine::~SnakeEngine() { //destructor } /* bool collision(int state, int x, int y,) { if ((lcd.getPixel(x, y) == 1 && ((state == 1) || (state == 4))) || (lcd.getPixel(x+1, y+1) == 1 && ((state == 2) || (state == 3)))) { // checks infront of head to see if pixel is set // due to the size of the head, there is an offset for the check for North and Eastward directions pad.led(1,0.9); return true; } else { pad.led(1,0.0); return = false; } } */ void SnakeEngine::game_init(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){ _food.init(mag); _body.init(); _food.rand_pos(pad); transition_black(lcd); transition_white(lcd); } void SnakeEngine::game_run(Gamepad &pad, N5110 &lcd){ //input??? move_body(pad, lcd); map(); //snake-snake collision //snake-wall collision snake_food_collision(pad, _body._length); } void SnakeEngine::move_body(Gamepad &pad, N5110 &lcd){ _body.snake_movement(pad); _body.draw_body(lcd); //printf("Moving!"); _food.do_food(lcd); snake_food_collision(pad, _body._length); if (_body._state > 0){ _body.snake_snake_collision(pad); } } void SnakeEngine::snake_food_collision(Gamepad &pad, int &_length) { if (_food._x == _body._x_head && _food._y == _body._y_head){ //printf("FOOD!"); _food.rand_pos(pad); pad.led(3,0.9); _length += 5; score++; } } void SnakeEngine::map() { switch(_map) { case 2: break; case 3: break; case 4: break; } } void SnakeEngine::draw_map2(N5110 &lcd){ lcd.drawRect(0, 0, 84, 48,FILL_TRANSPARENT); lcd.drawRect(1, 1, 82, 46,FILL_TRANSPARENT); } void SnakeEngine::draw_map3(N5110 &lcd){ lcd.drawRect(40, 0, 4, 16,FILL_BLACK); lcd.drawRect(40, 32, 4, 16,FILL_BLACK); lcd.drawRect(0, 21, 16, 4,FILL_BLACK); lcd.drawRect(68, 21, 16, 4,FILL_BLACK); } void SnakeEngine::draw_map4(N5110 &lcd){ lcd.drawRect(12, 8, 4, 4,FILL_BLACK); //NW square lcd.drawRect(12, 36, 4, 4,FILL_BLACK);//SW square lcd.drawRect(68, 8, 4, 4,FILL_BLACK); //NE square lcd.drawRect(68, 36, 4, 4,FILL_BLACK);//SE square lcd.drawRect(40, 6, 4, 8,FILL_BLACK); //N spot, vertical lcd.drawRect(38, 8, 8, 4,FILL_BLACK); //N spot, horizontal lcd.drawRect(26, 20, 4, 8,FILL_BLACK);//E spot, vertical lcd.drawRect(24, 22, 8, 4,FILL_BLACK);//E spot, horizontal lcd.drawRect(40, 34, 4, 8,FILL_BLACK);//S spot, vertical lcd.drawRect(38, 36, 8, 4,FILL_BLACK);//S spot, horizontal lcd.drawRect(54, 20, 4, 8,FILL_BLACK);//W spot, vertical lcd.drawRect(52, 22, 8, 4,FILL_BLACK);//W spot, horizontal } void SnakeEngine::transition_black(N5110 &lcd) { //lcd.clear(); for (int j = 0; j < 21; j += 4) { //j iterator controls the size and location of current loop //printf("j: %d\n", j); for (int i = 1; i < 84 - (2*j); i += 2) { //i iterator controls length of rectangles lcd.drawRect(j, j, i + 1, 4, FILL_BLACK); //top horizontal rectangle lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_BLACK); //bottom horizontal rectangle wait_ms(5); lcd.refresh(); //refreshes screen without clearing it to maintain previous loops } for (int i = 1; i < 43 - (2*j); i += 2) { //i iterator controls length of rectangles lcd.drawRect(80 - j, 4 + j, 4, i,FILL_BLACK); //right vertical rectangle lcd.drawRect(j, 44 - i - j, 4, i,FILL_BLACK); //left vertical rectangle wait_ms(5); lcd.refresh(); //refreshes screen without clearing it to maintain previous loops } } //wait_ms(500); } void SnakeEngine::transition_white(N5110 &lcd) { //lcd.clear(); for (int j = 0; j < 21; j += 4) { //printf("j: %d\n", j); for (int i = 1; i < 84 - (2*j); i += 2) { lcd.drawRect(j, j, i + 1, 4, FILL_WHITE); lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_WHITE); wait_ms(5); lcd.refresh(); } for (int i = 1; i < 43 - (2*j); i += 2) { lcd.drawRect(80 - j, 4 + j, 4, i,FILL_WHITE); lcd.drawRect(j, 44 - i - j, 4, i,FILL_WHITE); wait_ms(5); lcd.refresh(); } } //wait_ms(500); } void SnakeEngine::menu1_init(N5110 &lcd){ transition_black(lcd); transition_white(lcd); lcd.clear(); lcd.refresh(); //printf("Menu 2\n"); lcd.printString(" SNAKE",3,0); lcd.printString("Play",30,2); lcd.drawCircle(24,19,3,FILL_TRANSPARENT); lcd.printString("Maps",30,3); lcd.drawCircle(24,27,3,FILL_TRANSPARENT); lcd.refresh(); } void SnakeEngine::menu1_select(N5110 &lcd, Gamepad &pad){ //printf("Menu 1\n"); if (pad.X_pressed() == true){ //detect if 'up' selection _menu_select--; } else if (pad.B_pressed() == true){ //detect if 'down' selection _menu_select++; } //printf("Option: %d\n", _menu_select); if (pad.A_pressed() == true && _menu_select == 1){ //if option 1 selected and 'A' pressed //game_init(); //Initialise game _game_state = 3; //switch state } else if (pad.A_pressed() == true && _menu_select == 2){ //if option 2 selected and 'A' pressed menu2_init(lcd); //Initialise map menu _game_state = 2; //switch state } if (_menu_select > 2) { //bottom - top wrap around _menu_select = 1; } else if (_menu_select < 1) { ////top - bottom wrap around _menu_select = 2; } select_circles(lcd, _menu_select); //draw black circle in selected option } void SnakeEngine::menu2_init(N5110 &lcd){ //transition_black(lcd); //transition_white(lcd); //printf("Menu 1\n"); lcd.clear(); lcd.printString(" MAPS",0,0); lcd.printString("Empty",30,2); lcd.drawCircle(24,19,3,FILL_TRANSPARENT); lcd.printString("Ring",30,3); lcd.drawCircle(24,27,3,FILL_TRANSPARENT); lcd.printString("Cross",30,4); lcd.drawCircle(24,35,3,FILL_TRANSPARENT); lcd.printString("Spots",30,5); lcd.drawCircle(24,43,3,FILL_TRANSPARENT); lcd.refresh(); } void SnakeEngine::menu2_select(N5110 &lcd, Gamepad &pad){ //printf("Menu 2\n"); if (pad.X_pressed() == true){ _map--; } else if (pad.B_pressed() == true){ _map++; } else if (pad.Y_pressed() == true){ preview(lcd, _map); } else if (pad.A_pressed() == true){ //game_init(); _game_state = 3; } //printf("Map: %d\n", _map); if (_map > 4) { _map = 1; } else if (_map < 1) { _map = 4; } select_circles(lcd, _map); } void SnakeEngine::death_init(N5110 &lcd){ wait_ms(500); transition_black(lcd); transition_white(lcd); //printf("Game over\n"); lcd.printString(" GAME OVER ",0,0); lcd.printString(" Score =",3,1); char buffer[14]; if (score < 10){ sprintf(buffer," 0%2d",score); } else { sprintf(buffer," %2d",score); } lcd.printString(buffer,0,2); lcd.printString("Again!",24,4); lcd.drawCircle(24,35,3,FILL_TRANSPARENT); lcd.printString("Menu",24,5); lcd.drawCircle(24,44,3,FILL_TRANSPARENT); lcd.refresh(); } void SnakeEngine::death_select(N5110 &lcd, Gamepad &pad){ } void SnakeEngine::select_circles(N5110 &lcd, int line) { for(int i = 19; i < 52; i +=8) { lcd.drawCircle(24,i,2,FILL_WHITE); } lcd.drawCircle(24,11+line*8,2,FILL_BLACK); lcd.refresh(); wait_ms(200); //snake_select1.render(lcd, 0, 8); //wait_ms(200); //snake_select2.render(lcd, 0, 8); //wait_ms(200); //snake_select3.render(lcd, 0, 8); //wait_ms(200); } void SnakeEngine::preview(N5110 &lcd, int _map){ lcd.clear(); switch(_map) { case 1: lcd.clear(); lcd.printString("Empty",27,2); break; case 2: draw_map2(lcd); lcd.printString("Ring",30,2); break; case 3: draw_map3(lcd); lcd.printString("Cross",27,2); break; case 4: draw_map4(lcd); lcd.printString("Spots",27,2); break; } lcd.refresh(); wait_ms(1000); lcd.clear(); menu2_init(lcd); }