xxxxxxxxxx
39
class Snake {
constructor() {
this.pos = createVector(0, 0);
this.angle = 0;
this.prevPos = [];
for (let i = 0; i < 21; i++) {
this.prevPos.push(createVector(i, 0));
}
this.angle = 0;
}
update() {
if (keyIsDown(38)) {
this.pos.add(p5.Vector.fromAngle(this.angle, 2));
}
if (keyIsDown(37)) {
this.angle -= 0.1;
}
if (keyIsDown(39)) {
this.angle += 0.1;
}
this.prevPos.push(this.pos.copy());
if (this.prevPos.length > 30) {
this.prevPos.shift();
}
}
show() {
push();
// translate(0);
ellipseMode(CENTER);
fill(255);
noStroke();
for (let p of this.prevPos) {
circle(p.x, p.y, 10);
}
pop();
}
}