Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad N5110 Joystick
Game_engine/Game_engine.cpp@3:660de4311976, 2019-05-09 (annotated)
- Committer:
- el16dlc
- Date:
- Thu May 09 00:19:35 2019 +0000
- Revision:
- 3:660de4311976
- Parent:
- 2:0bd6711eae26
- Child:
- 4:0fc3441556e1
4
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 | 3:660de4311976 | 13 | snake.set_food_posX((rand()%(19) + 1) * 4); |
el16dlc | 3:660de4311976 | 14 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); |
el16dlc | 2:0bd6711eae26 | 15 | } |
el16dlc | 1:b9f3b7d869d8 | 16 | void GameEngine::draw(N5110 &lcd) { |
el16dlc | 1:b9f3b7d869d8 | 17 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // Draws screen limits |
el16dlc | 2:0bd6711eae26 | 18 | snake.draw_head(lcd); |
el16dlc | 3:660de4311976 | 19 | snake.draw_food(lcd); |
el16dlc | 2:0bd6711eae26 | 20 | } |
el16dlc | 2:0bd6711eae26 | 21 | |
el16dlc | 2:0bd6711eae26 | 22 | void GameEngine::get_dir(Gamepad &gamepad) { |
el16dlc | 3:660de4311976 | 23 | if ( gamepad.check_event(Gamepad::Y_PRESSED) && _direction != 1) { |
el16dlc | 2:0bd6711eae26 | 24 | _direction = 0; |
el16dlc | 2:0bd6711eae26 | 25 | } |
el16dlc | 3:660de4311976 | 26 | if ( gamepad.check_event(Gamepad::A_PRESSED) && _direction != 0) { |
el16dlc | 2:0bd6711eae26 | 27 | _direction = 1; |
el16dlc | 2:0bd6711eae26 | 28 | } |
el16dlc | 3:660de4311976 | 29 | if ( gamepad.check_event(Gamepad::X_PRESSED) && _direction != 3) { |
el16dlc | 2:0bd6711eae26 | 30 | _direction = 2; |
el16dlc | 2:0bd6711eae26 | 31 | } |
el16dlc | 3:660de4311976 | 32 | if ( gamepad.check_event(Gamepad::B_PRESSED) && _direction != 2) { |
el16dlc | 2:0bd6711eae26 | 33 | _direction = 3; |
el16dlc | 2:0bd6711eae26 | 34 | } |
el16dlc | 2:0bd6711eae26 | 35 | } |
el16dlc | 2:0bd6711eae26 | 36 | |
el16dlc | 2:0bd6711eae26 | 37 | void GameEngine::snake_move() { |
el16dlc | 2:0bd6711eae26 | 38 | if (_direction == 0) { |
el16dlc | 2:0bd6711eae26 | 39 | snake.set_snake_posY (snake.get_snake_posY()- 4); |
el16dlc | 2:0bd6711eae26 | 40 | } else if (_direction == 1) { |
el16dlc | 2:0bd6711eae26 | 41 | snake.set_snake_posY (snake.get_snake_posY()+ 4); |
el16dlc | 2:0bd6711eae26 | 42 | } else if (_direction == 2) { |
el16dlc | 2:0bd6711eae26 | 43 | snake.set_snake_posX (snake.get_snake_posX()- 4); |
el16dlc | 2:0bd6711eae26 | 44 | } else if (_direction == 3) { |
el16dlc | 2:0bd6711eae26 | 45 | snake.set_snake_posX (snake.get_snake_posX()+ 4); |
el16dlc | 2:0bd6711eae26 | 46 | } |
el16dlc | 3:660de4311976 | 47 | } |
el16dlc | 3:660de4311976 | 48 | |
el16dlc | 3:660de4311976 | 49 | void GameEngine::food_move() { |
el16dlc | 3:660de4311976 | 50 | if(snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY()) { |
el16dlc | 3:660de4311976 | 51 | snake.set_food_posX((rand()%(19) + 1) * 4); |
el16dlc | 3:660de4311976 | 52 | snake.set_food_posY((rand()%(11) + 1) * 4 - 2); |
el16dlc | 3:660de4311976 | 53 | } |
el16dlc | 3:660de4311976 | 54 | } |