xxxxxxxxxx
var cars = [];
var A
function setup(){ createCanvas(400,200);
// make 25 cars
for(var i = 0; i < A; i++) { cars[i] = new Car(); }}
A=100
function draw(){
//The A variable changes
A=A+1
// clear background
background(255);
// loop through each car
for(var i = 0; i < cars.length; i++) { cars[i].drive(); cars[i].display(); } }
// car constructor
function Car(ypos, speed){
this.xpos = random(width); this.ypos = random(height); this.speed = random(20); this.c = color(255,20, random(133,193)); // drive method
this.drive = function() { if(this.xpos > width) {
this.xpos = -200; // start offscreen
this.ypos = random(height); } this.xpos = this.xpos + this.speed; } // display method
this.display = function() { // body of the car
fill(this.c); rectMode(CORNER); rect(this.xpos, this.ypos, 100, 50); // wheels
fill(0); ellipse(this.xpos + 20, this.ypos + 45, 40, 40); ellipse(this.xpos + 80, this.ypos + 45, 40, 40); }}