game for collection

Dependencies:   mbed

redspider/redspider.cpp

Committer:
godv
Date:
2020-04-29
Revision:
2:fe4b9d530a8b

File content as of revision 2:fe4b9d530a8b:

#include "redspider.h"

// nothing doing in the constructor and destructor
redspider::redspider()
{

}

redspider::~redspider()
{

}

int redspider1[5][7] =   {
   { 0,1,1,1,1,0,0 },
   { 0,0,1,1,0,0,0 },
   { 1,1,1,1,1,1,1 },
   { 0,0,1,1,0,0,0 },
   { 0,1,1,1,1,0,0 },
   
   
   
};


void redspider::init(int x,int height,int width)
{
    _x = x;  // x value on screen is fixed
    _y = HEIGHT/2 - height/2;  // y depends on height of screen and height of Human
    _height = height;
    _width = width;
    _speed = 1;  // default speed
    _score = 1;  // start score from zero
    
   
}

void redspider::draw(N5110 &lcd)
{
    // draw Human in screen buffer. 
    lcd.drawSprite(_x,_y,_height,_width,(int*)redspider1);
}

void redspider::update(Direction d,float mag)
{
    _speed = int(mag*10.0f);  // scale is arbitrary, could be changed in future
    

    // update y value depending on direction of movement
    // North is decrement as origin is at the top-left so decreasing moves up
    if (d == N) {
        _y-=_speed;
    } 
    if (d == S) {
        _y+=_speed;
    }
    
    if (d == W) {
        _x-=_speed;
    } 
    else if (d == E) {
        _x+=_speed;
    }

    // check the x,y origin to ensure that the Human doesn't go off screen
    if (_y < 1) {
        _y = 1;
    }
    if (_y > HEIGHT - _height - 1) {
        _y = HEIGHT - _height - 1;
    }
    
    if (_x < 1) {
        _x = 1;
    }
    if (_x > WIDTH - _width - 1) {
        _x = WIDTH - _width - 1;
    }
}

void redspider::add_score()
{
    _score++;
}



void redspider::minus_score()
{
    _score--;
}

int redspider::get_score()
{
    return _score;
}




void redspider::set_velocity(Vector2D v)
{
    _velocity.x = v.x;
    _velocity.y = v.y;
}
 
Vector2D redspider::get_velocity()
{
    Vector2D v = {_velocity.x,_velocity.y};
    return v;
}

Vector2D redspider::get_pos() {
    Vector2D p = {_x,_y};
    return p;    
}
void redspider::set_pos(Vector2D p)
{
    _x = p.x;
    _y = p.y;
}