xxxxxxxxxx
99
var trex, trex_running, trex_collided;
var ground, invisibleGround, groundImage;
var cloudImage;
var score;
function preload(){
trex_running = loadAnimation("trex1.png","trex2.png","trex3.png");
trex_collided = loadImage("trex_collided.png");
groundImage = loadImage("ground2.png");
cloudImage = loadImage("Cloud3.png");
}
function setup() {
background(220)
createCanvas(600,200)
//create a trex sprite
trex = createSprite(50,160,20,50);
trex.addAnimation("running", trex_running);
trex.scale = 0.5;
//create a ground sprite
ground = createSprite(200,180,400,20);
ground.addImage("ground",groundImage);
ground.x = ground.width /2;
ground.velocityX = -4;
//creating invisible ground
invisibleGround = createSprite(200,190,400,10);
invisibleGround.visible = false;
//generate random numbers
var rand = Math.round(random(1,100))
console.log(rand)
}
function draw() {
//set background color
background(220);
//console.log(trex.y)
// jump when the space key is pressed
if(keyDown("space")&& trex.y >= 100) {
trex.velocityY = -10;
}
trex.velocityY = trex.velocityY + 0.5
if (ground.x < 0){
ground.x = ground.width/2;
}
//stop trex from falling down
trex.collide(invisibleGround);
//Spawn Clouds
spawnClouds()
//console.log(frameCount);
drawSprites();
}
//function to spawn the clouds
function spawnClouds()
{
// write your code here
if(frameCount % 60 === 0)
{
var cloud = createSprite(600, 70, 40, 20);
cloud.velocityX = -4;
cloud.addImage(cloudImage);
cloud.scale = 0.2;
cloud.depth = trex.depth;
trex.depth = trex.depth+1;
console.log(cloud.depth);
}
console.log(trex.depth);
}