xxxxxxxxxx
115
var j=0; //vairable for movement in Xaxis
var Hp = 0; //height of plane
var gravity = 1; //gravity for plane
var bounce = 30; //bounce height for plane
var c; //color of plane
var moving = 2; //moving speed of the scene
function setup() {
//createCanvas(windowWidth, windowHeight);
createCanvas(800, 350);
noStroke();
frameRate(120);
}
function mousePressed() {
Hp -= bounce; // plane up when mousepressed
}
function draw(){
background(0,0,20);
Hp+=gravity; //gravity for plane
j += moving; // building move in the right direction
// the first layer of buildings
for (var m=-20; m<20; m++){
var x1 = j+m*100;
var buildingHeight1 = 10+noise(m*10)*height*0.8;
var buildingWidth1 = 6+noise(m+10)*width*0.2;
var numXwindow1 = round(noise(m)*14);
var numYwindow1 = round(noise(m+20)*8);
var buildingColor1 = color(noise(m)*60,noise(m+10)*50,noise(m+20)*120,230);
drawBuilding(x1,buildingWidth1,buildingHeight1,buildingColor1,numXwindow1,numYwindow1)
}
//the second layer of buildings
for (var n=-20; n<20; n++){
var x2 = j+n*100;
var buildingHeight2 = 10+noise(n*20)*height*0.6;
var buildingWidth2 = 6+noise(n+20)*width*0.3;
var numXwindow2 = round(noise(n+10)*14);
var numYwindow2 = round(noise(n+30)*8);
var buildingColor2 = color(noise(n+10)*255,noise(n+20)*255,noise(n+30)*255);
drawBuilding(x2,buildingWidth2,buildingHeight2,buildingColor2,numXwindow2,numYwindow2)
}
var col1 = get(width*0.5-14,Hp+21.75); //right bottom point of the plane
let r1=col1[0];
let g1=col1[1];
let b1=col1[2];
var col2 = get(width*0.5+14,Hp+21.75);//left botto point of the plane
let r2=col2[0];
let g2=col2[1];
let b2=col2[2];
if(r1==0 && g1==0 && b1==20 && r2==0 && g2==0 && b2==20){
c=color(150);
}else{
c=color(random(255),random(255),255);
Hp=Hp+5; //plane changes color and speeds down when hitting the building
}
//draw the plane
push();
translate(width*0.5,Hp);
rectMode(CENTER);
fill(c);
rect(-7,15,3,10);
rect(7,15,3,10);
rect(0,-20,3,10);
rect(0,20,28,3.5); //bottom of the plane
triangle(15,-6,15,6,40,0);
ellipse(0,0,40,30);
fill(235);
ellipse(0,-23,40,8);
rect(-8,-2,10,15,20);
pop();
push();
ellipseMode(CENTER);
translate(width*0.5+40,Hp);
rotate(frameCount*10); //make the tail rotate
ellipse(0,0,20,5);
ellipse(0,0,5,20);
pop();
}
function drawBuilding(x,buildingWidth,buildingHeight,buildingColor,numXwindow,numYwindow) {
push();
fill(buildingColor);
translate(x,0);
rect(0,height,buildingWidth,-buildingHeight);
drawWindow(numXwindow,numYwindow,buildingWidth,buildingHeight);
pop();
}
function drawWindow(numXwindow,numYwindow,buildingWidth,buildingHeight){
windowWidth = buildingWidth/(2*numXwindow+1) //width = step
windowHeight = buildingHeight/(2*numYwindow+1) //height = step
for(var i=1; i<=numXwindow; i++){
for(var j=1; j<=numYwindow; j++){
push();
x=(2*i-1)*windowWidth;
y=(2*j-1)*windowHeight;
let OnOff = round(noise(i+10)*0.7+noise(j)*0.6)*255 //window light on/off
fill(OnOff);
translate(x,-y);
rect(0,height,windowWidth,-windowHeight)
pop();
}
}
}