我使用MenuStrip作为战斗中的动作命令为我和我的家人制作一个小游戏。但是,我无法弄清楚如何为魔术设置Click功能。因为menustrip大部分是空白的,所以我不能创建法术和目标列表,并为这些法术设置点击方法。我有点陷入僵局......
string[] Spells = { };
string[] Elements = { };
string[] Targets = { };
double[] Power = { };
double[] ManaCost = { };
StreamReader strReader = null;
try
{
FileStream flsStream = new FileStream(Application.StartupPath + "\\Spells.txt", FileMode.Open, FileAccess.Read);
strReader = new StreamReader(flsStream);
while (strReader.Peek() != -1)
{
string line = strReader.ReadLine();
string[] lines = line.Split(':');
Array.Resize(ref Spells, Spells.Length + 1);
Array.Resize(ref Elements, Elements.Length + 1);
Array.Resize(ref Targets, Targets.Length + 1);
Array.Resize(ref Power, Power.Length + 1);
Array.Resize(ref ManaCost, ManaCost.Length + 1);
Spells[Spells.Length - 1] = lines[0];
Elements[Elements.Length - 1] = lines[1];
Targets[Targets.Length - 1] = lines[2];
Power[Power.Length - 1] = Convert.ToDouble(lines[3]);
ManaCost[ManaCost.Length - 1] = Convert.ToDouble(lines[4]);
}
for (int x = 0; x < Spells.Length; x++)
{
mnuMagic.DropDownItems.Add(Spells[x]);
mnuMagic.DropDownItems[x].Name = string.Format("tsmi{0}", Spells[x]);
mnuMagic.DropDownItems[x].Text += string.Format(", {0} MP", ManaCost[x]);
if (Targets[x] == "Single")
{
try
{
if (Foe1.Status != "Dead")
{
if (Foe1.Health > Foe1.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[0]);
else if (Foe1.Health > Foe1.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[2]);
}
if (Foe2.Status != "Dead")
{
if (Foe2.Health > Foe2.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[0]);
else if (Foe2.Health > Foe2.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[2]);
}
if (Foe3.Status != "Dead")
{
if (Foe3.Health > Foe3.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[0]);
else if (Foe3.Health > Foe3.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[2]);
}
if (Foe4.Status != "Dead")
{
if (Foe4.Health > Foe4.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[0]);
else if (Foe4.Health > Foe4.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[2]);
}
if (Foe5.Status != "Dead")
{
if (Foe5.Health > Foe5.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[0]);
else if (Foe5.Health > Foe5.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[2]);
}
}
catch { }
}
else if (Targets[x] == "Ally")
{
try
{
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter1.Name,imgEnemyStatus.Images[0]);
if (Fighter2.Name != null)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter2.Name);
if (Fighter3.Name != null)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter3.Name);
}
catch { }
}
else if (Targets[x] == "All")
{
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add("All Enemies");
}
else if (Targets[x] == "Team")
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add("All Allies");
if (Elements[x] == "Fire")
mnuMagic.DropDownItems[x].ForeColor = Color.Red;
else if (Elements[x] == "Water")
mnuMagic.DropDownItems[x].ForeColor = Color.Blue;
else if (Elements[x] == "Earth")
mnuMagic.DropDownItems[x].ForeColor = Color.FromArgb(255, 128, 0);
else if (Elements[x] == "Wind")
mnuMagic.DropDownItems[x].ForeColor = Color.LimeGreen;
if (ManaCost[x] > Fighter1.Mana) { mnuMagic.DropDownItems[x].Enabled = false; }
}
}
catch { }
}
答案 0 :(得分:0)
我认为您应该开始将代码划分为子类。即使是小游戏,它也会非常有用。这是我拼凑的一些代码。它不能处理您拥有的所有内容,但您可以轻松扩展该功能。如果您需要其他信息,请告诉我。
public partial class Form1 : Form
{
private double CurrentMana { get; set; } = 50;
private List<BasicSpell> m_lstSpells = new List<BasicSpell>();
private List<BasicTarget> m_lstTargets = new List<BasicTarget>();
public Form1()
{
InitializeComponent();
Init();
try
{
ToolStripMenuItem tsmiMagic = new ToolStripMenuItem("Magic");
foreach (BasicSpell spell in m_lstSpells)
{
ToolStripMenuItem tsmiSpell = spell.ToToolStripMenuItem();
if (spell.CanHitAllEnemies)
{
ToolStripItem tsiAll = tsmiSpell.DropDownItems.Add("All");
tsiAll.Click += (sender, arguments) => m_lstTargets.ForEach((target) => TryCastSpell(spell, target));
}
else { } // This spell can not hit all enemies
for (int i = 0; i < m_lstTargets.Count; i++)
{
BasicTarget target = m_lstTargets[i];
ToolStripItem tsiTarget = tsmiSpell.DropDownItems.Add($"Target {i}: {target.Name}");
tsiTarget.Image = target.StatusImage;
tsiTarget.Click += (sender, arguments) => TryCastSpell(spell, target);
}
tsmiMagic.DropDownItems.Add(tsmiSpell);
}
m_tsCombatOptions.Items.Add(tsmiMagic);
}
catch (Exception ex)
{
MessageBox.Show($"{ex}");
}
}
private void TryCastSpell(BasicSpell spell, BasicTarget target)
{
if (!spell.Cast(target))
{
MessageBox.Show($"You can not cast this spell with your current mana!");
}
}
private void Init()
{
m_lstSpells.Add(new Fireball());
m_lstSpells.Add(new Fireball());
m_lstSpells.Add(new Fireball());
m_lstSpells.Add(new Fireball());
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
}
}
// TODO: POWER?
[Flags]
public enum Element
{
Fire = 0x0,
Earth = 0x1,
Water = 0x2,
Wind = 0x4
}
public abstract class BasicSpell
{
public string Name { get; set; }
public Element DamageType { get; set; }
public double ManaCost { get; set; }
public bool CanHitAllEnemies { get; set; } = false;
public abstract bool Cast(BasicTarget target);
public ToolStripMenuItem ToToolStripMenuItem()
{
ToolStripMenuItem tsmiSpell = new ToolStripMenuItem();
tsmiSpell.Text = $"{this.Name}, {ManaCost} MP";
return tsmiSpell;
}
}
public class Fireball : BasicSpell
{
public override bool Cast(BasicTarget target)
{
// TODO: Check if castable (maybe add caster as parameter)
target.HP -= 500;
return true;
}
}
public abstract class BasicTarget
{
public enum CombatStatus
{
Alive = 0,
Dead = 1
}
public enum TargetRelation
{
Enemy = 0,
Ally = 1
}
public string Name { get; set; }
public double HP { get; set; }
public CombatStatus Status { get; set; }
public TargetRelation Relation { get; set; }
public Image StatusImage => null;
}
public class Monster : BasicTarget
{
public Monster(string strName, int nHP)
{
this.Name = strName;
this.HP = nHP;
this.Status = CombatStatus.Alive;
this.Relation = TargetRelation.Enemy;
}
}