Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153798
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 399
  • 用 户 组: 普通用户
  • 注册时间: 2015-09-23 11:47
个人简介

程序猿啊程序猿

文章分类

全部博文(42)

文章存档

2016年(28)

2015年(14)

我的朋友

分类: JavaScript

2016-07-01 08:07:03



点击(此处)折叠或打开

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>贪吃蛇</title>
  6.     <style>
  7.       *{
  8.           padding:0;
  9.           margin:0;
  10.       }
  11.       #myCanvas{
  12.           position: absolute;
  13.           top:50px;
  14.           left:200px;
  15.       }
  16.       #handle{
  17.         position: absolute;
  18.         top:50px;
  19.         width:400px;
  20.         height: 400px;
  21.         right:100px;
  22.       }
  23.       button{
  24.         position: absolute;
  25.         width: 50px;
  26.         height: 30px;
  27.         font-size: 16px;
  28.         line-height: 30px;
  29.         text-align: center;
  30.       }
  31.       #total{
  32.         color: red;
  33.         font-size: 20px;
  34.         width: 40px;
  35.         text-align: center;
  36.         display: inline-block;
  37.         height: 30px;
  38.         line-height: 30px;
  39.       }
  40.       #right{
  41.         left:100px;
  42.         top:80px;
  43.       }
  44.       #bottom{
  45.         top:110px;
  46.         left: 50px;
  47.       }
  48.       #left{
  49.         top:80px;
  50.       }
  51.       #top{
  52.         left: 50px;
  53.         top:50px;
  54.       }
  55.       #pause,#begin{
  56.         left: 50px;
  57.         top:80px;
  58.         border:1px solid #ccc;
  59.       }
  60.       #begin{
  61.         display: none;
  62.       }
  63.       #restart{
  64.         left: 200px;
  65.         width: 80px;
  66.         height: 30px;
  67.         line-height: 30px;
  68.         color: red;
  69.       }
  70.     </style>
  71. </head>
  72. <body>
  73.     <canvas id="myCanvas" width="600" height="600"></canvas>
  74.   <div id="handle">
  75.       <p>吃掉的数量:<span id="total">0</span><p>
  76.       <button id="left" onclick="keydown(37);"></button>
  77.       <button id="right" onclick="keydown(39);"></button>
  78.       <button id="top" onclick="keydown(38);"></button>
  79.       <button id="bottom" onclick="keydown(40);"></button>
  80.       <button id="pause">暂停</button>
  81.       <button id="begin">开始</button>
  82.       <button id="restart">重新开始</button>
  83.   </div>
  84.     <script>
  85.   <!--
  86. //思路:1、先画蛇身,再画随机出现的食物。2、蛇身的每一个矩形块的左上角坐标都放在一个数组中,运动中的第一个矩形就是数组的最后一个元素,通过push,unshift,shift操作数组来实现蛇身的绘制。3、判断蛇身是否撞到边界或者自身来判断游戏结束与否
  87.   -->
  88.       var c=document.getElementById('myCanvas');
  89.       var ctx=c.getContext('2d');
  90.       var RW=20,RH=20; //小方格的长宽
  91.       var CW=600,CH=600; //画布长宽
  92.       var cols=30,rows=30; //画布的行数,列数
  93.       var snackL=5; //开始时蛇的长度
  94.       var a=[]; //储存蛇身各个矩形块左上角坐标的数组,
  95.       var x,y; //蛇身的小矩形的左上角坐标
  96.       var goto=3; //蛇运动的方向 0:上,1:下,2:左,3:右
  97.       var foodX,foodY; //随机食物的左上角坐标
  98.       var timer; //游戏定时器
  99.       var total=0; //吃掉的总数
  100.       var speed=150; //蛇运动的速度
  101.       function draw(){
  102.         ctx.clearRect(0,0,CW,CH);
  103.           //画方格
  104.           for(var i=0;i<=rows;i++){
  105.               ctx.beginPath(); //没有beginPath,会重复线条导致线条变粗
  106.               ctx.moveTo(0,i*RH);
  107.               ctx.lineTo(CW,i*RH);
  108.               ctx.strokeStyle="gray";
  109.               ctx.stroke();
  110.           }
  111.           for(var i=0;i<=cols;i++){
  112.               ctx.beginPath();
  113.               ctx.moveTo(i*RW,0);
  114.               ctx.lineTo(i*RW,CH);
  115.               ctx.stroke();
  116.           }
  117.         
  118.         //画蛇
  119.         for(i=0;i<snackL;i++){
  120.           ctx.beginPath();
  121.             ctx.moveTo(a[i].x,a[i].y);
  122.           ctx.lineTo(a[i].x+RW,a[i].y);
  123.           ctx.lineTo(a[i].x+RW,a[i].y+RH);
  124.           ctx.lineTo(a[i].x,a[i].y+RH);
  125.           ctx.closePath(); //闭合路线画出矩形
  126.           ctx.strokeStyle="white";
  127.           ctx.stroke();
  128.           ctx.fillStyle="blue";
  129.           ctx.fillRect(a[i].x,a[i].y,RW,RH);
  130.         }

  131.           ctx.beginPath(); //画出随机食物
  132.           ctx.moveTo(foodX,foodY);
  133.           ctx.lineTo(foodX+RW,foodY);
  134.           ctx.lineTo(foodX+RW,foodY+RH);
  135.           ctx.lineTo(foodX,foodY+RH);
  136.           ctx.strokeStyle="green";
  137.           ctx.stroke();
  138.           ctx.closePath();
  139.           ctx.fillStyle="red";
  140.           ctx.fillRect(foodX,foodY,RW,RH);
  141.       }
  142.       function random(){ //产生随机食物坐标
  143.           foodX=Math.floor(Math.random()*30)*RW;
  144.           foodY=Math.floor(Math.random()*30)*RH;
  145.       }
  146.       function init(){
  147.         for(var i=0;i<snackL;i++){
  148.           a[i]={x:i*RW,y:0};
  149.         }
  150.           random();
  151.           draw();
  152.       }
  153.       init(); //游戏初始化
  154.       timer=setInterval(snackM,speed);
  155.       function snackM(){
  156.         switch(goto){
  157.           case 0: //上
  158.                 a.push({x:a[snackL-1].x,y:a[snackL-1].y-RH}); //向上移动时,a数组后面添加一个元素
  159.           break;
  160.           case 1: //下
  161.                 a.push({x:a[snackL-1].x,y:a[snackL-1].y+RH}); //向下移动时,a数组后面添加一个元素
  162.           break;
  163.           case 2: //左
  164.                a.push({x:a[snackL-1].x-RW,y:a[snackL-1].y}); //向左移动时,a数组后面添加一个元素(蛇运动的第一个为数组a中最后一个)
  165.           break;
  166.           case 3: //右
  167.                a.push({x:a[snackL-1].x+RW,y:a[snackL-1].y}); //向右移动时,a数组后面添加一个元素
  168.           break;
  169.         }
  170.         a.shift();
  171.         draw();
  172.         eat();
  173.         die();
  174.       }

  175.       document.onkeydown=function(e){ //键盘改变蛇运动方向
  176.          var e=e||window.event;
  177.          // if(e.keyCode==37){ //左
  178.          // if(goto!=3&&goto!=2){goto=2}
  179.          // }
  180.          // if(e.keyCode==39){ //右
  181.          // if(goto!=3&&goto!=2){goto=3};
  182.          // }
  183.          // if(e.keyCode==38){ //上
  184.          // if(goto!=1&&goto!=0){goto=0}
  185.          // }
  186.          // if(e.keyCode==40){ //下
  187.          // if(goto!=0&&goto!=1){goto=1}
  188.          // }
  189.          keydown(e.keyCode);
  190.       }
  191.       function keydown(keyCode){ //网页按钮事件
  192.         switch(keyCode){
  193.           case 37:if(goto!=3&&goto!=2){goto=2};break;
  194.           case 39:if(goto!=3&&goto!=2){goto=3};break;
  195.           case 38:if(goto!=1&&goto!=0){goto=0};break;
  196.           case 40: if(goto!=0&&goto!=1){goto=1};break;
  197.         }
  198.       }
  199.       function eat(){
  200.         if(a[snackL-1].x==foodX&&a[snackL-1].y==foodY){
  201.           total++;
  202.           document.getElementById("total").innerHTML=total;
  203.           snackL++;
  204.           a.unshift({x:1,y:1}) //添加的元素在下次执行snackM()时又被删除了,这里可以在头部添加任何值
  205.           random();
  206.         }
  207.       }
  208.       function die(){
  209.         if(a[snackL-1].x>CW-RW||a[snackL-1].x<0||a[snackL-1].y>CH-RH||a[snackL-1].y<0){
  210.           clearInterval(timer); //蛇撞到边界时
  211.           alert("游戏结束");
  212.           return;
  213.         }
  214.         for(var i=1;i<snackL-1;i++){ //蛇撞到除了第一个矩形外的其他矩形时
  215.           if(a[snackL-1].x==a[i].x&&a[snackL-1].y==a[i].y){
  216.             clearInterval(timer);
  217.             alert("游戏结束");
  218.             return;
  219.           }
  220.         }
  221.       }
  222.       document.getElementById('begin').onclick=function(){
  223.          this.style.display="none";
  224.          document.getElementById('pause').style.display="block";
  225.          timer=setInterval(snackM,speed);

  226.       }
  227.       document.getElementById('pause').onclick=function(){
  228.         this.style.display="none";
  229.         document.getElementById('begin').style.display="block";
  230.         clearInterval(timer);
  231.       }
  232.       document.getElementById('restart').onclick=function(){
  233.         window.history.go(0); //返回上一个网页
  234.       }
  235.     </script>
  236. </body>
  237. </html>


阅读(2199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~