game html

Solutions on MaxInterview for game html by the best coders in the world

showing results for - "game html"
Lennard
23 May 2019
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>
María
13 Jun 2018
1(function)game=tag
2(function)=character=109
3(function)=vehicles=200
Fabio
29 Jun 2018
1<div>
2<script src="https://cdn.htmlgames.com/embed.js?game=MayaPyramidSolitaire&amp;bgcolor=white"></script>
3</div>
queries leading to this page
html gamge simplehow to create a html gamegame html w3schoolhtml5 or javascript for gamesjavascript how to make a gamehow to make html gameshtml game codemake games using html css and jsjava script how to make a gamehtml make your own gamehow to make games using js3html games w3schoolsmake javascript 2c html and css gamejavascript html5 gamehow to make a game in with htmljavascript games codehtml game programmingmini game htmlmake a game with javascripthtml games w3how do i create a game using htmlbuild a game using htmlsimplest game in htmlgame by htmlgame development w3simple game for htmlimportanttags to create a game in htmlis it possible to create a gamewith html and cssmake html based gamebrowser based game website using html 2c css 2c javascript 2c bootstrapsimple game html javascriptgame using html and javsscripthow you can to create game from html and csssimple html game codejava script game codejavascript game with htmlgame example in javascriptjava script making a gamehow to make a game in phpcan you make a game in htmlhtml programs for gameshow to make a simple game with code html5html gameghow to make games with htmlhow to create a game using htmlmake a game in javascripthtml game makinghow to make games on htmlhow to code a game in html5java script gamejavascript gmemake game in htmlhow to create an html game html game developmentcreate an html gamehtml basic gamecreate a simple html gamegame on html2d game in html and csscreate a game using html and jshtml 5 gamesimple game using html and csspython snake game w3 schoolgame code in htmlhow to create a game in htmlcreate game in html javascript gamehow to make game with htmljavascript game write a bsic html gamegame making javascriptlearn how to make games with html css and javascriptgaming htmlgame in html source codecanvas gamehow to create a js and html gamehow to make a game using htmlhow to make a game in html and javascriptsimple web game htmlbest way to do game in htmlhow to make a game using html5simple html gamegame htmljs gamebasic javascript game codegame jsw3schools game htmlhtml game examplegames using html css and bootsraphtml game w3schoolsflappy bird js w3schoolsjavascript html gameshtml and css gamehow to creat a game in htmlgame with javascriptcreate a game html and jsgame using htmlhow to make a game wiht htmlhow to make a simple game with htmlhow to make a 2d game in htmlhow to make an interactive game htmlcreate a default game in html and use in betweenhtml 5 game projectgame code htmlcreate a game in html 5how to make a simple html gamejs games create game with html 5learn how to make a game with htmlhtml 5 game tutorialhow to create a game from javascriptgames in javascriptcss html gamemake game in jsbuild html gameshow you kan to create game from html and csssimple game using htmlhtml how to make gameeasy html game to createhow to make js games with bootstrapcan we create a game using htmlhow to make games in the html css jshtml include gameshow to creat a game in html onlygames to develop using html css and phpgames in jscanvas javascript gamegame csshtml code for gaming websitehow to make game in htmlhtml game basicshow do i make a game in html csscreate game with htmlweb html gamehow to make games with htmlhow to put a javascript game into my html websitereally simple game html and csscreate html gamestart game page html csshow to make a html gamegame html codecan you make a game with htmlgame html filegame program in javascripthow to make game using html and css how to make game with htmlhow to code an html gamejavascript html css game examplegames with html codeshow to make a game on htmlhow to create game in htmlwc3 school game htmlmake g html gamehow to put a game in htmlcan i make a game using htmlweb made in js html gmaegame made in htmljava script gamedshtml codes for gamesgame with htmlgames in java scriptsimple game in htmlgame in htmlmaking a game in jsw3 html gamemaking html gamescan you create a game using html csshow to create a html 5 gamemaking a game in htmlmake game with htmljavascript gameshow to make a game in htmljavascript game htmlcode a game htmlhow to make a game with htmlgame html jshow to make a game in javascript for beginners with only codeswhat are the games using htmlhtml game w3 schoolsgame in jscan i make a game with htmlto make html gamehtml 2d ga 2cehow to make simple html gamemake a game with htmlcreate js gamehow javascript gamehow to include a game in htmlhtml game code fungame development w3schoolshow to develop a game using htmlhow to make a game with javascript and html5how to make a game in html applicationhow to make a 3d game in htmlsimple games using html and css with source codemake game htmlcreate game with html and csshtml css js gamesw3schools canvas gamegame using html css javascriptw3school canvas gamegame navigation html5html javascript gamehtml gameshtml and css gamesmake a game in html game in htmljavascript functions gamehtml game windowgame jsgame htmlhtml gaameshow to make game on htmlhow to make html gamehtml code for gamesjavascript making a tag gamehtml building game codehow to make game using htmlhow to make a game in html css and javascriptgame javascript functionsjavascript game codehow to make a game jscreate a game htmljavascript coding minigamehtml gamecreate a game in htmlhow to code a game in htmlcan you make game using htmlhtml game css jshtml register gamelearn html gamecreate a game with javascripthow to create a game using html and csscreate a video game app w3 schoolscode a game in htmlsimple game javascriptmake html gamecoding a game in htmljs gmaejs 2c php game htmlhtml css game detailhow to make a game with javascript and phpgame html