xxxxxxxxxx
102
let particles = [];
let carts = [];
let track;
let gate;
let canvas;
function preload(){
for (let i = 0;i<2; i++ ){
carts[i] = loadImage('cart'+ i + '.png');
}
track = loadImage('track.png');
gate = loadImage('gate.png');
}
function setup() {
// canvas = createCanvas(windowWidth, windowHeight);
// canvas.position(0,0);
// canvas.style('z-index', '-1')
createCanvas(600, 600);
}
function draw() {
background(255);
imageMode(CENTER);
image(track, width/2, height/2, width, height);
let input0 = floor(random(0,2));
let input1 = floor(random(0,2));
let output;
if (input0==1 && input1==1){
output = 1;
}
else {
output = 0;
}
let a = new particle(220,220, carts[input0], carts[input1], carts[output]);
particles.push(a);
for (let i = 0 ;i<1;i++){
particles[i].update();
particles[i].show();
if (particles[i].finished()) {
particles.splice(i,1);
}
}
// fill(20);
// noStroke();
// rect(width/2 - 25, height/2, 100, height/2);
// rect(width/2 - 125, 0, 100, height/2);
// rect(width/2 + 75, 0, 100, height/2);
image(gate, width/2, height/2 , width, height);
}
class particle {
constructor(w,h, img1, img2, img3){
this.y = height+height/10;
this.x = width/2;
this.vy = -5;
this.w = w;
this.h = h;
this.asset1 = img1;
this.asset2 = img2;
this.asset3 = img3;
}
update(){
this.y +=this.vy;
}
show(){
image(this.asset1, this.x-140, this.y, this.w, this.h);
image(this.asset2, this.x+130, this.y, this.w, this.h);
image(this.asset3, this.x , this.y, this.w, this.h);
}
swerve(){
image(this.asset2, this.x, this.y, this.w, this.h);
}
finished(){
return this.y < -100;
}
}