Opera News

Opera News App

How To Create Web Application game Using HTML5 and Javascript

Emose
By Emose | self meida writer
Published 7 months ago - 1 views

How To Create a Web Application Game using HTLM5 and JAVASCRIPT

Today, I will be creating a simple HTML game with pure JAVASCRIPT.

If you are a beginner to HTML and JAVASCRIPT, you can hook up with W3school. They provide unique tutorials for beginners.

Firstly, download a text editor from google. There are numerous text editor on google like Netbean, Notepad++, sublime etc. But for today's tutorial I will be using Notepad++.


Launch the Notepad++ software, after that create a new file and save the extension name as Rect.html, note the extension name must be .html.

In this tutorial, I will create a moving character with a keyboard press.

Next, we proceed with our code


<!Doctype>

<Html>

<head>

<title>Simple HTML Rectangle game<\title>

<\head>

<body>

<canvas id="game" style="border:2px solid black"><\canvas>

<\body>

<\Html>


Next, we create a new file name rect.js. this is a JAVASCRIPT extension file

Code:

var canvas =document.getElementById("game");

var ctx = canvas.getContext('2d');


canvas.width=300;

canvas.height=300;


//load the player image and the background image from your directory

var playerImage = new image();

playerImage.src="image/player.png";


var bg = new image();

bg.src= "image/tiles.png";


Function drawBg(){

ctx.beginPath();

ctx.drawImage(bg,0,0,300,300);

ctx.closePath();

}


//create a literal variable which will hold the players properties


var player = {x:100, y:100, img:playerImage,

drawPlayer:function(){

ctx.beginPath();

ctx.drawImage(this.img, this.x, this.y);

ctx.close();

}

}


var Frame = setInterval(draw, 100);


window.onkeydown =keypress;


Function keypress(e){

if(e.keyCode==37){

player.x -=5;

}

if(e.KeyCode ==38){

player.y -=5;

}

if(e.keyCode ==39){

player.x+=5;

}

if(e.keyCode ==40){

player.y+=5;

}

}

//we draw the player character


Function draw(){

//we clear the canvas

ctx.clearRect(0,0,canvas.width canvas.height)

drawBg();

player.drawPlayer();

}

Now our player can move around the canvas with a keyboard being pressed...

You have just created you first simple web application game.I hope you like the tutorial

Content created and supplied by: Emose (via Opera News )

Opera News is a free to use platform and the views and opinions expressed herein are solely those of the author and do not represent, reflect or express the views of Opera News. Any/all written content and images displayed are provided by the blogger/author, appear herein as submitted by the blogger/author and are unedited by Opera News. Opera News does not consent to nor does it condone the posting of any content that violates the rights (including the copyrights) of any third party, nor content that may malign, inter alia, any religion, ethnic group, organization, gender, company, or individual. Opera News furthermore does not condone the use of our platform for the purposes encouraging/endorsing hate speech, violation of human rights and/or utterances of a defamatory nature. If the content contained herein violates any of your rights, including those of copyright, and/or violates any the above mentioned factors, you are requested to immediately notify us using via the following email address operanews-external(at)opera.com and/or report the article using the available reporting functionality built into our Platform

YOU MAY LIKE

Check Out Essential, Stylish And Chic Accessories Every Fashionable Lady Should Have In Her Wardrobe

2 hours ago

28 🔥

Check Out Essential, Stylish And Chic Accessories Every Fashionable Lady Should Have In Her Wardrobe

Love To Be On Trend? Check Out 2021 Trendy Accessories Every Stylish Girl Should Have.

3 hours ago

17 🔥

Love To Be On Trend? Check Out 2021 Trendy Accessories Every Stylish Girl Should Have.

Latest 2021 Ponytail Hairstyles For Beautiful Black Women.

3 hours ago

110 🔥

Latest 2021 Ponytail Hairstyles For Beautiful Black Women.

Olamide, Tekno, Niniola, Simi and Others Out With New Music (Check Out The Full List)

3 hours ago

15 🔥

Olamide, Tekno, Niniola, Simi and Others Out With New Music (Check Out The Full List)

Keep Yourself Entertained With These 40+ Funny Nigerian Memes

3 hours ago

3 🔥

Keep Yourself Entertained With These 40+ Funny Nigerian Memes

Eat these foods to boost your sperm count as a man

3 hours ago

0 🔥

Eat these foods to boost your sperm count as a man

"I Can't Stop Laughing"- Check Out These Memes And Pictures That Will Make You Laugh.

3 hours ago

8 🔥

Check Out These Beautiful Pictures Of Hausa Women With Stunning Curves

4 hours ago

84 🔥

Check Out These Beautiful Pictures Of Hausa Women With Stunning Curves

Check out beautiful photos of actress Nancy Isime rocking braids and wigs, not her usual blonde hair

4 hours ago

39 🔥

Check out beautiful photos of actress Nancy Isime rocking braids and wigs, not her usual blonde hair

Top Beautiful Influencers That are Virtual Influencers And Why They Are Not Robots!

4 hours ago

0 🔥

Top Beautiful Influencers That are Virtual Influencers And Why They Are Not Robots!

COMMENTS