网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 html5实现经典坦克大战坦克乱走还能发出一个子弹
内容
    代码如下:
    <pre name=code class=html>tank.html</pre><pre name=code class=html><!doctype html>
    <html>
    <head>
    <meta charset=utf-8/>
    </head>
    <body onkeydown=getcommand();>
    <h1>hmtl5-经典的坦克大战</h1>
    <!--坦克大战的战场-->
    <canvas id=tankmap width=400px height=300px style=background-color:black></canvas>
    <span id=aa>数据</span>
    <!--把tankgames.js引入到本页面-->
    <script type=text/javascript src=tank.js></script>
    <script type=text/javascript>
    //得到画布
    var canvas1=document.getelementbyid(tankmap);
    //得到绘图上下文(你可以理解是画笔)
    var cxt=canvas1.getcontext(2d);
    //我的坦克 hero
    //方向
    var hero=new hero(140,140,0,herocolor);
    //定义一颗空子弹
    var herobullet=null;
    //定义敌人的坦克(敌人的坦克有多少? 思路 : 是单个单个的定义,还是放在数组中?)
    var enemytanks=new array();
    //先死后活 ,定3个,后面我们把敌人坦克的数量,作出变量
    //0->上, 1->右, 2->下 3->左
    for(var i=0;i<3;i++){
    //创建一个坦克
    var enemytank=new enemytank((i+1)*50,0,2,enmeycolor);
    //把这个坦克放入数组
    enemytanks[i]=enemytank;
    }
    //先调用一次
    flashtankmap();
    //专门写一个函数,用于定时刷新我们的作战区,把要在作战区出现的元素(自己坦克,敌人坦克,子弹,炸弹,
    //障碍物...)->游戏思想
    function flashtankmap(){
    //把画布清理
    cxt.clearrect(0,0,400,300);
    //我的坦克
    drawtank(hero);
    //画出自己的子弹
    //子弹飞效果是怎么出现的?[答 : 首先我们应该每隔一定时间(setinterval)就去刷新作战区,如果在刷新的时候,子弹坐标变换了,给人的感觉就是子弹在飞!]
    drawherobullet();
    //敌人的坦克
    //画出所有敌人坦克
    for(var i=0;i<3;i++){
    drawtank(enemytanks[i]);
    }
    }
    //这是一个接受用户按键函数
    function getcommand(){
    //我怎么知道,玩家按下的是什么键
    //说明当按下键后 事件--->event对象----->事件处理函数()
    var code=event.keycode;//对应字母的ascii码->我们看码表
    switch(code){
    case 87://上
    hero.moveup();
    break;
    case 68:
    hero.moveright();
    break;
    case 83:
    hero.movedown();
    break;
    case 65:
    hero.moveleft();
    break;
    case 74:
    hero.shotenemy();
    break;
    }
    //触发这个函数 flashtankmap();
    flashtankmap();
    //重新绘制所有的敌人的坦克.你可以在这里写代码(思想,我们干脆些一个函数,专门用于定时刷新我们的画布[作战区])
    }
    //每隔100毫秒去刷新一次作战区
    window.setinterval(flashtankmap(),100);
    </script>
    </body>
    </html></pre>
    tank.js
    代码如下:
    <pre></pre>
    <pre name=code class=html><pre name=code class=javascript>//为了编程方便,我们定义两个颜色数组
    var herocolor=new array(#ba9658,#fef26e);
    var enmeycolor=new array(#00a2b5,#00fefe);
    //其它的敌人坦克,这里的扩展性,还是不错的.
    //子弹类
    function bullet(x,y,direct,speed){
    this.x=x;
    this.y=y;
    this.direct=direct;
    this.speed=speed;
    this.timer=null;
    this.islive=true;
    this.run=function run(){
    //在该表这个子弹的坐标时,我们先判断子弹是否已经到边界
    if(this.x<=0||this.x>=400||this.y<=0||this.y>=300){
    //子弹要停止.
    window.clearinterval(this.timer);
    //子弹死亡
    this.islive=false;
    }else{
    //这个可以去修改坐标
    switch(this.direct){
    case 0:
    this.y-=this.speed;
    break;
    case 1:
    this.x+=this.speed;
    break;
    case 2:
    this.y+=this.speed;
    break;
    case 3:
    this.x-=this.speed;
    break;
    }
    }
    document.getelementbyid(aa).innertext=子弹x=+this.x+ 子弹y=+this.y;
    }
    }
    //这是一个tank类
    function tank(x,y,direct,color){
    this.x=x;
    this.y=y;
    this.speed=1;
    this.direct=direct;
    //一个坦克,需要两个颜色.
    this.color=color;
    //上移
    this.moveup=function(){
    this.y-=this.speed;
    this.direct=0;
    }
    //向右
    this.moveright=function(){
    this.x+=this.speed;
    this.direct=1;
    }
    //下移
    this.movedown=function(){
    this.y+=this.speed;
    this.direct=2;
    }
    //左
    this.moveleft=function(){
    this.x-=this.speed;
    this.direct=3;
    }
    }
    //定义一个hero类
    //x 表示坦克的 横坐标, y 表示纵坐标, direct 方向
    function hero(x,y,direct,color){
    //下面两句话的作用是通过对象冒充,达到继承的效果
    this.tank=tank;
    this.tank(x,y,direct,color);
    //增加一个函数,射击敌人坦克.
    this.shotenemy=function(){
    //创建子弹, 子弹的位置应该和hero有关系,并且和hero的方向有关.!!!
    //this.x 就是当前hero的横坐标,这里我们简单的处理(细化)
    switch(this.direct){
    case 0:
    herobullet=new bullet(this.x+9,this.y,this.direct,1);
    break;
    case 1:
    herobullet=new bullet(this.x+30,this.y+9,this.direct,1);
    break;
    case 2:
    herobullet=new bullet(this.x+9,this.y+30,this.direct,1);
    break;
    case 3: //右
    herobullet=new bullet(this.x,this.y+9,this.direct,1);
    break;
    }
    //调用我们的子弹run, 50 是老师多次测试得到的一个结论.
    var timer=window.setinterval(herobullet.run(),50);
    //把这个timer赋给这个子弹(js对象是引用传递!)
    herobullet.timer=timer;
    }
    }
    //定义一个enemytank类
    function enemytank (x,y,direct,color){
    //也通过对象冒充,来继承tank
    this.tank=tank;
    this.tank(x,y,direct,color);
    }
    //画出自己的子弹,多说一句,你也可以把该函数封装到hero类中
    function drawherobullet(){
    //这里,我们加入了一句话,但是要知道这里加,是需要对整个程序有把握
    if(herobullet!=null&&herobullet.islive){
    cxt.fillstyle=#fef26e;
    cxt.fillrect(herobullet.x,herobullet.y,2,2);
    }
    }
    //绘制坦克
    function drawtank(tank){
    //考虑方向
    switch(tank.direct){
    case 0: //上
    case 2:// 下
    //画出自己的坦克,使用前面的绘图技术
    //设置颜色
    cxt.fillstyle=tank.color[0];
    //韩老师使用 先死--->后活 (初学者最好用这个方法)
    //先画出左面的矩形
    cxt.fillrect(tank.x,tank.y,5,30);
    //画出右边的矩形(这时请大家思路->一定要一个参照点)
    cxt.fillrect(tank.x+15,tank.y,5,30);
    //画出中间矩形
    cxt.fillrect(tank.x+6,tank.y+5,8,20);
    //画出坦克的盖子
    cxt.fillstyle=tank.color[1];
    cxt.arc(tank.x+10,tank.y+15,4,0,360,true);
    cxt.fill();
    //画出炮筒(直线)
    cxt.strokestyle=tank.color[1];
    //设置线条的宽度
    cxt.linewidth=1.5;
    cxt.beginpath();
    cxt.moveto(tank.x+10,tank.y+15);
    if(tank.direct==0){
    cxt.lineto(tank.x+10,tank.y);
    }else if(tank.direct==2){
    cxt.lineto(tank.x+10,tank.y+30);
    }
    cxt.closepath();
    cxt.stroke();
    break;
    case 1: //右和左
    case 3:
    //画出自己的坦克,使用前面的绘图技术
    //设置颜色
    cxt.fillstyle=tank.color[0];
    //韩老师使用 先死--->后活 (初学者最好用这个方法)
    //先画出左面的矩形
    cxt.fillrect(tank.x,tank.y,30,5);
    //画出右边的矩形(这时请大家思路->一定要一个参照点)
    cxt.fillrect(tank.x,tank.y+15,30,5);
    //画出中间矩形
    cxt.fillrect(tank.x+5,tank.y+6,20,8);
    //画出坦克的盖子
    cxt.fillstyle=tank.color[1];
    cxt.arc(tank.x+15,tank.y+10,4,0,360,true);
    cxt.fill();
    //画出炮筒(直线)
    cxt.strokestyle=tank.color[1];
    //设置线条的宽度
    cxt.linewidth=1.5;
    cxt.beginpath();
    cxt.moveto(tank.x+15,tank.y+10);
    //向右
    if(tank.direct==1){
    cxt.lineto(tank.x+30,tank.y+10);
    }else if(tank.direct==3){ //向左
    cxt.lineto(tank.x,tank.y+10);
    }
    cxt.closepath();
    cxt.stroke();
    break;
    }
    }
    </pre>
    <pre></pre>
    </pre>
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/18 10:54:42