//LevelForm.cs
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace MyGame
{
public partial class LevelForm : Form
{
//定义怪物和玩家
Hero hero;
TurtleMonster monster;
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);
try
{
#region 阶段4
//实例化hero对象
hero = new Hero("李小侠",300,50,20,Image.FromFile("hero.gif"),this.picHero.Location,this.picHero.Size);
///设置玩家的属性值/
//hero.HeroName = "李小侠";
//hero.OriginalBlood = 300;
//hero.AttackPower = 50;
//hero.DefendPower = 20;
//hero.Image = Image.FromFile("hero.gif");
设置原始位置
//hero.OriginalLocation = this.picHero.Location;
设置当前位置-初始状态与原始位置相同
//hero.CurrentLocation = this.picHero.Location;
设置玩家的大小
//hero.Size = this.picHero.Size;
//将生命值显示到标签
this.lblHeroBlood.Width = hero.OriginalBlood;
this.lblHeroBlood.Height = 20;
//用于显示受伤的标签
this.lblHeroCurBlood.Width = hero.OriginalBlood;
this.lblHeroCurBlood.Height = 20;
//将图片显示到PictureBox
this.picHero.Image = hero.Image;
#endregion
#region 阶段3
//实例化怪物对象
monster = new TurtleMonster("小龟",Image.FromFile("turtle.gif"), this.picMonster.Location, this.picMonster.Size);
//monster.MonsterName = "小龟";
//monster.OriginalBlood = 200;
//monster.AttackPower = 50;
//monster.DefendPower = 10;
//monster.Image = Image.FromFile("turtle.gif");
设置原始位置
//monster.OriginalLocation = this.picMonster.Location;
设置当前位置-初始状态与原始位置相同
//monster.CurrentLocation = this.picMonster.Location;
设置玩家的大小
//monster.Size = this.picMonster.Size;
//将生命值显示到标签
this.lblMonsterBlood.Width = monster.OriginalBlood;
this.lblMonsterBlood.Height = 20;
this.lblMonCurBlood.Width = monster.OriginalBlood;
this.lblMonCurBlood.Height = 20;
//将图片显示到PictureBox
this.picMonster.Image = monster.Image;
#endregion
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void UpdateUI()
{
//移动到当前位置
this.picHero.Location = hero.CurrentLocation;
this.picMonster.Location = monster.CurrentLocation;
}
private void btnMove_Click(object sender, EventArgs e)
{
//调用玩家移动到怪物的方法
hero.Move(monster);
//重新显示控件
UpdateUI();
}
private void btnReturn_Click(object sender, EventArgs e)
{
//调用重载的返回
hero.Move();
//重新显示控件
UpdateUI();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void btnMonsterMove_Click(object sender, EventArgs e)
{
monster.Move(hero);
UpdateUI();
}
private void btnMonsterReturn_Click(object sender, EventArgs e)
{
monster.Move();
UpdateUI();
}
}
}
//TurtleMonster.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
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.MonsterName = name;
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; }
}
/// <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;
}
}
}
//Hero.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace MyGame
{
/// <summary>
/// MyGame中的玩家类
/// </summary>
public class Hero
{
public Hero() { }
public Hero(string name, int originalBlood, int attackPower, int defendPower,
Image image, Point originalLocation, Size size)
{
this.OriginalBlood = originalBlood;
this.HeroName = name;
this.AttackPower = attackPower;
this.DefendPower = defendPower;
this.Image = image;
this.OriginalLocation = originalLocation;
//当前位置赋予初始位置的参数即可
this.CurrentLocation = originalLocation;
this.Size = size;
}
//主角名字
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; }
}
/// <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;
}
}
}