html js飞机大战实例,js 飞机大战(示例代码)

functionEnemy(container,bullets,myplan,middleEnemy,maxEnemy,score){this.container =container;this.img = "images/enemy1_fly_1.png";this.createTime = 600; //创建敌机的间隔时间

this.bullets =bullets;this.dieImg = "images/enemy1_fly_3.gif";this.width = 34; //敌机的宽度

this.height = 24; //敌机的高度

this.myplan =myplan;this.count = 1; //小型敌机创建个数

this.dieCount = 1; //敌机消灭需子弹打击次数

this.middleEnemy =middleEnemy;this.maxEnemy =maxEnemy;this.score =score;

}

Enemy.prototype={

init:function(){var that = this;var middleEnemy = this.middleEnemy;var maxEnemy = this.maxEnemy;var count = 0;

setInterval(function(){

that.create();

count++;if(count % 5 == 0){

middleEnemy.create();

}if(count % 30 == 0){

maxEnemy.create();

count= 1;

}

},this.createTime);

},

create:function(){this.enemy = document.createElement("img");this.enemy.src = this.img;var enemyX = Math.random() * (this.container.offsetWidth - this.width);var enemyY = -1 * this.height;this.enemy.style.cssText = "position:absolute; left:" + enemyX + "px; top:" + enemyY + "px;";this.container.appendChild(this.enemy);var enemy = this.enemy;this.data_hitCount = 0;var container = this.container;var bullets = this.bullets;var dieImg = this.dieImg;var myplan = this.myplan;var plan = this.myplan.plan;var createBullets = this.myplan.createBullets;var dieCount = this.dieCount;var isremove = true; //是否可以移除敌机

var score = this.score;var that = this;this.enemy.timer = setInterval(function(){

enemy.style.top= enemy.offsetTop + 3 + "px";for(var i = 0; i < bullets.length; i++){if(bullets[i].offsetLeft + bullets[i].offsetWidth > enemy.offsetLeft && bullets[i].offsetLeft < enemy.offsetLeft +enemy.offsetWidth){if(bullets[i].offsetTop + bullets[i].offsetHeight > enemy.offsetTop && bullets[i].offsetTop < enemy.offsetTop + enemy.offsetHeight &&isremove){if(i == 0){

plan.src= "images/myover.gif";

container.οnmοusemοve= null;

clearInterval(myplan.createTimer);

gameOver();

}else{

container.removeChild(bullets[i]);

bullets.remove(bullets[i]);

that.data_hitCount++;if(that.data_hitCount ==dieCount){

isremove= false;

enemy.src=dieImg;

getScore(dieCount);

setTimeout(function(){

container.removeChild(enemy);

},300);

}

}

}

}

}if(enemy.offsetTop >=container.offsetHeight){

container.removeChild(enemy);

clearInterval(enemy.timer);

}

},30);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值