贪吃蛇02---用java script完成贪吃蛇-程序员宅基地

技术标签: ViewUI  java  javascript  

<!DOCTYPE html>
<html>
  <head>
  <meta charset="UTF-8">
    <title>贪吃蛇</title>
  </head>
  
  <body>
   <canvas id="game" width="450" height="450" style="border:1px solid red;margin:30px auto;display:
   block;"></canvas>
   <script type="text/javascript">
     var canvas =  document.getElementById("game");
     //拿到上下文,画图工具
     var cxt = canvas.getContext("2d");
     //每个格子的宽度
     var width = 15;
     //面向对象,蛇的身体是一节一节的
     /*
        定义一个Cell类,具备x和y还具备f
        x和y代表格子的坐标,f代表方向
        f方向
        1代表左,-1代表右
        2代表下,-2代表上
     */
    function Cell(x,y,f){
        this.x=x;
        this.y=y;
        this.f=f;
    }
    function Food(x,y){
        this.x=x;
        this.y=y;
    }
    //蛇的对象  蛇是由Cell组成的
    var snake =[];
    var length = 2;//蛇的长度
    var speed = 100;//100毫秒移动一个单元格
    var food= new Food(15,15);
    //初始化蛇的身体
    for(var i=0;i<length;i++){
     snake[i] = new Cell(i, 0, -1)
    }
    for(var i = 0;i<snake.length;i++){
    var cell= snake[i];
       cxt.fillStyle = "gray";
       if(i==snake.length-1){
         cxt.fillStyle="red";
       }
       cxt.beginPath();
       cxt.rect(cell.x*width ,cell.y*width,width,width);
       cxt.closePath();
       cxt.fill();
    }
     cxt.fillStyle = "green";
       cxt.beginPath();
       cxt.rect(food.x*width ,food.y*width,width,width); 
       cxt.closePath();
       cxt.fill();
       
       //怎么让蛇头跟着键盘动,监听键盘事件
       document.onkeydown = function(e){
         var code = e.keyCode;
         var direction = 0;
         switch(code){
          case 37:direction = 1;break;
          case 38:direction = 2;break;
          case 39:direction = -1;break;
          case 40:direction = -2;break;
          case 87:direction = 2;break;
          case 65:direction = 1;break;
          case 83:direction = -2;break;
          case 68:direction = -1;break;
         }//switch
         var head = snake[snake.length-1];
         if(direction!=0&&(head.f+direction)!=0){
         //调用蛇的移动方法
         moveSnake(direction);
         }//if(direction)
         
       }//function(e)
       function moveSnake(direction){
      
         var head = snake[snake.length-1];//蛇头
         if(!direction){
           direction=head.f;
         }
         var newSnake = [];
         var newHead = new Cell(head.x, head.y, head.f);
         for(var i = 1;i<snake.length;i++){
            newSnake[i-1] = snake[i];
         }
           newHead.f=direction;
           //修改x和y的值
           switch(direction){
            case 1:newHead.x-- ;break;
             case 2:newHead.y-- ;break;
              case -1:newHead.x++ ;break;
               case -2:newHead.y++  ;break;
           }
           newSnake[newSnake.length]=newHead
           snake=newSnake;
           gameOver();
           draw();
         }
          function gameOver(){
         //检查撞到游戏的边框
         var head = snake[snake.length-1];
           if(head.x>=30||head.y>=30||head.x<0||head.y<0){
              alert("撞墙,游戏结束");
              window.location.reload();
           }
           //不能咬到自己
           for(var i=0;i < snake.length-1;i++){
             if(snake[i].x == head.x && snake[i].y == head.y){
                           alert("撞到自己了,游戏结束");
              window.location.reload();
             }
           }
          }
         function draw(){
          cxt.clearRect(0, 0, 450, 450);
       cxt.fillStyle = "green";
       cxt.beginPath();
       cxt.rect(food.x*width ,food.y*width,width,width); 
       cxt.closePath();
       cxt.fill();
       //判断蛇的蛇头是不是和食物的坐标重合
       var head = snake[snake.length-1];
        if(head.x==food.x&&head.y==food.y){
          var newHead = new Cell(head.x, head.y, head.f);
           //修改x和y的值
           switch(newHead.f){
            case 1:newHead.x-- ;break;
             case 2:newHead.y-- ;break;
              case -1:newHead.x++ ;break;
               case -2:newHead.y++  ;break;
           }
           snake[snake.length] = newHead; 
           randFood(); 
        }
         for(var i = 0;i<snake.length;i++){
         var cell= snake[i];
         cxt.fillStyle = "gray";
         if(i==snake.length-1){
         cxt.fillStyle="red";
       }
       cxt.beginPath();
       cxt.rect(cell.x*width ,cell.y*width,width,width);
       cxt.closePath();
       cxt.fill();
       }
         }
         function randFood(){
            food.x=Math.ceil(Math.random()*28)+1;
            food.y=Math.ceil(Math.random()*28)+1;
         }
         var autoRun = setInterval(moveSnake,speed); 
    </script>
  </body>
