Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Snake/Snake.cpp

Committer:
AhmedPlaymaker
Date:
2019-03-26
Revision:
7:48ba87cd79b5
Child:
8:890b986b16a4

File content as of revision 7:48ba87cd79b5:

#include "Snake.h"

Snake::Snake()
{

}

Snake::~Snake()
{

}

//The Snake Sprite.
int snake_sprite[1][1] = {
    {1},
    };
    
int m = 0; //Variable used to allow a starting location for the player.
    

void Snake::init()
{

    _speed = 0.15;// change this according to the options selected
    
}


void Snake::draw(N5110 &lcd)
{   
    if(m == 0){
        _x = WIDTH/2;  //Spawns player sprite near the middle of the screen.
        _y = HEIGHT - 3; 
        m = m+1;  
        }
    //printf("SPRITE %d %d \n", _x, _y); 
    lcd.drawSprite(_x,_y,1,1,(int *)snake_sprite); //Function used to draw the sprite.
}



Vector2D Snake::get_pos()
{
    Vector2D snakepos = {_x,_y}; //Obtains the snake position.
    //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
    return snakepos;
}


void Snake::update(Direction d,float mag)
{
    _speed = int(mag*7.0f);  //Speed changes depending on how much you push the joystick.(As Of Now)


    // Diagonal speeds are /2 to prevent player from going double the speed.

    if (d == E) {
        _x+=_speed;
    }
    if (d == W) {
        _x-=_speed;
    }


    //Limits set so that the sprite does not travel off the screen.
    if (_y <= 0) {
        _y = 0;
    }
    if (_x <= 0) {
        _x = 0;
    }
    if (_x > 78) {
        _x = 78;
    }
    if (_y > 42) {
        _y = 42;
    }

}