文章目录
项目介绍及运行展示
模拟实现QQ农场的种菜环节,种菜包括播种、生长、开花、结果、收获五个步骤。菜的生长必须依照上述步骤进行,否则会弹出消息提示框。有一个标签栏显示你当前已经收获了多少果实。
设计思路及方法
界面部分
包括背景图片,5个PictureBox控件(播种,生长,开花,结果,收获),一个Label标签(显示目前收获的果实)。
枚举类型
有一个枚举变量PlaneState, 包括六种变量元素:Nothing,SowSeed,Growing,Blooming,BearFruit,Harvest。
主窗体程序
成员变量
包括一个列表Seeds:用来储存新一轮种植的作物(PictureBox);
一个PictureBox NewSeed: 用来表示刚刚产生的一个作物;
一个Bool型变量move_seed, 为true时表示Newseed可以跟随鼠标移动;
一个PlantState 型变量State,表示当前种植阶段,程序在不同状态之间转换,相当于一个状态机。
一个int型变量number,表示当前已收获的果实数量。
Form1_Load方法
主要进行一些成员变量的初始化,以及注册一些方法。
private void Form1_Load(object sender, EventArgs e)
{
this.State = PlantState.Nothing;
this.NewSeed = null;
this.Seeds = new List<PictureBox>();
this.move_seed = false;
this.number = 0;
this.MouseMove += new System.Windows.Forms.MouseEventHandler(Form1_MouseMove);
}
5组MouseEnter,MouseLeave方法
这是5个PictureBox控件的方法,注意这些方法我事先都在Form1.Designer.cs中进行了注册。
private void Sow_seed_MouseEnter(object sender, EventArgs e)
{
Sow_seed.Image = Image.FromFile("播种1.png");
}
private void Sow_seed_MouseLeave(object sender, EventArgs e)
{
Sow_seed.Image = Image.FromFile("播种.png");
}
private void Grow_seed_MouseEnter(object sender, EventArgs e)
{
Grow_seed.Image = Image.FromFile("生长1.png");
}
private void Grow_seed_MouseLeave(object sender, EventArgs e)
{
Grow_seed.Image = Image.FromFile("生长.png");
}
private void Bloom_seed_MouseEnter(object sender, EventArgs e)
{
Bloom_seed.Image = Image.FromFile("开花1.png");
}
private void Bloom_seed_MouseLeave(object sender, EventArgs e)
{
Bloom_seed.Image = Image.FromFile("开花.png");
}
private void Fruit_seed_MouseEnter(object sender, EventArgs e)
{
Fruit_seed.Image = Image.FromFile("结果1.png");
}
private void Fruit_seed_MouseLeave(object sender, EventArgs e)
{
Fruit_seed.Image = Image.FromFile("结果.png");
}
private void Harvest_seed_MouseEnter(object sender, EventArgs e)
{
Harvest_seed.Image = Image.FromFile("收获1.png");
}
private void Harvest_seed_MouseLeave(object sender, EventArgs e)
{
Harvest_seed.Image = Image.FromFile("收获.png");
}
NewSeed_Click方法
这个方法的注册在生成一个新的NewSeed之后,主要是改变move_seed的状态。
private void NewSeed_Click(object sender, EventArgs e)
{
if (this.move_seed == true)
{
this.move_seed = false;
}
else
this.move_seed = true;
}
Form1_MouseMove()方法
这个方法的注册在Form1_Load()函数中。
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(this.move_seed==true)
{
NewSeed.Location = new Point(e.X-20,e.Y-20);
}
}
5组Click方法
这是6个状态之间的转换(初始状态Nothing),其中第一个Click方法最重要,涉及新产生作物,成员变量都需要新定义。
private void Sow_seed_Click(object sender, EventArgs e)
{
if (this.State == PlantState.Nothing || this.State == PlantState.SowSeed || this.State == PlantState.Harvest)
{
this.NewSeed = new PictureBox();
this.NewSeed.Image = Properties.Resources.seed;
this.NewSeed.Location = new Point(51, 350);
this.NewSeed.Name = "NewSeed";
this.NewSeed.BackColor = Color.Transparent;
this.NewSeed.Size = new Size(Properties.Resources.seed.Width, Properties.Resources.seed.Height);
Controls.Add(this.NewSeed);
this.NewSeed.Click += new System.EventHandler(this.NewSeed_Click);
this.Seeds.Add(this.NewSeed);
this.State = PlantState.SowSeed;
this.number++;
}
else if (this.State == PlantState.Growing)
{
MessageBox.Show("有作物在生长期,不可播种!");
}
else if(this.State==PlantState.Blooming)
{
MessageBox.Show("有作物在开花期,不可播种!");
}
else if (this.State == PlantState.BearFruit)
{
MessageBox.Show("有作物在结果期,不可播种!");
}
}
private void Grow_seed_Click(object sender, EventArgs e)
{
switch(this.State)
{
case PlantState.Nothing:
MessageBox.Show("目前没有作物,无法生长!");
break;
case PlantState.SowSeed:
foreach (PictureBox Seed in this.Seeds)
{
Seed.Image = Properties.Resources.grow;
Seed.Size = new Size(Properties.Resources.grow.Width, Properties.Resources.grow.Height);
}
this.State = PlantState.Growing;
this.move_seed = false;
break;
case PlantState.Growing:
MessageBox.Show("已经生长过了!");
break;
case PlantState.Blooming:
MessageBox.Show("已经生长过了,在开花!");
break;
case PlantState.BearFruit:
MessageBox.Show("已经生长过了,在结果!");
break;
case PlantState.Harvest:
MessageBox.Show("已经生长过了,在收获!");
break;
default:
break;
}
}
private void Bloom_seed_Click(object sender, EventArgs e)
{
switch (this.State)
{
case PlantState.Nothing:
MessageBox.Show("没有作物,无法开花!");
break;
case PlantState.SowSeed:
MessageBox.Show("刚刚播种,无法开花!");
break;
case PlantState.Growing:
foreach (PictureBox Seed in this.Seeds)
{
Seed.Image = Properties.Resources.bloom;
Seed.Size = new Size(Properties.Resources.bloom.Width, Properties.Resources.bloom.Height);
}
this.State = PlantState.Blooming;
break;
case PlantState.Blooming:
MessageBox.Show("已经开过花了!");
break;
case PlantState.BearFruit:
MessageBox.Show("已经开过花了,正在结果!");
break;
case PlantState.Harvest:
MessageBox.Show("已经开过花了,正在收获!");
break;
default:
break;
}
}
private void Fruit_seed_Click(object sender, EventArgs e)
{
switch (this.State)
{
case PlantState.Nothing:
MessageBox.Show("没有作物,无法结果!");
break;
case PlantState.SowSeed:
MessageBox.Show("刚刚播种,无法结果!");
break;
case PlantState.Growing:
MessageBox.Show("刚刚生长,无法结果!");
break;
case PlantState.Blooming:
foreach (PictureBox Seed in this.Seeds)
{
Seed.Image = Properties.Resources.fruit;
Seed.Size = new Size(Properties.Resources.fruit.Width, Properties.Resources.fruit.Height);
}
this.State = PlantState.BearFruit;
break;
case PlantState.BearFruit:
MessageBox.Show("已经结果了,请收获!");
break;
case PlantState.Harvest:
MessageBox.Show("正在收获,无法结果!");
break;
default:
break;
}
}
private void Harvest_seed_Click(object sender, EventArgs e)
{
switch (this.State)
{
case PlantState.Nothing:
MessageBox.Show("没有作物,无法收获!");
break;
case PlantState.SowSeed:
MessageBox.Show("刚刚播种,无法收获!");
break;
case PlantState.Growing:
MessageBox.Show("刚刚生长,无法收获!");
break;
case PlantState.Blooming:
MessageBox.Show("刚刚开花,无法收获!");
break;
case PlantState.BearFruit:
foreach (PictureBox Seed in this.Seeds)
{
Seed.Dispose();
}
this.State = PlantState.Harvest;
this.label1.Text = "您目前收获了" + this.number + "个果实!";
break;
case PlantState.Harvest:
MessageBox.Show("已经收获过了!");
break;
default:
break;
}
}
遇到的困难及解决方法
/*
* 解决的问题:
* 1.使用List泛型集合来生成动态数组。
* 2.对窗体使用MouseMove事件来拖动PictureBox控件
* 3.用foreach语句来遍历PictureBox数组中的每个元素。
* 4.用Controls.Add()函数在窗体中添加控件。
* 5.用Dispose函数销毁控件。
* 6.将PictureBox控件的SizeMode属性设置为Zoom保证图片完全填充。
* 7.使用一个bool型变量move_seed来控制能否移动NewSeed。
* 8.NewSeed.Location = new Point(e.X-20,e.Y-20);保证NewSeed跟着鼠标移动。
*/