iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >js如何实现贪吃蛇游戏
  • 792
分享到

js如何实现贪吃蛇游戏

2023-06-14 05:06:48 792人浏览 独家记忆
摘要

本篇内容介绍了“js如何实现贪吃蛇游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!两个小时完成的,有点简陋。直接看效果。打开调试面板,在r

本篇内容介绍了“js如何实现贪吃蛇游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

两个小时完成的,有点简陋。

直接看效果。打开调试面板,在resource面板,新建snippet

粘贴以下代码,右键snippet,run。

js如何实现贪吃蛇游戏

js如何实现贪吃蛇游戏

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) } }}

“js如何实现贪吃蛇游戏”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

--结束END--

本文标题: js如何实现贪吃蛇游戏

本文链接: https://www.lsjlt.com/news/267911.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • js如何实现贪吃蛇游戏
    本篇内容介绍了“js如何实现贪吃蛇游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!两个小时完成的,有点简陋。直接看效果。打开调试面板,在r...
    99+
    2023-06-14
  • JS实现贪吃蛇小游戏
    目录一、初始化结构二、渲染蛇的颜色 三、蛇的运动四、蛇死亡的判定方式 蛇有两种判定死亡的方式五、食物的创建六、蛇吃食物边长七、开始游戏功能八、暂停/继续游戏功能页面效果: 贪吃蛇游...
    99+
    2024-04-02
  • 用JS实现贪吃蛇游戏
    本文实例为大家分享了JS实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 效果图: 完整代码如下: html: <!DOCTYPE html> <html la...
    99+
    2024-04-02
  • 用JS实现贪吃蛇小游戏
    本文实例为大家分享了JS实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 效果图: 完整代码如下: HTML <!DOCTYPE html> <html la...
    99+
    2024-04-02
  • JS+Canvas实现贪吃蛇小游戏
    今天呢,主要和小伙伴们分享一下一个贪吃蛇游戏从构思到实现的过程~因为我不是很喜欢直接PO代码,所以只copy代码的童鞋们请出门左转不谢。 按理说canvas与其应用是老生常谈了,可我...
    99+
    2024-04-02
  • js实现贪吃蛇游戏含注释
    本文实例为大家分享了js实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 两个小时完成的,有点简陋。 直接看效果。打开调试面板,在resource面板,新建snippet 粘贴...
    99+
    2024-04-02
  • js怎么实现贪吃蛇小游戏
    这篇文章将为大家详细讲解有关js怎么实现贪吃蛇小游戏,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。示例代码<!DOCTYPE html> <h...
    99+
    2024-04-02
  • QT如何实现贪吃蛇游戏
    这篇文章主要介绍了QT如何实现贪吃蛇游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。为了熟悉QT的相关知识,我用了大约8个小时的时间用QT再次写了一遍贪吃蛇。因为QT的机制...
    99+
    2023-06-15
  • 怎么用JS实现贪吃蛇游戏
    本文小编为大家详细介绍“怎么用JS实现贪吃蛇游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用JS实现贪吃蛇游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。效果图:完整代码如下:html:<!DO...
    99+
    2023-07-02
  • JavaScript实现贪吃蛇游戏
    本文实例为大家分享了JavaScript实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 通过JavaScript,我们可以实现贪吃蛇游戏,具体功能如下: (1)通过按上下左右键来...
    99+
    2024-04-02
  • python如何实现贪吃蛇游戏
    这篇文章主要介绍了python如何实现贪吃蛇游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。游戏实现效果如下:后面有完整代码和解析import sysimport...
    99+
    2023-06-14
  • Java实现贪吃蛇游戏
    下面是一个简单的Java实现贪吃蛇游戏的示例代码:```javaimport javax.swing.*;import java.a...
    99+
    2023-08-09
    Java
  • QT实现贪吃蛇游戏
    为了熟悉QT的相关知识,我用了大约8个小时的时间用QT再次写了一遍贪吃蛇。 因为QT的机制和平时写的程序流程不同,所以程序中可能没有遵守代码规范。 运行效果: 程序内除了实现贪吃蛇...
    99+
    2024-04-02
  • pygame实现贪吃蛇游戏
    本文实例为大家分享了pygame实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 为了简化起见,游戏素材暂定为两张简单的图片(文中用的是30*30)。大家很方便就能制作。 背景也...
    99+
    2024-04-02
  • python实现贪吃蛇游戏
    文章目录 1、效果2、实现过程3、代码 1、效果 2、实现过程 导入 Pygame 和 random 模块。初始化 Pygame。设置游戏界面大小、背景颜色和游戏标题。定义颜色常量。...
    99+
    2023-09-29
    python 游戏 pygame
  • 如何实现贪吃蛇Python小游戏
    这篇文章主要介绍“如何实现贪吃蛇Python小游戏”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“如何实现贪吃蛇Python小游戏”文章能帮助大家解决问题。贪吃蛇Python小游戏(源码+注释+粘贴即...
    99+
    2023-07-05
  • 如何使用HTML5实现贪吃蛇游戏
    这篇文章将为大家详细讲解有关如何使用HTML5实现贪吃蛇游戏,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。 游戏操作说明 通过方向键控制贪吃蛇上下左右移动。贪吃蛇吃到...
    99+
    2024-04-02
  • pygame实现贪吃蛇小游戏
    本文实例为大家分享了pygame实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 由于这段时间实在是太聊了,没什么事做,游戏也玩腻了,所以玩起来pygame。pygame是真的容...
    99+
    2024-04-02
  • C#实现贪吃蛇小游戏
    本文实例为大家分享了C#实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 之前一直想写一个贪吃蛇小游戏,上个周末终于有时间做了一个,现在和大家分享。 界面 界面比较简单,一个按钮...
    99+
    2024-04-02
  • C++如何实现简易贪吃蛇游戏
    这篇文章主要介绍C++如何实现简易贪吃蛇游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!C++实现建议贪吃蛇(不会闪屏幕)使用vs2013完成。记录踏上游戏开发的道路。效果图代码// 2021.7.24....
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作