Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad N5110 Joystick
Game_engine/Game_engine.cpp@10:aedca0082855, 2019-05-09 (annotated)
- Committer:
- el16dlc
- Date:
- Thu May 09 13:52:41 2019 +0000
- Revision:
- 10:aedca0082855
- Parent:
- 9:a7ea33e6bd82
Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el16dlc | 1:b9f3b7d869d8 | 1 | #include "Game_engine.h" |
el16dlc | 2:0bd6711eae26 | 2 | #include "Snake.h" |
el16dlc | 1:b9f3b7d869d8 | 3 | |
el16dlc | 10:aedca0082855 | 4 | // constructor and destructor empty |
el16dlc | 1:b9f3b7d869d8 | 5 | GameEngine::GameEngine() { |
el16dlc | 1:b9f3b7d869d8 | 6 | } |
el16dlc | 10:aedca0082855 | 7 | GameEngine::~GameEngine() { |
el16dlc | 10:aedca0082855 | 8 | } |
el16dlc | 1:b9f3b7d869d8 | 9 | |
el16dlc | 10:aedca0082855 | 10 | // Instance |
el16dlc | 2:0bd6711eae26 | 11 | Snake snake; |
el16dlc | 1:b9f3b7d869d8 | 12 | |
el16dlc | 8:264398d3c610 | 13 | void GameEngine::direction_reset() { |
el16dlc | 10:aedca0082855 | 14 | _direction = 0; // resets direction so menu can be selected using buttons |
el16dlc | 8:264398d3c610 | 15 | } |
el16dlc | 10:aedca0082855 | 16 | |
el16dlc | 2:0bd6711eae26 | 17 | void GameEngine::init() { |
el16dlc | 10:aedca0082855 | 18 | snake.init(); // initialize snake |
el16dlc | 5:23a85b16ee54 | 19 | _game_cont = true; |
el16dlc | 10:aedca0082855 | 20 | snake.set_food_posX((rand()%(19) + 1) * 4); // randomize horizontal food position |
el16dlc | 10:aedca0082855 | 21 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); // randomize vertical food position |
el16dlc | 4:0fc3441556e1 | 22 | _body_seg = 1; |
el16dlc | 8:264398d3c610 | 23 | _direction = 0; |
el16dlc | 9:a7ea33e6bd82 | 24 | _score = 0; |
el16dlc | 2:0bd6711eae26 | 25 | } |
el16dlc | 8:264398d3c610 | 26 | |
el16dlc | 1:b9f3b7d869d8 | 27 | void GameEngine::draw(N5110 &lcd) { |
el16dlc | 1:b9f3b7d869d8 | 28 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // Draws screen limits |
el16dlc | 10:aedca0082855 | 29 | snake.draw_head(lcd); // call head drawing |
el16dlc | 10:aedca0082855 | 30 | snake.draw_food(lcd); // call food drawing |
el16dlc | 2:0bd6711eae26 | 31 | } |
el16dlc | 2:0bd6711eae26 | 32 | |
el16dlc | 2:0bd6711eae26 | 33 | void GameEngine::get_dir(Gamepad &gamepad) { |
el16dlc | 10:aedca0082855 | 34 | if ( gamepad.check_event(Gamepad::Y_PRESSED) && _direction != 1) { // if Y button pressed go up unless snake is going down |
el16dlc | 2:0bd6711eae26 | 35 | _direction = 0; |
el16dlc | 2:0bd6711eae26 | 36 | } |
el16dlc | 10:aedca0082855 | 37 | else if ( gamepad.check_event(Gamepad::A_PRESSED) && _direction != 0) { // if A button pressed go down unless snake is going up |
el16dlc | 2:0bd6711eae26 | 38 | _direction = 1; |
el16dlc | 2:0bd6711eae26 | 39 | } |
el16dlc | 10:aedca0082855 | 40 | else if ( gamepad.check_event(Gamepad::X_PRESSED) && _direction != 3) { // if X button pressed go left unless snake is going right |
el16dlc | 2:0bd6711eae26 | 41 | _direction = 2; |
el16dlc | 2:0bd6711eae26 | 42 | } |
el16dlc | 10:aedca0082855 | 43 | else if ( gamepad.check_event(Gamepad::B_PRESSED) && _direction != 2) { // if B button pressed go right unless snake is going left |
el16dlc | 2:0bd6711eae26 | 44 | _direction = 3; |
el16dlc | 2:0bd6711eae26 | 45 | } |
el16dlc | 2:0bd6711eae26 | 46 | } |
el16dlc | 2:0bd6711eae26 | 47 | |
el16dlc | 10:aedca0082855 | 48 | void GameEngine::snake_move() { // checks direction of snake and increments snake in direction of travel |
el16dlc | 2:0bd6711eae26 | 49 | if (_direction == 0) { |
el16dlc | 2:0bd6711eae26 | 50 | snake.set_snake_posY (snake.get_snake_posY()- 4); |
el16dlc | 2:0bd6711eae26 | 51 | } else if (_direction == 1) { |
el16dlc | 2:0bd6711eae26 | 52 | snake.set_snake_posY (snake.get_snake_posY()+ 4); |
el16dlc | 2:0bd6711eae26 | 53 | } else if (_direction == 2) { |
el16dlc | 2:0bd6711eae26 | 54 | snake.set_snake_posX (snake.get_snake_posX()- 4); |
el16dlc | 2:0bd6711eae26 | 55 | } else if (_direction == 3) { |
el16dlc | 2:0bd6711eae26 | 56 | snake.set_snake_posX (snake.get_snake_posX()+ 4); |
el16dlc | 6:f02ea8ec42b3 | 57 | } |
el16dlc | 3:660de4311976 | 58 | } |
el16dlc | 3:660de4311976 | 59 | |
el16dlc | 3:660de4311976 | 60 | void GameEngine::food_move() { |
el16dlc | 5:23a85b16ee54 | 61 | srand(time(NULL)); |
el16dlc | 8:264398d3c610 | 62 | _food_reset = true; |
el16dlc | 10:aedca0082855 | 63 | if(snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY()) { // if snake head and food position in same place then reset food |
el16dlc | 3:660de4311976 | 64 | snake.set_food_posX((rand()%(19) + 1) * 4); |
el16dlc | 3:660de4311976 | 65 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); |
el16dlc | 10:aedca0082855 | 66 | while(_food_reset == true) { // loop until food reset flag is not triggered |
el16dlc | 8:264398d3c610 | 67 | _food_reset = false; |
el16dlc | 8:264398d3c610 | 68 | for (int k = _body_seg; k > 0; k--) { |
el16dlc | 8:264398d3c610 | 69 | 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())) { |
el16dlc | 10:aedca0082855 | 70 | // if food position is the same as any of the snake body segments then reset food |
el16dlc | 8:264398d3c610 | 71 | snake.set_food_posX((rand()%(19) + 1) * 4); |
el16dlc | 8:264398d3c610 | 72 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); |
el16dlc | 8:264398d3c610 | 73 | _food_reset = true; |
el16dlc | 8:264398d3c610 | 74 | } |
el16dlc | 7:72119ea3b3b1 | 75 | } |
el16dlc | 7:72119ea3b3b1 | 76 | } |
el16dlc | 10:aedca0082855 | 77 | _body_seg = _body_seg + 1; // increment number of body segments |
el16dlc | 10:aedca0082855 | 78 | _score = _score + 1; // increment score |
el16dlc | 3:660de4311976 | 79 | } |
el16dlc | 3:660de4311976 | 80 | } |
el16dlc | 10:aedca0082855 | 81 | |
el16dlc | 4:0fc3441556e1 | 82 | void GameEngine::snake_body(N5110 &lcd) { |
el16dlc | 4:0fc3441556e1 | 83 | _body_posX[0] = snake.get_snake_posX(); |
el16dlc | 4:0fc3441556e1 | 84 | _body_posY[0] = snake.get_snake_posY(); |
el16dlc | 4:0fc3441556e1 | 85 | for (int j = _body_seg; j > 0; j--) { |
el16dlc | 10:aedca0082855 | 86 | lcd.drawRect(_body_posX[j],_body_posY[j],4,4,FILL_BLACK); // draw each body part |
el16dlc | 4:0fc3441556e1 | 87 | _body_posX[j] = _body_posX[j-1]; |
el16dlc | 4:0fc3441556e1 | 88 | _body_posY[j] = _body_posY[j-1]; |
el16dlc | 4:0fc3441556e1 | 89 | } |
el16dlc | 4:0fc3441556e1 | 90 | } |
el16dlc | 5:23a85b16ee54 | 91 | |
el16dlc | 5:23a85b16ee54 | 92 | void GameEngine::check_wall_collision() { |
el16dlc | 10:aedca0082855 | 93 | if (snake.get_snake_posX() < 4 || snake.get_snake_posX() > 80 || snake.get_snake_posY() < 0 || snake.get_snake_posY() > 44) { // check if snake head has hit any of the walls |
el16dlc | 5:23a85b16ee54 | 94 | _game_cont = false; |
el16dlc | 5:23a85b16ee54 | 95 | } |
el16dlc | 5:23a85b16ee54 | 96 | } |
el16dlc | 5:23a85b16ee54 | 97 | |
el16dlc | 5:23a85b16ee54 | 98 | void GameEngine::check_snake_collision() { |
el16dlc | 5:23a85b16ee54 | 99 | for (int i = _body_seg; i > 1; i--) { |
el16dlc | 10:aedca0082855 | 100 | if (_body_posX[i] == snake.get_snake_posX() && _body_posY[i] == snake.get_snake_posY()) { // if snake head matches any part of snake body collision has occurred |
el16dlc | 10:aedca0082855 | 101 | _game_cont = false; |
el16dlc | 5:23a85b16ee54 | 102 | } |
el16dlc | 5:23a85b16ee54 | 103 | } |
el16dlc | 5:23a85b16ee54 | 104 | } |
el16dlc | 5:23a85b16ee54 | 105 | |
el16dlc | 8:264398d3c610 | 106 | // accessors |
el16dlc | 10:aedca0082855 | 107 | // this method gets the boolean value for checking whether the game should be running |
el16dlc | 5:23a85b16ee54 | 108 | bool GameEngine::get_game_cont() { |
el16dlc | 5:23a85b16ee54 | 109 | return _game_cont; |
el16dlc | 5:23a85b16ee54 | 110 | } |
el16dlc | 10:aedca0082855 | 111 | |
el16dlc | 10:aedca0082855 | 112 | // this method gets the direction set by the gamepad |
el16dlc | 8:264398d3c610 | 113 | int GameEngine::get_direction() { |
el16dlc | 8:264398d3c610 | 114 | return _direction; |
el16dlc | 8:264398d3c610 | 115 | } |
el16dlc | 10:aedca0082855 | 116 | |
el16dlc | 10:aedca0082855 | 117 | // this method gets the score |
el16dlc | 9:a7ea33e6bd82 | 118 | int GameEngine::get_score() { |
el16dlc | 9:a7ea33e6bd82 | 119 | return _score; |
el16dlc | 9:a7ea33e6bd82 | 120 | } |