> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.r6SqLFlWOoy/rev.534
 * 
 * authors: 
 *   Jackie Banda
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



var cars;
var n = 300; //random(300); //10; //2; //random(10);

var colors = [color (100, 405, 0),
            color (34, 139, 34),
                color(50, 107, 0)];

void setup() {
    size(300,300);
    rotate(180 *PI/ 180);
    translate (-300, -300);
    
    
    //not used
    textFont(loadFont("Courier New"),12);
    
    cars = new ArrayList(n);
    for (var i=0; i<n; i++) {
        cars[i] = new Car();
        cars[i].x = random(width);
        cars[i].y = random(height);
        //cars[i].color = color(random(255),random(255),random(255));
        cars[i].color = colors[floor(random(3))];
        cars[i].speed = random(.25);
    }
}

void draw() {
    background(255);
    for (var i=0; i<n; i++) {
        cars[i].move();
        cars[i].draw();
    }
}

function Car(x, y, c, speed) {
    this.x = x;
    this.y = y;
    this.color = c;
    this.w = 5;
    this.h = 8;
    this.speed = speed;
    this.lasttime = millis();

    
    //or you could do: function draw() {
    this.draw = function() {
        pushStyle();
        
        //if car is under mouse, make it red
        if (this.undermouse()) {
            fill(color(255,0,0));
        } else {
            fill(this.color);
        }
        rect(this.x-this.w, this.y-this.h, this.w, this.h);
        
        //draw the speed on top of the car
        //fill(255,255,255);
        //text(this.speed.toString(), this.x-50, this.y+10);
        popStyle();
    }
    
    this.move = function() {
        //a local, temporary variable to store the time
        var thistime = millis();
        
        //Better way to do accurate speed (no jumping around
        //when you change the speed). Increment the position
        //based on the speed:
        //position = lastposition + (speed*dt)
        this.y += this.speed * (thistime-this.lasttime);
        
        //restrict position to within width of sketch
        this.y = (this.y % (width + this.w*2));
        
        this.lasttime = thistime;
    }
    
    this.undermouse = function() {
        if (mouseX > this.x-this.w && mouseX < this.x &&
           mouseY > this.y && mouseY < this.y+this.h) {
               return true;
        }
        
        return false;
    }
    
}

//Click the mouse to change the speed.
//If the mouseX is at the middle of the screen (width/2),
//the speed is multiplied by 1.0
//To the right it goes up to 2.0, to the left down to 0.0
void mousePressed() {
    //another local, temporary variable
    var multiplier = mouseY /  (height/2.0);
    for (var i=0; i<n; i++) {
        cars[i].speed *= multiplier;
    }
}