原生js实现贪吃蛇游戏


Posted in Javascript onOctober 26, 2020

原生JavaScript实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

原生js实现贪吃蛇游戏

代码:

<script>
 var timer = null;
 var oMain = document.getElementById("main");
 
 function Map(atom,xnum,ynum){//地图,设置单位大小,及根据单位大小创建地图
  this.atom = atom;
  this.xnum = xnum;
  this.ynum = ynum;
  
  this.create = function(){
  
  this.canvas = document.createElement("div");
  this.canvas.style.cssText = "position: relative;top: 40px;border: 1px solid red;background: #F1F1F1;"
  this.canvas.style.width = this.atom * this.xnum + "px";//画布宽
  this.canvas.style.height = this.atom * this.ynum + "px";//画布高
  main.appendChild(this.canvas);
  }
 }
 
 function Food(map){//食物
  this.width = map.atom;
  this.height = map.atom;
  //实现随机背景色
  this.bgColor = "rgb("+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+")";
 
  this.x = Math.floor(Math.random()*map.xnum);
  this.y = Math.floor(Math.random()*map.ynum);
  
  this.flag = document.createElement('div');
  this.flag.style.width = this.width + 'px';
  this.flag.style.height = this.height + 'px';
  this.flag.style.backgroundColor = this.bgColor;
  this.flag.style.borderRadius = '50%';
  this.flag.style.position = "absolute";
  this.flag.style.left = this.x * this.width + 'px';
  this.flag.style.top = this.y * this.height + 'px';
  
  map.canvas.appendChild(this.flag);
 }
 
 //重新开始
 function restart(map,snake){
  for(var i=0; i<snake.body.length; i++){
  map.canvas.removeChild(snake.body[i].flag);
  }
  snake.body = [
  {x : 2,y : 0},//蛇头
  {x : 1,y : 0},//蛇身
  {x : 0,y : 0}//蛇尾
  ]
  snake.direction = "right";
  snake.display();
  
  map.canvas.removeChild(food.flag);
  food = new Food(map);
 }
 
 function Snake(map){
  this.width = map.atom;
  this.height = map.atom;
  
  this.direction = "right";
  
  this.body = [
  {x : 2,y : 0},
  {x : 1,y : 0},
  {x : 0,y : 0}
  ]
  
  this.display = function(){
  for(var i=0; i<this.body.length; i++){
   if(this.body[i].x != null){
   var s = document.createElement('div');
   //将节点保存
   this.body[i].flag = s;
   
   //设置样式
   s.style.width = this.width + 'px';
   s.style.height = this.height + 'px';
   s.style.backgroundColor = "rgb("+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+")";
   s.style.borderRadius = '50%';
   
   //设置位置
   s.style.position = "absolute";
   s.style.left = this.body[i].x * this.width + 'px';
   s.style.top = this.body[i].y * this.height + 'px';
   
   //添加到地图
   map.canvas.appendChild(s);
   }
  }
  }
  
  this.run = function(){
  for(var i=this.body.length-1; i>0; i--){
   this.body[i].x = this.body[i-1].x;
   this.body[i].y = this.body[i-1].y
   
  }
  switch(this.direction){
   case "left":this.body[0].x -= 1;break;
   case "right":this.body[0].x += 1;break;
   case "up":this.body[0].y -= 1;break;
   case "down":this.body[0].y += 1;break;
  }
  //吃食物
  if(this.body[0].x == food.x && this.body[0].y == food.y){
   this.body.push({x : null,y : null,flag : null});
   
   map.canvas.removeChild(food.flag);
   food = new Food(map);
  }
  
  //判断游戏结束
  if(this.body[0].x<0 || this.body[0].x>map.xnum-1 || this.body[0].y<0 || this.body[0].y>map.ynum-1){
   clearInterval(timer);
   alert("GAME OVER!");
   restart(map,this);
   return false;
  }
  for(var i=4; i<this.body.length; i++){
   if(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y){
   clearInterval(timer);
   alert("GAME OVER!");
   restart(map,this);
   return false;
   }
  }
  
  //删除原来
  for(var i=0; i<this.body.length; i++){
   if(this.body[i].flag != null){
   map.canvas.removeChild(this.body[i].flag);
   }
  }
  this.display();
  }
 }

 
 //创建地图对象
 var map = new Map(20,40,20);
 map.create();
 
 //创建食物对象
 var food = new Food(map);
 
 //创建蛇对象
 var snake = new Snake(map);
 snake.display();
 
 
 //加上键盘事件(改变蛇头方向)
 window.onkeydown = function(e){
  event = e || window.event;
  
  switch(event.keyCode){
  case 38:
   if(snake.direction != "down")//禁止掉头
   snake.direction = "up";
  break;
  case 40:
   if(snake.direction != "up")
   snake.direction = "down";
  break;
  case 37:
   if(snake.direction != "right")
   snake.direction = "left";
  break;
  case 39:
   if(snake.direction != "left")
   snake.direction = "right";
  break;
  }
 }
 
 var speed = 100;
 
 function start(){
  clearInterval(timer);
  timer = setInterval(function(){
  snake.run();
  document.getElementById("score").innerHTML = snake.body.length-3;
  start();
  },speed)
 }
 
 //难度
 document.getElementById("1").onclick = function(){
  speed = 100;
 }
 
 document.getElementById("2").onclick = function(){
  speed = 50;
 }
 
 document.getElementById("3").onclick = function(){
  speed = 20;
 }
 
 document.getElementById("begin").onclick = function(){
  start();
 }
 
 document.getElementById("pause").onclick = function(){
  
  clearInterval(timer);
 }
