game html

 <!-- A SIMPLE SNAKE GAME MADE BY HTML AND CSS -->

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style>
  html, body {
    height: 100%;
    margin: 0;
  }

  body {
    background: black;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  canvas {
    border: 1px solid white;
  }
  </style>
</head>
<body>
<canvas width="400" height="400" id="game"></canvas>
<script>
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');

var grid = 16;
var count = 0;
  
var snake = {
  x: 160,
  y: 160,
  
  // snake velocity. moves one grid length every frame in either the x or y direction
  dx: grid,
  dy: 0,
  
  // keep track of all grids the snake body occupies
  cells: [],
  
  // length of the snake. grows when eating an apple
  maxCells: 4
};
var apple = {
  x: 320,
  y: 320
};

// get random whole numbers in a specific range
// @see https://stackoverflow.com/a/1527820/2124254
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

// game loop
function loop() {
  requestAnimationFrame(loop);

  // slow game loop to 15 fps instead of 60 (60/15 = 4)
  if (++count < 4) {
    return;
  }

  count = 0;
  context.clearRect(0,0,canvas.width,canvas.height);

  // move snake by it's velocity
  snake.x += snake.dx;
  snake.y += snake.dy;

  // wrap snake position horizontally on edge of screen
  if (snake.x < 0) {
    snake.x = canvas.width - grid;
  }
  else if (snake.x >= canvas.width) {
    snake.x = 0;
  }
  
  // wrap snake position vertically on edge of screen
  if (snake.y < 0) {
    snake.y = canvas.height - grid;
  }
  else if (snake.y >= canvas.height) {
    snake.y = 0;
  }

  // keep track of where snake has been. front of the array is always the head
  snake.cells.unshift({x: snake.x, y: snake.y});

  // remove cells as we move away from them
  if (snake.cells.length > snake.maxCells) {
    snake.cells.pop();
  }

  // draw apple
  context.fillStyle = 'red';
  context.fillRect(apple.x, apple.y, grid-1, grid-1);

  // draw snake one cell at a time
  context.fillStyle = 'green';
  snake.cells.forEach(function(cell, index) {
    
    // drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
    context.fillRect(cell.x, cell.y, grid-1, grid-1);  

    // snake ate apple
    if (cell.x === apple.x && cell.y === apple.y) {
      snake.maxCells++;

      // canvas is 400x400 which is 25x25 grids 
      apple.x = getRandomInt(0, 25) * grid;
      apple.y = getRandomInt(0, 25) * grid;
    }

    // check collision with all cells after this one (modified bubble sort)
    for (var i = index + 1; i < snake.cells.length; i++) {
      
      // snake occupies same space as a body part. reset game
      if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
        snake.x = 160;
        snake.y = 160;
        snake.cells = [];
        snake.maxCells = 4;
        snake.dx = grid;
        snake.dy = 0;

        apple.x = getRandomInt(0, 25) * grid;
        apple.y = getRandomInt(0, 25) * grid;
      }
    }
  });
}

// listen to keyboard events to move the snake
document.addEventListener('keydown', function(e) {
  // prevent snake from backtracking on itself by checking that it's 
  // not already moving on the same axis (pressing left while moving
  // left won't do anything, and pressing right while moving left
  // shouldn't let you collide with your own body)
  
  // left arrow key
  if (e.which === 37 && snake.dx === 0) {
    snake.dx = -grid;
    snake.dy = 0;
  }
  // up arrow key
  else if (e.which === 38 && snake.dy === 0) {
    snake.dy = -grid;
    snake.dx = 0;
  }
  // right arrow key
  else if (e.which === 39 && snake.dx === 0) {
    snake.dx = grid;
    snake.dy = 0;
  }
  // down arrow key
  else if (e.which === 40 && snake.dy === 0) {
    snake.dy = grid;
    snake.dx = 0;
  }
});

// start the game
requestAnimationFrame(loop);
</script>
</body>
</html>

3.83
6

                                    &lt;div&gt;
