javascript making a tag game

 <!-- 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>

5
1
Bippy 8155 points

                                    //Javascript game template
//Move player with arrow keys

var canvas = document.createElement(&quot;canvas&quot;);
canvas.width = 500;
canvas.height = 500;
document.body.appendChild(canvas);
var ctx = canvas.getContext(&quot;2d&quot;);

var player = {x: canvas.width / 2, y: canvas.height / 2, speed: 10};
var keys = [];

function update() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  
  ctx.beginPath();
  ctx.fillStyle = &quot;red&quot;;
  ctx.fillRect(player.x, player.y, 50, 50);
  
  if (keys[37])
    player.x -= player.speed;
  if (keys[38])
    player.y -= player.speed;
  if (keys[39])
    player.x += player.speed;
  if (keys[40])
    player.y += player.speed;
  
  requestAnimationFrame(update);
}
update();

document.onkeydown = function(e) {
  keys[e.keyCode] = true;
}
document.onkeyup = function(e) {
  keys[e.keyCode] = false;
}

5 (1 Votes)
0
3.75
4
AMINA ARSHAD 125 points

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

3.75 (4 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 code a game on javascript how to code a game javascript how to make a simple game with code html5 build game javascript build a simple game in javascript how to create game with javascript build game in javascript game development in javascript? simplest game in html game on html basic game in js how to make game app on javascript game code in html javascript make games build a basic js game create game with html and css game tutorial in javascript making javascript games how to make a javascript game with code how to make js browser game create a game with javascript quickly can you create a game using html css how to make game on javascript creating a javascript game how to make a simple game in javascript make game in html making a game in js how to make a javascript game in html code a game in javascript writing a game in javascript game html code javascript game creation can we make games using javascript To make HTML GAME how to make a game from javascript Simple game in JavaScript source Code how to include a game in html basic js game javascript code for a game how to create an html game make html based game simple js game create game with html 5 learn make game in javascript how to make a make game in js simple game in js how to code a game with js how to build a game in javascript simple game using html how to make game with js js game code can u make a game with js html how to make game is it possible to create a gamewith html and css game in html source code codes for javascript to make an game codes for java script to make an game game made with javascript how to make game on html game development in javascript javascript code of a game how to make game in html simple game in html create a gamews in javascript how to make a basic game in js code a game html javascript simple game code how to make a game in html5 and javascript make game by js create a game html code a game in html creating a game with javascript javascript make a game how to make simple html game java script game js game examples make a game in html how to create game using javascript create game in html html make your own game how to make a game in HTML APPLICATION creating a game in js how to develop game in js how to make game using html and css javascript code a game making a game with javascript code simple game using Java script interactive game javascript make a game using javascript develop game in javascript code game in javascript coding a game in javascript javascript create web game how to make a simple game with html game code in js how to create a javascript game make a game with html make game javascript how to make js games code javascript game how to make a game in javascript tutorial game making javascript simple javascript game tutorial game made in html how to create game in javascript game java script creating javascript games game javascript code javascript building game building game javascript creating a game in javascript html game making js game example javascript game engine what is the best way to make a game in javascript making game in javascript create a game with javascript how make game javascript building a simple game with javascript java script game examples how to code a simple javascript game how to create a game in js create a simple game javascript can i make a game with javascript html js how make game build a javascript game making a game with java script how to create a html 5 game a simple game to create with javascript simple game code in javascript how to make a game javascript create game with javascript game with js make game in javascript javascript tutorial game how to make a simple js game howb to make a game in js make game with html game with javascript make game with javascript how to make games on javascript what do I need to build a game with javascript how do you make a game with javascript javascript game making can you make a game with javascript how to create a html game build a simple javascript game make game with js makeing a javscript game html basic game can I use javascript for making game? javascript game html web html game flappy bird js w3schools javascript gme write a bsic html game html building game code Browser-based Game Website using HTML, CSS, JavaScript, Bootstrap how to make an interactive game html javascript game code simple game for html js simple game javascript game tutorial for beginners java scripts command game javascript videogame projects 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 making html games how to make games on html make javascript games create a game in html how to make a javascript gfame create ganes with javascript javascript simple games learn how to make a game with html how to create game in html how to code a game in html how to code a js games chreate a game with js write games in javascript how to put a javascript game into my html website learn how to make games with html css and javascript game html js javascript game? how to creat a game in html only how to creat a game in html using javascript to make a game w3schools html game javascript making games tutorial simple game html javascript make games using html css and js easy html game to create easy html game html gamge simple How do you create a game in Javascript' How do you create a video game injs? How do you create a video game i js? game.js canvas game How to make a asimple JS game create js game example how to create bakamon game in javascript games with js build games with javascript w3schools game html how to create a game from javascript game using html and javsscript can you make games with javascript easiest javascript games build a game using html game program in javascript how to build a javascript game without html game development w3schools simple javascript game example games for pc in html css how to make javascript games putting a python game in javascript html game basics html game window build a game in javascript html game code fun html css game detail first javascript game js game design can you make game using html create interactive game in javascript building a game using javascript and html make a game in js website making game in html' javascript game program exemple start game page html css video game javascript lingo game create with javascript tutorial lingo game create with javascript how to make js games with bootstrap create javascript game how to make a game in javascript for beginners with only codes javascript html games javascript html gammes Different ways to go about making a javascript game game on javascript create simple web game create games beginner javascript games with html codes make html game how to create games in html js and css WHAT ARE THE GAMES USING HTML web made in js html gmae how to make game with html game using html how to create a easy game in javascript how to make games in js importanttags to create a game in html how to make game using html can you make a game in html can we create a game using html games using html css javascript gaming html html css js games how to create a game in javascript js html and css game creating game in js how to make games in the html css js games in java script games in js how to make a simple game in html how to do javascript game make a game with javascript and html5 html and js games how to make a 2d game in html can i make a game using html html include games make a game in js games create javascript how to create a js and html game building game in javascript how to make a game with javascript create simple games with javascript how to make a game with javascript and html5 how to make a html game html and css game making a game with javsacript Learn HTML game how to make a game in html css and javascript easy to make javascript gams write javascript games trist javascript games html register game html gaames write a game in javascript js simple game example html game example create js game how to make games using js3 create a game html and js css html game html game programming creating game using javascript simple javascript games for beginners javascript game with html javascript as a game 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 javascript simple game make your own game with js javascript html5 game make a js browser game create a game in javascript javascript game programming making the easiest game in javascript javascript build a web game what we need to make a game with javascript javascript game development tutorials how to make games with html sample games javascript build html games how to make a game with javascript and php how to make html games game makig using js game in js html game css js Games to develop using html css and php build simple game js creating a javascript game in the browser javascript game code tutorial creating a game using javascript game javascript tutorial game in html make game html Game like tutorial for js game example in javascript create an html game html 5 game tutorial making game with javascript building a game with javascript how to code an html game create game in javascript build games with js html game development js gmae java script how to make a game making games in javascript html code for gaming website game using html css javascript Making JS games basic javascript game code how to code games in javascript canvas javascript game really simple game html and css simple game using html and css games using html css and bootsrap making a game with js minigame javascript 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 building games with javascript build game with javascript java script making a game basic games javascript how to create games using javascript html games w3schools javascript game. w3school canvas game html and css games python snake game w3 school simple web game html game js game navigation html5 wc3 school game html game.html making a game with javascript how to make a 3d game in html simple javascript games how to make a simple game with javascript make a simple game with javascript game html make a game with javascript make a game in javascript create a video game app w3 schools game.css can i create games in javascript how to create a simple game in javascript how to make a game only using js html games w3 2d game in html and css how to make html game javascript html css game example could you make a simple game with javascript 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 how to make a game in javascript for beginners make javascript, html and css game how to make a browser game with javascript how can i build game by js how to make a game in php html 2d ga,e games javascript code create a default game in html and use in between javascript making a tag game html5 javascript game make g html game mini game html javascript games code build a game javascript how do i make a game in html css simple games using html and css with source code games for javascript how to make a game in html and javascript game programming javascript create game using java script how to make game in js how to create a javascript game in website java for html code game online create game with html create a game using javascript javascript game development code reference html game code building games in javascript making a game using javascript for beginners making a game using javascript simple games in js how to code a game in javascri[t how to create a game in javascript with graphics how to code a javascript game js, php game html html games project game in html5 online game devolpment javascript html how to build a javascript game how to make agame in javascript how to make agame in javascri learn how to make games with javascript how to create html games make js game make js game for browser game in javascript making game with only javascript javascript games tutorials develop a game in html how to make a video game in html an create games with javascript simple js games game using javascript code simple html game making a game in css make games using javascript javascript web game tutorial how to add html games html games code to add making games using javascript is making a javascript game easy how to make a game with html javascript games tutorial simple javascript game basic javascript game game using html and css how to make game in javascript create game canvas html5 make a javscript game HOW TO CREATE A GAME in javasc ript html code to make a game how to make an HTML game coding a javascript game game javascript create html game how to make a basic game in javascript how to create game javascript game with javascript code a javascript game javascript game basics make html games making a game wid html only html making a game wid html make games with javascript how to make a gmae using html making a javascript game how to code a game with javascript make a game javascript easy javascript games to make javascript game development tutorial javascript making a game who to program html canvas games make a simple game using javascript make a javascript game how to codea js game simple games to make in javascript runner game with html css and javascript tutorial html5 game code how to make game with javascript create game with js make games with css how to code a game in html js css how to make a game html codes for html to make game codes for java script to make games html code for games how to create a simple game using js how to code a js game how to make a game using js simple diy javascript game How to code a game in javascript javascript game example how to make a html gsme ] games in HTML simple game in javascript html css and javascript basic game how to make games in html 5 how to make a n onlin html game how to make games in javascript how to make a good game by javascript how to make a web game with javascript basic game javascript how to make a online game usinfg html how to create a game in html games made on canvas tag in html making a game in javascript how to make a game with js making a game in html making a html game tutorial 1 how to make games with javascript how to make a game using javascript first simple game coding in html video game javascrip beginner how to create a game using javascript games with javascript how to make games with only JS HOW TO CREATE A GAME WITH JAVASCRIPT web games in javascript javascript game development with demo gave development with html simple html game code mini html js game how to make games using javascript build game using javascript build a game using javascript game javascript w games in html5 mak a game using HTML, CSS and JS javascript game in js Create a Javascript Game javascript videogame how to make a game in html javascript game tutorial make a game with js how to make a js game html5 game tutorial making games in js making games with js how to make a game with javascript and html5 css html games html5 canvas game javascript game tutorials how to build a video game in javascript how to make a game in javascript javasript game html game make games in javascript js games javacript games make javascript game how to make a game using html5 how to code a game in js simple game with javascript making games with javascript games in javascript js game javascript game development javascript game how to add javascrpt game in html game in javascript code create game javascript add game php games created with javascript javascript games how to make a vr game in unity How to make a 2d game unity how to make game using javascript game creator html css javascript using js to make a game create game javascript html5 how to make a game in js how to make a javascript game
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