Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad N5110 Joystick
Game_engine/Game_engine.cpp
- Committer:
- el16dlc
- Date:
- 2019-05-09
- Revision:
- 5:23a85b16ee54
- Parent:
- 4:0fc3441556e1
- Child:
- 6:f02ea8ec42b3
File content as of revision 5:23a85b16ee54:
#include "Game_engine.h" #include "Snake.h" GameEngine::GameEngine() { } GameEngine::~GameEngine() { } Snake snake; 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; } void GameEngine::draw(N5110 &lcd) { lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // Draws screen limits snake.draw_head(lcd); snake.draw_food(lcd); } void GameEngine::get_dir(Gamepad &gamepad) { if ( gamepad.check_event(Gamepad::Y_PRESSED) && _direction != 1) { _direction = 0; } if ( gamepad.check_event(Gamepad::A_PRESSED) && _direction != 0) { _direction = 1; } if ( gamepad.check_event(Gamepad::X_PRESSED) && _direction != 3) { _direction = 2; } if ( gamepad.check_event(Gamepad::B_PRESSED) && _direction != 2) { _direction = 3; } } void GameEngine::snake_move() { if (_direction == 0) { snake.set_snake_posY (snake.get_snake_posY()- 4); } else if (_direction == 1) { snake.set_snake_posY (snake.get_snake_posY()+ 4); } else if (_direction == 2) { snake.set_snake_posX (snake.get_snake_posX()- 4); } else if (_direction == 3) { snake.set_snake_posX (snake.get_snake_posX()+ 4); } } void GameEngine::food_move() { srand(time(NULL)); 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); _body_seg = _body_seg + 1; } } void GameEngine::snake_body(N5110 &lcd) { _body_posX[0] = snake.get_snake_posX(); _body_posY[0] = snake.get_snake_posY(); for (int j = _body_seg; j > 0; j--) { lcd.drawRect(_body_posX[j],_body_posY[j],4,4,FILL_BLACK); _body_posX[j] = _body_posX[j-1]; _body_posY[j] = _body_posY[j-1]; } } void GameEngine::check_wall_collision() { if (snake.get_snake_posX() == 0 || snake.get_snake_posX() > 80 || snake.get_snake_posY() < 0 || snake.get_snake_posY() > 44) { _game_cont = false; } } void GameEngine::check_snake_collision() { for (int i = _body_seg; i > 1; i--) { if (_body_posX[i] == snake.get_snake_posX() && _body_posY[i] == snake.get_snake_posY()) { _game_cont = false; } } } // accessor bool GameEngine::get_game_cont() { return _game_cont; }