xxxxxxxxxx
212
//Creating variables for main sprites
var sword, swordImage, gameOver, gameOverImage, fruit1, fruit2, fruit3, fruit4, fruit1Image, fruit2Image, fruit3Image, fruit4Image, alien, rand, backgroundImage, bg, position;
//Creating variables for sprite groups
var fruitGroup, alienGroup, fruit;
//Creating variables for sound
var swoosh, game_Over;
//Creating variables for gamestae sprites
var gameState = 1;
var END = 0;
var PLAY = 1;
//Creating variable for score
var score;
function preload()
{
//Loading images for sprites
swordImage=loadImage("sword.png");
fruit1Image = loadImage("fruit1.png");
fruit2Image = loadImage("fruit2.png");
fruit3Image = loadImage("fruit3.png");
fruit4Image = loadImage("fruit4.png");
backgroundImage = loadImage("Bg.png");
gameOverImage = loadImage("gameover.png");
//Loading animation for sprites
alienAnima = loadAnimation("alien1.png", "alien2.png");
//Loading sounds for sprites
swoosh = loadSound("knifeSwooshSound.mp3");
game_Over = loadSound("gameover.mp3");
}
function setup()
{
//Creating canvas of 580 * 520
createCanvas(580, 520);
//Creating sprite for sword
sword = createSprite(40,200,20,20);
sword.addImage(swordImage);
sword.scale = 0.7;
score = 0;
//Creating sprite for game over
gameOver=createSprite(300, 300, 10, 10);
gameOver.addImage(gameOverImage);
gameOver.scale=1;
//Creating groups for fruit and enemy sprite
fruitGroup = new Group();
enemyGroup = new Group();
}
function draw()
{
//Adding bg color
background("lightblue");
text("SCORE : " +score, 500, 50);
//Creating function for fruits and enemies
fruits();
enemy();
if(gameState === PLAY)
{
//Adding if statement for cutting fruits
if(sword.isTouching(fruitGroup))
{
//Adding statement for destroying every fruit when touched by sword
fruitGroup.destroyEach();
//Adding sound effect
swoosh.play();
score = score+1;
}
//Adding if statement for touching alien
if(sword.isTouching(enemyGroup))
{
sword.x = 200;
sword.y = 200;
game_Over.play();
score = score-5;
gameState = END;
}
//Adding controls to the sword for player
sword.x=mouseX;
sword.y=mouseY;
gameOver.visible = false;
}
else if(gameState === END)
{
enemyGroup.setVelocityEach(0);
fruitGroup.setVelocityEach(0);
fruitGroup.destroyEach();
enemyGroup.destroyEach();
sword.x = 200;
sword.y = 150;
gameOver.visible = true;
}
drawSprites();
}
//Creating an additional function for fruits
function fruits()
{
//Adding statement for fruits to come after every 80 frames
if(frameCount%80 === 0)
{
position = Math.round(random(1, 2))
fruit = createSprite(400, 200, 20, 20)
fruit.scale = 0.2
//Adding rings collide feature to the fruits
//fruit.debug = true;
fruit.setCollider("circle", 0, 0, 100);
//Adding random no. for fruits to come
rand = Math.round(random(1, 4));
if(rand == 1)
{
fruit.addImage(fruit1Image);
}
else if(rand == 2)
{
fruit.addImage(fruit2Image);
}
else if(rand == 3)
{
fruit.addImage(fruit3Image);
}
else if(rand == 4)
{
fruit.addImage(fruit4Image);
}
//Adding extra important details for fruits
fruit.y = Math.round(random(40, 340));
fruit.velocityX = -(7+(score/4));
fruit.lifetime = 100;
fruitGroup.add(fruit);
if(position == 1)
{
fruit.x = 400;
fruit.velocityX = -(7+(score/4));
}
else
{
if(position == 2)
{
fruit.x = 0;
}
//Increasing the velocity of fruit after reaching 10 score
fruit.velocityX = (7+(score/10));
}
}
}
//Creating an additional function for aliens
function enemy()
{
//Adding statement for fruits to come after every 200 frames
if(frameCount%200 === 0)
{
//Creating sprite for alien
alien = createSprite(400, 200, 20, 20);
//Adding animation for alien
alien.addAnimation("moving", alienAnima);
//Adding random no. for aliens to come
alien.y = Math.round(random(100, 300));
alien.velocityX = -(7+(score/100));
alien.setLifetime = 50;
//Adding rings collide feature to the fruits
//alien.debug = true;
alien.setCollider("circle", 0, 0, 25);
enemyGroup.add(alien);
}
}