Fruit Slicer Game Using HTML CSS And JavaScript

Introduction
Hey coders, welcome to another new blog. Today in this blog we’ve created a fruit slicer game using HTML CSS and JavaScript. A fruit slicer game is interactive and user engaging game and I believe that developing these type of games surely enhances our coding skills and helps us in UI skills as well.
HTML code sets up the basic structure of our game while CSS adds interactivity to our game. Using CSS we have styled our fruit slicer game. JavaScript controls the functionality of our fruit slicer game.
In this blog you’ll learn that how to create this fruit slicer game. HTML and CSS builds the UI of our game and some interactivity but the functionality of our game like score count and reset game buttons, are all controlled by our JavaScript code. To create this type of game all you need is the knowledge of HTML CSS and JavaScript.
Let’s see the code.
HTML (index.html)
Get Discount on Top Educational Courses
This is our HTML code which sets up the basic structure of our fruit game slicer game. HTML code is use for any webpage to build their basic structure. Using the below code we’ve developed our game’s basic structure.
- <!DOCTYPE html>: This defines the document type of our code file and ensure that our file is a HTML document.
- <title>: sets the title of our webpage as “Fruit Slicer Game.”
- <link>: These tag links external CSS file and jQuery UI CSS file for styling our webpage.
- <body>: This contains the main content of our webpage.
- <div id=”container”>: Acts as a container for the entire content of the page.
- <div id=”instructions”>: Displays the instructions for the game – “Slice Fruits.”
- <div id=”fruitcontainer”>: It contains elements related to the game like scores, left chances, game introduction, and the image of a fruit.
- <div id=”score”>: This section displays player score.
- <div class=”scoreDisplay”>: This contains an image and the score value.
- <div id=”trialsleft”>: This shows how many chances are left.
- <div id=”front”>: Contains a game introduction message and an image of a fruit logo.
- <div id=”startReset”>: Displays a button to start or reset the game.
- <div id=”gameOver”>: This is for the gameover condition.
- <audio id=”slicesound”>: This contains 3 audio files with some id and classes.
- <script>: This links external JavaScript files and links which will maintain the functionality of our game.
CodeWithCurious.com - Fruit Slicer Game
Slice Fruits
0
Are you ready for the ultimate slice action ?
Start Game
CSS (Style.css)
This is our CSS code which styles our fruit slicer game. This code adds interactivity to our webpage.
- First it sets the entire html document. The html element takes full height of 100%, background properties and font-family property.
- Next, we styled the #container by giving some height and width. #container also take margin, padding, border-radius and a position:relative as well.
- #front sets font size, color, height, width, and some padding. It hides the elements initially and z-index sets as 2.
- #front img takes a width of 280px.
- #instruction sets width 450px and height 50px. It also takes margin, font-size and line height properties. A border radius of 20px, box shadow, background color and color property are also given to the #instruction element.
- #fruitcontainer sets a background image with background size as cover. It sets height, width, margin and padding as well. Background color of this section set as black and text color as white. Text align center with overflow hidden and some box shadow is given to the #fruitcontainer.
- .fruit class sets display as none and position absolute. The #score also sets it display as none.
- .scoreDisplay class sets z index 1 and sets display: flex. A transparent background, position, font size and justify items are set to this section.
- #score img takes width, align items and padding property.
- #trailsleft sets display flex, position absolute, z-index 1,margin, left and a transparent background color.
- Sets positioning, width, padding, margin, cursor style, text alignment, background color, box shadow, border radius, and transition for an element with ID “startReset”.
- #startReset adjust styling for the active state of the element.
- At last we styled the #gameOver. #gameOver Sets box sizing, width, height, background, text color, text transformation to uppercase, text alignment, font size, positioning, and z-index.
html {
height: 100%;
background: radial-gradient(circle, #fff, rgb(189, 182, 182));
background-image: url(https://raw.githubusercontent.com/Saumya-07/Fruit-Slicer/master/images/bg3%20-%20Copy.jpg);
background-size: cover;
font-family: cursive, sans-serif;
}
#container {
width: 750px;
height: 600px;
margin: 10px auto;
padding: 20px;
border-radius: 10px;
position: relative;
}
#front {
font-size: 40px;
color: #d3901d;
width: 650px;
height: 450px;
padding: 10px;
margin: 30px auto 20px auto;
z-index: 2;
display: none;
}
#front img {
width: 280px;
}
#instructions {
width: 450px;
height: 50px;
margin: 10px auto;
font-size: 30px;
background-color: #d3901d;
color: #2e1d11;
text-align: center;
line-height: 50px;
border-radius: 20px;
box-shadow: 0px 4px 0px 0px #775012;
}
#fruitcontainer {
background: url(https://raw.githubusercontent.com/Saumya-07/Fruit-Slicer/master/images/wood-bg2.jpg);
background-size: cover;
width: 650px;
height: 450px;
padding: 10px;
margin: 30px auto 20px auto;
background-color: white;
color: black;
text-align: center;
font-family: cursive, sans-serif;
overflow: hidden;
border-radius: 20px;
box-shadow: 0px 4px 0px 0px #4b4b4e;
position: relative;
}
.fruit {
display: none;
position: absolute;
}
#score {
display: none;
}
.scoreDisplay {
z-index: 1;
display: flex;
background-color: transparent;
color: #888e61;
position: absolute;
font-size: 30px;
justify-items: center;
}
#score img {
width: 40px;
align-items: center;
padding-right: 5px;
}
#trialsleft {
margin-top: 7px;
display: flex;
position: absolute;
left: 550px;
background-color: transparent;
z-index: 1;
}
.life {
width: 30px;
padding-right: 5px;
}
#startReset {
position: relative;
width: 90px;
padding: 10px;
margin: 0 auto;
cursor: pointer;
text-align: center;
background-color: #8d8315;
box-shadow: 0px 4px 0px 0px #5c5619;
border-radius: 5px;
transition: all 0.2s;
}
#startReset:active {
background-color: #69620c;
box-shadow: 0px 0px #5c5619;
top: 4px;
color: white;
}
#gameOver {
box-sizing: border-box;
width: 500px;
height: 300px;
background: transparent;
color: #d3901d;
text-transform: upperCase;
text-align: center;
font-size: 3em;
position: absolute;
top: 170px;
left: 150px;
z-index: 2;
}
Javascript (Script.js)
This is our JavaScript code which adds functionality to our fruit slicer game. This code adds logic in our game and make it user interactive. In this code we’ve define some functions which will add the logic and will add the functionality to our fruit slice game.
- First we accessed all the essential html element using DOM and stored them in some variables. These variable are that we will use later in our code.
- Next, when the game start or reset button is clicked, if game is already playing it will reload the page to restart the game. Else it will set the score to zero and resets all the initial game conditions.
- In the fruit slicing conditions, when the mouse hover over a fruit using mouseover event it increases scores, plays sound and a new fruit will come after some delay.
- Next, the addhearts() function displays the remaining chances of user.
- The startAction() function generates a random fruit and position it at top. Then it moves down in steps slowly. If fruit goes out of the frame a chance will be decreased.
- The chooseRandom() function randomly generates a fruit from the fruits array.
- At last stopAction() function stops the fruit movement and hide it when game is
This is how our JavaScript code works.
var playing = false;
var score;
var trialsleft;
var step; //for random steps
var action; //for settime interval
var fruits = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; //for fruits
$(function () {
//click on start or reset button
$("#front").show();
$("#startReset").click(function () {
if (playing == true) {
//if we are playing
location.reload(); //reload page
} else {
//if we are not playing from before
$("#front").hide();
$("#score").show();
playing = true;
//set score to 0
score = 0;
$("#scoreValue").html(score);
//show trials left box
$("#trialsleft").show();
trialsleft = 3;
addhearts();
//hide game over box
$("#gameOver").hide();
//change button to reset game
$("#startReset").html("Reset Game");
//start action
startAction();
}
});
//slice a fruit
$("#fruit1").mouseover(function () {
score++; // increase score
$("#scoreValue").html(score);
//play sound
$("#slicesound")[0].play();
//stop fruit
clearInterval(action);
//hide fruit
$("#fruit1").hide("explode", 500); //slice fruit
//send new fruit
setTimeout(startAction, 500);
});
//functions
//addhearts
function addhearts() {
$("#trialsleft").empty();
for (i = 0; i < trialsleft; i++) {
$("#trialsleft").append(
'
'
);
}
}
//start action
function startAction() {
//generate random fruit
$("#fruit1").show();
//choose random fruit
chooseRandom();
//random position
$("#fruit1").css({
left: Math.round(550 * Math.random()),
top: -50,
});
//generate random step
step = 1 + Math.round(5 * Math.random()); //change steps
//descend fruits down by 10ms
action = setInterval(function () {
//move fruit by one step
$("#fruit1").css("top", $("#fruit1").position().top + step);
//check if the fruit is too low
if ($("#fruit1").position().top > $("#fruitcontainer").height() - 50) {
//yes it is low
// check trails left
if (trialsleft > 1) {
//generate a fruit
$("#fruit1").show();
//choose random fruit
chooseRandom();
//random position
$("#fruit1").css({
left: Math.round(550 * Math.random()),
top: -50,
});
//generate random step
step = 1 + Math.round(5 * Math.random()); //change steps
//reduce trials by one
trialsleft--;
//populate trails left box by one
addhearts();
} else {
//game over
playing = false; //we are ot playing any more
$("#score").hide();
$("#startreset").html("Start Game");
$("#gameOver").show();
$("#gameOver").html(
"Game Over!
Your score is " + score + "
"
);
$("#trialsleft").hide();
stopAction(); //stops Action
}
}
}, 10);
}
//choose random fruits
function chooseRandom() {
$("#fruit1").attr(
"src",
"https://raw.githubusercontent.com/Saumya-07/Fruit-Slicer/master/images/" +
fruits[Math.round(9 * Math.random())] +
".png"
);
}
// Stop Action
function stopAction() {
clearInterval(action);
$("#fruit1").hide();
}
});
Output:

Disclaimer: The code provided in this post is sourced from GitHub and is distributed under the MIT License. All credits for the original code go to the respective owner. We have shared this code for educational purposes only. Please refer to the original repository for detailed information and any additional usage rights or restrictions.
electronics Store website using html CSS and JavaScript Introduction Hello coders, welcome to another new project. As you know our today’s project …
Digital Marketing Agency website Using HTML, CSS and JavaScript Introduction Hello coders, welcome to another new blog. Today in this project we’ll …
Fruit Slicer Game Using HTML CSS And JavaScript Introduction Hey coders, welcome to another new blog. Today in this blog we’ve created …
Event Organizers Website Using Html Css And JavaScript Introduction Hello coders, welcome to another new blog. As you know that our today’s …