基于C#弹幕类射击游戏的实现——(八)敌机

本文介绍了如何在C#弹幕射击游戏中创建和控制敌人。敌机基于GameEnemy类,通过重写MoveLogic和ShootLogic实现AI行为。游戏包含不同类型的敌人,如直飞型、追踪型和高级AI型,展示如何通过简单方式实现敌机的多样化行为。鼓励读者尝试添加更复杂的敌机AI。
摘要由CSDN通过智能技术生成

有了弹幕,有了主角,敌人当然就该上场了。

其实敌人和主角类似,也是一架飞机,不同点在于敌人的飞机是AI控制的,而玩家的飞机是自己控制而已~

 

public class GameEnemy : GameObject
	{
		protected int Life;
		public bool IsLive;
		protected int Type;
		protected Vector2 Speed;
		protected Vector2 Accel;
		
		/// <summary>
		/// 敌机的位图大小盒子
		/// </summary>
		public Box SourceBox
		{
			get
			{
				return new Box((int)Position.X, (int)Position.Y, EnemyWidth, EnemyHeight);
			}
		}
		
		/// <summary>
		/// 敌人发射子弹的类型
		/// </summary>
		public enum ShootType
		{
			None, // 不发射子弹
			Line, // 直线子弹
			Trace, // 追踪子弹
			Fan_3, // 成扇形发射3颗子弹
			Fan_5
		}
		
		protected ShootType mShootType;
		
		protected int EnemyWidth;
		protected int EnemyHeight;
		protected int HalfEnemyWidth;
		protected int HalfEnemyHeight;
		
		public GameEnemy(Vector2 position, int life, int type, ShootType shootType)
		{
			this.IsLive = true;
			
			base.Position = position;
			this.Speed = new Vector2(0, 0);
			this.Accel = new Vector2(0, 0);
			
			base.BoxCollider = new Box(0, 0, Data.EnemyBoxColliderRectangle[type].Width, Data.EnemyBoxColliderRectangle[type].Height);
			
			this.Life = life;
			this.Type = type;
			this.mShootType = shootType;
			
			this.EnemyWidth = Data.EnemySourceRectangle[type].Width;
			this.EnemyHeight = Data.EnemySourceRectangle[type].Height;
			this.HalfEnemyWidth = this.EnemyWidth / 2;
			this.HalfEnemyHeight = this.EnemyHeight / 2;
		}
		
		public void DecLife(int decLife)
		{
			Life -= decLife;
			
			if ( Life <= 0 )
			{
				IsLive = false;
				GameBombMana
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值