时间:2021-05-25
本文实例为大家分享了js canvas实现简单贪吃蛇的具体代码,供大家参考,具体内容如下
Js原生贪吃蛇:canvas
HTML
<canvas id="can"></canvas>CSS
#can{ background: #000000; height: 400px; width: 850px;}JS
//公共板块var blockSize = 10;//地图的高宽var mapW = 300;var mapH = 150;//历史食物varvar historyfood = new Array();//食物数组var img = new Image()var arrFood = ["ananas.png","hamburg.png","watermelon.png"]historyfood =[{x: 0,y:0}];//贪吃蛇默认值var snake = [{x:3,y:0},{x:2,y:0},{x:1,y:0}]//贪吃蛇方向var directionX = 1;var directionY = 0;//添加一个标记,标记食物是否被吃掉//默认值:没有被吃掉var isFood = false;//判断游戏状态//默认游戏继续var gameState = false;//限制贪吃蛇所移动的方向不能反方向操作var lockleft = true;var lockright = true;var lockup = true;var lockdown = true;//贪吃蛇分数var count = 0;//贪吃蛇速度var speed = 1000 - (count + 5);$(function () { $("#divContainer").height($("#can").height()); //1,获取到画布对象 var can = document.getElementById("can"); //2,获取到画图工具箱 var tools = can.getContext('2d'); tools.beginPath(); //设置食物默认值 var XY = Randomfood(); console.log(XY); var x1 = Randomfood().x; var y1 = Randomfood().y; img.src = "/aimless/img/GluttonousSnakeFood/"+ arrFood[Math.floor(Math.random() * arrFood.length)]; //控制贪吃蛇移动 document.addEventListener('keydown',function (e){ switch (e.keyCode) { case 38: if (lockup){ directionX = 0; directionY = -1; lockdown = false; lockleft = true; lockright = true; } break; case 40: if (lockdown){ directionX = 0; directionY = 1; lockup = false; lockleft = true; lockright = true; } break; case 37: if (lockleft){ directionX = - 1; directionY = 0; lockright = false; lockup = true; lockdown = true; } break; case 39: if (lockright){ directionX = 1; directionY = 0; lockleft = false; lockup = true; lockdown = true; } break; } }) setIntervalSnake(tools,x1,y1); //4,找位置})function setIntervalSnake(tools,x1,y1){ setInterval(function (){ if (gameState){ return; } //1,擦除画板 tools.clearRect(0,0,850,420); var oldHead = snake[0]; if (oldHead.x < 0 || oldHead.y < 0 || oldHead.x * blockSize >= mapW || oldHead.y * blockSize >= mapH){ gameState = true; alert("游戏结束"); }else { //蛇移动 if (snake[0].x * blockSize === x1 && snake[0].y * blockSize === y1){ isFood = true; } else { snake.pop() } var newHead = { x: oldHead.x + directionX, y: oldHead.y + directionY } snake.unshift(newHead); } //2,判断食物是否被吃掉,吃掉刷新,不吃掉不刷新 if (isFood){ count = count + 1; $("#count").html(count); x1 = Randomfood().x; y1 = Randomfood().y; img.src = "/aimless/img/GluttonousSnakeFood/"+ arrFood[Math.floor(Math.random() * arrFood.length)]; isFood = false; } tools.drawImage(img,x1,y1,blockSize,blockSize); //蛇身数组 var Thesnakebody = new Array(); //3,画蛇 for (var i = 0; i < snake.length; i++){ if (i == 0){ tools.fillStyle = "#9933CC"; } else { var newHead1 = { x: snake[i].x, y: snake[i].y } Thesnakebody.unshift(newHead1); tools.fillStyle = "#33adcc"; } tools.fillRect(snake[i].x * blockSize,snake[i].y * blockSize,blockSize,blockSize); } // //判断蛇头咬到了蛇身 Thesnakebody.forEach(item=>{ if(item.x == snake[0].x && item.y == snake[0].y){ gameState = true; setIntervalSnake(tools,x1,y1); } }) //4,画地图 var width = Math.round($("#can").width() / blockSize); var height = Math.round($("#can").height() / blockSize); for (var i = 1; i < width;i++){ tools.moveTo(0,blockSize * i); tools.lineTo($("#can").width(),blockSize * i); } for (var i = 1; i < height;i++){ tools.moveTo(blockSize * i,0); tools.lineTo(blockSize * i,$("#can").height()); } tools.strokeStyle = "#FFFFFF"; //5,绘制 tools.stroke(); },speed / 3);}//随机食物不function Randomfood() { var RandomX = Math.floor(Math.random() * mapW / blockSize) * blockSize; var RandomY = Math.floor(Math.random() * mapH / blockSize) * blockSize; setInterval(function (){ snake.forEach(item=>{ console.log(RandomX / blockSize,RandomY / blockSize); // console.log(item.x,item.y); if(item.x == RandomX / blockSize && item.y == RandomY / blockSize){ return Randomfood(); } else { return ; } }) }, 10 / 3); var newRandom = { x: RandomX, y: RandomY } return newRandom;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了原生js实现的贪吃蛇网页版游戏。分享给大家供大家参考。具体实现方法如下:原生js写的贪吃蛇网页版游戏Star={init:function(){v
本文实例为大家分享了canvas实现贪吃蛇效果的具体代码,供大家参考,具体内容如下效果展示:源码展示:页面布局展示:worm.html贪吃蛇canvas{bor
使用原生js实现贪吃蛇小游戏,首先这个小游戏的目录结构如下:有贪吃蛇,食物,地图,还有游戏当我们在浏览器打开index.html的时候,会出现移动的小蛇,随机生
本文实例为大家分享了Javascript实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下前言原生JavaScript实现贪吃蛇小游戏GitHub地址直接复制
javascript实现简单的贪吃蛇游戏,功能很简单,代码也很实用,就不多废话了,小伙伴们参考注释吧。贪吃蛇varmap;//地图varsnake;//蛇var