Now we are ready to start the project.
Start the Project:
Now to start the project open VS Code and Open a new folder using
File > Open Folder
Now Create a new index.html file which will like this.
Now we will create a div for our board in the body element.
Now lets create and the CSS stylesheet (style.css) and script.js file and include them.
Now our initial steps are almost over. Now lets style the html and write the logic.
We will add a logic later to play/pause the game in main.js file
Style the index.html using CSS:
Create a
style.css file in your project root folder and copy and paste the fillowing css code to your style.css file.
Here we have just given some styles to improve our ui.
Style for snake and food are also written which we will use later on.
Writing the logic of Snake Game in Javascript:
So to write the logic for our Snake Game please create a main.js file in your project level root folder. Now lets start to write our logic.
First we declare the variables we use in our project. To play the game engine we will use the
requestAnimationFrame function as it is more optimized for animating web page.
Here we have declared
Snake Body using a snakeArray variable, position of food using a
food object and default moving direction using
movingDirection. the
lastPaintTime is the variable to store the last time when the screen was painted. RequestId is id of requested frame, which we will use to start/stop the game.
a , b are the number of grids in the board.
speed here is our desired speed.
Now its time to setting up the grids in the board.
Now lets write the logic to controll the framerate of the game. In this case we will use the recursive
requestAnimationFrame function to get our desired frame rate
now we will create an
gameEngine() function to write the logic behind the gameplay.
Now before writing the gameEngine() function we need some functions to create which will assist us for gameplay.
1st, let's create the
drawSnakeAndFood() function which will help us to draw the snake and food for us.
Always remember that in grid in CSS the x axis from left to right (columns) and y axis is from top to bottom (rows). While controlling the snake movement this information is needed to design the controlling system.
2nd, lets create
snakeHaveEatenFood() function which will handle the event when the snake will eat the food.
The function will be run when the snake will eat the food
Explanation: The logic behind this is when the
Head of Snake and
Food have same coordinates that means the snake have eaten the food and then the length of the snake will be increased by one and food position will be changed using the
generateRandom(max , min) function which will generate a random number between the max and min number (i.e. - we want to generatea coordinte value between a and b)
But... where is generateRandom(max , min) function ?!!!
let's create it.
Now we are ready to code our
gameEngine() function 6 parts.
1. We will check if the Snake Head has walked on it Body. If so we will end the game and start a new one.
2. We will check if the Snake eat the food.
3. In this part, we will change the coordinates of the snake's body parts (except Head) so that the snake can move
4. We will move the snakes Head position according to the instructions from the Controls.
5. Here we write the logic to make the snake wrap when crossing the wall.
6. Finally, we draw the snake using drawSnakeAndFood() function.
Now lets code:
We are almost done. Now its time to create game controls.
Now we are done with our project. Now open your LiveSever by RIght Click and Go Live to view your project in your browser
Note: the playPauseGame(e) function takes the input e and play and pause the game accordingly using requestId using the requestAnimationFrame() function (to start the game) and cancelAnimationFrame() function (to stop the game). Now our project is over.
Finalize:
Now your project files will look like:
You can view a demo of the snake game
here .
You can also download the source code from
github.
Excercise :
1. Add a scoreboard to the game and display the current score and the highest score.
2. Write a logic to prevent the food to be placed on snaked body.
Follow US:
You can now explore more project from us on
github.
Comments
Post a Comment