XJEL2645 (18/19) / Mbed 2 deprecated 2645_Mygame

Dependencies:   mbed Gamepad N5110

snake/snake.cpp

Committer:
694617778
Date:
2019-04-13
Revision:
1:b49c36604125
Parent:
0:b67614a0c4cf
Child:
2:934daa65f73d

File content as of revision 1:b49c36604125:

#include "snake.h"

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

}

snake::~snake()
{

}

void snake::init(int size,int length)
{
    _size = size;
    _length = length;
    _x[0] = WIDTH/2 -  _size/2;
    _y[0] = HEIGHT/2 - _size/2;

    _x[1] = WIDTH/2 -  _size/2 -1*size;
    _y[1] = HEIGHT/2 - _size/2;
    
    _x[2] = WIDTH/2 -  _size/2 -2*size;
    _y[2] = HEIGHT/2 - _size/2;           
     
    _score = 0;  // start score from zero

}

int snake::get_length(){
    int d=_length;
    return d;
    }

void snake::update(int direction, int length)
{
    for(int k = length - 1; k > 0; k--){
    _x[k] = _x[k-1];
    _y[k] = _y[k-1];
    }
    if(direction == 0){
     _x[0] = _x[0] + 1;
     }else if(direction == 1){
         _y[0] = _y[0] + 1;
         }else if(direction == 2){
             _x[0] = _x[0] - 1;
             }else if(direction == 3){
                 _y[0] = _y[0] - 1;
    }
                 

}
 

void snake::draw(N5110 &lcd, int length)
{

    // draw snake in screen buffer. 
        lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
        for (int i = 0; i < length; i++){
        lcd.drawRect(_x[i],_y[i],_size,_size,FILL_BLACK);
      }

}


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

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