[Canvas]游戏增加怪物爆炸效果,改善箭头形状

请点此下载代码并用浏览器打开试玩。

图例:

代码:

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>地图加载 英雄可以射箭了 怪物爆炸 19.3.7 7:59 by:逆火狂飙 horn19782016@163.com</title>
     
     <style>
     #canvas{
        background:#ffffff;
        cursor:pointer;
        margin-left:10px;
        margin-top:10px;
        -webkit-box-shadow:3px 3px 6px rgba(0,0,0,0.5);
        -moz-box-shadow:3px 3px 6px rgba(0,0,0,0.5);
        box-shadow:3px 3px 6px rgba(0,0,0,0.5);
     }
     
     #controls{
        margin-top:10px;
        margin-left:15px;
     }
     </style>
     
    </head>

     <body onload="init()">
        <div id="controls">
            <input id='animateBtn' type='button' value='开始'/>
        </div>
     
        <canvas id="canvas" width="160px" height="160px" >
            出现文字表示你的浏览器不支持HTML5
        </canvas>
     </body>
</html>
<script type="text/javascript">
<!--
var paused=true;
animateBtn.onclick=function(e){
    paused=! paused;
    
    if(paused){
        animateBtn.value="开始";    

    }else{    
        animateBtn.value="停止";
        
        window.requestAnimationFrame(animate); 
    }
}

var ctx;// 绘图环境
var r;// 表盘半径
var terrain;
var hero;
var arrows;
var monsterMng;
function init(){
    // init Canvas
    var canvas=document.getElementById('canvas');
    r=160; 
    canvas.width =2*r;
    canvas.height=2*r;
    ctx=canvas.getContext('2d');
    
    terrain=new Terrain();
    hero=new Hero();
    
    arrows=new Array();
    monsterMng=new MonsterMng();
    
    // 响应键盘事件
    canvas.addEventListener('keydown', doKeyDown, true);
    canvas.focus();  
    window.addEventListener('keydown', doKeyDown, true);
};

function draw(){
    // Clear Canvas
    ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
    ctx.fillStyle="#ECF5FF";
    ctx.fillRect(0,0,ctx.canvas.width,ctx.canvas.height);
    
    terrain.paint(ctx);
    monsterMng.paint(ctx);
    hero.paint(ctx);

    for(var i=arrows.length-1;i>-1;i--){
        var arrow=arrows[i];
        var x=parseInt(arrow.x / 32,10);
        var y=parseInt(arrow.y / 32,10);

        // 箭矢射中怪物所在
        if(monsterMng.getValue(x,y)==1){
            monsterMng.setValue(x,y,2);
            arrows.splice(i,1);
            break;
        }
        
        // 箭矢射中树木房屋等障碍物
        if(terrain.getValue(x,y)!=0){
            arrows.splice(i,1);
        }
    }
    
    // 画箭矢
    for(var i=0;i<arrows.length;i++){
        var arrow=arrows[i];

        arrow.paint(ctx);
    }
    
    // 胜利条件
    if(monsterMng.hasMonster()==false){
        alert("You win!");
        init();
    }
}

function animate(){
    if(!paused){
        draw();
        window.requestAnimationFrame(animate); /// 让浏览器自行决定帧速率
    }
}

function doKeyDown(e) {
    var keyID = e.keyCode ? e.keyCode :e.which;
    if(keyID === 38 || keyID === 87)  { // up arrow and W
        hero.move('up');
        e.preventDefault();
    }
    if(keyID === 39 || keyID === 68)  { // right arrow and D
        hero.move('right');
        e.preventDefault();
    }
    if(keyID === 40 || keyID === 83)  { // down arrow and S
        hero.move('down');
        e.preventDefault();
    }
    if(keyID === 37 || keyID === 65)  { // left arrow and A
        hero.move('left');
        e.preventDefault();
    }

    if(keyID === 32 )  { // SpaceBar
        var a=new Arrow(hero.x+16,hero.y+16,hero.direction);
        arrows.push(a);
        console.log('arrows.length='+arrows.length);
        e.preventDefault();
    }
}

