Dart DocumentationsnakeSnake

Snake class

class Snake
{
 num x, y;
 num size;
 num speed;
 
 num renderTime;
 
 List<snakeElement> elements;
 List<Bonus> bonuses;
 
 Snake(x, y, size, speed)
 {
   print("create main snake");
   
   this.size = size;
   this.speed = speed;
   
   this.renderTime = new DateTime.now().millisecondsSinceEpoch;
   
   this.elements =[];
   this.bonuses = [];
   
   newElement(x, y);
 }
 
 newElement([x, y])
 {
   if(this.elements.length == 0 && x != null && y != null) //premier enfant
   {
     print("first snakeElement");
     snakeElement firstSlement = new snakeElement(this, x, y);
     firstSlement.elementId = 0;
     this.elements = [firstSlement];
   }
   else
   {
     snakeElement lastElement = this.elements[(this.elements.length)-1];
     snakeElement newElement = new snakeElement(this, lastElement.x + lastElement.size*2*-lastElement.moveX, lastElement.y + lastElement.size*2*-lastElement.moveY);
     newElement.elementId = this.elements.length;
     this.elements.add(newElement);
     print(this.elements.length);
   }
 }
 
 void requestRedraw() {
   window.requestAnimationFrame(animate);
 }
 
 animate(num _)
 {
   num time = new DateTime.now().millisecondsSinceEpoch;
   num modifier = time - renderTime;
   
   ctx.clearRect(0, 0, WIDTH, HEIGHT);
   for(var el in elements)
   {
     el.update(modifier);
     el.draw();
   }
   
   for(var el in bonuses)
   {
     el.draw();
   }
   
   Random random = new Random();
   if((random.nextDouble() * 100).round() == 1)
   {
     bonuses.add(new Bonus(random.nextInt(WIDTH), random.nextInt(HEIGHT)));
   }
   
   renderTime = time;
   
   checkCollision();
   
   requestRedraw();
 }
 
 checkCollision()
 {
   /*for(var el in elements)
   {
     if(el.elementId > 0)
     {
       if(isCollisionBetween2Circles(elements[0], el))
       {
         print("GAME OVER");
         this.elements =[];
         this.newElement();
       }
         
       
     }
   }*/
   
   var elems = elements.toList();
   for (snakeElement el in elems) {
     if(el.elementId > 0)
     {
       if(isCollisionBetween2Circles(elements[0], el))
       {
         elements.removeRange(1, elements.length);
         //newElement();
       }
     }
     
   }
   
   var list = bonuses.toList();
   for (Bonus el in list) {
     if(isCollisionBetween2Circles(elements[0], el))
     {
       newElement();
      bonuses.remove(el);
     }
   }
   
   /*for(var el in bonuses)
   {
       if(isCollisionBetween2Circles(elements[0], el))
       {
         newElement();
         //bonuses.remove(el);
       }
   }*/
 }
 
 isCollisionBetween2Circles(elem1, elem2)
 {
   num distanceBetweenCircles = sqrt((elem1.x - elem2.x) * (elem1.x - elem2.x) + (elem1.y - elem2.y) * (elem1.y - elem2.y));
   
   if((distanceBetweenCircles < elem1.size) || (distanceBetweenCircles < elem2.size))
     return true;
   else
     return false;
 }
 
 init()
 {
   document.onKeyDown.listen(handleInput);
   requestRedraw();
 }
 
 handleInput(KeyboardEvent e)
 {
   switch(e.keyCode)
   {
     case 38:
       if(this.elements[0].moveY != 1)
       {
         this.elements[0].moveY = -1;
         this.elements[0].moveX = 0;
       }
       break;
     case 40:
       if(this.elements[0].moveY != -1)
       {
         this.elements[0].moveY = 1;
         this.elements[0].moveX = 0;
       }
       break;
     case 37: 
       if(this.elements[0].moveX != 1)
       {
         this.elements[0].moveX = -1;
         this.elements[0].moveY = 0;
       }
       break;
     case 39: 
       if(this.elements[0].moveX != -1)
       {
         this.elements[0].moveX = 1;
         this.elements[0].moveY = 0;
       }
       break;
   }
 }
}

Constructors

new Snake(x, y, size, speed) #

