varfloorPos_y;varcar;functionsetup(){createCanvas(1200,800);floorPos_y=600;car={x:200,height:120,length:130,wheelDia:80,color:color(255,100,100)};}functiondraw(){background(102,179,255);noStroke();fill(130);rect(0,floorPos_y,width,height-floorPos_y);renderCar(car);car.x+=1;if(car.x>width+500){car.x=-500;}}functionrenderCar(_car){fill(_car.color);stroke(0);//draw the car's bodyrect(_car.x,floorPos_y-_car.height,_car.length,_car.height-_car.wheelDia/2,20);//draw the windscreenfill(0,255,255);rect(_car.x+_car.length *0.7,floorPos_y-_car.height*0.8,_car.length *0.2,_car.height*0.3,5);//draw the wheelsfill(65);ellipse(_car.x,floorPos_y-_car.wheelDia/2,_car.wheelDia );ellipse(_car.x+_car.length,floorPos_y-_car.wheelDia/2,_car.wheelDia);}
varbus;varfloorPos_y;functionsetup(){createCanvas(1200,800);floorPos_y=600;bus={x:200,length:800,height:400,wheelDia:100,driver_pos_x:0.15,//relative proportion to the length of the buspassengers: [0.4,0.5,0.6]};}functiondraw(){background(102,179,255);noStroke();fill(130);rect(0,floorPos_y,width,height-floorPos_y);//draw the busstroke(0);fill(250,190,0);rect(bus.x,floorPos_y-bus.height,bus.length,bus.height-bus.wheelDia/2,200,25,25,25 );//the wheelsfill(100);ellipse(bus.x+bus.wheelDia,floorPos_y-bus.wheelDia/2,bus.wheelDia )ellipse(bus.x-bus.wheelDia+bus.length,floorPos_y-bus.wheelDia/2,bus.wheelDia )//the windowsfill(0,255,255);rect(bus.x+bus.length *0.075,floorPos_y-bus.height*0.9,bus.length *0.2,bus.height*0.4,150,10,10,10 );rect(bus.x+bus.length *0.3,floorPos_y-bus.height*0.9,bus.length *0.65,bus.height*0.4,10);varbasePos_y=floorPos_y-bus.height*0.9+bus.height*0.4;varcenterPos_x=bus.x+bus.driver_pos_x*bus.length;// translates the value in pixel valuedrawPerson(centerPos_x,basePos_y );for(vari=0;i<bus.passengers.length;i++){varcenterPos_x=bus.x+bus.passengers[i] *bus.length;drawPerson(centerPos_x,basePos_y);}}functiondrawPerson(center_pos_x,base_pos_y){fill(255,50,50);rect(center_pos_x-20,base_pos_y-80,70,80,10,10,0,0);fill(255,150,150);ellipse(center_pos_x,base_pos_y-80,70);fill(0);ellipse(center_pos_x-20,base_pos_y-80,5);}
Leave a Reply