&lt;script src=&quot;https://cdn.htmlgames.com/embed.js?game=MayaPyramidSolitaire&amp;amp;bgcolor=white&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

3.83 (6 Votes)
0
4.4
5

                                    (function)game=tag
(function)=character=109
(function)=vehicles=200

4.4 (5 Votes)
0
3.6
5

                                    &lt;div&gt;
&lt;script src=&quot;https://cdn.htmlgames.com/embed.js?game=MayaPyramidSolitaire&amp;amp;bgcolor=white&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

3.6 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to make a simple game with code html5 simplest game in html game on html game code in html create game with html and css game html w3school making a game in html can you create a game using html css make game in html game by html game html code To make HTML GAME html 5 game how to include a game in html how to create an html game make html based game create game with html 5 simple game using html html how to make game is it possible to create a gamewith html and css game in html source code how to make game on html game code html how to make game in html simple game in html code a game html create a game html code a game in html how to make simple html game game html file make a game in html create game in html html make your own game how to make a game in HTML APPLICATION how to make game using html and css how to make a simple game with html w3 html game make a game with html game made in html html game making how to make a game using html5 how to create a html 5 game make game with html how to create a html game html basic game javascript game html web html game flappy bird js w3schools game with javascript javascript gme write a bsic html game html building game code make a game in javascript Browser-based Game Website using HTML, CSS, JavaScript, Bootstrap how to make an interactive game html simple game for html how to put a game in html game with html html5 or javascript for games javascript how to make a game how to make a game in with html simple game javascript how to make a game on html how to make a game using html games in javascript making html games how to make games on html create a game in html learn how to make a game with html how to create game in html how to code a game in html how to put a javascript game into my html website learn how to make games with html css and javascript game html js how to creat a game in html only how to creat a game in html simple game html javascript make games using html css and js easy html game to create html gamge simple game.js canvas game w3schools game html how to create a game from javascript game using html and javsscript build a game using html game program in javascript game development w3schools how to make a game with html html game basics html code for games html game window html game code fun html css game detail can you make game using html start game page html css how to make js games with bootstrap how to make a game in javascript for beginners with only codes javascript html games games with html codes make html game WHAT ARE THE GAMES USING HTML web made in js html gmae how to make game with html game using html importanttags to create a game in html how to make game using html can you make a game in html java script game can we create a game using html javascript game code gaming html html css js games how to make games in the html css js games in java script games in js how to make a game in html how to make a 2d game in html can i make a game using html html include games how to create a js and html game how to make a game with javascript and html5 html and css game game making javascript Learn HTML game how to make a game in html css and javascript html register game making a game in js html gaames html game example create js game how to make games using js3 create a game html and js css html game html game programming javascript game with html html 5 game project javascript coding minigame create a game using html and js java script game code ghow to make games with html w3schools canvas game create a simple html game js game make a game with javascript javascript html5 game how to make games with html build html games how to make a game with javascript and php how to make html games game in js html game css js Games to develop using html css and php game in html make game html create a game with javascript game example in javascript create an html game html 5 game tutorial how to code an html game html game development js gmae java script how to make a game html code for gaming website game using html css javascript basic javascript game code canvas javascript game really simple game html and css simple game using html and css games using html css and bootsrap how to create a game using html and css how to create a game using html game development w3 html javascript game how to make a game js java script making a game html games w3schools javascript game. w3school canvas game html and css games python snake game w3 school simple html game simple web game html game js game navigation html5 wc3 school game html game.html html games how to make a 3d game in html game html create a video game app w3 schools game.css html games w3 2d game in html and css simple html game code how to make html game javascript html css game example how javascript game java script gameds game javascript functions javascript functions game make game in js html codes for games html programs for games how to make a simple html game make javascript, html and css game how to make a game in php html 2d ga,e create a default game in html and use in between javascript making a tag game html game make g html game mini game html javascript games code how do i make a game in html css simple games using html and css with source code how to make a game in html and javascript javascript games js games create game with html html game code javascript game js, php game html how to create a game in html
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source