xxxxxxxxxx
117
class SlowCircle{
constructor(x,y, rad=30){
this.x = x
this.y = y
this.rad = rad
this.maxSpeed = 5
this.acc = 1
this.speedX = 3
this.speedY = 3
this.targetX = 0
this.targetY = 0
}
draw(){
// define the target speed
let targetSpeedX = this.maxSpeed, targetSpeedY = this.maxSpeed
// do X stuff
if (this.targetX<this.x)
targetSpeedX = -targetSpeedX
// slowly change the actual speed
if (this.speedX < targetSpeedX)
this.speedX +=this.acc
else this.speedX -=this.acc
// slowly change the position
this.x+=this.speedX
// do Y stuff
if (this.targetY<this.y)
targetSpeedY = -targetSpeedY
// slowly change the actual speed
if (this.speedY < targetSpeedY)
this.speedY += this.acc
else this.speedY -= this.acc
// slowly change the position
this.y +=this.speedY
circle(this.x, this.y, this.rad)
}
}
// class Snake{
// constructor(rad=50, pieces=4){
// this.pieces = []
// this.targetX = 0
// this.targetY = 0
// this.rad = rad
// for (let i=0; i<pieces; i++){
// this.pieces.push(new SlowCircle(0,0, rad))
// }
// }
// draw(){
// for (let i=0; i<this.pieces.length; i++){
// this.pieces[i].targetX = this.targetX+i*this.rad/2
// this.pieces[i].targetY = this.targetY+i*this.rad/2
// this.pieces[i].draw()
// }
// }
// }
class Snake{
constructor(rad, pieces, x, y){
this.rad = rad
this.x = x
this.y = y
this.targetX = 0
this.targetY = 0
this.circles = []
for(let i=0;i<pieces;i++){
this.circles[i] = new SlowCircle(x,y,rad, rad/2)
x = x+rad/4
y = y+rad/4
}
}
draw(){
this.circles[0].targetX = this.targetX
this.circles[0].targetY = this.targetY
this.circles[0].draw()
this.x = this.circles[0].x
this.y = this.circles[0].y
this.circles[1].targetX = this.circles[0].x
this.circles[1].targetY = this.circles[0].y
this.circles[1].draw()
let xdiff = this.circles[1].x - this.circles[0].x
let ydiff = this.circles[1].y - this.circles[0].y
for(let i = 2;i<this.circles.length;i++){
this.circles[i].targetX = this.circles[i-1].targetX + xdiff
this.circles[i].targetY = this.circles[i-1].targetY + ydiff
this.circles[i].draw()
}
}
addPiece(){
let lastcircle = this.circles[this.circles.length-1]
let newcircle = new SlowCircle(lastcircle.x + this.rad, lastcircle.y + this.rad, this.rad, this.rad/2)
newcircle.targetX = lastcircle.x
newcircle.targetY = lastcircle.y
this.circles.push(newcircle)
}
}
function setup() {
createCanvas(windowWidth,windowHeight);
}
snake = new Snake(50,7,0,0)
function draw() {
background(0);
snake.targetX = mouseX
snake.targetY = mouseY
snake.draw()
}