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

How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to

Things to note 1. First, you need to create a my....

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...

Example of adding attributes using style in html

Add inline styles to the required links: Copy code...

Examples of optimistic locking and pessimistic locking in MySQL

The task of concurrency control in a database man...

Detailed explanation of the use of Vue image drag and drop zoom component

The specific usage of the Vue image drag and drop...

Detailed explanation of Vue save automatic formatting line break

I searched for many ways to change it online but ...

mysql5.7.19 zip detailed installation process and configuration

MySQL v5.7.19 official version (32/64 bit install...

Summary of CSS3 practical methods (recommended)

1. Rounded border: CSS CodeCopy content to clipbo...

Detailed explanation of the principles of Vue's responsive system

Table of contents The basic principles of Vue'...

Implementing simple tabs with js

Tab selection cards are used very frequently on r...

Detailed explanation of slave_exec_mode parameter in MySQL

Today I accidentally saw the parameter slave_exec...

Basic usage and examples of yum (recommended)

yum command Yum (full name Yellow dog Updater, Mo...

Detailed explanation of data types in JavaScript basics

Table of contents 1. Data Type 1.1 Why do we need...