</html>
 

 

转载于:https://www.cnblogs.com/miria-486/p/8472613.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30695195/article/details/99010148

智能推荐

Ubuntu/Windows Python 调用matlab(conda虚拟环境)_error: could not create 'build': 拒绝访问。-程序员宅基地

记录下自己遇到的问题,不管是在windows还是linux下,安装完matlab,想要用python调用matlab,关键是在matlab的安装目录下的extern/engines/python下,运行python setup.py install 即可。由于我都是用的conda的python虚拟环境,所以强调下conda的虚拟环境下怎么做windows下注意使用管理员权限打开conda控..._error: could not create 'build': 拒绝访问。

Wiki基础知识_wiki知识-程序员宅基地

首先,我们要搞清楚:wiki概念的发明人是Ward Cunningham。wiki这个字到底是什幺意思呢?根据FAQ的说法,WikiWiki一词来源于夏威夷语的“wee kee wee kee”,原本是“快点快点”(quick)的意思。实际上 wiki 也真的是既简单又快速,你可以看到 wiki 每天都在成长。 新概念的定义总是让人有点摸不着头脑,wiki 也不例外。先看看简单解释:Wiki-_wiki知识

hadoop集群搭建过程中所遇问题总结_hadoop集群从节点没有tmp文件-程序员宅基地

在搭建过程中遇到了些问题,个人认为不是很典型。_hadoop集群从节点没有tmp文件

SpringBoot项目运行时出现A cookie header was received警告问题_note: further occurrences of this error will be lo_嘟嘟的程序员铲屎官的博客-程序员宅基地

运行项目的时候出现A cookie header was received[Hm_lvt_eaa57ca47dacb4ad4f5a257001a3457c=1656209978,1657770127,1657812148,1657850976;] that contained an invalid cookie. That cookie will be ignored.Note: further occurrences of t警告信息,对异常进行记录。_note: further occurrences of this error will be logged at debug level.

ubuntu14.04 gitweb搭建-程序员宅基地

本地配置gitweb服务1.先通过mirror命令clone下载mirror镜像(非完整代码) repo init -u ssh://account@server:port/platform/manifest -b branch_name --mirror repo sync2.先安装 gitweb和 apache2 sudo apt-get install apache2 git-...

python animation save_阅读许多文件并创建animationpython-程序员宅基地

我写了脚本,从fits文件创建动画(电影)。一个文件的大小为2.8 MB,文件数为9000个。这是密码import numpy as npimport matplotlibmatplotlib.use("Agg")import matplotlib.pyplot as pltimport matplotlib.animation as animationimport osimport pyfits...

随便推点

python文件处理:解析.xml文件-程序员宅基地

XML(eXtensible Markup Language)指可扩展标记语言,被设计用来传输和存储数据,已经日趋成为当前许多新生技术的核心,在不同的领域都有着不同的应用。它是web发展到一定阶段的必然产物,既具有SGML的核心特征,又有着HTML的简单特性,还具有明确和结构良好等许多新的特性。在做目标检测手工标注数据集的时候,通常会把标注的目标位置信息写入一个.xml文件,但在检查数据和数据清洗的

pythondjango开发实例_简单的python django框架开发例子1-程序员宅基地

在此目录下/home/ubuntu/mysite/mysite(目录可能有所不同,但都是在mysite/mysite下)下创建 views.py 文件内容如下:from django.http import HttpResponse,Http404import datetimeimport osdef sayhi(request):return HttpResponse('Hello World,...

网页版blibli如何下载视频?-程序员宅基地

在使用网页版blibli的时候想要下载视频却没有找到对应的地方,于是在网上搜寻了一些教程,记录于此,以便日后再次查看。方法一:1、选择你要下载的视频,复制其网页地址2、在blibli的网址.com 前加上jj再回车3、在新打开的页面中输入你在第一步复制的网址,再回车在这一步中,你也可以直接使用关键词进行搜索4、然后选择右边的格式即可下载方法二用方法一下载时有时会遇到以下问题...

listview空数据的时候显示。_listview中为什么空值也能加入-程序员宅基地

首先当空listview为空的时候我们,我们需要给出一些友好的提示,这个时候我们就会需要用到相关的一些fan_listview中为什么空值也能加入

MAC IDEA安装与常用插件与环境配置_idea mac安装插件-程序员宅基地

MAC IDEA 的安装于环境配置Reference: https://blog.csdn.net/mjshuang/article/details/91448545一、检查jdk1. 安装jdk 1.8在terminal中输入Java -version_idea mac安装插件

HTML+CSS搜索框拼接/CSS3实现背景颜色渐变_css 同时设置边框和背景的渐变-程序员宅基地

HTML/CSS搜索框核心:搜索框大盒子不要加宽高,里面文本输入框与搜索的button用float拼接<div class="search"> <input type="text" placeholder="请输入关键字"> <button></button></div>.header .search input { float: left; padding-left: 16px; width:_css 同时设置边框和背景的渐变

推荐文章

热门文章

相关标签