欢迎来到程序小院
坦克大战(二)
玩法:键盘(A W S D)键来控制方向,空格键发射子弹,N:下一关,P:上一关,Enter:开始,赶紧去闯关吧^^。
开始游戏
https://www.ormcc.com/play/gameStart/221
html
<CENTER><div id="game-area"></div></CENTER>
css
canvas{
display: block;
touch-action: none;
user-select: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
width: 414px;
height: 550px;
cursor: inherit;
}
js
game.physics.startSystem(Phaser.Physics.ARCADE);
soundStart = game.add.audio("sound-start");
soundFire = game.add.audio("sound-fire");
soundHit = game.add.audio("sound-hit");
soundBoom1 = game.add.audio("sound-boom1");
soundBoom2 = game.add.audio("sound-boom2");
soundWin = game.add.audio("sound-win");
soundOver = game.add.audio("sound-over");
soundStart.play();
map = game.add.tilemap("levels");
map.addTilesetImage("tile");
map.playTimer = 0;
map.playIndex = 0;
enemies = game.add.group();
enemies.enableBody = true;
for (var i=0; i<8; i++){
var imgID=parseInt(i/4)*32+(i%4)*2;
var enemy = enemies.create(0, 0, "enemy",imgID).kill();
enemy.animations.add("up",[imgID, imgID + 1], 5, true);
enemy.animations.add("right",[imgID + 8, imgID + 9], 5, true);
enemy.animations.add("down",[imgID + 16, imgID + 17], 5, true);
enemy.animations.add("left",[imgID + 24, imgID + 25], 5, true);
enemy.body.collideWorldBounds = true;
enemy.timeToMove = 0;
}
//创建主角
player = game.add.sprite(26*8,38*8, "tank",0);
game.physics.arcade.enable(player,Phaser.Physics.ARCADE);
player.body.collideWorldBounds = true;
player.animations.add("up", [0, 1], 5, true);
player.animations.add("right",[8, 9], 5, true);
player.animations.add("down",[16, 17], 5, true);
player.animations.add("left", [24, 25], 5, true);
levelLayer = map.createLayer("level-"+_levelInfo[0]);
map.setCollisionByExclusion([5,6],true,levelLayer);
levelLayer.resizeWorld();
//按键
cursors = game.input.keyboard.createCursorKeys();
actKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
actKey.onDown.add(this.actKeyDown, this);
myFires = game.add.group();
myFires.enableBody = true;
for (var i=0;i<2;i++){ //这里限制发弹数量
var fire = myFires.create(0,0,"bullet",0).kill();
fire.checkWorldBounds = true;
fire.outOfBoundsKill = true;
}
enemyFires = game.add.group();
enemyFires.enableBody = true;
explodes = game.add.group(); //爆炸效果
explodes.enableBody = true;
bores = game.add.group();//这个就叫它敌机生成器吧
bores.enableBody = true;
bores.create(0,0,"bore",0).kill();
if(!game.device.desktop){
this.addTouchKey(); //移动端显示虚拟按键
}
scoreText = game.add.text(16, 16, "Enemy: " + score, { fontSize: "16px", fill: "#fff" });
scoreText.fixedToCamera=true;
};
this.update = function(){
game.physics.arcade.collide(player, levelLayer, this.bossHit, null, this);
game.physics.arcade.collide(enemies, levelLayer, this.bossHit, null, this);
game.physics.arcade.overlap(player, enemies, this.tankHit, null, this);
game.physics.arcade.overlap(player, enemyFires, this.fireHit, null, this);
game.physics.arcade.overlap(enemies, myFires, this.fireHit, null, this);
game.physics.arcade.overlap(myFires, levelLayer, this.tileHit, null, this);
game.physics.arcade.overlap(enemyFires, levelLayer, this.tileHit, null, this);
player.body.velocity.setTo(0,0);
if(!isOver){
if(cursors.right.isDown || touchRight){
this.playerMove(8,0);
player.animations.play("right");
facing=1;
}else if(cursors.left.isDown || touchLeft){
this.playerMove(-8,0);
player.animations.play("left");
facing=3;
}else if(cursors.up.isDown || touchUp){
this.playerMove(0,-8);
player.animations.play("up");
facing=0;
}else if(cursors.down.isDown || touchDown){
this.playerMove(0,8);
player.animations.play("down");
facing=2;
}else{
player.animations.stop();
}
enemies.forEachAlive(this.enemyMove,this);
//this.mapTilePlay(); // 水的动画效果
this.enemyMake();
}
};
this.playerMove = function(xx,yy){
player.x = (yy!=0) ? Math.round(player.x/8)*8 : player.x;
player.y = (xx!=0) ? Math.round(player.y/8)*8 : player.y;
player.body.velocity.setTo(xx*8,yy*8);
};
this.enemyMove = function(enemy){
if(game.time.now>=enemy.timeToMove){
var go = parseInt(Math.random()*7);
go = go>3 ? go-3 : go; // 减少几率往上
enemy.body.velocity.setTo((go==1?8:go==3?-8:0)*5,(go==0?-8:go==2?8:0)*5);
enemy.animations.play(["up","right","down","left"][go]);
enemy.timeToMove=game.time.now+Math.random()*2000;
if(Math.random() < 0.5){ // 随机开炮...
var xx = enemy.x+(go==3?0:go==1?10:5);
var yy = enemy.y+(go==0?0:go==2?10:5);
soundFire.play();
var fire = enemyFires.getFirstDead(true, xx, yy,"bullet",go);
fire.body.velocity.setTo((go==1?8:go==3?-8:0)*20,(go==2?8:go==0?-8:0)*20);
fire.checkWorldBounds = true;
fire.outOfBoundsKill = true;
}
}
};
源码
需要源码请关注添加好友哦^ ^
转载:欢迎来到本站,转载请注明文章出处
https://ormcc.com/