//---------------------------------------------------地形类定义开始------------------------------------------------------------------->>
Terrain=function(){
    this.files=["road.png","tree.png","home.png"];
    
    this.maps=new Array(
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,1,1,1,1,1,1,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,2,2,2,2,2,2,2,0],
        [0,0,0,0,0,0,2,0,0,0],
        [0,0,0,2,0,0,2,0,0,0],
        [0,0,0,2,0,0,0,0,0,0],
        [0,2,2,2,2,2,2,2,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,1,1,1,1,1,1,1,1,0],
    );
}
Terrain.prototype={
    files:[],
    // Property
    maps:0,
    
    // method
    paint:function(ctx){
        for(var i=0;i<this.maps.length;i++){
            var arr=this.maps[i];
            
            for(var j=0;j<arr.length;j++){
                var value=arr[j];
                
                var img=new Image();
                img.src=this.files[value];
                
                ctx.drawImage(img,i*32,j*32);
            }
        }
    },

    getValue:function(i,j){
        if(i<0 || i>=this.maps.length){
            return undefined;
        }
        var arr=this.maps[i];
        if(j<0 || j>=arr.length){
            return undefined;
        }
        var value=arr[j];
        return value;
    },
    
    setValue:function(i,j,value){
        if(i<0 || i>=this.maps.length){
            return undefined;
        }
        var arr=this.maps[i];
        if(j<0 || j>=arr.length){
            return undefined;
        }
        arr[j]=value;
    },
}
//---------------------------------------------------地形类定义结束-------------------------------------------------------------------<<

//---------------------------------------------------英雄类定义开始------------------------------------------------------------------->>
Hero=function(){
    this.pngs=[
        {left:0,top:10,40,height:40},
        {left:0,top:66,40,height:40},
        {left:0,top:120,40,height:40},
        {left:0,top:180,40,height:40},
    ];
}
Hero.prototype={
    pngs:[],
    x:160,
    y:160,
    xTo:160,
    yTo:160,
    step:32,
    direction:'up',
    
    paint:function(ctx){
        var img=new Image();
        img.src='bowman.png';
        
        var index=0;
        if(this.direction=='up'){
            index=3;
        }
        if(this.direction=='down'){
            index=0;
        }
        if(this.direction=='left'){
            index=1;
        }
        if(this.direction=='right'){
            index=2;
        }
        var pos=this.pngs[index];
        ctx.drawImage(img,pos.left,pos.top,pos.width,pos.height,this.x,this.y,pos.width,pos.height);
    },
    
    move:function(direction){
        this.direction=direction;
        
        if(this.direction=='up'){
            this.yTo-=this.step;
        }
        if(this.direction=='down'){
            this.yTo+=this.step;
        }
        if(this.direction=='left'){
            this.xTo-=this.step;
        }
        if(this.direction=='right'){
            this.xTo+=this.step;
        }
        
        if(terrain.getValue(this.xTo/this.step,this.yTo/this.step)==0){
            this.x=this.xTo;
            this.y=this.yTo;
        }else{
            this.xTo=this.x;
            this.yTo=this.y;
        }
    }
}
//---------------------------------------------------英雄类定义结束-------------------------------------------------------------------<<

