A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xt654005440 中级黑马   /  2013-8-7 21:17  /  1083 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

RT:下面是我自己写的个小RPG游戏,现在我只实现了角色使用普通攻击方式;原想这要有若干技能(比如战士怒斩伤害60消耗30MP),而且要前添加入回复状态(比如25HP/回合)。如何以面向对象的思维方法将这些融入进去,希望有兴趣的给点意见及你的实现方法?{:soso_e100:}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RPG小游戏
{
    class BOSS                              //BOSS
    {
        public string Name = "大大王";
        //private int hp = 10000;
        public int HP { set; get; }
    }
    class Role     //角色
    {
        public int HP { set; get; }        //HP属性
        public int MP { set; get; }        //MP属性
        public int AP { set; get; }        //AP属性
    }
    class Fighter : Role   //战士
    {
        public string Name { set; get; }    //名字
        public int Fight()             //普通攻击
        {
            
            //Console.WriteLine("普通攻击造成30点伤害\t");
            int x = 30;
            return x;
            
        }
        
    }
    class Magic : Role   //法师
    {
        public string Name { set; get; }    //名字
        public int Fight()             //普通攻击
        {
            //Console.WriteLine("普通攻击造成25点伤害\t");
            int x = 25;
            return x;
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            
            BOSS boss = new BOSS();             //实例化BOSS
            boss.HP = 1000;                     //BOSS的HP为1000
            Console.WriteLine("欢迎来到RPG小游戏,BOSS: {0},HP {1}\t", boss.Name, boss.HP);
            Console.WriteLine("请问您要创建哪种职业,战士请选“1”、法师请选“2”\n");
        id:     //设置标签
            string str = Console.ReadLine();    //选择职业
            int i1;
            if (int.TryParse(str, out i1))      //判断输入是否合法
            {
                switch (i1)
                {
                    case 1:
                        Fighter fighter = new Fighter();                //实例化Fighter
                        Console.WriteLine("给你的战士取个名字吧!");   
                        fighter.Name = Console.ReadLine();              //为Name赋值
                        fighter.HP = 250;                               //为HP赋值
                        fighter.MP = 80;                                //为MP赋值   
                        fighter.AP = 150;                               //为AP赋值
                        Console.WriteLine("{0}: {1} HP {2} MP {3} AP {4}",fighter.Name,fighter.Name, fighter.HP, fighter.MP, fighter.AP);
                        Console.WriteLine();
                        while (boss.HP > 0)             //判断BOSS的HP时候大于0
                        {
                            Console.WriteLine("请选择攻击方式:1、普通攻击 2、技能一 3、技能二 4、怒气技能 ");
                            int i2 = Convert.ToInt32(Console.ReadLine());
                            switch (i2)                             //选择攻击方式
                            {
                                case 1: //普通攻击
                                    Console.WriteLine("普通攻击造成30点伤害\t");
                                    boss.HP = boss.HP - fighter.Fight();
                                    Console.Write("BOSS的HP剩下 {0}\n", boss.HP);
                                    break;
                                case 2: //技能,待定
                                    break;
                                default: break;
                            }
                        }                     
                        break;
                    case 2:
                        Magic magic = new Magic();
                        Console.WriteLine("给你的法师取个名字吧!");
                        magic.Name = Console.ReadLine();
                        magic.HP = 80;
                        magic.MP = 250;
                        magic.AP = 100;
                        Console.WriteLine("{0}: {1} HP {2} MP {3} AP {4}", magic.Name, magic.Name, magic.HP, magic.MP, magic.AP);
                        Console.WriteLine();
                        while (boss.HP > 0)                        
                        {
                            Console.WriteLine("请选择攻击方式:1、普通攻击 2、技能一 3、技能二 4、怒气技能 ");
                            int i2 = Convert.ToInt32(Console.ReadLine());
                            switch (i2)
                            {
                                case 1:
                                    Console.WriteLine("普通攻击造成25点伤害\t");
                                    boss.HP = boss.HP - magic.Fight();
                                    Console.Write("BOSS的HP剩下 {0}\n", boss.HP);
                                    break;
                                case 2:
                                    break;
                            }
                        }
                        break;
                    default: Console.WriteLine("您的选择无效!"); break;
                }
            }
            else
            {
                Console.WriteLine("请重新选择。\n");
                goto id;            //跳转到id位置
               
            }                     
            Console.ReadKey();                     
        }
    }
}

评分

参与人数 1技术分 +1 收起 理由
赵宗荣 + 1

查看全部评分

1 个回复

