ELEC2645 (2017/18) / Mbed OS el16ajm

Engine/Engine.cpp

Committer:
Andrew_M
Date:
2018-05-07
Revision:
13:81573be8fac6
Parent:
12:d3eef5ea3f43
Child:
14:a57a40ff9430

File content as of revision 13:81573be8fac6:

#include "Engine.h"

Engine::Engine()
{

}

Engine::~Engine()
{

}

void Engine::init()     // initialise the game parameters
{
    _solid.init(10,15);
    _noodles.init(10,10);
    _gameOver = false;
    lvlTwo()    ;
}
void Engine::setLvl(int _levelToSet)
{
    _lvl = _levelToSet;
}

void Engine::loadLvl()
{
    if (_lvl == 1) {
        lvlOne();
    } else if (_lvl == 2) {
        lvlTwo();
    } else if (_lvl == 3) {
        lvlThree();
    }
}

void Engine::lvlOne()
{
    memset(_grid, 0, sizeof(_grid));
}

void Engine::lvlTwo()
{
    int _newGrid[22][22] = {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0},
        {0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0},
        {0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
        {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
    };

    for(int i = 0; i < 22; i++) {
        for (int j = 0; j < 22; j++) {
            _grid[i][j] = _newGrid[i][j];
        }
    }
}

void Engine::lvlThree()
{
    int _newGrid[22][22] = {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0},
        {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0},
        {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
    };

    for(int i = 0; i < 22; i++) {
        for (int j = 0; j < 22; j++) {
            _grid[i][j] = _newGrid[i][j];
        }
    }
}

void Engine::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
}

void Engine::draw(N5110 &lcd)
{
    lcd.drawRect(0,0,48,48,FILL_TRANSPARENT);

    for (int j = 0; j < 22; j++) {
        for (int i = 0; i < 22; i++) {
            if (_grid[i][j] != 0) {
                lcd.drawRect((2 * i) + 2,(2 * j) + 2,2,2,FILL_BLACK);
            }
        }
    }

    lcd.printString("Score",50,0);


    int _score = (_solid.getLength() - 3);

    char buffer1[21];
    sprintf(buffer1,"%3d",_score);
    lcd.printString(buffer1,50,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
}

void Engine::gameOverScreen(N5110 &lcd)
{
    for (int i = 0; i < 22; i++) {
        for (int j = 0; j < 22; j++) {
            lcd.drawRect((2 * i) + 2,(2 * j) + 2,2,2,FILL_BLACK);
            wait(0.01);
            lcd.refresh();
        }
    }
}

void Engine::update(Gamepad &pad)
{
    loadLvl();

    _solid.update(_d);

    int _l =_solid.getLength();

    for (int z = 0; z < _l; z++) {

        if (_solid.getX(z) > 21 || _solid.getX(z) < 0) {
            _gameOver = true;
        } else if (_solid.getY(z) > 21 || _solid.getY(z) < 0) {
            _gameOver = true;
        }

        if (_grid[_solid.getX(z)][_solid.getY(z)] != 1) {
            _grid[_solid.getX(z)][_solid.getY(z)] = 1;
        } else {
            _gameOver = true;
        }
    }

    _grid[_noodles.getY()][_noodles.getY()] = 2;

    if (checkFood()) { //checks if the head has reached food
    
        growSnake(pad);

        bool empty = false; //makes a varible for finding an empty square
        while (!empty) {    //loops until empty square found
            _noodles.random(); //randomise position of noodles
            if ( _grid[_noodles.getY()][_noodles.getY()] == 0) {
                empty = true; //stops looping when free space is found
                // no need to set the grid as it will put the noodles in the new location the next time 'update' is called
            }

        }
    }
}

bool Engine::checkFood()
{
    if (_grid[_solid.getX(0)][_solid.getY(0)] == 2) {
        return true;
    } else {
        return false;
    }
}

void Engine::growSnake(Gamepad &pad)
{
    pad.tone(750.0,0.1);
    if (_solid.getLength()<484) { //checks if the snake can grow any more

        _solid.grow();  //makes snake longer

    }
}

float Engine::getScore()
{
    return _score;
}


bool Engine::getGameOver()
{
    return _gameOver;
}