//---------------------------------------------------箭矢类定义开始------------------------------------------------------------------->>
Arrow=function(x,y,direction){
    this.x=x;
    this.y=y;
    this.direction=direction;
}
Arrow.prototype={
    x:0,
    y:0,
    velocity:1,// 飞行速度
    len:16,// 箭杆长
    direction:'',
    
    // method
    paint:function(ctx){
        var x1=this.x,y1=this.y;
        var leaf=4;//箭头箭羽长
        var x2,y2,x3,y3;
        var angle=getRad(15);
    
        if(this.direction=='up'){
            this.y-=this.velocity;
            y1=this.y-this.len;
            x1=this.x;
            
            y2=y1+leaf*Math.cos(angle);
            x2=x1-leaf*Math.sin(angle);
            y3=y2;
            x3=x1+leaf*Math.sin(angle);
        }
        if(this.direction=='down'){
            this.y+=this.velocity;
            y1=this.y+this.len;
            x1=this.x;
            y2=y1-leaf*Math.cos(angle);
            x2=x1-leaf*Math.sin(angle);
            y3=y2;
            x3=x1+leaf*Math.sin(angle);
        }
        if(this.direction=='left'){
            this.x-=this.velocity;
            x1=this.x-this.len;
            y1=this.y;
            
            x2=x1+leaf*Math.cos(angle);
            y2=y1-leaf*Math.sin(angle);
            x3=x2;
            y3=y1+leaf*Math.sin(angle);
        }
        if(this.direction=='right'){
            this.x+=this.velocity;
            x1=this.x+this.len;
            y1=this.y;
            
            x2=x1-leaf*Math.cos(angle);
            y2=y1-leaf*Math.sin(angle);
            x3=x2;
            y3=y1+leaf*Math.sin(angle);
        }

        ctx.beginPath();
        ctx.lineWidth = 2;
        ctx.strokeStyle = "#ff0000";
        ctx.lineTo(this.x,this.y);
        ctx.lineTo(x1,y1);
        ctx.lineTo(x2,y2);
        ctx.lineTo(x3,y3);
        ctx.lineTo(x1,y1);

        ctx.stroke();
        ctx.closePath();
    },
}
//---------------------------------------------------箭矢类定义结束-------------------------------------------------------------------<<

//---------------------------------------------------怪物管理类定义开始------------------------------------------------------------------->>
MonsterMng=function(){
    this.files=["","skull.png","bomb.png",];
    
    this.maps=new Array(
        [0,0,0,0,0,0,0,0,0,0],
        [0,1,0,0,0,0,0,0,1,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,1,0,0,0,0,0,0,1,0],
        [0,0,0,0,0,0,0,0,0,0],
    );
}
MonsterMng.prototype={
    files:[],
    // Property
    maps:[],
    
    // method
    paint:function(ctx){
        for(var i=0;i<this.maps.length;i++){
            var arr=this.maps[i];
            
            for(var j=0;j<arr.length;j++){
                var value=arr[j];
                
                if(value!=0){
                    var img=new Image();
                    img.src=this.files[value];
                    
                    ctx.drawImage(img,i*32,j*32);
                }
            }
        }
    },
    
    // 判断怪物还是否存在
    hasMonster:function(){
        var count=0;
        for(var i=0;i<this.maps.length;i++){
            var arr=this.maps[i];
            
            for(var j=0;j<arr.length;j++){
                var value=arr[j];
                
                if(value==1){
                    count++;// 累计怪物数
                }
            }
        }
        
        // 怪物数大于零就是还有怪物
        return count>0;
    },
    
    getValue:function(i,j){
        if(i<0 || i>=this.maps.length){
            return undefined;
        }
        var arr=this.maps[i];
        if(j<0 || j>=arr.length){
            return undefined;
        }
        var value=arr[j];
        return value;
    },
    
    setValue:function(i,j,value){
        if(i<0 || i>=this.maps.length){
            return undefined;
        }
        var arr=this.maps[i];
        if(j<0 || j>=arr.length){
            return undefined;
        }
        arr[j]=value;
    },
}
//---------------------------------------------------怪物管理类定义结束-------------------------------------------------------------------<<
//  常规函数:角度得到弧度
function getRad(degree){
    return degree/180*Math.PI;
}
//-->
</script>

2019年3月7日08点50分

原文地址:https://www.cnblogs.com/heyang78/p/10487320.html