Joe Shotton / Mbed 2 deprecated ELEC2645_Project_ll16j23s

Dependencies:   mbed ll16j23s_test_docs

SnakeEngine/SnakeEngine.cpp

Committer:
JoeShotton
Date:
2020-05-23
Revision:
6:6c9453397f4a
Parent:
5:06fa7674622a
Child:
7:dd84e0fab346

File content as of revision 6:6c9453397f4a:

#include "SnakeEngine.h"

SnakeEngine::SnakeEngine()
{
//constructor
    score = 0;
}

SnakeEngine::~SnakeEngine()
{
//destructor
}
 
/* bool collision(int state, int x, int y,) {
     if ((lcd.getPixel(x, y) == 1 && ((state == 1) || (state == 4))) || (lcd.getPixel(x+1, y+1) == 1 && ((state == 2) || (state == 3)))) {
        // checks infront of head to see if pixel is set
        // due to the size of the head, there is an offset for the check for North and Eastward directions
        pad.led(1,0.9);
        return true; 
    } else {
        pad.led(1,0.0);
        return = false;
    }
}
*/
void SnakeEngine::init(FXOS8700CQ &mag){
    _food.init(mag);
    _body.init();
    //_food.rand_pos(_food._x, _food._y);
}

void SnakeEngine::move_body(Gamepad &pad, N5110 &lcd, bool &death){
    _body.snake_movement(pad);
    _body.draw_body(lcd);
    //printf("Moving!");
    _food.do_food(lcd);
    snake_food_collision(pad, _body._length);
    if (_body._state > 0){
        _body.snake_snake_collision(pad, death);
    }
}

void SnakeEngine::snake_food_collision(Gamepad &pad, int &_length) {
    if (_food._x == _body._x_head && _food._y == _body._y_head){
        //printf("FOOD!");
    _food.rand_pos(pad, _food._x, _food._y);
    pad.led(3,0.9);
    _length += 5;
    score++;
    }
}

void SnakeEngine::transition_black(N5110 &lcd) {
    //lcd.clear();
    for (int j = 0; j < 21; j += 4) {
        printf("j: %d\n", j);
        for (int i = 1; i < 84 - (2*j); i += 2) {
            lcd.drawRect(j, j, i + 1, 4, FILL_BLACK);
            lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_BLACK);  
            wait_ms(5);   
            lcd.refresh();
        }
        for (int i = 1; i < 43 - (2*j); i += 2) {
            lcd.drawRect(80 - j, 4 + j, 4, i,FILL_BLACK);
            lcd.drawRect(j, 44 - i - j, 4, i,FILL_BLACK); 
            wait_ms(5);   
            lcd.refresh();
        }
    }
    wait_ms(500);      
}

void SnakeEngine::transition_white(N5110 &lcd) {
    //lcd.clear();
    for (int j = 0; j < 21; j += 4) {
        printf("j: %d\n", j);
        for (int i = 1; i < 84 - (2*j); i += 2) {
            lcd.drawRect(j, j, i + 1, 4, FILL_WHITE);
            lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_WHITE);  
            wait_ms(5);   
            lcd.refresh();
        }
        for (int i = 1; i < 43 - (2*j); i += 2) {
            lcd.drawRect(80 - j, 4 + j, 4, i,FILL_WHITE);
            lcd.drawRect(j, 44 - i - j, 4, i,FILL_WHITE); 
            wait_ms(5);   
            lcd.refresh();
        }
    }
    //wait_ms(500);      
}