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.
Dependencies: mbed
snake/snake.cpp
- Committer:
- weiway
- Date:
- 2018-05-08
- Revision:
- 20:980b37fde361
- Parent:
- 18:e58a1f8e72ad
File content as of revision 20:980b37fde361:
#include "snake.h"
snake::snake(){
}
snake::~snake(){
}
void snake::init(){ //initialise
for (int i = 0; i <_point ; i++) {//the initial length of snake = 6 pixel. i++ until it = 6 .
//the position both old and new x are changing as the snake move to east initially. And old and new y keep same position.
_xoldpos[i] = 30;
_yoldpos[i] = 23;
_xnewpos[i] = 30 ;
_ynewpos[i] = 23;
}
dir_snake = 3; // 3 = east . initial direction
_array = 100;
_point = 6;
}
//direction of the snake
void snake::update(Direction d,float mag){
if (dir_snake == 1){
s() ;}
else if (dir_snake == 2){
n();}
else if (dir_snake == 3){
e();}
else if (dir_snake == 4){
w();}
if ((d == S) )
{ dir_snake = 1 ;}
if ((d == N) )
{ dir_snake = 2 ;}
if ((d == E) )
{ dir_snake = 3 ;}
if ((d == W) )
{ dir_snake = 4 ;}
}
//draw the snake . the length of the snake is changing as the got point changing
void snake::draw(N5110 &lcd)
{
for(int i=0; i<_point; i++) {
lcd.setPixel(_xnewpos[i],_ynewpos[i]);
}
}
Vector2D snake::get_pos()//get the position of snake in 2D vector. From pong sample code
{
Vector2D p = {_xnewpos[0],_ynewpos[0]};
return p;
}
void snake::set_pos(Vector2D p)//set the position in 2D vector . From pong sample code
{
_xnewpos[0] = p.x;
_ynewpos[0] = p.y;
}
void snake::point()//To add the point that the snake ate.
{
_point++;
}
int snake::get_point()//used to display the number of point
{
return _point -6 ;
}
void snake::n() {//snake move to north
_xnewpos[0] = _xoldpos[0] ;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];
}
_ynewpos[0] = _yoldpos[0]- 1;
for (int i = 0; i < _array-1; i++) {
_ynewpos[i+1] = _yoldpos[i];
}
for (int i = 0; i < _array; i++) {
_xoldpos[i] = _xnewpos[i];
_yoldpos[i] = _ynewpos[i];
}
}
void snake::s(){//snake move to south
_xnewpos[0] = _xoldpos[0] ;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];
}
_ynewpos[0] = _yoldpos[0]+ 1;
for (int i = 0; i < _array-1; i++) {
_ynewpos[i+1] = _yoldpos[i];
}
for (int i = 0; i < _array; i++) {
_xoldpos[i] = _xnewpos[i];
_yoldpos[i] = _ynewpos[i];
}
}
void snake::w(){//snake move to west
_xnewpos[0] = _xoldpos[0]-1;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];
}
_ynewpos[0] = _yoldpos[0];
for (int i = 0; i < _array-1; i++) {
_ynewpos[i+1] = _yoldpos[i];
}
for (int i = 0; i < _array; i++) {
_xoldpos[i] = _xnewpos[i];
_yoldpos[i] = _ynewpos[i];
}
}
void snake::e(){//snake move to east
_xnewpos[0] = _xoldpos[0]+1;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];
}
_ynewpos[0] = _yoldpos[0];
for (int i = 0; i < _array-1; i++) {
_ynewpos[i+1] = _yoldpos[i];
}
for (int i = 0; i < _array; i++) {
_xoldpos[i] = _xnewpos[i];
_yoldpos[i] = _ynewpos[i];
}
}