xxxxxxxxxx
86
var trex, trex_running, trex_collided;
var ground, invisibleGround, groundImage;
var cloud, cloudImage;
var y ;
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("cloud.png");
}
function setup() {
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;
}
function draw() {
//set background color
background(180);
//generate random numbers
y = Math.round(random(30,80))
console.log(y)
// jump when the space key is pressed
if(keyDown("space")&& trex.y >= 160) {
trex.velocityY = -10;
}
trex.velocityY = trex.velocityY + 0.8
if (ground.x < 0){
ground.x = ground.width/2;
}
//stop trex from falling down
trex.collide(invisibleGround);
trex.depth = trex.depth + 10;
//Spawn Clouds
spawnClouds()
drawSprites();
}
//function to spawn the clouds
function spawnClouds(){
// write your code here
if(frameCount % 80 == 0){
cloud = createSprite(700,y,400,20);
cloud.addImage("Cloud",cloudImage);
cloud.scale = 1.25;
cloud.velocityX = -2;
cloud.depth = trex.depth;
}
}