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:
3:660de4311976
Parent:
2:0bd6711eae26
Child:
4:0fc3441556e1

File content as of revision 3:660de4311976:

#include "Game_engine.h"
#include "Snake.h"

GameEngine::GameEngine() {
}

GameEngine::~GameEngine() {
}
Snake snake;

void GameEngine::init() {
    snake.init();
    snake.set_food_posX((rand()%(19) + 1) * 4);
    snake.set_food_posY((rand()%(11) + 1) * 4 - 2);
}
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() {
    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);
    }    
}