xxxxxxxxxx
88
let dice = [];
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
rectMode(CENTER);
ellipseMode(CENTER);
let i = new die(
width / 2, // x
height / 2, // y
random(-2, 2), // xspeed
random(-2, 2), // yspeed
1 // rspeed
);
dice.push(i);
}
function draw() {
background(300,100,100);
colorMode(HSB);
for (let i = 0; i < dice.length; i = i + 1) {
dice[i].update();
}
}
function mouseDragged() {
let i = new die(
mouseX, // x
mouseY, // y
random(-2, 2), // xspeed
random(-2, 2), // yspeed
1 // rspeed
);
dice.push(i);
}
class die {
constructor(x, y, xS, yS, rS) {
this.x = x;
this.y = y;
this.xS = xS;
this.yS = yS;
this.rS = rS;
this.a = 0;
}
update() {
push();
translate(this.x, this.y);
rotate(this.a);
stroke(0);
strokeWeight(2);
fill(200);
//left plane
rect(20, 20, 40, 40);
//right plane
quad(40, 0, 70, 20, 70, 60, 40, 40);
//bottom plane
quad(1, 42, 40, 42, 68, 61, 30, 60);
fill(0);
//left dots
ellipse(25, 10, 8, 8);
ellipse(9, 28, 8, 8);
//right dots
ellipse(50, 15, 5, 5);
ellipse(56, 30, 5, 5);
ellipse(62, 45, 5, 5);
//bottom dot
ellipse(34, 51, 10, 5);
pop();
this.x = this.x + this.xS;
this.y = this.y + this.yS;
this.a = this.a + this.rS;
if (this.x >= width || this.x <= 0) {
this.xS = this.xS * -1;
}
if (this.y >= height || this.y <= 0) {
this.yS = this.yS * -1;
}
}
}