how to create a game using javascript

Solutions on MaxInterview for how to create a game using javascript by the best coders in the world

showing results for - "how to create a game using javascript"
Astrid
21 Sep 2019
1<html>
2<head>
3<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
4<style>
5canvas {
6    border:1px solid #d3d3d3;
7    background-color: #f1f1f1;
8}
9</style>
10</head>
11<body onload="startGame()">
12<script>
13
14var myGamePiece;
15var myObstacles = [];
16var myScore;
17
18function startGame() {
19    myGamePiece = new component(30, 30, "red", 10, 120);
20    myGamePiece.gravity = 0.05;
21    myScore = new component("30px", "Consolas", "black", 280, 40, "text");
22    myGameArea.start();
23}
24
25var myGameArea = {
26    canvas : document.createElement("canvas"),
27    start : function() {
28        this.canvas.width = 480;
29        this.canvas.height = 270;
30        this.context = this.canvas.getContext("2d");
31        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
32        this.frameNo = 0;
33        this.interval = setInterval(updateGameArea, 20);
34        },
35    clear : function() {
36        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
37    }
38}
39
40function component(width, height, color, x, y, type) {
41    this.type = type;
42    this.score = 0;
43    this.width = width;
44    this.height = height;
45    this.speedX = 0;
46    this.speedY = 0;    
47    this.x = x;
48    this.y = y;
49    this.gravity = 0;
50    this.gravitySpeed = 0;
51    this.update = function() {
52        ctx = myGameArea.context;
53        if (this.type == "text") {
54            ctx.font = this.width + " " + this.height;
55            ctx.fillStyle = color;
56            ctx.fillText(this.text, this.x, this.y);
57        } else {
58            ctx.fillStyle = color;
59            ctx.fillRect(this.x, this.y, this.width, this.height);
60        }
61    }
62    this.newPos = function() {
63        this.gravitySpeed += this.gravity;
64        this.x += this.speedX;
65        this.y += this.speedY + this.gravitySpeed;
66        this.hitBottom();
67    }
68    this.hitBottom = function() {
69        var rockbottom = myGameArea.canvas.height - this.height;
70        if (this.y > rockbottom) {
71            this.y = rockbottom;
72            this.gravitySpeed = 0;
73        }
74    }
75    this.crashWith = function(otherobj) {
76        var myleft = this.x;
77        var myright = this.x + (this.width);
78        var mytop = this.y;
79        var mybottom = this.y + (this.height);
80        var otherleft = otherobj.x;
81        var otherright = otherobj.x + (otherobj.width);
82        var othertop = otherobj.y;
83        var otherbottom = otherobj.y + (otherobj.height);
84        var crash = true;
85        if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
86            crash = false;
87        }
88        return crash;
89    }
90}
91
92function updateGameArea() {
93    var x, height, gap, minHeight, maxHeight, minGap, maxGap;
94    for (i = 0; i < myObstacles.length; i += 1) {
95        if (myGamePiece.crashWith(myObstacles[i])) {
96            return;
97        } 
98    }
99    myGameArea.clear();
100    myGameArea.frameNo += 1;
101    if (myGameArea.frameNo == 1 || everyinterval(150)) {
102        x = myGameArea.canvas.width;
103        minHeight = 20;
104        maxHeight = 200;
105        height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
106        minGap = 50;
107        maxGap = 200;
108        gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
109        myObstacles.push(new component(10, height, "green", x, 0));
110        myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
111    }
112    for (i = 0; i < myObstacles.length; i += 1) {
113        myObstacles[i].x += -1;
114        myObstacles[i].update();
115    }
116    myScore.text="SCORE: " + myGameArea.frameNo;
117    myScore.update();
118    myGamePiece.newPos();
119    myGamePiece.update();
120}
121
122function everyinterval(n) {
123    if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
124    return false;
125}
126
127function accelerate(n) {
128    myGamePiece.gravity = n;
129}
130</script>
131<br>
132<button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
133<p>Use the ACCELERATE button to stay in the air</p>
134<p>How long can you stay alive?</p>
135</body>
136</html>
Mirko
02 Jun 2019
1//Javascript game template
2//Move player with arrow keys
3
4var canvas = document.createElement("canvas");
5canvas.width = 500;
6canvas.height = 500;
7document.body.appendChild(canvas);
8var ctx = canvas.getContext("2d");
9
10var player = {x: canvas.width / 2, y: canvas.height / 2, speed: 10};
11var keys = [];
12
13function update() {
14  ctx.clearRect(0, 0, canvas.width, canvas.height);
15  
16  ctx.beginPath();
17  ctx.fillStyle = "red";
18  ctx.fillRect(player.x, player.y, 50, 50);
19  
20  if (keys[37])
21    player.x -= player.speed;
22  if (keys[38])
23    player.y -= player.speed;
24  if (keys[39])
25    player.x += player.speed;
26  if (keys[40])
27    player.y += player.speed;
28  
29  requestAnimationFrame(update);
30}
31update();
32
33document.onkeydown = function(e) {
34  keys[e.keyCode] = true;
35}
36document.onkeyup = function(e) {
37  keys[e.keyCode] = false;
38}
queries leading to this page
html5 or javascript for gameshtml5 game tutoriallearn how to make games with html css and javascriptsample games javascriptbuild game in javascriptgame using javascript codecreate game in javascriptmaking a simple game on jsgame development in javascript 3fjavascript game tutorialsjavascript make gamemake a game jsdevelop game in javascriptbuild game with javascripthow to create game with javascriptjava scripts command gamehow to make game with jscreate a game with javascriptjavascript game htmlhow to make agame in javascrihow to code games in javascriptmaking games with jsmake javascript gamejs game exampleshtml css js gameshow to make games on javascriptcreating a game using javascripthow to code a game in javascri 5btbuild a simple javascript gamecode game in javascriptmake javascript gameshow to create a game in javasc ript java script gamegame javascriptjavascript building gamegame in javascript codew3schools html gamehow to code a game with jssimple js gameshow to create games in html js and csshow to make a game in html css and javascripthow to make games in html 5how to create a simple game in javascriptbuild simple game jshow to make a game using jsminigame javascriptmaking javascript gamesjavascript html gammeshow do i make a game in html cssjavascript gmehow to create game javascriptnew game javascriptbuild a basic js gamejava script making a gamejavascript game enginecreate a gamews in javascriptgames using html css javascriptmake game with jsputting a python game in javascripthow to make a 2d game in htmljs game examplemaking a game using jshow to create html gameshow to make a video game in html anjavascript making a game tutorialhow to make games in jshow to code a game on javascriptcoding with javascript gamemake a javascript gamecode a javascript gamecreate games with javascriptreally simple game html and csscoding a javascript gamecreate a video game app w3 schoolsgames created with javascriptjavascript game basicsmake game by jshow javascript gamegave development with htmljavascript html5 gamehow to make a javascript game with codecan i create games in javascriptsimple javascript gameshow to build a javascript gamemake a game in jsmaking a game in htmlhow to make a html gamehow to make html gamesjavacript gameshow to make a game in javascript codecreate html gamemake a game with javascripthtml and js gamesmaking a game using javascripthtml code to make a gamehow to code a game in html js csshow to create games with javascriptbuilding a simple game with javascriptmake javascript game tutorialmaking game in javascripthow to make a game using javascriptmake games using javascriptmaking games using javascripthow can i make a game with javascripthow to build a javascript game without htmlgame using javascriptjavascript game programmingsimple game using javascriptgame on javascriptfirst simple game coding in htmlmake games using html css and jscreating a javascript gamejavascript game development code referencehow to make game with javascripthow to make a gmae using htmlvideo game javascrip beginnerhow to code a javascript gamewrite a game in javascriptscratch game js libraryhow to make a web game with javascriptmake your own game with jsusing js to make a gamegames with javascripthow to make games on htmlbasic js gameeasy to make javascript gamsjavascript coding minigamehtml game programmingsimple game in jslingo game create with javascript tutorialhow to make a simple game in htmlbasic games javascripthtml games code to addmake game javascriptjavascript make gameshtml5 javascript gamemaking a game wid html only htmlgame jscodes for javascript to make an gamegames with jssimple game javascripteasy html game to createlingo game create with javascriptgame tutorial in javascriptjavascript game tutorial for beginnershow to code a js gamesrunner game with html css and javascript tutorialmini game htmlmake game with javascriptgame javascript tutorialjavascript build a web gamebuilding a game with javascripta simple game to create with javascripthow to make a basic game in jschreate a game with jswhat do i need to build a game with javascriptbuild games with javascriptmaking games in javascriptbuilding game in javascripthow to make a game with javascripthow to put a javascript game into my html websitebuild a simple game in javascripthow to create game using javascripthow to make a game htmljavascript gamehow to build a game in javascripthow to make a game in with htmlcreating javascript gamescreate ganes with javascripthow to make a good game by javascriptsimple js gamecan you make a game in javascriptjavascript game tutorialhow to make javascript gamehtml game basicseasiest javascript gamesbuild game javascriptdifferent ways to go about making a javascript gamehow to make a small game in javascriptcreate game with htmlinteractive game javascripthow to make a simple game with html and cssmaking a game in jsbuild a game javascripthowb to make a game in jsjavascript game with htmlbuild a javascript gamebuilding a game using javascripthow to make a game with javascript and html5 cssgames in javascriptmaking games with javascriptgame programming javascriptwriting a game in javascriptcreating a game in jsgame using html and cssgames javascript codecreate a game with javascript and htmlmaking a game with java scriptgame code in javascriptwho to program html canvas gameshow to make a game with jshow to make a browser game with javascripthow to make a asimple js gamehtml js how make gamesimple game code in javascripthow to creat a game in html onlyhow make a game with java scriptgame development in javascripthow make game javascriptmaking a game with jsjavascript game development tutorialsgame javascript whow to make a javascript gfamehow to create a game from javascriptcan we make games using javascriptlearn make game in javascripthow to develop game in jsgames in html5online game devolpment javascript htmlhow to make game in javascriptcoding a game in javascripthow to make a game javascripthow to create a simple game using jssimple javascript game examplehow to make a game with html and jsgame like tutorial for jsmake a javscript gamejavascript game program exemplemaking html gamesgame code in jsbuilding game javascriptsimple javascript gamehow to make a 2d game unitymake a js gamehtml gamehow to create a js and html gamehow to do javascript gamehow to make game on javascriptcreate simple web gamehow can i build game by jshtml gamge simplesimple game in javascriptusing javascript to make a gamegame using html and javsscriptjavascript code a gamejava for html code game onlineis making a javascript game easyhow to make a game on htmlcodes for html to make gamemake games with cssjs game designhow to make games with javascriptmaking a simple game in javascriptjavascript gamescreating game in jsjavascript making a gamejavascript create web gamehow to make a game in html5 and javascriptcodes for java script to make games build game using javascripthow do you create a video game i js 3fmaking game javascriptdesigning game with javascripthow to make a game in jshow to make a game from javascriptjs simple gamegames create javascriptgames for pc in html cssmaking game with javascripthtml code for gameshow to create a game in javascript with graphicswrite javascript gameshow to make a online game usinfg htmlcan i use javascript for making game 3fbuilding a javascript gamemake game in jscould you make a simple game with javascriptcan i make a game with javascriptjavascript how to make gamemake games with javascripthtml game css jscan you make a game with javascripthow to make a game in htmlmake a js browser gamehow to make a javascript gamemaking js gameshow to create a javascript gamehow to make game app on javascriptjavascript game 3fgames for javascripthtml5 game codegame in html5how to create a game with javascriptgame java scriptjavascript game code tutorialcan u make a game with jsmak a game using html 2c css and jsmaking game in html 27games in jscreate game with jscreate simple game with javascripthow to make agame in javascriptmakeing a javscript gamemake game in javascripthow to code a simple javascript gamejavascript webpage gamesimple games to make in javascriptcreate simple games with javascripthow to code a game in jsgame navigation html5html games projectjavascript tutorial game how to make games in javascripthow to make a game using htmlmaking a game using javascript for beginnerssimple html gamecreating game using javascripthtml game developmenthow to make a game in javascript for beginnershow to develop a game with jscreate a game using javascriptmaking games in jshtml css game detailgame using html css javascriptjavascript game examplehow to make games using javascriptgame made with javascriptbuild a game using javascriptjavascript game creationhow to create a game in javascriptdevelop a game in htmlcreate a game in html languagejavascript html gameslearn how to make a game with htmlhow to make a js gamehow to make game using javascriptmake a game with jsbasic game in jshow do you create a game in javascript 27game html jsvideo game javascriptmaking game in jsmini html js gamejavascript games tutorialshtml game windowhow to make a game using html5javascript videogamemake a game in javascripthow to make a game in html and javascriptcodes for java script to make an gamejavascript code for a gamejs html and css gamehow to build a video game in javascriptjava script gamedsadd game phpcreate a game in javascriptcreate javascript gamehow to make a 3d game in htmlhow do you create a video game injs 3flearn how to make games with javascriptcreate game javascript html5game making javascripthow to make an html gamecreate a game html and jscreate js gamemaking a javascript gamemake html gamesjavascript simple gamescan you make games using javascriptsimple game using html and csshtml css and javascript basic gamejs games game with javascriptsimple diy javascript gamethings to add to your js gamemake js game for browserbasic javascript game codegame with javascripthow to create a game using javascripthow to make a javascript game in htmlhow to make a game in javascript tutorialhow do you make a game with javascripthow to create bakamon game in javascriptgame program in javascriptmaking game with only javascripthtml game code funcode a game in javascripthow to add javascrpt game in htmljava script how to make a gamehow to make js browser gamehow to make javascript gamesjavascript code of a gamegame makig using jsjs game codehow to make a game in javascript for beginners with only codescreate a game with javascript quicklymaking a js gamemake javascript 2c html and css gamehow to code a js gamecreate game using java scriptbuilding games with javascriptjavascript make a gamehow to create a javascript game in websitesimple game using java scriptcan you make games with javascripthow to create game in javascriptmaking a game in csshtml gameshow to make a game only using jscreate interactive game in javascriptjava script game examplesgames in htmljavascript web game tutorialhow to make games with htmlhow to make games using js3how to make game in jssimple game in javascript source codehow to make a html gsme 5dhow to make js gamessimple web game htmlgames made on canvas tag in htmlhow to make a simple js gamejavasript gamemaking a game in javascriptmake games in javascriptmake a game using javascripthow to make a vr game in unityhow to make a n onlin html gamemake game javascript tutorialjavascript games tutorialmaking the easiest game in javascriptbuild a game in javascripthow to make a game with htmlmaking a game wid htmlmaking a game with javsacriptgame with jshtml 5 game tutorialbuild a game using htmlfirst javascript gamehow to create a game in jsgame jsgame creator html css javascriptcan you make game in javascripteasy javascript games to makejavascript simple game codehow to create games with htmllearn html gamejs simple game examplehow to make a simple game with javascriptjavascript game developmentmake a simple game using javascriptmake a simple game with javascriptmake a game with javascript and html5javascript game development tutorialbasic javascript gamejavascript game development with demolearning to create game with jsjavascript making games tutorialgame javascript codemake a game in js websitecreate js game examplehow javascript games makehtml5 canvas gamebest way to make a game in javascriptsimple javascript game tutorialhow to create a easy game in javascriptcreating a game in javascriptmake g html gamehow to add html gamesjs gamejavascript videogame projectshow to create games in javascriptweb games in javascripthow to create a game in htmlcreate a simple game javascripthow to create games using javascriptjavascript making a tag gamesimple games in jsjavascript game makinghow to codea js gamemaking a html game tutorial 1making a game with javascript codehow to put a game in htmlcreating a game with javascriptjavascript game codebasic game javascripthow to make games with only jshow to make a game in javascriptwhat is the best way to make a game in javascriptcreating a javascript game in the browsergame in htmlcreate game canvas html5javascript simple gamehow to code a game in javascript create game javascriptjavascript html css game examplebuilding a game using javascript and htmlwrite games in javascriptcreate a javascript gamehtml 5 game projectsimple html game codecode javascript gamehow to make a simple game in javascriptsimple javascript games for beginnersweb html gamehow to creat a game in htmlmaking a game with javascriptcreate game with javascriptjavascript how to make a gamewhat we need to make a game with javascripthow to code a game javascripthtml basic gamemake html gamecreate a new game jsmake js gamehow to make a make game in jseasy html gamehow to make a game with javascript and html5javascript as a gamehow to make a game jsmake a game javascriptjavascript basic game tutorialhow to make a basic game in javascripttrist javascript gamesgame using htmlwrite a bsic html gamebuild games with jshow make a game with javascripthtml building game codebuilding games in javascriptcreate games beginner javascriptsimple game with javascripthow to code a game with javascriptgame in javascriptjavascript game in jshow to create a game using javascript