Snake(x, y, size, speed)
{
 print("create main snake");
 
 this.size = size;
 this.speed = speed;
 
 this.renderTime = new DateTime.now().millisecondsSinceEpoch;
 
 this.elements =[];
 this.bonuses = [];
 
 newElement(x, y);
}

Properties

List<Bonus> bonuses #

List<Bonus> bonuses

List<snakeElement> elements #

List<snakeElement> elements

num renderTime #

num renderTime

num size #

num size

num speed #

num speed

num x #

num x

num y #

num x, y

Methods

dynamic animate(num _) #

animate(num _)
{
 num time = new DateTime.now().millisecondsSinceEpoch;
 num modifier = time - renderTime;
 
 ctx.clearRect(0, 0, WIDTH, HEIGHT);
 for(var el in elements)
 {
   el.update(modifier);
   el.draw();
 }
 
 for(var el in bonuses)
 {
   el.draw();
 }
 
 Random random = new Random();
 if((random.nextDouble() * 100).round() == 1)
 {
   bonuses.add(new Bonus(random.nextInt(WIDTH), random.nextInt(HEIGHT)));
 }
 
 renderTime = time;
 
 checkCollision();
 
 requestRedraw();
}

dynamic checkCollision() #

checkCollision()
{
 /*for(var el in elements)
 {
   if(el.elementId > 0)
   {
     if(isCollisionBetween2Circles(elements[0], el))
     {
       print("GAME OVER");
       this.elements =[];
       this.newElement();
     }
       
     
   }
 }*/
 
 var elems = elements.toList();
 for (snakeElement el in elems) {
   if(el.elementId > 0)
   {
     if(isCollisionBetween2Circles(elements[0], el))
     {
       elements.removeRange(1, elements.length);
       //newElement();
     }
   }
   
 }
 
 var list = bonuses.toList();
 for (Bonus el in list) {
   if(isCollisionBetween2Circles(elements[0], el))
   {
     newElement();
    bonuses.remove(el);
   }
 }
 
 /*for(var el in bonuses)
 {
     if(isCollisionBetween2Circles(elements[0], el))
     {
       newElement();
       //bonuses.remove(el);
     }
 }*/
}

dynamic handleInput(KeyboardEvent e) #

handleInput(KeyboardEvent e)
{
 switch(e.keyCode)
 {
   case 38:
     if(this.elements[0].moveY != 1)
     {
       this.elements[0].moveY = -1;
       this.elements[0].moveX = 0;
     }
     break;
   case 40:
     if(this.elements[0].moveY != -1)
     {
       this.elements[0].moveY = 1;
       this.elements[0].moveX = 0;
     }
     break;
   case 37: 
     if(this.elements[0].moveX != 1)
     {
       this.elements[0].moveX = -1;
       this.elements[0].moveY = 0;
     }
     break;
   case 39: 
     if(this.elements[0].moveX != -1)
     {
       this.elements[0].moveX = 1;
       this.elements[0].moveY = 0;
     }
     break;
 }
}

dynamic init() #

init()
{
 document.onKeyDown.listen(handleInput);
 requestRedraw();
}

dynamic isCollisionBetween2Circles(elem1, elem2) #

isCollisionBetween2Circles(elem1, elem2)
{
 num distanceBetweenCircles = sqrt((elem1.x - elem2.x) * (elem1.x - elem2.x) + (elem1.y - elem2.y) * (elem1.y - elem2.y));
 
 if((distanceBetweenCircles < elem1.size) || (distanceBetweenCircles < elem2.size))
   return true;
 else
   return false;
}

dynamic newElement([x, y]) #

newElement([x, y])
{
 if(this.elements.length == 0 && x != null && y != null) //premier enfant
 {
   print("first snakeElement");
   snakeElement firstSlement = new snakeElement(this, x, y);
   firstSlement.elementId = 0;
   this.elements = [firstSlement];
 }
 else
 {
   snakeElement lastElement = this.elements[(this.elements.length)-1];
   snakeElement newElement = new snakeElement(this, lastElement.x + lastElement.size*2*-lastElement.moveX, lastElement.y + lastElement.size*2*-lastElement.moveY);
   newElement.elementId = this.elements.length;
   this.elements.add(newElement);
   print(this.elements.length);
 }
}

void requestRedraw() #

void requestRedraw() {
 window.requestAnimationFrame(animate);
}