Dart DocumentationsnakesnakeElement

snakeElement class

class snakeElement
{
 num x, y;
 num size;
 num speed;
 int moveX = 0;
 int moveY = 0;
 
 Snake snake;
 int elementId;
 
 snakeElement(snake, x, y)
 {
   print("new Snake Element");
   
   this.snake = snake;
   this.x = x;
   this.y = y;
   
   this.size = snake.size;
   
   elementId = snake.elements.length;
 }

 draw()
 {
   ctx.beginPath();
   ctx.lineWidth = 2;
   ctx.fillStyle = "green";
   ctx.strokeStyle = "green";
   ctx.arc(this.x, this.y, this.size, 0, PI*2, false);
   ctx.fill();
   ctx.closePath();
   ctx.stroke();
 }
 
 update(modifier)
 {
   /*this.x = this.x + this.speed * modifier/1000 * this.moveX;
   this.y = this.y + this.speed * modifier/1000 * this.moveY;*/
   
   this.x += this.moveX * snake.speed;
   this.y += this.moveY * snake.speed;
   
   if(this.elementId > 0)
   {
     snakeElement elementBeforeMe = snake.elements[this.elementId - 1];
     if(elementBeforeMe.x + elementBeforeMe.size*2 < this.x)
     {
       if(!(this.x > elementBeforeMe.x + WIDTH/2))
         this.moveX = -1;
       this.moveY = 0;
     }
     else if(elementBeforeMe.x > this.x + this.size*2)
     {
       if(!(this.x < elementBeforeMe.x - WIDTH/2))
         this.moveX = 1;
       this.moveY = 0;
     }
     if(elementBeforeMe.y + elementBeforeMe.size*2 < this.y)
     {
       if(!(this.y > elementBeforeMe.y + HEIGHT/2))
         this.moveY = -1;
       this.moveX = 0;
     }
     else if(elementBeforeMe.y > this.y + this.size*2)
     {
       if(!(this.y < elementBeforeMe.y - HEIGHT/2))
         this.moveY = 1;
       this.moveX = 0;
     }
     
   }
   
   if(x >= WIDTH)
     x -= WIDTH;
   else if(x <= 0)
     x += WIDTH;
   
   if(y >= HEIGHT)
     y -= HEIGHT;
   else if(y <= 0)
     y += HEIGHT;
 }
}

Constructors

new snakeElement(snake, x, y) #

snakeElement(snake, x, y)
{
 print("new Snake Element");
 
 this.snake = snake;
 this.x = x;
 this.y = y;
 
 this.size = snake.size;
 
 elementId = snake.elements.length;
}

Properties

int elementId #

int elementId

int moveX #

int moveX = 0

int moveY #

int moveY = 0

num size #

num size

Snake snake #

Snake snake

num speed #

num speed

num x #

num x

num y #

num x, y

Methods

dynamic draw() #

draw()
{
 ctx.beginPath();
 ctx.lineWidth = 2;
 ctx.fillStyle = "green";
 ctx.strokeStyle = "green";
 ctx.arc(this.x, this.y, this.size, 0, PI*2, false);
 ctx.fill();
 ctx.closePath();
 ctx.stroke();
}

dynamic update(modifier) #

update(modifier)
{
 /*this.x = this.x + this.speed * modifier/1000 * this.moveX;
 this.y = this.y + this.speed * modifier/1000 * this.moveY;*/
 
 this.x += this.moveX * snake.speed;
 this.y += this.moveY * snake.speed;
 
 if(this.elementId > 0)
 {
   snakeElement elementBeforeMe = snake.elements[this.elementId - 1];
   if(elementBeforeMe.x + elementBeforeMe.size*2 < this.x)
   {
     if(!(this.x > elementBeforeMe.x + WIDTH/2))
       this.moveX = -1;
     this.moveY = 0;
   }
   else if(elementBeforeMe.x > this.x + this.size*2)
   {
     if(!(this.x < elementBeforeMe.x - WIDTH/2))
       this.moveX = 1;
     this.moveY = 0;
   }
   if(elementBeforeMe.y + elementBeforeMe.size*2 < this.y)
   {
     if(!(this.y > elementBeforeMe.y + HEIGHT/2))
       this.moveY = -1;
     this.moveX = 0;
   }
   else if(elementBeforeMe.y > this.y + this.size*2)
   {
     if(!(this.y < elementBeforeMe.y - HEIGHT/2))
       this.moveY = 1;
     this.moveX = 0;
   }
   
 }
 
 if(x >= WIDTH)
   x -= WIDTH;
 else if(x <= 0)
   x += WIDTH;
 
 if(y >= HEIGHT)
   y -= HEIGHT;
 else if(y <= 0)
   y += HEIGHT;
}