标签:window 中心 update div interval 指令 return jpg 开始
//封装一个画坦克的函数,传两个参数x,y,分别代表左上角的横纵坐标 //再增加一个参数dir来表示方向 上下左右分别传"u" "d" "l" "r" function drawTank(x,y,dir){ var cxt = getCxt(); switch(dir){ case "u": //此时造一个向上的坦克 cxt.fillStyle = "#542174"; cxt.fillRect(x,y,20,65); cxt.fillRect(x+70,y,20,65); cxt.fillRect(x+23,y+10,44,50); cxt.fillStyle = "#FCB827"; cxt.beginPath(); cxt.arc(x+45,y+35,16,0,2*Math.PI,false); cxt.closePath(); cxt.fill(); cxt.strokeStyle = "#FCB827"; cxt.lineWidth = "8.0"; cxt.moveTo(x+45,y+35); cxt.lineTo(x+45,y-25); cxt.stroke(); break; case "d": //此时造向下的坦克 cxt.fillStyle = "#542174"; cxt.fillRect(x,y,20,65); cxt.fillRect(x+70,y,20,65); cxt.fillRect(x+23,y+10,44,50); cxt.fillStyle = "#FCB827"; cxt.beginPath(); cxt.arc(x+45,y+35,16,0,2*Math.PI,false); cxt.closePath(); cxt.fill(); cxt.strokeStyle = "#FCB827"; cxt.lineWidth = "8.0"; cxt.moveTo(x+45,y+35); cxt.lineTo(x+45,y+95); //和向上造相比,只有炮筒需要改变 cxt.stroke(); break; case "l": //此时造向左的坦克 cxt.fillStyle = "#542174"; cxt.fillRect(x,y,65,20); //和向上造坦克相比,画第一个矩形时长宽互换即可 cxt.fillRect(x,y+70,65,20); //向左的坦克,注意坐标之间的转换即可,以下类似不再一一解释 cxt.fillRect(x+10,y+23,50,44); cxt.fillStyle = "#FCB827"; cxt.beginPath(); cxt.arc(x+35,y+45,16,0,2*Math.PI,false); cxt.closePath(); cxt.fill(); cxt.strokeStyle = "#FCB827"; cxt.lineWidth = "8.0"; cxt.moveTo(x+35,y+45); cxt.lineTo(x-25,y+45); cxt.stroke(); break; case "r": cxt.fillStyle = "#542174"; cxt.fillRect(x,y,65,20); //和造向左的坦克类似,只要改动炮筒即可向右 cxt.fillRect(x,y+70,65,20); cxt.fillRect(x+10,y+23,50,44); cxt.fillStyle = "#FCB827"; cxt.beginPath(); cxt.arc(x+35,y+45,16,0,2*Math.PI,false); cxt.closePath(); cxt.fill(); cxt.strokeStyle = "#FCB827"; cxt.lineWidth = "8.0"; cxt.moveTo(x+35,y+45); cxt.lineTo(x+95,y+45); cxt.stroke(); } }
drawTank(myTank.x,myTank.y,myTank.direction);
window.onkeydown = function(eve){ switch(eve.keyCode){ case 38: case 87: myTank.y -= myTank.step; //Y坐标减小向上移动 myTank.direction = "u"; //改变成向上的方向 break; case 40: case 83: myTank.y += myTank.step; //Y坐标增加向下移动 myTank.direction = "d"; //改变为向下的方向 break; case 37: case 65: myTank.x -= myTank.step; //X坐标减小向左移动 myTank.direction = "l"; //改变为向左的方向 break; case 39: case 68: myTank.x += myTank.step; //X坐标增加向右移动 myTank.direction = "r"; //改变为向右的方向 } };
myTank.turnUp = function(){ myTank.y -= myTank.step; myTank.direction = "u"; }; myTank.turnDown = function(){ myTank.y += myTank.step; myTank.direction = "d"; }; myTank.turnLeft = function(){ myTank.x -= myTank.step; myTank.direction = "l"; }; myTank.turnRight = function(){ myTank.x += myTank.step; myTank.direction = "r"; };
window.onkeydown = function(eve){ switch(eve.keyCode){ case 38: case 87: myTank.turnUp(); break; case 40: case 83: myTank.turnDown(); break; case 37: case 65: myTank.turnLeft(); break; case 39: case 68: myTank.turnRight(); } };
var myTank = { x : 350, y : 400, step : 3, direction : "u", turnUp : function(){ myTank.y -= myTank.step; myTank.direction = "u"; }, turnDown : function(){ myTank.y += myTank.step; myTank.direction = "d"; }, turnLeft : function(){ myTank.x -= myTank.step; myTank.direction = "l"; }, turnRight : function(){ myTank.x += myTank.step; myTank.direction = "r"; } };
var myTank = { turnUp : function(){ if((myTank.y-25) >= 0){ myTank.y -= myTank.step; myTank.direction = "u"; } } };
var myTank = { turnDown : function(){ if((myTank.y+90) <= 500){ myTank.y += myTank.step; myTank.direction = "d"; } } };
var myTank = { turnLeft : function(){ if((myTank.x-25) >= 0){ myTank.x -= myTank.step; myTank.direction = "l"; } }, turnRight : function(){ if((myTank.x+90) <= 800){ myTank.x += myTank.step; myTank.direction = "r"; } } };
//封装一个获取绘图环境的函数 function getCxt(){ var myCanvas = document.getElementById(‘floor‘), myContext = myCanvas.getContext(‘2d‘); return myContext; } //为了防止重复地获取节点影响性能,我们将获取到的绘图环境(也就是画笔对象)存起来 var oCxt = getCxt(); //封装一个画坦克的函数,传两个参数x,y,分别代表左上角的横纵坐标 //再增加一个参数dir来表示方向 上下左右分别传"u" "d" "l" "r" function drawTank(x,y,dir){ switch(dir){ case "u": //此时造一个向上的坦克 oCxt.fillStyle = "#542174"; oCxt.fillRect(x,y,20,65); oCxt.fillRect(x+70,y,20,65); oCxt.fillRect(x+23,y+10,44,50); oCxt.fillStyle = "#FCB827"; oCxt.beginPath(); oCxt.arc(x+45,y+35,16,0,2*Math.PI,false); oCxt.closePath(); oCxt.fill(); oCxt.strokeStyle = "#FCB827"; oCxt.lineWidth = "8.0"; oCxt.moveTo(x+45,y+35); oCxt.lineTo(x+45,y-25); oCxt.stroke(); break; case "d": //此时造向下的坦克 oCxt.fillStyle = "#542174"; oCxt.fillRect(x,y,20,65); oCxt.fillRect(x+70,y,20,65); oCxt.fillRect(x+23,y+10,44,50); oCxt.fillStyle = "#FCB827"; oCxt.beginPath(); oCxt.arc(x+45,y+35,16,0,2*Math.PI,false); oCxt.closePath(); oCxt.fill(); oCxt.strokeStyle = "#FCB827"; oCxt.lineWidth = "8.0"; oCxt.moveTo(x+45,y+35); oCxt.lineTo(x+45,y+95); //和向上造相比,只有炮筒需要改变 oCxt.stroke(); break; case "l": //此时造向左的坦克 oCxt.fillStyle = "#542174"; oCxt.fillRect(x,y,65,20); //和向上造坦克相比,画第一个矩形时长宽互换即可 oCxt.fillRect(x,y+70,65,20); //向左的坦克,注意坐标之间的转换即可,以下类似不再一一解释 oCxt.fillRect(x+10,y+23,50,44); oCxt.fillStyle = "#FCB827"; oCxt.beginPath(); oCxt.arc(x+35,y+45,16,0,2*Math.PI,false); oCxt.closePath(); oCxt.fill(); oCxt.strokeStyle = "#FCB827"; oCxt.lineWidth = "8.0"; oCxt.moveTo(x+35,y+45); oCxt.lineTo(x-25,y+45); oCxt.stroke(); break; case "r": oCxt.fillStyle = "#542174"; oCxt.fillRect(x,y,65,20); //和造向左的坦克类似,只要改动炮筒即可向右 oCxt.fillRect(x,y+70,65,20); oCxt.fillRect(x+10,y+23,50,44); oCxt.fillStyle = "#FCB827"; oCxt.beginPath(); oCxt.arc(x+35,y+45,16,0,2*Math.PI,false); oCxt.closePath(); oCxt.fill(); oCxt.strokeStyle = "#FCB827"; oCxt.lineWidth = "8.0"; oCxt.moveTo(x+35,y+45); oCxt.lineTo(x+95,y+45); oCxt.stroke(); } } //初始化一个对象myTank,用来存储一些属性和方法 var myTank = { x : 350, y : 400, step : 3, direction : "u", turnUp : function(){ if((myTank.y-25) >= 0){ //加判断条件防止开出边界 myTank.y -= myTank.step; myTank.direction = "u"; } }, turnDown : function(){ if((myTank.y+90) <= 500){ myTank.y += myTank.step; myTank.direction = "d"; } }, turnLeft : function(){ if((myTank.x-25) >= 0){ myTank.x -= myTank.step; myTank.direction = "l"; } }, turnRight : function(){ if((myTank.x+90) <= 800){ myTank.x += myTank.step; myTank.direction = "r"; } } }; //先画一个坦克出来 drawTank(myTank.x,myTank.y,myTank.direction); //一开始先造一个向上的出来 //封装一个更新战场的函数 function updateFloor(){ oCxt.clearRect(0,0,800,500); //更新之前先清除画布 drawTank(myTank.x,myTank.y,myTank.direction); //清除完之后重新造坦克,坦克要移动就必须实时地根据坐标重新来造 } //设置一个间歇调用的函数,每隔100ms更新一下战场 setInterval(function(){ updateFloor(); },100); //响应玩家的操作指令 window.onkeydown = function(eve){ switch(eve.keyCode){ case 38: case 87: myTank.turnUp(); break; case 40: case 83: myTank.turnDown(); break; case 37: case 65: myTank.turnLeft(); break; case 39: case 68: myTank.turnRight(); } };
(PS:一个人的力量毕竟有限,如在阅读的过程中发现有描述不当或者错误的地方欢迎随时指正,笔者不胜感激!)
标签:window 中心 update div interval 指令 return jpg 开始
原文地址:http://www.cnblogs.com/zhouhuan/p/H5_tankgame3.html