xxxxxxxxxx
40
var delay = 40;
class Snake {
// Constructor
constructor(x, y) {
this.pos = createVector(x, y);
this.tail = [createVector(x - 2, y), createVector(x - 1, y)];
this.speed = createVector(1, 0);
this.timeToGo = 0;
this.generateFood();
}
// Display
display() {
// Head
fill(255, 0, 85);
noStroke();
ellipse(size * this.pos.x, size * this.pos.y, size, size);
// Tail
fill(255, 0, 105);
for (let i = 0; i < this.tail.length; i++)
ellipse(size * this.tail[i].x, size * this.tail[i].y, size, size);
}
// Control
control() {
this.timeToGo++;
if (this.timeToGo == delay) {
this.timeToGo = 0;
this.tail.shift();
this.tail.push(this.pos.copy());
this.pos.add(this.speed);
}
}
// Eating
}