Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SnakeFood/SnakeFood.cpp
- Committer:
- AhmedPlaymaker
- Date:
- 2019-04-17
- Revision:
- 33:249cf423fb18
- Parent:
- 32:3a3bdeffdf62
- Child:
- 36:dfdd619874ae
File content as of revision 33:249cf423fb18:
#include "SnakeFood.h"
SnakeFood::SnakeFood()
{
}
SnakeFood::~SnakeFood()
{
}
//SnakeFood Sprite.
int food_sprite[3][3] = {
    {0,1,0},
    {1,0,1},
    {0,1,0},
    };
    
void SnakeFood::init() //Delete Int
{
    fooddrop=0;
    n = 0;
}
void SnakeFood::draw(N5110 &lcd, int blockgap, int blockbuff)
{   
    //_velocity.x = 1;
    _velocity.y = 1;
    
    //add sideways velocity incase I want to make it dodgy.
    
    //Resets SnakeFood position if it reaches the bottom of the screen.
    //Also prevents SnakeFood from going out of the side of the screen.
    if(_fx < 0){
        _fx = 0;
        }
    if(_fx > 81){
        _fx = 81;
        }
    if(_fy >= 48){
        n = 0;
        }
    
     if(n == 0){
        _fx = (rand() % 82);  //change the number here so that there is always an unsurity(a little) if the food will come or not(change the limits for this)
        _fy = -2;
        if(blockbuff >= (blockgap - 5))  { //this makes sure that the snake food appears seperated from the block
            _fy = -15;
        }
        n = n+1;  
        }
    lcd.drawSprite(_fx,_fy,3,3,(int *)food_sprite); //Function to draw the sprite.
}
Vector2D SnakeFood::get_pos() //Obtains the X and Y coordinate of the target.
{
    Vector2D snakefoodpos = {_fx,_fy};
    //printf("snakefoodpos from food = %f %f \n", snakefoodpos.x, snakefoodpos.y);
    return snakefoodpos;
}
void SnakeFood::update()
{
    if(fooddrop == 0)  {
        _fx += _velocity.x;
        _fy += _velocity.y;
    }
    fooddrop += 1;
    
    if(fooddrop == 1)  {  //make this a variable multiple of game speed
        fooddrop = 0;
    }
}
void SnakeFood::set_pos(Vector2D p)
{
    _fx = p.x;
    _fy = p.y;
}