wu weilong / Mbed 2 deprecated frdm_gpio1

Dependencies:   mbed

Revision:
10:ae5f62a1e40e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/element/element.cpp	Wed May 08 20:08:17 2019 +0000
@@ -0,0 +1,121 @@
+#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;
+}
+    
+
+
+        
\ No newline at end of file