倒序浏览
看看我这个代码,你有能借鉴的地方没
  1. using System;
  2. namespace Game
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {

  8.             Hero h = new Hero();

  9.             bool bselect;
  10.             do
  11.             {
  12.                 bselect = false;
  13.                 Console.WriteLine("请选择职业:1.法师 2.战士");
  14.                 string sinputhero = Console.ReadLine();
  15.                 switch(sinputhero)
  16.                 {
  17.                     case "1":
  18.                         h = (Mage)new Mage();                    
  19.                     break;
  20.                     case "2":
  21.                         h = (Warrior)new Warrior();   
  22.                     break;
  23.                     default:
  24.                         Console.WriteLine("输入错误,请重新输入!");
  25.                         bselect = true;                        
  26.                     break;
  27.                 }
  28.             }while(bselect);
  29.             //报告
  30.             Console.WriteLine("角色创建成功!");
  31.             while(h.hp>0)
  32.             {
  33.                 Console.WriteLine("你现在想做什么?1.打怪 2.休息 3.属性报告");
  34.                 string sinputhero = Console.ReadLine();
  35.                 switch(sinputhero)
  36.                 {
  37.                     case "1":
  38.                         //h.Fight();  自动匹配同等级的怪物
  39.                         Random r = new Random();
  40.                         int ir = r.Next(h.level-2,h.level+3);

  41.                         h.Fight(new Boss(ir));   
  42.                
  43.                     break;
  44.                     case "2":
  45.                         h.hp+=20;
  46.                     break;
  47.                     case "3":
  48.                         h.Showme();
  49.                     break;
  50.                     default:
  51.                         Console.WriteLine("输入错误,请重新输入!");
  52.                         bselect = true;                        
  53.                     break;
  54.                 }



  55.             }
  56.             Console.WriteLine("你的角色已死亡,游戏结束!");        

  57.             //PK();
  58.             //Raid();
  59.             //m.Showme();
  60.             //w.Showme();
  61.             Console.ReadKey();
  62.         }
  63.     }
  64.    
  65.     //定义一个Boss类
  66.     class Boss
  67.     {
  68.         public int hp;
  69.         public int ap;
  70.         public int level;

  71.         public Boss(int level)
  72.         {   
  73.             this.ap = 2*level;
  74.             this.hp = 50*level;
  75.             this.level = level;
  76.         }

  77.     }
  78.     //定义一个英雄类,英雄类有一个PK()和一个Raid() PK(Hero h1,Hero h2) Raid(Hero h,Boss b)
  79.     class Hero
  80.     {
  81.         public virtual void Fight(Boss b)
  82.         {
  83.         }
  84.         public virtual void Showme()
  85.         {
  86.             Console.WriteLine("属性:");
  87.             Console.WriteLine("等级:{0}",this.level);
  88.             Console.WriteLine("当前经验/升级经验:{0}/{1}",this.exp,this.maxexp);
  89.             Console.WriteLine("生命值:{0}",this.hp);
  90.             Console.WriteLine("攻击强度:{0}",this.ap);
  91.             Console.WriteLine("护甲值:{0}",this.hujia);

  92.         }
  93.         //等级
  94.         public int level = 1;  

  95.         //最大经验值
  96.         protected int maxexp = 100;

  97.         //当前经验值
  98.         protected int exp = 0;

  99.         //攻击强度 生命值 护甲值
  100.         protected int ap = 10;
  101.         public int hp = 100;
  102.         protected int hujia = 1;

  103.         //升级的动作
  104.         protected virtual void Levelup()
  105.         {
  106.             this.level++;
  107.             this.maxexp = 100 * level;  
  108.             this.ap += 10;
  109.             this.hp += 100;   
  110.             this.hujia++;      
  111.         }

  112.         //升级的动作用于调试
  113.         public void Levelup(int i1)
  114.         {
  115.             for(int i=0;i<i1;i++)
  116.             {
  117.                  this.Levelup();
  118.             }
  119.         }
  120.         
  121.         //获取经验
  122.         public void Getexp(int i)
  123.         {
  124.             if(i>=this.maxexp)
  125.             {
  126.                 Console.WriteLine("经验值太多,无法吸收");
  127.             }
  128.             else
  129.             {
  130.                 this.exp += i;
  131.                 while(this.exp>=this.maxexp)
  132.                 {
  133.                      this.exp -= this.maxexp;
  134.                      this.Levelup();
  135.                 }
  136.             }
  137.         }
  138.     }

  139.     //定义两个类继承英雄:法师、战士
  140.     class Mage:Hero
  141.     {
  142.         public override void Fight(Boss b)
  143.         {
  144.             int itemp = 0;
  145.             while(this.hp>0 && b.hp>0)
  146.             {
  147.                 itemp++;
  148.                 Console.WriteLine("第{0}战斗回合:",itemp);
  149.                 this.hp -= b.ap;
  150.                 b.hp -= this.fp;
  151.             }
  152.             if(this.hp>=0)
  153.             {
  154.                 Console.WriteLine("Exp++!!!!!!!!!!!");
  155.                 this.Getexp(20);
  156.             }
  157.         }

  158.         protected int fp = 10;
  159.         protected int mp = 100;
  160.         public override void Showme()
  161.         {
  162.             Console.WriteLine("属性:");
  163.             Console.WriteLine("等级:{0}",this.level);
  164.             Console.WriteLine("当前经验/升级经验:{0}/{1}",this.exp,this.maxexp);
  165.             Console.WriteLine("生命值:{0}",this.hp);
  166.             Console.WriteLine("魔法值:{0}",this.mp);
  167.             Console.WriteLine("攻击强度:{0}",this.ap);
  168.             Console.WriteLine("法术强度:{0}",this.fp);
  169.             Console.WriteLine("护甲值:{0}",this.hujia);

  170.         }
  171.         protected override void Levelup()
  172.         {
  173.             this.level++;
  174.             this.maxexp = 100 * level;  
  175.             this.ap += 6;
  176.             this.hp += 80;   
  177.             this.hujia ++;      
  178.         }

  179.     }
  180.     class Warrior:Hero
  181.     {
  182.         public override void Fight(Boss b)
  183.         {
  184.             int itemp = 0;
  185.             while(this.hp>0 && b.hp>0)
  186.             {
  187.                 itemp++;
  188.                 Console.WriteLine("第{0}战斗回合:",itemp);
  189.                 this.hp -= b.ap;
  190.                 b.hp -= this.ap;
  191.             }
  192.             if(this.hp>=0)
  193.             {
  194.                 this.Getexp(20);
  195.             }
  196.         }

  197.         protected override void Levelup()
  198.         {
  199.             this.level++;
  200.             this.maxexp = 100 * level;  
  201.             this.ap += 12;
  202.             this.hp += 120;   
  203.             this.hujia +=2;      
  204.         }

  205.     }
  206. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
赵宗荣 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马