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.
element/element.cpp
- Committer:
- wuweilong
- Date:
- 2019-05-08
- Revision:
- 10:ae5f62a1e40e
File content as of revision 10:ae5f62a1e40e:
#include "element.h"
element::element()
{
}
element::~element()
{
}
void element::init(int size ,int foodx ,int foody,int snakex, int snakey ,int n)
{
_size=size;
_foodpos.x=foodx;
_foodpos.y=foody;
for(int i=0;i<n;i++){
_snakebody[i].x=snakex+2*i;
_snakebody[i].y=snakey;
}
}
void element::update()
{
srand(time(NULL));
_foodpos.x=rand()%(WIDTH-2);
_foodpos.y=rand()%(HEIGHT-2);
}
Vector2D element::getfoodpos(){
Vector2D p;
p=_foodpos;
return p;
}
void element::draw(N5110 &lcd,int n){
lcd.drawRect(_foodpos.x,_foodpos.y,_size,_size,FILL_BLACK);
for(int i=0;i<n;i++){
lcd.drawRect(_snakebody[i].x,_snakebody[i].y,3,3,FILL_BLACK);
}
}
void element::input(Gamepad &pad){
_d=pad.get_direction();
}
void element::move(int n){
int k=0;
if(_snakebody[0].x>_snakebody[1].x)
{
k=0;//
}
if(_snakebody[0].x<_snakebody[1].x)
{
k=1;
}
if(_snakebody[0].y<_snakebody[1].y)
{
k=2;
}
if(_snakebody[0].y>_snakebody[1].y)
{
k=3;
}
for(int i=n-1;i>0;i--)
{
_snakebody[i].x=_snakebody[i-1].x;
_snakebody[i].y=_snakebody[i-1].y;
}
if (_d == N && k!=3 )
{
_snakebody[0].y-=1;
} if (_d == S && k!=2) {
_snakebody[0].y+=1;
}if(_d==E && k!=1){
_snakebody[0].x+=1;
} if(_d==W && k!=0){
_snakebody[0].x-=1;
}if(_d==CENTRE && k==0)
{
_snakebody[0].x+=1;
}
if(_d==CENTRE && k==1)
{
_snakebody[0].x-=1;
}
if(_d==CENTRE && k==2)
{
_snakebody[0].y-=1;
}
if(_d==CENTRE && k==3)
{
_snakebody[0].y+=1;
}
}
Vector2D element::getsnakepos(int n){
Vector2D p;
p=_snakebody[n];
return p;
}