Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad N5110 Joystick
Game_engine/Game_engine.cpp@7:72119ea3b3b1, 2019-05-09 (annotated)
- Committer:
- el16dlc
- Date:
- Thu May 09 09:09:07 2019 +0000
- Revision:
- 7:72119ea3b3b1
- Parent:
- 6:f02ea8ec42b3
- Child:
- 8:264398d3c610
8
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 | 1:b9f3b7d869d8 | 4 | GameEngine::GameEngine() { |
el16dlc | 1:b9f3b7d869d8 | 5 | } |
el16dlc | 1:b9f3b7d869d8 | 6 | |
el16dlc | 1:b9f3b7d869d8 | 7 | GameEngine::~GameEngine() { |
el16dlc | 1:b9f3b7d869d8 | 8 | } |
el16dlc | 2:0bd6711eae26 | 9 | Snake snake; |
el16dlc | 1:b9f3b7d869d8 | 10 | |
el16dlc | 2:0bd6711eae26 | 11 | void GameEngine::init() { |
el16dlc | 2:0bd6711eae26 | 12 | snake.init(); |
el16dlc | 5:23a85b16ee54 | 13 | _game_cont = true; |
el16dlc | 3:660de4311976 | 14 | snake.set_food_posX((rand()%(19) + 1) * 4); |
el16dlc | 3:660de4311976 | 15 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); |
el16dlc | 4:0fc3441556e1 | 16 | _body_seg = 1; |
el16dlc | 2:0bd6711eae26 | 17 | } |
el16dlc | 1:b9f3b7d869d8 | 18 | void GameEngine::draw(N5110 &lcd) { |
el16dlc | 1:b9f3b7d869d8 | 19 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // Draws screen limits |
el16dlc | 2:0bd6711eae26 | 20 | snake.draw_head(lcd); |
el16dlc | 3:660de4311976 | 21 | snake.draw_food(lcd); |
el16dlc | 2:0bd6711eae26 | 22 | } |
el16dlc | 2:0bd6711eae26 | 23 | |
el16dlc | 2:0bd6711eae26 | 24 | void GameEngine::get_dir(Gamepad &gamepad) { |
el16dlc | 3:660de4311976 | 25 | if ( gamepad.check_event(Gamepad::Y_PRESSED) && _direction != 1) { |
el16dlc | 2:0bd6711eae26 | 26 | _direction = 0; |
el16dlc | 2:0bd6711eae26 | 27 | } |
el16dlc | 6:f02ea8ec42b3 | 28 | else if ( gamepad.check_event(Gamepad::A_PRESSED) && _direction != 0) { |
el16dlc | 2:0bd6711eae26 | 29 | _direction = 1; |
el16dlc | 2:0bd6711eae26 | 30 | } |
el16dlc | 6:f02ea8ec42b3 | 31 | else if ( gamepad.check_event(Gamepad::X_PRESSED) && _direction != 3) { |
el16dlc | 2:0bd6711eae26 | 32 | _direction = 2; |
el16dlc | 2:0bd6711eae26 | 33 | } |
el16dlc | 6:f02ea8ec42b3 | 34 | else if ( gamepad.check_event(Gamepad::B_PRESSED) && _direction != 2) { |
el16dlc | 2:0bd6711eae26 | 35 | _direction = 3; |
el16dlc | 2:0bd6711eae26 | 36 | } |
el16dlc | 2:0bd6711eae26 | 37 | } |
el16dlc | 2:0bd6711eae26 | 38 | |
el16dlc | 2:0bd6711eae26 | 39 | void GameEngine::snake_move() { |
el16dlc | 2:0bd6711eae26 | 40 | if (_direction == 0) { |
el16dlc | 2:0bd6711eae26 | 41 | snake.set_snake_posY (snake.get_snake_posY()- 4); |
el16dlc | 2:0bd6711eae26 | 42 | } else if (_direction == 1) { |
el16dlc | 2:0bd6711eae26 | 43 | snake.set_snake_posY (snake.get_snake_posY()+ 4); |
el16dlc | 2:0bd6711eae26 | 44 | } else if (_direction == 2) { |
el16dlc | 2:0bd6711eae26 | 45 | snake.set_snake_posX (snake.get_snake_posX()- 4); |
el16dlc | 2:0bd6711eae26 | 46 | } else if (_direction == 3) { |
el16dlc | 2:0bd6711eae26 | 47 | snake.set_snake_posX (snake.get_snake_posX()+ 4); |
el16dlc | 6:f02ea8ec42b3 | 48 | } |
el16dlc | 3:660de4311976 | 49 | } |
el16dlc | 3:660de4311976 | 50 | |
el16dlc | 3:660de4311976 | 51 | void GameEngine::food_move() { |
el16dlc | 5:23a85b16ee54 | 52 | srand(time(NULL)); |
el16dlc | 3:660de4311976 | 53 | if(snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY()) { |
el16dlc | 3:660de4311976 | 54 | snake.set_food_posX((rand()%(19) + 1) * 4); |
el16dlc | 3:660de4311976 | 55 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); |
el16dlc | 7:72119ea3b3b1 | 56 | for (int k = _body_seg; k > 0; k--) { |
el16dlc | 7:72119ea3b3b1 | 57 | 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 | 7:72119ea3b3b1 | 58 | snake.set_food_posX((rand()%(19) + 1) * 4); |
el16dlc | 7:72119ea3b3b1 | 59 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); |
el16dlc | 7:72119ea3b3b1 | 60 | } |
el16dlc | 7:72119ea3b3b1 | 61 | } |
el16dlc | 4:0fc3441556e1 | 62 | _body_seg = _body_seg + 1; |
el16dlc | 3:660de4311976 | 63 | } |
el16dlc | 3:660de4311976 | 64 | } |
el16dlc | 4:0fc3441556e1 | 65 | void GameEngine::snake_body(N5110 &lcd) { |
el16dlc | 4:0fc3441556e1 | 66 | _body_posX[0] = snake.get_snake_posX(); |
el16dlc | 4:0fc3441556e1 | 67 | _body_posY[0] = snake.get_snake_posY(); |
el16dlc | 4:0fc3441556e1 | 68 | for (int j = _body_seg; j > 0; j--) { |
el16dlc | 4:0fc3441556e1 | 69 | lcd.drawRect(_body_posX[j],_body_posY[j],4,4,FILL_BLACK); |
el16dlc | 4:0fc3441556e1 | 70 | _body_posX[j] = _body_posX[j-1]; |
el16dlc | 4:0fc3441556e1 | 71 | _body_posY[j] = _body_posY[j-1]; |
el16dlc | 4:0fc3441556e1 | 72 | } |
el16dlc | 4:0fc3441556e1 | 73 | } |
el16dlc | 5:23a85b16ee54 | 74 | |
el16dlc | 5:23a85b16ee54 | 75 | void GameEngine::check_wall_collision() { |
el16dlc | 5:23a85b16ee54 | 76 | if (snake.get_snake_posX() == 0 || snake.get_snake_posX() > 80 || snake.get_snake_posY() < 0 || snake.get_snake_posY() > 44) { |
el16dlc | 5:23a85b16ee54 | 77 | _game_cont = false; |
el16dlc | 5:23a85b16ee54 | 78 | } |
el16dlc | 5:23a85b16ee54 | 79 | } |
el16dlc | 5:23a85b16ee54 | 80 | |
el16dlc | 5:23a85b16ee54 | 81 | void GameEngine::check_snake_collision() { |
el16dlc | 5:23a85b16ee54 | 82 | for (int i = _body_seg; i > 1; i--) { |
el16dlc | 5:23a85b16ee54 | 83 | if (_body_posX[i] == snake.get_snake_posX() && _body_posY[i] == snake.get_snake_posY()) { |
el16dlc | 5:23a85b16ee54 | 84 | _game_cont = false; |
el16dlc | 5:23a85b16ee54 | 85 | } |
el16dlc | 5:23a85b16ee54 | 86 | } |
el16dlc | 5:23a85b16ee54 | 87 | |
el16dlc | 5:23a85b16ee54 | 88 | } |
el16dlc | 5:23a85b16ee54 | 89 | |
el16dlc | 5:23a85b16ee54 | 90 | // accessor |
el16dlc | 5:23a85b16ee54 | 91 | bool GameEngine::get_game_cont() { |
el16dlc | 5:23a85b16ee54 | 92 | return _game_cont; |
el16dlc | 5:23a85b16ee54 | 93 | } |