//Hero.cs
namespace MyGame
{
/// <summary>
/// 武器+伤害
/// </summary>
public enum Weapon
{
Knife = 5, Sword = 10, Hammer = 15
}
/// <summary>
/// MyGame中的玩家类
/// </summary>
public class Hero
{
public Hero() { }
public Hero(string name, int originalBlood, int attackPower, int defendPower,
Image image, Point originalLocation, Size size,Weapon weapon)
{
this.OriginalBlood = originalBlood;
//初始状态原始生命和当前声明相同
this.CurrentBlood = originalBlood;
this.HeroName = name;
this.AttackPower = attackPower;
this.DefendPower = defendPower;
this.Image = image;
this.OriginalLocation = originalLocation;
//当前位置赋予初始位置的参数即可
this.CurrentLocation = originalLocation;
this.Size = size;
this.Weapon = weapon;
}
//主角名字
private string heroName;
public string HeroName
{
get { return heroName; }
set { heroName = value; }
}
//原始生命值
private int originalBlood;
public int OriginalBlood
{
get { return originalBlood; }
set { originalBlood = value; }
}
//攻击力
private int attackPower;
public int AttackPower
{
get { return attackPower; }
set { attackPower = value; }
}
//防御力
private int defendPower;
public int DefendPower
{
get { return defendPower; }
set { defendPower = value; }
}
//玩家的图片
private Image image;
public Image Image
{
get { return image; }
set { image = value; }
}
//原始位置
private Point originalLocation;
public Point OriginalLocation
{
get { return originalLocation; }
set { originalLocation = value; }
}
//当前位置
private Point currentLocation;
public Point CurrentLocation
{
get { return currentLocation; }
set { currentLocation = value; }
}
// 大小
private Size size;
public Size Size
{
get { return size; }
set { size = value; }
}
//第三章添加的属性 玩家持有的武器
private Weapon weapon;
public Weapon Weapon
{
get { return weapon; }
set { weapon = value; }
}
//怪物的当前生命值
private int currentBlood;
public int CurrentBlood
{
get { return currentBlood; }
set
{
if (value <= 0)
{
currentBlood = 0;
}
else
{
currentBlood = value;
}
}
}
/// <summary>
/// 移动到怪物的左下角
/// </summary>
/// <param name="monster">玩家所移动到的怪物对象</param>
public void Move(TurtleMonster monster)
{
//移动到怪物左下角
this.CurrentLocation = new Point(
monster.OriginalLocation.X,
monster.OriginalLocation.Y + monster.Size.Height);
}
/// <summary>
/// 重载Move方法 返回原始位置
/// </summary>
public void Move()
{
//返回是将原始位置设为当前位置
this.CurrentLocation = this.OriginalLocation;
}
/// <summary>
/// 攻击怪物,并输出信息
/// </summary>
/// <param name="monster">攻击的怪物</param>
/// <returns>返回message</returns>
public string Attack(TurtleMonster monster)
{
//输出message
string message = string.Format("我是{0},看招。", this.HeroName);
//受到的伤害值
int totalDemage;
//判断攻击力是否小于防御力
if ((int)this.Weapon + this.AttackPower < monster.DefendPower)
{
//如果小于防御力伤害值设为0
totalDemage = 0;
}
else
{
//大于防御力,伤害值设为武器攻击力+攻击力-防御力
totalDemage = (int)this.Weapon + this.AttackPower - monster.DefendPower;
}
//计算当前的生命值
monster.CurrentBlood = monster.CurrentBlood - totalDemage;
return message;
}
/// <summary>
/// 移动到怪物的左下角
/// </summary>
/// <param name="monster">玩家所移动到的怪物对象</param>
public void Move(StructMonster monster)
{
//移动到怪物左下角
this.CurrentLocation = new Point(
monster.OriginalLocation.X,
monster.OriginalLocation.Y + monster.Size.Height);
}
/// <summary>
/// 攻击怪物,并输出信息
/// </summary>
/// <param name="monster">攻击的怪物</param>
/// <returns>返回message</returns>
public string Attack(StructMonster monster)
{
//输出message
string message = string.Format("我是{0},看招。", this.HeroName);
//受到的伤害值
int totalDemage;
//判断攻击力是否小于防御力
if ((int)this.Weapon + this.AttackPower < monster.DefendPower)
{
//如果小于防御力伤害值设为0
totalDemage = 0;
}
else
{
//大于防御力,伤害值设为武器攻击力+攻击力-防御力
totalDemage = (int)this.Weapon + this.AttackPower - monster.DefendPower;
}
//计算当前的生命值
monster.CurrentBlood = monster.CurrentBlood - totalDemage;
return message;
}
}
}
//TurtleMonster.cs
namespace MyGame
{
/// <summary>
/// MyGame中的怪物类
/// </summary>
public class TurtleMonster
{
public TurtleMonster() { }
public TurtleMonster(string name,Image image, Point originalLocation, Size size)
{
this.OriginalBlood = 200;
this.AttackPower = 50;
this.DefendPower = 10;
//初始状态原始生命和当前声明相同
this.CurrentBlood = this.OriginalBlood;
this.MonsterName = name;
this.AttackPower = attackPower;
this.DefendPower = defendPower;
this.Image = image;
this.OriginalLocation = originalLocation;
this.CurrentLocation = originalLocation;
this.Size = size;
}
//怪物名字
private string monsterName;
public string MonsterName
{
get { return monsterName; }
set { monsterName = value; }
}
//原始生命值
private int originalBlood;
public int OriginalBlood
{
get { return originalBlood; }
set { originalBlood = value; }
}
//攻击力
private int attackPower;
public int AttackPower
{
get { return attackPower; }
set { attackPower = value; }
}
//防御力
private int defendPower;
public int DefendPower
{
get { return defendPower; }
set { defendPower = value; }
}
//怪物的图片
private Image image;
public Image Image
{
get { return image; }
set { image = value; }
}
//原始位置
private Point originalLocation;
public Point OriginalLocation
{
get { return originalLocation; }
set { originalLocation = value; }
}
//当前位置
private Point currentLocation;
public Point CurrentLocation
{
get { return currentLocation; }
set { currentLocation = value; }
}
// 大小
private Size size;
public Size Size
{
get { return size; }
set { size = value; }
}
//怪物的当前生命值
private int currentBlood;
public int CurrentBlood
{
get { return currentBlood; }
set
{
if (value <= 0)
{
currentBlood = 0;
}
else
{
currentBlood = value;
}
}
}
/// <summary>
/// 怪物移动的方法
/// </summary>
/// <param name="hero">移动到玩家</param>
public void Move(Hero hero)
{
//移动到玩家
this.CurrentLocation = new Point(
hero.OriginalLocation.X,
hero.OriginalLocation.Y - hero.Size.Height
);
}
/// <summary>
/// 回到原位置方法
/// </summary>
public void Move()
{
//返回是将原始位置设为当前位置
this.CurrentLocation = this.OriginalLocation;
}
/// <summary>
/// 攻击玩家,并输出信息
/// </summary>
/// <param name="hero">攻击的玩家</param>
/// <returns>返回message</returns>
public string Attack(Hero hero)
{
string message = string.Format("吃我{0}一招!", this.MonsterName);
int totalDemage;
//判断攻击力是否小于防御力
if (this.AttackPower < hero.DefendPower)
{
//如果小于防御力伤害值设为0
totalDemage = 0;
}
else
{
//如果大于防御力,伤害值设为攻击力-防御力
totalDemage = this.AttackPower - hero.DefendPower;
}
//当前血格不能减为0
hero.CurrentBlood = hero.CurrentBlood - totalDemage;
return message;
}
/// <summary>
/// 攻击玩家,并输出信息
/// </summary>
/// <param name="hero">攻击的玩家</param>
/// <returns>返回message</returns>
public string Attack(StructHero hero)
{
string message = string.Format("吃我{0}一招!", this.MonsterName);
int totalDemage;
//判断攻击力是否小于防御力
if (this.AttackPower < hero.DefendPower)
{
//如果小于防御力伤害值设为0
totalDemage = 0;
}
else
{
//如果大于防御力,伤害值设为攻击力-防御力
totalDemage = this.AttackPower - hero.DefendPower;
}
//当前血格不能减为0
hero.CurrentBlood = hero.CurrentBlood - totalDemage;
return message;
}
/// <summary>
/// 怪物移动的方法
/// </summary>
/// <param name="hero">移动到玩家</param>
public void Move(StructHero hero)
{
//移动到玩家
this.CurrentLocation = new Point(
hero.OriginalLocation.X,
hero.OriginalLocation.Y - hero.Size.Height
);
}
}
}
//levelForm.cs
namespace MyGame
{
public partial class LevelForm : Form
{
//定义怪物和玩家
Hero hero;
TurtleMonster monster;
//StructHero hero;
public LevelForm()
{
InitializeComponent();
}
private void LevelForm_Load(object sender, EventArgs e)
{
this.lblHeroBlood.Size = new Size(0, 20);
this.lblMonsterBlood.Size = new Size(0, 20);
//初始化标签
this.lblHeroCurBlood.Size = new Size(0, 20);
this.lblMonCurBlood.Size = new Size(0, 20);
this.lblMessage.Text = "";
try
{
hero = new Hero("李小侠", 300, 50, 20, Image.FromFile("hero.gif"),
this.picHero.Location, this.picHero.Size, Weapon.Sword);
//将生命值显示到标签
this.lblHeroBlood.Width = hero.OriginalBlood;
this.lblHeroBlood.Height = 20;
//用于显示受伤的标签
this.lblHeroCurBlood.Width = hero.CurrentBlood;
this.lblHeroCurBlood.Height = 20;
//将图片显示到PictureBox
this.picHero.Image = hero.Image;
//怪物对象
monster = new TurtleMonster("小龟",Image.FromFile("turtle.gif"),
this.picMonster.Location, this.picMonster.Size);
//将生命值显示到标签
this.lblMonsterBlood.Width = monster.OriginalBlood;
this.lblMonsterBlood.Height = 20;
this.lblMonCurBlood.Width = monster.CurrentBlood;
this.lblMonCurBlood.Height = 20;
//将图片显示到PictureBox
this.picMonster.Image = monster.Image;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void UpdateUI()
{
//移动到当前位置
this.picHero.Location = hero.CurrentLocation;
this.picMonster.Location = monster.CurrentLocation;
//玩家生命值变化条
this.lblHeroCurBlood.Width = hero.CurrentBlood;
this.lblHeroCurBlood.Height = 20;
//怪物生命值变化条
this.lblMonCurBlood.Width = monster.CurrentBlood;
this.lblMonCurBlood.Height = 20;
}
private void btnAttack_Click(object sender, EventArgs e)
{
//string message = null;
调用玩家移动到怪物的方法
//hero.Move(monster);
重新显示控件
//UpdateUI();
调用玩家攻击方法
//message = hero.Attack(monster);
重新显示控件
//UpdateUI();
//this.lblMessage.Text = message;
}
private void btnReturn_Click(object sender, EventArgs e)
{
调用重载的返回
//hero.Move();
重新显示控件
//UpdateUI();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void btnMonsterAttack_Click(object sender, EventArgs e)
{
string message = null;
monster.Move(hero);
UpdateUI();
message = monster.Attack(hero);
UpdateUI();
this.lblMessage.Text = message;
}
private void btnMonsterReturn_Click(object sender, EventArgs e)
{
monster.Move();
UpdateUI();
}
}
}