首页 > 分享 > 原生JS贪吃蛇游戏

原生JS贪吃蛇游戏

最新推荐文章于 2022-06-23 11:04:44 发布

波帝伯爵 于 2017-11-05 22:07:59 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原生JS版的贪吃蛇

简版效果图

这里写图片描述

上代码

css

<style> body, div, p, input{ padding:0; margin:0; } #box{ width:1410px; height:1005px; margin:50px auto; } #map{ width:1000px; height:1000px; background-color:#d1d5d3; position:relative; display:inline-block; } #panel{ width:400px; height:1000px; display:inline-block; position:relative; vertical-align:top; } #log{ width:100%; height:90%; background-color:#d1d5d3; overflow:auto; } #panel .btn{ width:150px; height:90px; font-size:30px; line-height:90px; text-align:center; margin:8px 20px; } #map .snake{ position:absolute; background-color:blueviolet; } #map .food{ position:absolute; background-color:#303ad2; } #map .head{ background-color:red; z-index:999; } #socre{ font-size:50px; line-height:100px; padding-left:20px } </style>

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 html

<div id="box"> <div id="map"></div> <div id="panel"> <div id="log"></div> <p id="socre">累计分数:</p> </div> </div>1234567 js

以下为部分代码,完整代码请移步github

class Snake { constructor(container, oriLenth, size, console){ this.container = container; this.snake = []; this.size = size; this.HeadCorrd = [1, oriLenth]; this.Console = console; this.Score = 0; this.InitSnake(oriLenth); } get Head(){ return this.snake[0]; } get Tail(){ return this.snake[this.Length - 1]; } get Length(){ return this.snake.length; } set SetDirection(direction){ this.direct = direction; } set SetFood(food){ this.food = food; } get GetScore(){ return this.Score; } CheckHeadCoord(){ if(this.HeadCorrd[0] === -1 || this.HeadCorrd[0] === _map.Max_X || this.HeadCorrd[1] === -1 || this.HeadCorrd[1] === _map.Max_Y){ document.onkeydown = null; return false; } else return true; } InitSnake(length){ var node; for(var i = 0; i < length; i++){ node = this.AddNode(); node.style.left = i * this.size + "px"; node.style.top = 0; this.snake.push(node); this.container.appendChild(node); } this.snake.reverse(); this.snake[0].className += " head"; } AddNode(){ var node = document.createElement("div"); node.style.width = this.size + 'px'; node.style.height = this.size + 'px'; node.className = 'snake'; return node; } Move(){ if(!this.direct){ return; } //移动头部 var headCoord = [this.Head.style.top, this.Head.style.left]; var target; var flag = true; switch(this.direct){ case 37: this.direct = 'left'; if(this.CheckHeadCoord()){ target = this.Head.offsetLeft - this.size; if(target >= 0){ this.Head.style.left = target + "px" } this.HeadCorrd[1]--; } else flag = false; break; case 38: this.direct = 'up'; if(this.CheckHeadCoord()){ target = this.Head.offsetTop - this.size; if(target >= 0){ this.Head.style.top = target + "px" } this.HeadCorrd[0]--; } else flag = false; break; case 39: this.direct = 'right'; if(this.CheckHeadCoord()){ target = this.Head.offsetLeft + this.size; if(target < _map.Width){ this.Head.style.left = target + "px" } this.HeadCorrd[1]++; } else flag = false; break; case 40: this.direct = 'down'; if(this.CheckHeadCoord()){ target = this.Head.offsetTop + this.size; if(target < _map.Width){ this.Head.style.top = target + "px" } this.HeadCorrd[0]++; } else flag = false; break; } if(flag){ this.Tail.style.top = headCoord[0]; this.Tail.style.left = headCoord[1]; this.snake.splice(1, 0, this.Tail); this.snake.pop(); this.Eate(); } else { this.Console.Log("撞了南墙,游戏结束") } // this.Console.Log(`${this.HeadCorrd[0]}_${this.HeadCorrd[1]}`); //移动尾部 } Eate(){ if(this.food.Coord[1] + 1 === this.HeadCorrd[0] && this.food.Coord[0] + 1 === this.HeadCorrd[1]){ this.food.foodElement.className = "snake"; this.snake.push(this.food.foodElement); this.container.appendChild(this.food.foodElement); this.Console.Log(`累计分数:${this.Score++}`); var food = new Food(this.container, this.Console, _map.Size, _map.Max_X, _map.Max_Y); food.GenerateFood(); this.food = food; } } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167

相关知识

原生JS贪吃蛇游戏
贪吃蛇游戏程序设计实验报告
python+pygame 贪吃蛇游戏
基于单片机的贪吃蛇游戏设计
JS 原生js实现贪吃蛇小游戏(含详细注释)
用C语言实现贪吃蛇游戏
贪吃蛇大作战下载
贪吃蛇案例详解
JS游戏贪吃蛇
单片机贪吃蛇 毕业设计.pdf

网址: 原生JS贪吃蛇游戏 https://m.mcbbbk.com/newsview710744.html

所属分类:萌宠日常
上一篇: 好长好长的蛇
下一篇: [基础知识] 白花蛇草