js实现贪吃蛇游戏含注释
作者:越学越害怕 发布时间:2024-04-19 10:05:44
标签:js,贪吃蛇
本文实例为大家分享了js实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
两个小时完成的,有点简陋。
直接看效果。打开调试面板,在resource面板,新建snippet
粘贴以下代码,右键snippet,run。
clearInterval(timer);
document.body.innerHTML = "";
//每秒移动多少格
let speed = 10;
let speedUpMul = 3;
//是否能穿墙
let isThroughTheWall = true;
//行数
let row = 40;
let headColor = 'red';
let bodyColor = 'green';
let foodColor = 'yellow';
let borderColor = 'grey';
// 游戏全局变量
let hasFood = false;
//游戏状态
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('游戏结束');
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('游戏结束');
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)
}
}
}
来源:https://blog.csdn.net/weixin_42525191/article/details/114670004
0
投稿
猜你喜欢
- MySQL出现乱码的原因要了解为什么会出现乱码,我们就先要理解:从客户端发起请求,到MySQL存储数据,再到下次从表取回客户端的过程中,哪些
- 前言这篇文章主要介绍了Python 字符串去除空格的6种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,来
- 本文实例为大家分享了python3实现ftp服务功能的具体代码,供大家参考,具体内容如下功能介绍:可执行的命令:lspwdcd put rm
- 前言:饼状图是用来呈现一个数据系列中各项的大小与各项占项总和的百分比,Matplotlib 提供了plt.pie()方法绘制柱状图,语法格式
- 在昨天的文章,《 block 和 inline 的区别是?》里,我给大家留了个问题——LI 元素到底是block level 的,还是 in
- 一、mongodb安装在官网下载适应于自己平台的mongodb,在此安装环境为Windows7-64bit下载完成后直接安装,连续点击nex
- import retext='V101_renow.Android.2.2.Normal.1.Alpha.apk?IMSI=4600
- 数据标准化(归一化)处理是数据挖掘的一项基础工作,不同评价指标往往具有不同的量纲和量纲单位,这样的情况会影响到数据分析的结果,为了消除指标之
- 本文实例讲述了python 协程 gevent原理与用法。分享给大家供大家参考,具体如下:geventgreenlet已经实现了协程,但是这
- 前言Python语言处理字符串、数组类的问题时有一定概率需要使用切片方法,比如:Leetcode_5。学习官方解法时发现切片的索引可以超出字
- js的eval代码快速解密有一段js代码内容如下:eval(function(E,I,A,D,J,K,L,H){function C(A)后
- MySQL表中的约束(constraint)为了保证数据的完整性,(数据的精确性和可靠性)SQL规范以约束的方式对表数据进行额外的条件限制,
- pandas模块pandas是一个强大的分析结构化数据的工具集;它的使用基础是Numpy(提供高性能的矩阵运算);用于数据挖掘和数据分析,同
- 前言首先需要知道的是,js中有6个值为false,分别是: 0, '', null, undefined, NaN 和 fa
- namedtuple 就是命名的 tuple,比较像 C 语言中 struct。一般情况下的 tuple 是 (item1, item2,
- 背景:目前keras框架使用简单,很容易上手,深得广大算法工程师的喜爱,但是当部署到客户端时,可能会出现各种各样的bug,甚至不支持使用ke
- UPA2008于2008年10月24日在深圳举行,托哥、绿桔应邀主持了一场圆桌会和一场工作坊,以下是圆桌会议《商业价值与用户价值的平衡》的现
- Selenium一、简介selenium是一个用于Web应用自动化程序测试的工具,测试直接运行在浏览器中,就像真正的用户在操作一样selen
- 由于项目是thinkPHP做后端框架,一直以来都是多页面的后端路由,想使用火热的webpack有点无从下手(原谅我太菜,而且推广vue只有我
- 如何在网站上提供音乐下载?为用户提供歌曲下载,一般有两种方式,一是直接通过Http,浏览器下载,二是通过ftp协议下载。我们来用Http和浏