js實(shí)現(xiàn)貪吃蛇游戲含注釋
本文實(shí)例為大家分享了js實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
兩個(gè)小時(shí)完成的,有點(diǎn)簡(jiǎn)陋。
直接看效果。打開調(diào)試面板,在resource面板,新建snippet
粘貼以下代碼,右鍵snippet,run。


clearInterval(timer);
document.body.innerHTML = "";
//每秒移動(dòng)多少格
let speed = 10;
let speedUpMul = 3;
//是否能穿墻
let isThroughTheWall = true;
//行數(shù)
let row = 40;
let headColor = 'red';
let bodyColor = 'green';
let foodColor = 'yellow';
let borderColor = 'grey';
// 游戲全局變量
let hasFood = false;
//游戲狀態(tài)
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('游戲結(jié)束');
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);
//上右下左
let direction = [1, 2, 3, 4]
snake.dire = direction[Math.floor(Math.random() * 4)];
if(snake.dire == 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.dire == 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.dire == 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.dire == 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(gamestaus == 'pause'){
return;
}
if(gamestaus == 'gameover'){
clearInterval(timer);
alert('游戲結(jié)束');
return;
}
initFood();
let snakeHeadX = snake.head.x;
let snakeHeadY = snake.head.y;
let color = '';
if(snake.dire == 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.dire == 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.dire == 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.dire == 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)
/**
* 初始化食物
*/
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.dire == 1){
if(!hasAccelerate){
clearInterval(timer);
hasAccelerate = true;
speed = speed * speedUpMul;
timer = setInterval(game, 10 / speed * 100)
}
}
}
if(e.keyCode == 39){
//右
if(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){
//下
if(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){
//左
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)
}
}
}
//空格鍵暫停
if(e.keyCode == 32){
if(gamestaus == 'start'){
gamestaus = 'pause';
}else if(gamestaus == '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){
//右
if(snake.dire == 2 && hasAccelerate){
clearInterval(timer);
hasAccelerate = false;
speed = speed / speedUpMul;
timer = setInterval(game, 10 / speed * 100)
}
}
if(e.keyCode == 40){
//下
if(snake.dire == 3 && hasAccelerate){
clearInterval(timer);
hasAccelerate = false;
speed = speed / speedUpMul;
timer = setInterval(game, 10 / speed * 100)
}
}
if(e.keyCode == 37){
//左
if(snake.dire == 4 && hasAccelerate){
clearInterval(timer);
hasAccelerate = false;
speed = speed / speedUpMul;
timer = setInterval(game, 10 / speed * 100)
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序swiper組件實(shí)現(xiàn)抖音翻頁(yè)切換視頻功能的實(shí)例代碼
這篇文章主要介紹了微信小程序swiper組件實(shí)現(xiàn)抖音翻頁(yè)切換視頻功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
JavaScript數(shù)組reduce()方法的語(yǔ)法與實(shí)例解析
js函數(shù)中有三個(gè)在特定場(chǎng)合很好用的函數(shù):reduce(),map(),filter(),這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組reduce()方法的相關(guān)資料,需要的朋友可以參考下2021-07-07
詳解JavaScript中if語(yǔ)句優(yōu)化和部分語(yǔ)法糖小技巧推薦
在前端日常開發(fā)過(guò)程中,if?else判斷語(yǔ)句使用的次數(shù)應(yīng)該是比較頻繁的了,一些較為復(fù)雜的場(chǎng)景,可能會(huì)用到很多判斷,本文給大家介紹JavaScript中if語(yǔ)句優(yōu)化和部分語(yǔ)法糖小技巧,感興趣的朋友一起看看吧2022-05-05
input 禁止輸入特殊字符的四種實(shí)現(xiàn)方式
某些特殊字符傳入后臺(tái)會(huì)產(chǎn)生錯(cuò)誤,可能導(dǎo)致sql注入,所以要想法從根本上攔截,接下來(lái)通過(guò)本文給大家介紹input 禁止輸入特殊字符的方式,對(duì)input 禁止特殊字符知識(shí)感興趣的朋友一起看下吧2016-08-08
JS(JQuery)操作Array的相關(guān)方法介紹
本篇文章主要是對(duì)JS(JQuery)操作Array的相關(guān)方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
功能強(qiáng)大的Bootstrap組件(結(jié)合js)
這篇文章主要介紹了功能強(qiáng)大的Bootstrap組件,介紹js結(jié)合Bootstrap組件的使用方法,感興趣的小伙伴們可以參考一下2016-08-08
一文詳解preact的高性能狀態(tài)管理Signals
這篇文章主要介紹了一文詳解preact的高性能狀態(tài)管理Signals,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2022-09-09
javascript下利用for( in )語(yǔ)句 獲得所有事件名稱的代碼
2008-02-02
原生JS實(shí)現(xiàn)非常好看的計(jì)數(shù)器
這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)非常好看的計(jì)數(shù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