</script>

代码仅有js部分,完整代码可以上我的github免费下载

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jQuery获取文本节点之 text()/val()/html() 方法区别
Mar 01 Javascript
javascript学习笔记(二) js一些基本概念
Jun 18 Javascript
浅谈JavaScript Array对象
Dec 29 Javascript
基于Echarts 3.19 制作常用的图形(非静态)
May 19 Javascript
vue-cli 自定义指令directive 添加验证滑块示例
Oct 19 Javascript
javacript replace 正则取字符串中的值并替换【推荐】
Sep 13 Javascript
vue axios请求频繁时取消上一次请求的方法
Nov 10 Javascript
javascript json字符串到json对象转义问题
Jan 22 Javascript
js实现unicode码字符串与utf8字节数据互转详解
Mar 21 Javascript
JS左右无缝轮播功能完整实例
May 16 Javascript
layui 根据后台数据动态创建下拉框并同时默认选中的实例
Sep 02 Javascript
js判断一个对象是数组(函数)的方法实例
Dec 19 Javascript
JavaScript实现五子棋小游戏
Oct 26 #Javascript
详解vue3.0 的 Composition API 的一种使用方法
Oct 26 #Javascript
vue3.0 加载json的方法(非ajax)
Oct 26 #Javascript
Vue+Element自定义纵向表格表头教程
Oct 26 #Javascript
vue3.0 的 Composition API 的使用示例
Oct 26 #Javascript
Vue中用JSON实现刷新界面不影响倒计时
Oct 26 #Javascript
jQuery实现简单评论区功能
Oct 26 #jQuery
You might like
使用NetBeans + Xdebug调试PHP程序的方法
2011/04/12 PHP
php 数组使用详解 推荐
2011/06/02 PHP
分享ThinkPHP3.2中关联查询解决思路
2015/09/20 PHP
THINKPHP-Apache服务器中使用Alias虚拟目录URL重写 隐藏index.php
2021/03/09 PHP
jquery 简单图片导航插件jquery.imgNav.js
2010/03/17 Javascript
使用Mootools动态添加Css样式表代码,兼容各浏览器
2011/12/12 Javascript
javascript中注册和移除事件的4种方式
2013/03/20 Javascript
优化Jquery,提升网页加载速度
2013/11/14 Javascript
判断客户浏览器是否支持cookie的示例代码
2013/12/23 Javascript
JavaScript的21条基本知识点
2014/03/04 Javascript
JavaScript作用域链示例分享
2014/05/27 Javascript
jQuery模拟新浪微博首页滚动效果的方法
2015/03/11 Javascript
轻松学习Javascript闭包函数
2015/12/15 Javascript
JavaScript统计字符串中每个字符出现次数完整实例
2016/01/28 Javascript
让html元素随浏览器的大小自适应垂直居中的实现方法
2016/10/12 Javascript
基于JS实现仿百度百家主页的轮播图效果
2017/03/06 Javascript
微信小程序利用co处理异步流程的方法教程
2017/05/20 Javascript
JS奇技之利用scroll来监听resize详解
2017/06/15 Javascript
Vue.js + Nuxt.js 项目中使用 Vee-validate 表单校验
2019/04/22 Javascript
Vue 动态添加路由及生成菜单的方法示例
2019/06/20 Javascript
微信小程序swiper禁止用户手动滑动代码实例
2019/08/23 Javascript
解决Vue.js应用回退或刷新界面时提示用户保存修改问题
2019/11/24 Javascript
浅谈Vue static 静态资源路径 和 style问题
2020/11/07 Javascript
Python实现图片转字符画的示例
2017/08/22 Python
pyqt5简介及安装方法介绍
2018/01/31 Python
tensorflow 获取模型所有参数总和数量的方法
2018/06/14 Python
用python3教你任意Html主内容提取功能
2018/11/05 Python
python操作docx写入内容,并控制文本的字体颜色
2020/02/13 Python
基于canvas的骨骼动画的示例代码
2018/06/12 HTML / CSS
精灵市场:Pixie Market
2019/06/18 全球购物
自然健康的概念:Natural Healthy Concepts
2020/01/26 全球购物
Ajax的工作原理
2015/12/04 面试题
优秀的毕业生的自我评价
2013/12/12 职场文书
经销商订货会主持词
2014/03/27 职场文书
财务会计求职信范文
2015/03/20 职场文书
给学校的建议书400字
2015/09/14 职场文书