面向对象的文字游戏
Players.cs
using System;
namespace _7._21
{
class Player
{
//名称
public string name = Game.givename;
//public string Name { get; }
//最大血量
public int maxHp = Tools.Random(90, 110);
//成长血量
public int g_Hp = Tools.Random(90, 110);
//当前血量
private int currentHp = 0;
//属性
public int CurrentHp
{
set
{
currentHp = value < 0 ? 0 : value ;
currentHp = value > maxHp ? maxHp : value;
}
get
{
return currentHp;
}
}
//攻击力
public int atk = Tools.Random(10, 15);
//成长攻击力
public int g_atk = Tools.Random(10, 15);
//速度
public int speed = Tools.Random(10, 15);
//成长速度
public int g_speed = Tools.Random(10, 15);
//闪避
public int miss = Tools.Random(5, 10);
//暴击
public int satk = Tools.Random(5, 10);
//经验
public int exp = 0;
//等级 n*exp
public int level = 1;
//钱
public int money = 0;
public Player()
{
CurrentHp = maxHp;
Debug.Log("角色创建成功!", ConsoleColor.Blue);
}
//显示信息
public void ShowInfo()
{
string str = string.Format("{0}当前属性:", name);
Debug.Log(str, ConsoleColor.Green);
str = string.Format("血量:{0}/{1},攻击:{2},速度:{3},暴击:{4},闪避:{5}", currentHp, maxHp, atk, speed, satk, miss);
Debug.Log(str, ConsoleColor.Green);
str = string.Format("等级:{0},经验:{1}/{2},金钱:{3}", level, exp, level * 100, money);
Debug.Log(str, ConsoleColor.Green);
}
//增加经验
public bool AddExp(int exp)
{
this.exp += exp;
bool isLevelUp = false;
//判断升级
while(this.exp>=100*level)
{
this.exp -= 100 * level;
level++;
//增加属性
maxHp += g_Hp;
atk += g_atk;
speed += g_speed;
//血量回满
currentHp = maxHp;
//打印
Debug.Log("恭喜等级提升为" + level + "级!", ConsoleColor.Yellow, 0);
ShowInfo();
isLevelUp = true;
}
return isLevelUp;
}
//增加金钱
public void AddMoney(int money)
{
this.money += money;
}
//减少金钱
public bool SubMoney(int money)
{
if(this.money >=money)
{
this.money -= money;
return true;
}
return false;
}
public void AddAtk(int atk)
{
this.atk += atk;
}
//攻击
public bool Attack(Enemy enemy)
{
return enemy.GetAttack(atk);
}
//受到攻击
public bool GetAttack(Enemy enemy)
{
//判断是否命中
if(Tools.Random(0, 100) < miss)
{
Debug.Log("你躲过了" + enemy.name + "的攻击");
return false;
}
//判断是否暴击
int num = enemy.atk;
if(Tools.Random(0, 100) < enemy.satk)
{
num = enemy.atk * 2;
}
CurrentHp -= num;
Debug.Log("你受到" + enemy.name + num+"点伤害。");
if (CurrentHp <= 0)
{
//死亡
Debug.Log("你被" + enemy.name + "无情的杀死了!");
return true;
}
return false;
}
}
}
Enemy.cs
namespace _7._21
{
class Enemy
{
public int id = 0;
public string name = "I型崩坏兽";
public int hp = 30;
public int atk = 20;
public int speed = 10;
public int satk = 5;
public int miss = 5;
public int exp = 50;
public int money = 5;
//攻击
public bool Attack()
{
return Game.player.GetAttack(this);
}
//受到攻击
public bool GetAttack(int damage)
{
if(Tools .Random(0, 100) < miss)
{
Debug.Log(name + "躲过了你的攻击");
return false;
}
if(Tools.Random(0, 100) < Game.player.satk)
{
damage *= 2;
}
hp -= damage;
Debug.Log("你对" + name + "造成了" + damage + "点伤害");
bool res = false;
if (hp <= 0)
{
res = true;
Debug.Log("你击败了" + name + "!经验+" + exp + ",金钱+" + money);
Game.player.AddExp(exp);
Game.player.AddMoney(money);
}
return res;
}
}
}
Tools.cs
using System;
namespace _7._21
{
class Tools
{
public static int Random(int min,int max)
{
return new Random().Next(min, max + 1);
}
}
}
Debug.cs
using System;
using System.Threading;
namespace _7._21
{
//输出调试类
class Debug
{
//输出
public static void