当前位置:编程学习 > C#/ASP.NET >>

Head First C#(冒险游戏)

 

\

冒险游戏的界面如上图所示,就是简单的闯关游戏,一共有5种道具和3种怪物

道具分别为:剑、弓箭、锤子、蓝药水和红药水

怪物分别为:蝙蝠、幽灵、食尸鬼

类图视图如下所示,怪物类、道具类和玩家类继承于移动类。

 

\

代码实现如下:

[csharp]
?namespace 冒险游戏 

    public partial class Form1 : Form 
    { 
        private Game game; 
        private Random random = new Random(); 
     
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        public void UpdateCharacters() 
        { 
            Player.Visible = true; 
            Player.Location = game.PlayerLocation; 
            lbPlayer.Text = game.PlayerHitPoints.ToString(); 
            
            //显示和隐藏生成的敌人 
            #region Enemy                    
            int enemiesShown = game.Enemies.Count; 
             
            foreach (Enemy enemy in game.Enemies) 
            { 
                if (enemy is Bat) 
                { 
                    bat.Location = enemy.Location; 
                    lbBat.Text = enemy.HitPoints.ToString(); 
                    if (enemy.HitPoints > 0) 
                    {                        
                        bat.Visible = true; 
                    } 
                    else 
                    { 
                        enemiesShown--; 
                        bat.Visible = false; 
                        game.Enemies.Remove(enemy); 
                        break; 
                    } 
                } 
 
                if (enemy is Ghost) 
                { 
                    ghost.Location = enemy.Location; 
                    lbGhost.Text = enemy.HitPoints.ToString(); 
                    if (enemy.HitPoints > 0) 
                    {                      
                        ghost.Visible = true; 
                    } 
                    else 
                    { 
                        enemiesShown--; 
                        ghost.Visible = false; 
                        game.Enemies.Remove(enemy); 
                        break; 
                    } 
                } 
 
                if (enemy is Ghoul) 
                { 
                    ghoul.Location = enemy.Location; 
                    lbGhoul.Text = enemy.HitPoints.ToString(); 
                  &nbs

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,