Presentation is loading. Please wait.

Presentation is loading. Please wait.

人工智慧概論 坦克AI 498g0061-許顥.

Similar presentations


Presentation on theme: "人工智慧概論 坦克AI 498g0061-許顥."— Presentation transcript:

1 人工智慧概論 坦克AI 498g0061-許顥

2 主題-坦克大戰 運用程式的運作,簡單的做出坦克車之間的射擊遊戲同時還使用人工智慧的方法,使遊戲能夠更靈活多樣。
運用簡單的WASD以及J,來擊敗電腦(NPC)過關。

3 程式碼-電腦AI private static Image[] imgEnemy1 = new Image[]
Resources.enemy1U, Resources.enemy1D, Resources.enemy1L, Resources.enemy1R }; private static Image[] imgEnemy2 = new Image[] { Resources.enemy2U, Resources.enemy2D, Resources.enemy2L, Resources.enemy2R private static Image[] imgEnemy3 = new Image[] Resources.enemy3U, Resources.enemy3D, Resources.enemy3L, Resources.enemy3R private int type; //定义敌人的类型 public int Type //设置属性 get { return Type; } set { Type = value; } }

4 private static int speed; //定义速度和生命力值
private static int life; private Random rdm = new Random();//定义随机数,来控制敌人出现的不同方向 //設置速度 private static int SetSpeed(int type) { switch (type) case 0: speed = 5; break; case 1: speed = 3; case 2: speed = 8; } return speed; //返回速度值 //設置生命

5 private static int SetLife(int type)
{ switch (type) case 0: life = 4; break; case 1: life = 6; case 2: life = 7; } return life; //返回速度值 //构造方法,用来实例化敌人对象 public Enemy(int x,int y,int type,directions dir) :base(x,y,SetLife(type),imgEnemy1[0].Width,imgEnemy1[0].Height,SetSpeed(type),dir) this.type = type; Beborn(); //構造出生用 //構造敵人隨機方向

6 public void EnemyAI() { if (rdm.Next(0, 100) < 2) switch(rdm.Next(0,4)) case 0: dir = directions.U; break; case 1: dir = directions.D; case 2: dir = directions.L; case 3: dir = directions.R; } //重写Move方法

7 public override void Move()
{ EnemyAI(); //先判斷隨機方向,然后再讓他移動 Adjustdirection(); if (this.X > 720 || this.X < 0 || this.Y > 720 || this.Y < 0) //如果剛好在四個邊界,隨機賦予的一個方向 switch (rdm.Next(0, 4)) case 0: dir = directions.U; break; case 1: dir = directions.D; case 2: dir = directions.L; case 3: dir = directions.R; } base.Move();//调用基类的Move方法 //重写绘制方法

8 public override void Draw(Graphics g)
{ if (BornTimer < 48) //在閃耀效果加載前不能移動 BornTimer++; return; } Move();//调用Move方法 switch (type) //先判斷類型,再判斷方向,再確定圖片的加載 case 0: switch (dir) case directions.U: g.DrawImage(imgEnemy1[0], this.X, this.Y); break; case directions.D: g.DrawImage(imgEnemy1[1], this.X, this.Y); case directions.L: g.DrawImage(imgEnemy1[2], this.X, this.Y); case directions.R: g.DrawImage(imgEnemy1[3], this.X, this.Y);

9 case 1: switch (dir) { case directions.U: g.DrawImage(imgEnemy2[0], this.X, this.Y); break; case directions.D: g.DrawImage(imgEnemy2[1], this.X, this.Y); case directions.L: g.DrawImage(imgEnemy2[2], this.X, this.Y); case directions.R: g.DrawImage(imgEnemy2[3], this.X, this.Y); } case 2: g.DrawImage(imgEnemy3[0], this.X, this.Y); g.DrawImage(imgEnemy3[1], this.X, this.Y); g.DrawImage(imgEnemy3[2], this.X, this.Y); g.DrawImage(imgEnemy3[3], this.X, this.Y);

10 if (rdm.Next(0, 10) < 3) //使敵人隨機發射子弹
{ Fire(); } //敵人開火方法 public override void Fire() Singleton.Instance.AddElement(new EnemyMissile(this, 1, 15, 3)); //对象.生命.速度.威力 //给敌人添加死亡的方法 public void IsDead() if (this.Life == 0) // Sound soundBlast = new Sound(Resources.blast); // soundBlast.Play(); Singleton.Instance.AddElement(new Blast(this.X - 25, this.y - 25)); Singleton.Instance.RemoveElement(this); //给敌人添加出生的方法 public override void Beborn() Singleton.Instance.AddElement(new Born(this.X, this.Y));

11 #region 判斷我的子彈是否打到敵人坦克
for (int i = 0; i < myMissile.Count; i++) { for (int j = 0; j < enemy.Count; j++) try if (myMissile[i].GetRectangle().IntersectsWith(enemy[j].GetRectangle())) enemy[j].Life -= myMissile[i].power; enemy[j].IsDead(); myMissile.Remove(myMissile[i]); } catch

12 THE END


Download ppt "人工智慧概論 坦克AI 498g0061-許顥."

Similar presentations


Ads by Google