Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

GameObjects/Snake/Snake.cpp

Committer:
AhmedPlaymaker
Date:
2019-05-06
Revision:
81:4c1641e10dcd
Parent:
79:35cb65c52d25
Child:
83:329da564799a

File content as of revision 81:4c1641e10dcd:

#include "Snake.h"

Snake::Snake()
{

}

Snake::~Snake()
{

}

//The Snake Sprite.
int snake_sprite[3][3] = {
    {0,1,0},
    {1,0,1},
    {0,1,0},
};

void Snake::init(int length, int speed)
{
    _length = length;
    if(length >= 10) {
        _length = 10;   //to stop the snake length virtually at 10 when it goes past it.
    }

    _speed = speed;// change this according to the options selected
    reset = 0; //Variable used to allow a starting location for the player.
}


void Snake::draw(N5110 &lcd, int length)
{
    _length = length;
    if(reset == 0) {

        Vector2D p = {WIDTH/2, HEIGHT - 3};  //Spawns player sprite near the middle of the screen.
        Snake::set_pos(p);

        reset = reset+1;
    }
    //printf("SPRITE %d %d \n", _x[0], _y[0]);

    for(int i=0; i<=_length-1; i++)  {
        lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite.
    }
}


void Snake::set_pos(Vector2D p)
{
    _x[0] = p.x;  //Spawns player sprite near the middle of the screen.
    _y[0] = p.y;

    for(int i=0; i<=8; i++)  {
        _x[i+1] = _x[i];
        _y[i+1] = _y[i] - 3;
    }
}

Vector2D Snake::get_pos(int snakeIndex)
{
    Vector2D snakepos; //Stores the snake position.

    for(int i = (snakeIndex + 1); i<=10; i++)  {
        if(_length == i)  {
            snakepos.x = _x[i - (snakeIndex + 1)];
            snakepos.y = _y[i - (snakeIndex + 1)];
            return snakepos;
        }
    }
    if(_length >= 10)  {
        snakepos.x = _x[9 - snakeIndex];
        snakepos.y = _y[9 - snakeIndex];
        return snakepos;
    }
    snakepos.x = NULL;
    snakepos.y = NULL;
    return snakepos;
    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
}

void Snake::update(Direction d, int* b)
{
    Snake::chainSnakeTogether(b);

    Snake::mooveSnake(d, b);

    Snake::_setSnakeLimits();
}

void Snake::chainSnakeTogether(int* b)
{
    //this makes all of the snake beeds chained together by making the lower ones drag towards where the top one was in the previous loop
    //the b[i] makes sure that the snake beed doesn't move if that beed is deactivated by colliding with a barrier. b[i] also signifies the specific beed number by i.
    for(int i=1; i<=9; i++)  {
        if((_length > i)&&(_x[i-1] != _x[i]))  {
            if ((_x[i-1] > _x[i])&&(b[i] == 1)&&(b[i-1] == 1))  {
                _x[i-1]-=_speed;
            }
            if ((_x[i-1] < _x[i])&&(b[i] == 1)&&(b[i-1] == 1))  {
                _x[i-1]+=_speed;
            }
        }
    }
}

void Snake::mooveSnake(Direction d, int* b)
{
    //this makes the controls of W/E directions only exclusive to the top beed in the snake
    for(int i=0; i<=9; i++)  {
        if((_length == i+1)&&(b[i] == 1))  {

            if (d == E) {
                _x[i]+= _speed;
            }

            if (d == W) {
                _x[i]-= _speed;
            }

        }
    }
}

void Snake::_setSnakeLimits()
{
    // the following makes sure that when the length is increased, the snake stays where it was when it ate food.

    for(int i=1; i<=9; i++)  {

        if(_length < (i+1))  {
            _x[i] = _x[i-1];
        }
    }

    //Limits set so that the snake does not travel off the screen.
    for(int i=0; i<=9; i++)  {

        if (_x[i] <= 0) {
            _x[i] = 0;
        }

        if (_x[i] > 81) {
            _x[i] = 81;
        }
    }
}