Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad N5110 Joystick
Diff: Game_engine/Game_engine.cpp
- Revision:
- 8:264398d3c610
- Parent:
- 7:72119ea3b3b1
- Child:
- 9:a7ea33e6bd82
--- a/Game_engine/Game_engine.cpp Thu May 09 09:09:07 2019 +0000 +++ b/Game_engine/Game_engine.cpp Thu May 09 10:55:22 2019 +0000 @@ -8,13 +8,18 @@ } Snake snake; +void GameEngine::direction_reset() { + _direction = 0; +} void GameEngine::init() { snake.init(); _game_cont = true; snake.set_food_posX((rand()%(19) + 1) * 4); snake.set_food_posY((rand()%(11) + 1) * 4 - 2); _body_seg = 1; + _direction = 0; } + void GameEngine::draw(N5110 &lcd) { lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // Draws screen limits snake.draw_head(lcd); @@ -50,13 +55,18 @@ void GameEngine::food_move() { srand(time(NULL)); + _food_reset = true; if(snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY()) { snake.set_food_posX((rand()%(19) + 1) * 4); snake.set_food_posY((rand()%(11) + 1) * 4 - 2); - for (int k = _body_seg; k > 0; k--) { - if((_body_posX[k] == snake.get_food_posX() && _body_posY[k] == snake.get_food_posY()) || (snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY())) { - snake.set_food_posX((rand()%(19) + 1) * 4); - snake.set_food_posY((rand()%(11) + 1) * 4 - 2); + while(_food_reset == true) { + _food_reset = false; + for (int k = _body_seg; k > 0; k--) { + if((_body_posX[k] == snake.get_food_posX() && _body_posY[k] == snake.get_food_posY()) || (snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY())) { + snake.set_food_posX((rand()%(19) + 1) * 4); + snake.set_food_posY((rand()%(11) + 1) * 4 - 2); + _food_reset = true; + } } } _body_seg = _body_seg + 1; @@ -87,7 +97,10 @@ } -// accessor +// accessors bool GameEngine::get_game_cont() { return _game_cont; } +int GameEngine::get_direction() { + return _direction; +}