日期:2014-05-16 浏览次数:20284 次
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<style type="text/css">
#map{width:700px; height:600px; border:0px solid #CCC; overflow:hidden;}
.maps{width:10px; height:10px; float:left; overflow:hidden;border:1px solid red;}
.enemy{background:black;}
.tank{background:blue;}
.bullet{background:purple;}
</style>
<script type="text/javascript">
// tank运行定时器
var tankInt;
// tank运行状态
var tankIsRunning = false;
// 定义方格
function Box(x,y){
this.x = x;
this.y = y;
}
// 初始化地图
function initMap(){
for(var i=1;i<=50;i++){
for(var j=1;j<=50;j++){
$("<div id=map_"+i+"_"+j+" class='maps'></div>").appendTo($('#map'));
if(j==50){
$('<div style="clear:both;"></div>').appendTo($('#map'));
}
}
}
}
// 定义tank
var tank = [
[new Box(1,1),new Box(2,1),new Box(2,2),new Box(3,2),new Box(1,3),new Box(2,3)],
[new Box(1,1),new Box(1,2),new Box(2,2),new Box(2,3),new Box(3,1),new Box(3,2)],
[new Box(1,2),new Box(2,1),new Box(2,2),new Box(2,3),new Box(3,1),new Box(3,3)],
[new Box(1,2),new Box(1,3),new Box(2,1),new Box(2,2),new Box(3,2),new Box(3,3)]
];
// 坦克头位置
var tankHead = [new Box(3,2),new Box(2,3),new Box(1,2),new Box(2,1)];
// tank运行方向
var tankDirection = 3;
// 横向位移
var xMove = 20;
// 纵向位移
var yMove = 20;
// 当前坦克位置
var nowTank;
// 当前坦克头位置
var nowTankHead;
// 子弹容器
var bullets = [];
// 绘制tank
function drawTank(){
for(var i=0;i<tank[tankDirection].length;i++){
var point = tank[tankDirection][i];
var x = point.x+xMove;
var y = point.y+yMove;
$("#map_"+x+"_"+y).addClass('tank');
}
nowTank = tank[tankDirection];
nowTankHead = tankHead[tankDirection];
}
// 清除tank
function cleanTank(){
for(var i=0;i<nowTank.length;i++){
var point = nowTank[i];