Sunday, August 22, 2021

Fruit Ninja Game




Codes: 

//Creating Background
var bg = createSprite(200,200);
bg.setAnimation('<Animation Name>');

//Creating Sword
var sword = createSprite(200,200);
sword.setAnimation('<Animation Name>');
sword.scale = 0.2;
sword.visible = false;

//Creating Fruit and Bomb Groups
var fruitGrp = createGroup();
var bombGrp = createGroup();

//Creating Game State
var gameState = 'start';

//Creating Score
var score = 0;

//Creating Ready Logo
var ready = createSprite(200,200);
ready.setAnimation('<Animation Name>')

//Creating Over Logo
var over = createSprite(200,175);
over.setAnimation('<Animation Name>');
over.visible =false;

//Creating Fruits Spawn Function
function Fruits(){
  
 if(frameCount%80===0){
   var fruit = createSprite(random(5,395),-10);
   var r = Math.round(random(1,4));
   fruit.setAnimation('<Animation Name>');
   fruit.scale= 0.13;
   fruit.velocityY = (10+(score/25));
   fruit.lifetime = 50;
   fruitGrp.add(fruit);
   
 }
}

//Creating Bomb Spawn Function
function Bomb(){
  
 if(frameCount%110===0){
   var bomb = createSprite(random(5,395),-10);
   var r = Math.round(random(1,4));
   bomb.setAnimation('<Animation Name>');
   bomb.scale= 0.13;
   bomb.velocityY = (10+(score/35));
   bomb.lifetime = 50;
   bombGrp.add(bomb);
   
 }
}

//Creating Scoring Function
function Score(){
  
  if(sword.isTouching(fruitGrp)){
    
    playSound("sound://category_swish/deep_swish_1.mp3")
    fruitGrp.destroyEach();
    score = score + 5;
  }
}

//Creating Game Over Function
function Over(){
  
  if(sword.isTouching(bombGrp)){
    playSound("sound://category_music/8bit_game_over_1.mp3")
    fruitGrp.destroyEach();
    bombGrp.destroyEach();
    gameState='over'
  }
}


function draw() {
  
//Displaying Sprites
  drawSprites();
  
  if(gameState==='start'){
    
    over.visible =false;
    ready.visible =true;
    
    if(frameCount%50 === 0){
      gameState='play'
     
    }
    
  }
  
  if(gameState==='play'){
    
    sword.visible = true;
    ready.visible = false;
    
//Sword Movement with Mouse
    sword.x = World.mouseX;
    sword.y = World.mouseY;
    
//Calling functions
    Fruits();
    Bomb();
    Score();
    Over();
    
    fill('white');
    textSize(20)
    text("Score: "+score,300,30);
  }
  
  if(gameState==='over'){
    
    sword.visible = false;
    over.visible =true;
    
    
    fill('white');
    textSize(30)
    text('Total Score: '+score,100,250);
    text('Press ENTER to restart',45,300);
    
    if(keyDown('enter')){
      
      gameState = 'start';
      score = 0;
      fruitGrp.destroyEach();
      bombGrp.destroyEach();
    }
    
  }
}
  
 

Previous Post
Next Post

post written by:

0 Comments: