cocos2dx飞机大战游戏代码

http://download.csdn.net/detail/ylm0302/6435251
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法提供完整的代码,但我可以给您提供一些思路和示例代码。 1. 创建飞机类 ```typescript class Plane extends cc.Node { private speed: number = 10; private bullets: Bullet[] = []; private isShooting: boolean = false; constructor() { super(); const sprite = new cc.Sprite("plane.png"); this.addChild(sprite); this.schedule(this.shootBullet, 0.2, cc.macro.REPEAT_FOREVER); this.scheduleUpdate(); } public update(dt: number) { const keyboard = cc.systemEvent.keyboard; if (keyboard.isKeyDown(cc.macro.KEY.left)) { this.x -= this.speed; } if (keyboard.isKeyDown(cc.macro.KEY.right)) { this.x += this.speed; } if (keyboard.isKeyDown(cc.macro.KEY.up)) { this.y += this.speed; } if (keyboard.isKeyDown(cc.macro.KEY.down)) { this.y -= this.speed; } } private shootBullet() { const bullet = new Bullet(); this.addChild(bullet); this.bullets.push(bullet); bullet.setPosition(this.x, this.y + 20); } public removeBullet(bullet: Bullet) { const index = this.bullets.indexOf(bullet); if (index !== -1) { this.bullets.splice(index, 1); } } } ``` 2. 创建子弹类 ```typescript class Bullet extends cc.Node { private speed: number = 20; constructor() { super(); const sprite = new cc.Sprite("bullet.png"); this.addChild(sprite); this.scheduleUpdate(); } public update(dt: number) { this.y += this.speed; if (this.y > cc.winSize.height) { this.removeFromParent(); } } } ``` 3. 创建敌机类 ```typescript class Enemy extends cc.Node { private speed: number = 5; private hp: number = 3; constructor() { super(); const sprite = new cc.Sprite("enemy.png"); this.addChild(sprite); this.scheduleUpdate(); } public update(dt: number) { this.y -= this.speed; if (this.y < -cc.winSize.height) { this.removeFromParent(); } } public hit() { this.hp--; if (this.hp <= 0) { this.removeFromParent(); } } } ``` 4. 创建游戏场景 ```typescript class GameScene extends cc.Scene { private plane: Plane; private enemies: Enemy[] = []; constructor() { super(); this.init(); } private init() { const bg = new cc.Sprite("bg.png"); bg.setPosition(cc.winSize.width / 2, cc.winSize.height / 2); this.addChild(bg); this.plane = new Plane(); this.plane.setPosition(cc.winSize.width / 2, 100); this.addChild(this.plane); this.schedule(this.addEnemy, 1, cc.macro.REPEAT_FOREVER); this.scheduleUpdate(); } public update(dt: number) { this.checkCollision(); } private addEnemy() { const enemy = new Enemy(); enemy.setPosition(Math.random() * cc.winSize.width, cc.winSize.height); this.addChild(enemy); this.enemies.push(enemy); } private checkCollision() { for (let i = this.enemies.length - 1; i >= 0; i--) { const enemy = this.enemies[i]; const box1 = enemy.getBoundingBox(); for (let j = this.plane.childrenCount - 1; j >= 0; j--) { const bullet = this.plane.children[j] as Bullet; if (bullet) { const box2 = bullet.getBoundingBox(); if (box1.intersects(box2)) { enemy.hit(); this.plane.removeBullet(bullet); } } } if (box1.intersects(this.plane.getBoundingBox())) { cc.director.loadScene(new GameOverScene()); } } } } ``` 以上代码仅供参考,可能存在错误或不足之处,请根据实际情况进行修改和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值