ELEC2645 (2018/19) / Mbed 2 deprecated el17dtt

Dependencies:   mbed

GameEngine/engine.cpp

Committer:
batJoro
Date:
2019-05-04
Revision:
8:b3738229ba85
Parent:
6:4c55dd4b6d42
Child:
9:dc13042b09f5

File content as of revision 8:b3738229ba85:


#include "engine.h"

Engine::Engine(){
    
    
}

Engine::~Engine(){
    
}

void Engine::init(int screenHeight, int screenWidth, int speed){

    setSpeed(speed);
    _screen_height = screenHeight;
    _screen_width = screenWidth;    
}

void Engine::setSpeed(int speed) {
    if (speed <= 0) _speed = 0;
    
    _speed = speed;
}

void Engine::update(Gamepad &pad, N5110 &lcd) {
    
    clearScreen(lcd);
    
    for (int y = 0; y < _screen_height / 2; y++) {
        for (int x = 0; x < _screen_width; x++) {
            
            float fPointCentre = 0.5f;
            float fRoadSpace = 0.6f;
            float fSideSpace = fRoadSpace * 0.15f;
            
            // for easier calculating the sides
            fRoadSpace *= 0.5f;
            
            int leftGrass = (fPointCentre - fRoadSpace - fSideSpace) * _screen_width;
            int leftSide = (fPointCentre - fRoadSpace) * _screen_width;
            int rightGrass = (fPointCentre + fRoadSpace + fSideSpace) * _screen_width;
            int rightSide = (fPointCentre + fRoadSpace) * _screen_width;
            
            int row = _screen_height / 2 + y;
            
            if ( x >= 0 && x < leftGrass) {
                if ( y % 2 == 0 ) 
                lcd.setPixel(x, row, true);
            } 
            if ( x >= leftGrass && x < leftSide) {
                if ( x % 2 == 0 ) 
                    lcd.setPixel(x, row, true);
            }
            if ( x >= rightSide && x < rightGrass ) {
                 if ( x % 2 == 0 ) 
                    lcd.setPixel(x, row, true);
            } 
            if ( x >= rightGrass && x < _screen_width) {
                if ( y % 2 == 0 ) 
                lcd.setPixel(x, row, true);
            }        
        }
    }
    lcd.refresh();
    wait(200);   
}

int Engine::car1[6*9] = {
        0,0,0,0,1,0,0,0,0,
        0,0,0,1,1,1,0,0,0,
        0,0,0,1,1,1,0,0,0,
        1,0,1,1,1,1,1,0,1,
        1,1,1,1,1,1,1,1,1,
        0,0,0,1,0,1,0,0,0
    };
    
int Engine::car2[6*9] = {
        0,0,1,0,0,1,1,0,0,
        0,1,0,0,1,1,1,0,0,
        0,1,1,1,1,1,0,0,0,
        1,0,1,1,1,0,0,0,0,
        0,0,0,1,1,1,1,0,0,
        0,0,1,0,0,0,0,0,0
    };
    
 int Engine::car3[6*9] = {
        0,0,1,1,0,0,1,0,0,
        0,0,1,1,1,0,0,1,0,
        0,0,0,1,1,1,1,1,0,
        0,0,0,0,1,1,1,0,1,
        0,0,1,1,1,1,0,0,0,
        0,0,0,0,0,0,1,0,0
     };