how to create a game in html

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

showing results for - "how to create a game in html"
Jona
24 Sep 2020
1 <!-- A SIMPLE SNAKE GAME MADE BY HTML AND CSS -->
2
3<!DOCTYPE html>
4<html>
5<head>
6  <title></title>
7  <style>
8  html, body {
9    height: 100%;
10    margin: 0;
11  }
12
13  body {
14    background: black;
15    display: flex;
16    align-items: center;
17    justify-content: center;
18  }
19  canvas {
20    border: 1px solid white;
21  }
22  </style>
23</head>
24<body>
25<canvas width="400" height="400" id="game"></canvas>
26<script>
27var canvas = document.getElementById('game');
28var context = canvas.getContext('2d');
29
30var grid = 16;
31var count = 0;
32  
33var snake = {
34  x: 160,
35  y: 160,
36  
37  // snake velocity. moves one grid length every frame in either the x or y direction
38  dx: grid,
39  dy: 0,
40  
41  // keep track of all grids the snake body occupies
42  cells: [],
43  
44  // length of the snake. grows when eating an apple
45  maxCells: 4
46};
47var apple = {
48  x: 320,
49  y: 320
50};
51
52// get random whole numbers in a specific range
53// @see https://stackoverflow.com/a/1527820/2124254
54function getRandomInt(min, max) {
55  return Math.floor(Math.random() * (max - min)) + min;
56}
57
58// game loop
59function loop() {
60  requestAnimationFrame(loop);
61
62  // slow game loop to 15 fps instead of 60 (60/15 = 4)
63  if (++count < 4) {
64    return;
65  }
66
67  count = 0;
68  context.clearRect(0,0,canvas.width,canvas.height);
69
70  // move snake by it's velocity
71  snake.x += snake.dx;
72  snake.y += snake.dy;
73
74  // wrap snake position horizontally on edge of screen
75  if (snake.x < 0) {
76    snake.x = canvas.width - grid;
77  }
78  else if (snake.x >= canvas.width) {
79    snake.x = 0;
80  }
81  
82  // wrap snake position vertically on edge of screen
83  if (snake.y < 0) {
84    snake.y = canvas.height - grid;
85  }
86  else if (snake.y >= canvas.height) {
87    snake.y = 0;
88  }
89
90  // keep track of where snake has been. front of the array is always the head
91  snake.cells.unshift({x: snake.x, y: snake.y});
92
93  // remove cells as we move away from them
94  if (snake.cells.length > snake.maxCells) {
95    snake.cells.pop();
96  }
97
98  // draw apple
99  context.fillStyle = 'red';
100  context.fillRect(apple.x, apple.y, grid-1, grid-1);
101
102  // draw snake one cell at a time
103  context.fillStyle = 'green';
104  snake.cells.forEach(function(cell, index) {
105    
106    // drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
107    context.fillRect(cell.x, cell.y, grid-1, grid-1);  
108
109    // snake ate apple
110    if (cell.x === apple.x && cell.y === apple.y) {
111      snake.maxCells++;
112
113      // canvas is 400x400 which is 25x25 grids 
114      apple.x = getRandomInt(0, 25) * grid;
115      apple.y = getRandomInt(0, 25) * grid;
116    }
117
118    // check collision with all cells after this one (modified bubble sort)
119    for (var i = index + 1; i < snake.cells.length; i++) {
120      
121      // snake occupies same space as a body part. reset game
122      if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
123        snake.x = 160;
124        snake.y = 160;
125        snake.cells = [];
126        snake.maxCells = 4;
127        snake.dx = grid;
128        snake.dy = 0;
129
130        apple.x = getRandomInt(0, 25) * grid;
131        apple.y = getRandomInt(0, 25) * grid;
132      }
133    }
134  });
135}
136
137// listen to keyboard events to move the snake
138document.addEventListener('keydown', function(e) {
139  // prevent snake from backtracking on itself by checking that it's 
140  // not already moving on the same axis (pressing left while moving
141  // left won't do anything, and pressing right while moving left
142  // shouldn't let you collide with your own body)
143  
144  // left arrow key
145  if (e.which === 37 && snake.dx === 0) {
146    snake.dx = -grid;
147    snake.dy = 0;
148  }
149  // up arrow key
150  else if (e.which === 38 && snake.dy === 0) {
151    snake.dy = -grid;
152    snake.dx = 0;
153  }
154  // right arrow key
155  else if (e.which === 39 && snake.dx === 0) {
156    snake.dx = grid;
157    snake.dy = 0;
158  }
159  // down arrow key
160  else if (e.which === 40 && snake.dy === 0) {
161    snake.dy = grid;
162    snake.dx = 0;
163  }
164});
165
166// start the game
167requestAnimationFrame(loop);
168</script>
169</body>
170</html>
Tessa
09 Mar 2018
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>
Clayton
27 Nov 2017
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 tutorialcan you make a game in htmlhow to make a simple html gamelearn how to make games with html css and javascriptcreate a game in html 5sample games javascriptbuild game in javascriptjs 2c php game htmlcreate game in javascriptmaking a simple game on jsgame development in javascript 3fhtml and css gamemake a game in html game using javascript codejavascript 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 jsjava script game codecreate a game with javascripthtml make your own game how to make game with htmljavascript 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 javascriptgame with htmlhow to code a game in javascri 5btbuild a simple javascript gamecode game in javascriptmake javascript gamesw3schools game htmlhow to create a game using html and csshow to create a game in javasc ript java script gamecreate an html gamegame javascriptjavascript building gamegame in javascript codew3schools html gameghow to make games with htmlsimplest game in htmlhow to code a game with jssimple game html javascriptsimple 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 jscreate a simple html gameminigame javascriptmaking javascript gamesjavascript html gammeshow do i make a game in html cssjavascript gmehow to make js games with bootstraphow to create game javascriptgame development w3new game javascriptbuild a basic js gamejava script making a gamejavascript game enginegame made in htmlcan i make a game using htmlcreate a gamews in javascriptgames using html css javascriptmake game with jsputting a python game in javascripthow to make a 2d game in htmljs game exampleto make html gamemaking a game using jshow to create html gameshow to make a video game in html anjavascript making a game tutorialhow to make games in jssimple game in htmlhow to code a game on javascriptcoding with javascript gamemake a javascript gamehow to make an interactive game htmlcode a javascript gamecreate games with javascriptreally simple game html and csshow to make game using html and csscoding a javascript gamecreate a video game app w3 schoolsgames created with javascriptjavascript game basicsmake game by jshow to make a simple game with code html5how javascript gamegave development with htmljavascript html5 gamehow to make a javascript game with codecan i create games in javascripthow to create a html 5 gamesimple javascript gameshow to build a javascript gamemake a game in jsmaking a game in htmlmake a game with htmlhtml games w3how to make a html gamehow to make html gamesjavacript gameshow to make a game in javascript codecreate html gamehtml programs for gamesmake a game with javascripthtml and css gameshtml 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 javascriptgame cssmake 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 javascriptgames in java scriptmake games using html css and jsfirst simple game coding in htmlcreating a javascript gamejavascript game development code referencehow to make game with javascripthow to code a game in htmlhow to make a gmae using htmlvideo game javascrip beginnerhow to code a javascript gamewrite a game in javascriptmake html based gamescratch game js libraryhtml codes for gameshow 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 game examplehtml game makinghtml games code to addmake game javascriptmake game with htmljavascript 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 jsweb made in js html gmaechreate 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 make a game in with htmlcreate ganes with javascripthow to build a game in javascriptcreating javascript gameshow 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 htmlgame htmlgame in html source codemaking 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 jshtml gamejavascript game development tutorialsgame javascript whow to make a javascript gfamecreate game with html 5html code for gaming websitehow 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 javsscriptgame on htmljavascript 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 gamesimple game using htmlhow to make a game in html5 and javascriptcreate game in html codes 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 javascriptgame htmlcan you create a game using html cssgames for pc in html cssmaking game with javascripthtml code for gameshow to create a game in javascript with graphicscode a game htmlwrite 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 scriptcreate a game htmljavascript 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 javascriptcode a game in htmlhow 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 projecthtml game codejavascript 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 code a game in html5how to create an html game how to develop a game with jscreate a game using javascripthow you can to create game from html and cssmaking games in jshtml css game detailgame using html css javascriptjavascript game examplehow to make games using javascripthow to make a simple game with htmlgame development w3schoolsgame 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 27python snake game w3 schoolgame html jsvideo game javascriptmaking game in jshow to create game in htmlcoding a game in htmlmini html js gamejavascript games tutorialshtml game windowhow to make a game using html5javascript videogamemake game in htmlmake 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 gamecreate a game using html and jshow to build a video game in javascriptcanvas gamejava script gamedsadd game phpcreate a game in javascripthow to make a game in phpcreate javascript gamehow to make a 3d game in htmlstart game page html csshow to make game in htmlhow do you create a video game injs 3fhow to make a game in html applicationlearn how to make games with javascriptcreate game javascript html5game making javascripthow to make an html gamecan you make game using htmlcreate a game html and jscreate js gamemaking a javascript gamemake html gameswhat are the games using htmljavascript simple gamescan you make games using javascriptsimple game using html and csshtml css and javascript basic gamehow to make game using htmljs gamescan we create a game using html game with javascriptsimple diy javascript gamethings to add to your js gamemake js game for browserbasic javascript game codegame with javascripthow to make game with htmlhow 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 javascriptcan i make a game with htmlgame program in javascripthow to make game on htmlhow to include a game in htmlmaking 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 do i create a game using htmlhow to make javascript gamesjavascript code of a gamegame makig using jsjs game codehow to make a game in javascript for beginners with only codeshtml games w3schoolscreate 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 scriptbuild html gamescan 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 jshow to make a game with javascript and phpsimple game in javascript source codehow to make a html gsme 5dhow you kan to create game from html and csshow 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 tutorialbest way to do game in htmlcreate game with html and cssmaking the easiest game in javascriptbuild a game in javascriptis it possible to create a gamewith html and csshow to make a game with htmlhow to make simple html gamemaking a game wid htmlgame with jsfirst javascript gamebuild a game using htmlmaking a game with javsacripthtml 5 game tutorialhow to create a game in jsgame html codegame jsgame creator html css javascripthtml gaamescan you make game in javascriptflappy bird js w3schoolshow to code an html gameeasy javascript games to makegames with html codesjavascript simple game codejs gmaehow to make a game wiht htmlhow to create games with htmlsimple game for htmllearn html gamejs simple game examplegame code in htmlhow 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 codegaming htmlmake 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 gameshtml register gamejs gamejavascript videogame projectshow to create games in javascriptweb games in javascripthow to create a game in htmlimportanttags 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 gamecreate a game in htmlmaking a game with javascript codehow to put a game in htmlmaking a html game tutorial 1creating a game with javascriptjavascript game codebrowser based game website using html 2c css 2c javascript 2c bootstrapbasic 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 javascriptgames using html css and bootsrapcreate 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 javascripthtml how to make gamehow to code a game javascriptjavascript games codehtml basic gamehow to create a html 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 html5simple games using html and css with source codejavascript as a gamehow to make a game jsmake a game javascriptjavascript basic game tutorialhtml include gamestrist javascript gameshow to make a basic game in javascriptgame 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 in html