Sunday, August 22, 2021

Plane Adventure Game

Codes:

var plane = createSprite(50,200);

plane.setAnimation('<Animation Name>');

plane.scale = 0.7;

plane.visible = false;


var coinGroup = createGroup();

var cloudGroup = createGroup();


var gameOver = createSprite(200,150);

gameOver.setAnimation('<Animation Name>');

gameOver.visible = false;


var getReady = createSprite(200,200);

getReady.setAnimation("<Animation Name>");

getReady.visible = false;



var gameState = 'start';


var  score = 0;

var hiScore = 0;


function Movement(){

  

  plane.velocityX = 0;

  plane.velocityY = 0;

  

  if(keyDown("up")){

    plane.velocityY = -10

  }

  

  if(keyDown("down")){

    plane.velocityY = 10

  }

  

  if(keyDown("RIGHT_ARROW")){

    plane.velocityX = 10

  }

  

  if(keyDown("LEFT_ARROW")){

    plane.velocityX = -10

  }

}


function SpawnCoins(){

  if(World.frameCount%80 === 0){

    var rand = random(10,390)

    var coin = createSprite(410,rand);

    coin.setAnimation("<Animation Name>");

    coin.velocityX = -5;

    coin.scale = 0.4;

     coin.lifetime = 100;

     

     coinGroup.add(coin)

  }

}


function SpawnClouds(){

  if(World.frameCount%110 === 0){

    var rand = random(10,390)

    var cloud = createSprite(410,rand);

    cloud.setAnimation("<Animation Name>");

    cloud.velocityX = -5;

    cloud.scale = 0.2;

    cloud.lifetime = 100;

    cloudGroup.add(cloud);

  }

}


function Score(){

  

  if(plane.isTouching(coinGroup)){

    score++;

    coinGroup.destroyEach();

  }

}


function HiScore(){

  if(score>hiScore){

    hiScore = score

  }

}


function GameOver(){

  

  if(plane.isTouching(cloudGroup)){

    coinGroup.destroyEach();

    cloudGroup.destroyEach();

    gameState = 'over'

  }

}


function draw() {

  

  background('cyan');

  

  drawSprites();

  

  createEdgeSprites();

  

  plane.collide(edges);

  

  if(gameState === 'start'){

    getReady.visible = true;

    gameOver.visible = false;

    plane.visible = false;


    if(World.frameCount%50 === 0){

      gameState = 'play'

    }

  }

  

  if(gameState === 'play'){

    

  getReady.visible = false;

  plane.visible = true;

    

  Movement();

  SpawnCoins();

  SpawnClouds();

  Score();

  HiScore();

  GameOver();

  

  fill("blue");

  textSize(20)

  text('Score: '+score,300,30)

  }

  

  if(gameState === 'over'){

    plane.visible = false;

    gameOver.visible = true;

    

    fill("blue");

  textSize(30)

  text('High Score: '+hiScore,100,250)

    text('Score: '+score,140,300)

  text('Press ENTER to restart',50,350)

    

    if(keyDown('enter')){

      gameState = 'start';

      plane.x = 50;

      plane.y = 200;

      score = 0;

    }

  }

}


Previous Post
Next Post

post written by:

0 Comments: