js to implement the snake game with comments

js to implement the snake game with comments

This article example shares the specific code of js to implement the snake game for your reference. The specific content is as follows

It took two hours to complete, it's a bit crude.

See the effect directly. Open the debug panel, and create a new snippet in the resource panel.

Paste the following code, right-click snippet, and run.

clearInterval(timer);
document.body.innerHTML = "";

//How many squares does it move per second let speed = 10;
let speedUpMul = 3;

//Can it go through the wall? let isThroughTheWall = true;

//Number of rows let row = 40;
let headColor = 'red';
let bodyColor = 'green';
let foodColor = 'yellow';
let borderColor = 'grey';


//Game global variables let hasFood = false;
//Game status let gamestaus = 'start';
let hasAccelerate = false;

let mainContainer = document.createElement("div");
mainContainer.style.width = 20 * row + 1 + "px";
mainContainer.style.height = 20 * row + 1 + "px";
mainContainer.style.margin = "0 auto";
mainContainer.style.position = "relative";
mainContainer.style.border = "1px solid " + borderColor;

document.body.appendChild(mainContainer);

for(let i = 0;i<row;i++){
 let marginTop = 20 * i + "px";
 for(let j = 0; j < row; j++) {
 let marginLeft = j * 20 + "px";
 let tempDiv = document.createElement('div');
 tempDiv.style.width = "19px";
 tempDiv.style.height = "19px";
 tempDiv.style.marginTop = marginTop;
 tempDiv.style.marginLeft = marginLeft;
 tempDiv.style.border = "0.5px solid " + borderColor;
 tempDiv.style.position = "absolute";
 tempDiv.id = j + "div" + i;
 mainContainer.appendChild(tempDiv);
 }
}

class Cell{
 constructor(x, y, color){
 if (isThroughTheWall) {
  if(x < 0) x = row-1;
  if(x > row - 1) x = 0;
  if(y < 0) y = row - 1;
  if(y > row - 1) y = 0;
 }else{
  if(x < 0 || y < 0 || x > row - 1 || y > row - 1){
  clearInterval(timer);
  alert('Game over');
  return;
  }
 }
 this.x = x;
 this.y = y;
 this.color = color;
 let tempDiv = document.getElementById(x + 'div' + y);
 if(tempDiv) tempDiv.style.backgroundColor = color;
 }
}

snake = {
 head : {},
 body : [],
 dire : 1
}

let headx = Math.floor(Math.random() * 14) + 3;
let heady = Math.floor(Math.random() * 14) + 3;
snake.head = new Cell(headx, heady, headColor);

//up, right, down, left let direction = [1, 2, 3, 4]

snake.dire = direction[Math.floor(Math.random() * 4)];

if(snake.direct == 1){
 snake.body.push(new Cell(snake.head.x, snake.head.y+1, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y+2, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y+3, bodyColor));
}

if(snake.direct == 2){
 snake.body.push(new Cell(snake.head.x-1, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x-2, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x-3, snake.head.y, bodyColor));
}

if(snake.direct == 3){
 snake.body.push(new Cell(snake.head.x, snake.head.y-1, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y-2, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y-3, bodyColor));
}

if(snake.direct == 4){
 snake.body.push(new Cell(snake.head.x+1, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x+2, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x+3, snake.head.y, bodyColor));
}

function game(){
 if(gamestatus == 'pause'){
 return;
 }
 if(gamestatus == 'gameover'){
 clearInterval(timer);
 alert('Game over');
 return;
 }
 initFood();
 let snakeHeadX = snake.head.x;
 let snakeHeadY = snake.head.y;
 let color = '';
 if(snake.direct == 1){
 let tempDiv = document.getElementById(snakeHeadX + 'div' + (snakeHeadY-1));
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX, snakeHeadY - 1, headColor);
 }
 if(snake.direct == 2){
 let tempDiv = document.getElementById((snakeHeadX + 1) + 'div' + snakeHeadY);
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX + 1, snakeHeadY, headColor);
 }
 if(snake.direct == 3){
 let tempDiv = document.getElementById(snakeHeadX + 'div' + (snakeHeadY+1));
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX, snakeHeadY + 1, headColor);
 }
 if(snake.direct == 4){
 let tempDiv = document.getElementById((snakeHeadX - 1) + 'div' + snakeHeadY);
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX - 1, snakeHeadY, headColor);
 }
 snake.body.unshift(new Cell(snakeHeadX, snakeHeadY, bodyColor));
 if(color && color == foodColor){
 hasFood = false;
 initFood();
 }else if(color && color == bodyColor){
 gamestaus = 'gameover'; 
 }else{
 let lastBody = snake.body.pop();
 new Cell(lastBody.x, lastBody.y, '');
 }
}
var timer = setInterval(game, 10 / speed * 100)


/**
 * Initialize food */
function initFood(){
 while(!hasFood){
 let x = Math.floor(Math.random() * row);
 let y = Math.floor(Math.random() * row);
 let snakeBody = snake.body;
 let enable = true;
 if(snake.head.x == x && snake.head.y == y){
  enable = false;
 }
 for(sBody of snakeBody){
  if(sBody.x == x && sBody.y == y){
  enable = false;
  break;
  }
 }
 if(enable){
  new Cell(x, y, foodColor);
  hasFood = true;
 }
 }
}

document.onkeydown = function(e){
 if(e.keyCode == 38){
 //if(snake.dire != 3 && snake.dire != 1){
  snake.dire = 1;
 }else if(snake.direct == 1){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }

 }

 if(e.keyCode == 39){
 //Rightif(snake.dire != 4 && snake.dire != 2){
  snake.dire = 2;
 }else if(snake.dire == 2){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }
 }

 if(e.keyCode == 40){
 //Nextif(snake.dire != 1 && snake.dire != 3){
  snake.dire = 3;
 }else if(snake.dire == 3){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }
 }

 if(e.keyCode == 37){
 //Left if (snake.dire != 2 && snake.dire != 4) {
  snake.dire = 4;
 }else if(snake.dire == 4){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }
 }
 //Spacebar pause if(e.keyCode == 32){
 if(gamestatus == 'start'){
  gamestaus = 'pause';
 }else if(gamestatus == 'pause'){
  gamestaus = 'start';
 }
 }
}

document.onkeyup = function(e){
 if(e.keyCode == 38){
 // if (snake.dire == 1 && hasAccelerate) {
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }

 }

 if(e.keyCode == 39){
 //Rightif(snake.dire == 2 && hasAccelerate){
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }
 }

 if(e.keyCode == 40){
 //Nextif(snake.dire == 3 && hasAccelerate){
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }
 }

 if(e.keyCode == 37){
 //Left if (snake.dire == 4 && hasAccelerate) {
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }
 }
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS implements the snake game
  • JavaScript to achieve the idea of ​​​​snake game
  • JavaScript implementation of classic snake game
  • Use js to write a simple snake game
  • JavaScript Snake Implementation Code
  • JavaScript to implement the web version of the snake game
  • Implementing the Snake Game in JavaScript
  • Native js to realize a simple snake game
  • Writing Snake Game with Native JS
  • JavaScript exquisite snake implementation process

<<:  Introduction to who command examples in Linux

>>:  Configure Mysql master-slave service implementation example

Recommend

Typescript+react to achieve simple drag and drop effects on mobile and PC

This article shares the specific code of typescri...

Analysis of the pros and cons of fixed, fluid, and flexible web page layouts

There is a question that has troubled web designe...

Specific use of Linux dirname command

01. Command Overview dirname - strip non-director...

Vue uses the video tag to implement video playback

This article shares the specific code of Vue usin...

Solution to the problem of slow docker pull image speed

Currently, Docker has an official mirror for Chin...

Analysis of basic usage of ul and li

Navigation, small amount of data table, centered &...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

foreman ubuntu16 quick installation

Quickstart Guide The Foreman installer is a colle...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

Detailed explanation of the production principle of jQuery breathing carousel

This article shares the specific process of the j...

Summary of Creating and Using Array Methods in Bash Scripts

Defining an array in Bash There are two ways to c...

This article will show you how to use Vue 3.0 responsive

Table of contents Use Cases Reactive API related ...

CSS3 realizes the effect of triangle continuous enlargement

1. CSS3 triangle continues to zoom in special eff...

The use of mysql unique key in query and related issues

1. Create table statement: CREATE TABLE `employee...

MySQL 8.0.20 installation and configuration tutorial under Docker

Docker installs MySQL version 8.0.20 for your ref...