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.cpp
00001 #include "element.h" 00002 element::element() 00003 { 00004 00005 } 00006 00007 element::~element() 00008 { 00009 00010 } 00011 void element::init(int size ,int foodx ,int foody,int snakex, int snakey ,int n) 00012 { 00013 _size=size; 00014 _foodpos.x=foodx; 00015 _foodpos.y=foody; 00016 for(int i=0;i<n;i++){ 00017 _snakebody[i].x=snakex+i; 00018 _snakebody[i].y=snakey; 00019 } 00020 00021 } 00022 void element::update() 00023 { 00024 00025 srand(time(NULL)); 00026 _foodpos.x=rand()%(WIDTH-10); 00027 _foodpos.y=rand()%(HEIGHT-10); 00028 00029 } 00030 00031 Vector2D element::getfoodpos(){ 00032 Vector2D p; 00033 p=_foodpos; 00034 return p; 00035 } 00036 void element::draw(N5110 &lcd,int n){ 00037 lcd.drawRect(_foodpos.x,_foodpos.y,_size,_size,FILL_BLACK); 00038 for(int i=0;i<n;i++){ 00039 lcd.drawRect(_snakebody[i].x,_snakebody[i].y,2,2,FILL_BLACK); 00040 } 00041 } 00042 void element::input(Gamepad &pad){ 00043 _d=pad.get_direction(); 00044 } 00045 void element::move(int n){ 00046 int k=0; 00047 if(_snakebody[0].x>_snakebody[1].x) 00048 { 00049 k=0;// 00050 } 00051 if(_snakebody[0].x<_snakebody[1].x) 00052 { 00053 k=1; 00054 } 00055 if(_snakebody[0].y<_snakebody[1].y) 00056 { 00057 k=2; 00058 } 00059 if(_snakebody[0].y>_snakebody[1].y) 00060 { 00061 k=3; 00062 } 00063 00064 for(int i=n-1;i>0;i--) 00065 { 00066 _snakebody[i].x=_snakebody[i-1].x; 00067 _snakebody[i].y=_snakebody[i-1].y; 00068 } 00069 00070 if (_d == N && k!=3 ) 00071 { 00072 _snakebody[0].y-=1; 00073 00074 00075 } if (_d == S && k!=2) { 00076 00077 _snakebody[0].y+=1; 00078 00079 }if(_d==E && k!=1){ 00080 00081 _snakebody[0].x+=1; 00082 00083 00084 00085 } if(_d==W && k!=0){ 00086 00087 _snakebody[0].x-=1; 00088 00089 00090 }if(_d==CENTRE && k==0) 00091 00092 { 00093 _snakebody[0].x+=1; 00094 } 00095 if(_d==CENTRE && k==1) 00096 00097 { 00098 _snakebody[0].x-=1; 00099 } 00100 if(_d==CENTRE && k==2) 00101 00102 { 00103 _snakebody[0].y-=1; 00104 } 00105 if(_d==CENTRE && k==3) 00106 00107 { 00108 _snakebody[0].y+=1; 00109 } 00110 00111 } 00112 00113 Vector2D element::getsnakepos(int n){ 00114 Vector2D p; 00115 p=_snakebody[n]; 00116 return p; 00117 } 00118 00119 00120 00121
Generated on Wed Jul 13 2022 11:12:21 by
