xxxxxxxxxx
57
function setup() {
createCanvas(600, 400);
background(102);
x1 = width / 2;
y1 = height / 2;
x1speed = 1;
y1speed = 0.3;
star= new Star();
}
function draw(){
star.move();
star.display();
}
class Star {
constructor(x1,y1,x1speed,y1speed){
this.x1=x1;
this.y1=y1;
this.x1speed= x1speed;
this.y1speed= y1speed;
}
display(){
translate(x1, y1);
fill("aqua");
beginShape();
vertex(0, -50);
vertex(14, -20);
vertex(47, -15);
vertex(23, 7);
vertex(29, 40);
vertex(0, 25);
vertex(-29, 40);
vertex(-23, 7);
vertex(-47, -15);
vertex(-14, -20);
scale(0.3);
endShape(CLOSE);
}
move() {
this.x1+=this.x1speed;
this.y1+=this.y1speed;
this.x1speed = this.bounce(this.x1, 0, width, this.x1speed);
this.y1speed = this.bounce(this.y1, 0, height, this.y1speed);
}
bounce(pos, low, high, speed) {
if (pos < low || pos > high) {
speed *= -1;
return speed;
} else return speed;
}
}