h5 canvas 简易祖玛游戏
2017-06-14 16:42
281 查看
效果图:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>祖玛游戏</title> <style type="text/css"> body{ background: #000; } .canvas{ width: 600px; height: 600px; margin: 10px auto; border: 1px solid red; } canvas{ background: #fff; } </style> </head> <body> <div class="canvas"> <canvas id="box" width="600" height="600"> </canvas> </div> </body> <script type="text/javascript"> var box=document.getElementById('box'); var oc=box.getContext('2d'); var a=-0.5*Math.PI; var b=1*Math.PI; var dia; var diy; var dix; var bull=[]; var iround=0; box.onmousemove=function(e){ var x2=e.clientX-box.offsetLeft; var y2=e.clientY-box.offsetTop; console.log("x2="+x2) console.log("y2="+y2) var a1=x2-300; var b1=y2-250; var c1=Math.sqrt(a1*a1+b1*b1); console.log("c1="+c1) if (a1>0&&b1>0) { iround=Math.asin(b1/c1)+0.5*Math.PI; }else if (a1>0) { iround=Math.asin(a1/c1); } if(a1<0&&b1>0){ iround=-(Math.asin(b1/c1)+0.5*Math.PI); }else if(a1<0){ iround=Math.asin(a1/c1); } console.log(iround) } var img=new Image(); img.src='img/person.png' img.onload=function(){ tab() } //生成子弹 var bullet=[]; box.onclick=function(e){ var x3=e.clientX-this.offsetLeft; var y3=e.clientY-this.offsetTop; var a3=x3-300; var b3=y3-250; var c3=Math.sqrt(a3*a3+b3*b3); var speed=5; var xs=speed*a3/c3; var ys=speed*b3/c3; bullet.push({ x:300, y:250, xs:xs, ys:ys }); } function pz(x1,y1,x2,y2){ var disx=x1-x2; var disy=y1-y2; var c=Math.sqrt(disx*disx+disy*disy) if (c<40) { return true; } else{ return false; } } function tab(){ var timer1=setInterval(function(){ bull.push({ x:300, y:0, na:-0.5*Math.PI }); },400); var timer2=setInterval(function(){ // console.log(na) for (var i=0;i<bull.length;i++) { bull[i].na+=1*Math.PI/180; if (bull[i].na>2*Math.PI) { // bull[i].na=a; alert('再来一次'); clearInterval(timer1); clearInterval(timer2); clearInterval(timer3); } if (bull[i].na<1*Math.PI) { dia=bull[i].na-a; bull[i].y=250-250*Math.cos(dia); bull[i].x=250*Math.sin(dia)+300; } else{ dia=bull[i].na-b; bull[i].x=175-175*Math.cos(dia)+50; bull[i].y=250-175*Math.sin(dia); } } for (var i=0;i<bullet.length;i++) { bullet[i].x=bullet[i].x+bullet[i].xs; bullet[i].y=bullet[i].y+bullet[i].ys; } for (var i=0 ;i<bull.length;i++) { for (var j=0;j<bullet.length;j++) { if(pz(bull[i].x,bull[i].y,bullet[j].x,bullet[j].y)){ bull.splice(i,1); bullet.splice(j,1); break; } } } },30); var timer3=setInterval(function(){ oc.clearRect(0,0,box.width,box.height); // oc.clearRect(0,0,box.width,box.height); oc.beginPath(); oc.arc(300,250,250,-90*Math.PI/180,180*Math.PI/180,false); oc.stroke(); oc.beginPath(); oc.arc(225,250,175,1*Math.PI,2*Math.PI,false); oc.stroke(); oc.beginPath(); oc.arc(400,250,20,2*Math.PI,false); oc.stroke(); for (var i=0;i<bull.length;i++) { oc.beginPath(); oc.arc(bull[i].x,bull[i].y,20,2*Math.PI,false); oc.closePath(); oc.fill(); } // 绘制青蛙 oc.save(); oc.translate(300,250); oc.beginPath(); oc.rotate(iround); oc.translate(-40,-40); oc.drawImage(img,0,0) oc.closePath(); oc.restore(); // 绘制子弹 for (var i=0;i<bullet.length;i++) { oc.save(); oc.fillStyle='red'; oc.beginPath(); oc.arc(bullet[i].x,bullet[i].y,20,2*Math.PI,false); oc.closePath(); oc.stroke(); oc.fill(); oc.restore(); } //绘制文字 oc.save(); oc.font='60px impact'; oc.textBaseline='top'; oc.fillStyle='red'; oc.shadowOffsetX=10; oc.shadowOffsetY=10; oc.shadowColor='green'; oc.shadowBlur=5; var w=oc.measureText('祖玛游戏').width; var h=60; oc.fillText('祖玛游戏',(box.width-w)/2,450); oc.restore(); },1000/24); } </script> </html>
相关文章推荐
- h5-10 canvas 简易祖玛
- canvas制作简易祖玛游戏
- canvas实现的一个简易字母游戏
- H5canvas赛车游戏-基于lufylegend引擎
- Canvas实例-简易祖玛
- 游戏的套路你知道吗? H5 Canvas刮刮乐
- 关于H5-canvas游戏开发初谈
- H5 canvas游戏开发教程集合
- H5canvas赛车游戏-基于lufylegend引擎
- H5简单版祖玛游戏
- HTML5 canvas游戏开发实战 6 : 俄罗斯方块
- [译]怎样用HTML5 Canvas制作一个简单的游戏
- H5 游戏开发技术要点
- h5游戏之js图形封装使用04(完结)
- h5中使用canvas绘制线段、多边形、圆、圆弧
- iOS 简易游戏操作控制盘设计(仿iOS 王者荣耀操作盘)
- H5 canvas 测试
- 第八讲:HTML5中canvas实现小球击打小方块游戏
- H5游戏的最大特点是什么?
- 简易的扑克牌比点数游戏