Today we will embark on an exciting adventure, where we will be making our very own snake game. You’ll learn how to work through a problem by breaking it down into smaller simpler steps. By the end of this journey, you will have learned some new things, and you’ll feel confident to explore more on your own.
Let’s begin by creating a file “snake.html” that will contain all our code.
Since this is an HTML file, the first thing we need is the <!DOCTYPE>declaration . In snake.html type the following:
<!DOCTYPE html> <html> <head> <title>Snake Game</title> </head> <body> Welcome to Snake! </body> </html>
Great, now go ahead and open
snake.html in your preferred browser. You should be able to see Welcome to Snake!
We are off to a good start
Creating the Canvas
To be able to create our game, we have to make use of HTML
Replace the welcome message in
snake.html with the following:
<canvas id="gameCanvas" width="300" height="300"><canvas>
The id is what identifies the canvas and should always be specified. We will use it to access the canvas later. The width and height are the dimensions of the canvas, and should also be specified. In this case, 300 x 300 pixels.
Your snake.html file should now look like this.
<!DOCTYPE html> <html> <head> <title>Snake Game</title> </head> <body> <canvas id="gameCanvas" width="300" height="300"></canvas> </body> </html>
If you refresh your browser page where you previously opened
snake.htmlyou will now see a blank page. This is because, by default, the canvas is empty and has no background. Lets fix that.
If you put the
_<script>_ tag before the
_<canvas>_ your code won’t work, as the HTML will not be loaded.
<script></script> tags. Update your code as below.
<!DOCTYPE html> <html> <head> <title>Snake Game</title> </head> <body> <canvas id="gameCanvas" width="300" height="300"></canvas> <script> /** CONSTANTS **/ const CANVAS_BORDER_COLOUR = 'black'; const CANVAS_BACKGROUND_COLOUR = "white"; // Get the canvas element var gameCanvas = document.getElementById("gameCanvas"); // Return a two dimensional drawing context var ctx = gameCanvas.getContext("2d"); // Select the colour to fill the canvas ctx.fillStyle = CANVAS_BACKGROUND_COLOUR; // Select the colour for the border of the canvas ctx.strokestyle = CANVAS_BORDER_COLOUR; // Draw a "filled" rectangle to cover the entire canvas ctx.fillRect(0, 0, gameCanvas.width, gameCanvas.height); // Draw a "border" around the entire canvas ctx.strokeRect(0, 0, gameCanvas.width, gameCanvas.height); </script> </body> </html>
First we get the canvas element using the id (gameCanvas) we specified earlier. We then get the canvas “2d” context, which means we will be drawing into 2D space.
Finally we draw a 300 x 300 white rectangle with a black border. This covers the entire canvas, starting from the top left corner (0, 0).
If you reload
snake.html in your browser, you should see a white box with a black border! Good job, we have a canvas that we can use to create our snake game! On to the next challenge!
how to create a Sidebar Menu using HTML and CSS only. Previously I have shared a Responsive Navigation Menu Bar using HTML & CSS only, now it’s time to create a Side Navigation Menu Bar that slides from the left or right side.
#sidebar menu using html css #side navigation menu html css #css side navigation menu bar #,pure css sidebar menu #side menu bar html css #side menu bar using html css
In this video we broadly cover the concepts of CSS Flex box and CSS Grid system and Some CSS Properties such as nth child selector, ::before & ::after much more.
Don't forget to join the channel for more videos like this. Code Savvy
0:00 - Intro
0:10 - Result
0:38 - Project Setup
01:35 – Reset HTML
02:21 – Left Container HTML
03:41 – Wrapper
14:58 – Bottom Shoe Nav
26:23 – Right Container HTML
33:10 – Product Size
35:49 – Reviews
41:11 – GSAP Animations
Hello Readers, welcome to my other blog, today in this blog I’m going to create a Responsive Footer by using HTML & CSS only. Earlier I have shared How to create a Responsive Navigation Menu and now it’s time to create a footer section.
As you can see on the image which is given on the webpage. There are various important topics there like About us, Our services and subscribes, some social media icons, and a contact section for easy connection. I want to tell you that it is fully responsive. Responsive means this program is fit in all screen devices like tablet, small screen laptop, or mobile devices.
#responsive footer html css template #footer design in html #simple footer html css code #simple responsive footer codepen #responsive footer code in html and css #responsive footer html css codepen