目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
private Panel pl;
private void Form_Load(object sender, EventArgs e)
{
PlState[, 0] = PlState[0, 1] = PlState[0, 2] = PlState[0, 3] =
PlState[, 0] = PlState[1, 1] = PlState[1, 2] = PlState[1, 3] =
PlState[, 0] = PlState[2, 1] = PlState[2, 2] = PlState[2, 3] =
PlState[, 0] = PlState[3, 1] = PlState[3, 2] = PlState[3, 3] =
PlState[, 0] = PlState[4, 3] = true;
}
private void button_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
panel.Location = position[0, 0];
panel.Location = position[0, 1];
panel.Location = position[0, 3];
panel.Location = position[2, 0];
panel.Location = position[2, 1];
panel.Location = position[2, 3];
panel.Location = position[3, 1];
panel.Location = position[3, 2];
panel.Location = position[4, 0];
panel.Location = position[4, 3];
PlState[, 0] = PlState[4, 3] = true;
PlState[, 1] = PlState[4, 2] = false;
}
#region 赵云
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources.a3);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.PV;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 曹操
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources.a1);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.P;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 张飞
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources.a4);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.PV;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 马超
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources.a5);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.PV;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 关羽
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources.a2);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.PH;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 黄忠
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources.a6);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.PV;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 士兵一
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources.a7);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.P;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 士兵二
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources.a8);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.P;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 士兵三
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources.a9);
panel.BackgroundImage = (Image)(Properties.Resources._010);
PStyle = PStyles.P;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 士兵四
private void panel_Click(object sender, EventArgs e)
{
panel.BackgroundImage = (Image)(Properties.Resources._003);
panel.BackgroundImage = (Image)(Properties.Resources._001);
panel.BackgroundImage = (Image)(Properties.Resources._004);
panel.BackgroundImage = (Image)(Properties.Resources._005);
panel.BackgroundImage = (Image)(Properties.Resources._002);
panel.BackgroundImage = (Image)(Properties.Resources._006);
panel.BackgroundImage = (Image)(Properties.Resources._007);
panel.BackgroundImage = (Image)(Properties.Resources._008);
panel.BackgroundImage = (Image)(Properties.Resources._009);
panel.BackgroundImage = (Image)(Properties.Resources.a10);
PStyle = PStyles.P;
pl = panel;
PosX = intX(panel);
PosY = intY(panel);
}
#endregion
#region 判断是否成功
private bool Successful()
{
if (panel.Location == new Point(108, 343))
{
MessageBox.Show("恭喜你,曹操已经成功逃离华容道!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
else
return false;
}
#endregion
private void Form_KeyUp(object sender, KeyEventArgs e)
{
blUp = blDown = blLeft = blRight = false;
switch (e.KeyData)
{
case Keys.Up:
blUp = true;
break;
case Keys.Down:
blDown = true;
break;
case Keys.Left:
blLeft = true;
break;
case Keys.Right:
blRight = true;
break;
}
MovePosition(pl, PosX, PosY);
if (Successful())
button_Click(sender, e);
}
public enum plEnumerate : int
{
plSize =,
plX =,
plY =,
};
public bool[,] PlState = new bool[(int)plEnumerate.plY, (int)plEnumerate.plX];
private Point[,] position = new Point[, 4] {
{ new Point(, 43), new Point(108, 43), new Point(208, 43), new Point(308, 43) },
{ new Point(, 143), new Point(108, 143), new Point(208, 143), new Point(308, 143) },
{ new Point(, 243), new Point(108, 243), new Point(208, 243), new Point(308, 243) },
{ new Point(, 343), new Point(108, 343), new Point(208, 343), new Point(308, 343) },
{ new Point(, 443), new Point(108, 443), new Point(208, 443), new Point(308, 443) },
};
public Point[,] GetPosition()
{
return this.position;
}
public enum PStyles : int
{
P = 0,
P = 1,
PV = 2,
PH = 3,
};
private int m_PStyle;
public PStyles PStyle
{
get { return (PStyles)this.m_PStyle; }
set { this.m_PStyle = (int)value; }
}
private int m_X =, m_Y = 0;
public int PosX
{
get { return this.m_X; }
set { this.m_X = value; }
}
public int PosY
{
get { return this.m_Y; }
set { this.m_Y = value; }
}
#region 计算选中的人物方格在数组中的列索引
private int intX(Panel pl)
{
int i = pl.Location.X -;
return i /;
}
#endregion
#region 计算选中的人物方格在数组中的行索引
private int intY(Panel pl)
{
int i = pl.Location.Y -;
return i /;
}
#endregion
private bool blUp, blDown, blLeft, blRight;
#region 移动人物位置
public bool MovePosition(Panel pl, int x, int y)
{
#region 上移
if (blUp && (y -) >= 0)
{
switch (PStyle)
{
case PStyles.P:
case PStyles.PH:
if (!PlState[y -, x] && !PlState[y - 1, x + 1])
{
pl.Location = GetPosition()[y -, x];
if (PStyle == PStyles.P)
{
PlState[y +, x] = false;
PlState[y +, x + 1] = false;
}
else if (PStyle == PStyles.PH)
{
PlState[y, x] = false;
PlState[y, x +] = false;
}
PlState[y -, x] = true;
PlState[y -, x + 1] = true;
PosY -=;
return true;
}
else return false;
case PStyles.PV:
case PStyles.P:
if (!PlState[y -, x])
{
pl.Location = GetPosition()[y -, x];
if (PStyle == PStyles.PV)
{
PlState[y +, x] = false;
}
else if (PStyle == PStyles.P)
{
PlState[y, x] = false;
}
PlState[y -, x] = true;
PosY -=;
return true;
}
else return false;
}
}
#endregion
#region 下移
else if (blDown)
{
switch (PStyle)
{
case PStyles.P:
if ((y +) < (int)plEnumerate.plY && !PlState[y + 2, x] && !PlState[y + 2, x + 1])
{
pl.Location = GetPosition()[y +, x];
PlState[y, x] = false;
PlState[y, x +] = false;
PlState[y +, x] = true;
PlState[y +, x + 1] = true;
PosY +=;
return true;
}
else return false;
case PStyles.PV:
if ((y +) < (int)plEnumerate.plY && !PlState[y + 2, x])
{
pl.Location = GetPosition()[y +, x];
PlState[y, x] = false;
PlState[y +, x] = true;
PosY +=;
return true;
}
else return false;
case PStyles.P:
if ((y +) < (int)plEnumerate.plY && !PlState[y + 1, x])
{
pl.Location = GetPosition()[y +, x];
PlState[y, x] = false;
PlState[y +, x] = true;
PosY +=;
return true;
}
else return false;
case PStyles.PH:
if ((y +) < (int)plEnumerate.plY && !PlState[y + 1, x] && !PlState[y + 1, x + 1])
{
pl.Location = GetPosition()[y +, x];
PlState[y, x] = false;
PlState[y, x +] = false;
PlState[y +, x] = true;
PlState[y +, x + 1] = true;
PosY +=;
return true;
}
else return false;
}
}
#endregion
#region 左移
else if (blLeft)
{
switch (PStyle)
{
case PStyles.PV:
case PStyles.P:
if (x - >= 0 && !PlState[y, x - 1] && !PlState[y + 1, x - 1])
{
pl.Location = GetPosition()[y, x -];
switch (PStyle)
{
case PStyles.P:
PlState[y, x +] = false;
PlState[y +, x + 1] = false;
break;
case PStyles.PV:
PlState[y, x] = false;
PlState[y +, x] = false;
break;
}
PlState[y, x -] = true;
PlState[y +, x - 1] = true;
PosX -=;
return true;
}
else return false;
case PStyles.P:
case PStyles.PH:
if (x - >= 0 && !PlState[y, x - 1])
{
pl.Location = GetPosition()[y, x -];
if (PStyle == PStyles.PH)
{
PlState[y, x +] = false;
}
else
{
PlState[y, x] = false;
}
PlState[y, x -] = true;
PosX -=;
return true;
}
else return false;
}
}
#endregion
#region 右移
else if (blRight)
{
switch (PStyle)
{
case PStyles.P:
if (x + < (int)plEnumerate.plX && !PlState[y, x + 2] && !PlState[y + 1, x + 2])
{
pl.Location = GetPosition()[y, x +];
PlState[y, x] = false;
PlState[y +, x] = false;
PlState[y, x +] = true;
PlState[y +, x + 2] = true;
PosX +=;
return true;
}
else return false;
case PStyles.P:
if (x + < (int)plEnumerate.plX && !PlState[y, x + 1])
{
pl.Location = GetPosition()[y, x +];
PlState[y, x] = false;
PlState[y, x +] = true;
PosX +=;
return true;
}
else return false;
case PStyles.PH:
if (x + < (int)plEnumerate.plX && !PlState[y, x + 2])
{
pl.Location = GetPosition()[y, x +];
PlState[y, x] = false;
PlState[y, x +] = true;
PosX +=;
return true;
}
else return false;
case PStyles.PV:
if (x + < (int)plEnumerate.plX && !PlState[y, x + 1] && !PlState[y + 1, x + 1])
{
pl.Location = GetPosition()[y, x +];
PlState[y, x] = false;
PlState[y +, x] = false;
PlState[y, x +] = true;
PlState[y +, x + 1] = true;
PosX +=;
return true;
}
else return false;
}
}
#endregion
return false;
}
#endregion
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.panel = new System.Windows.Forms.Panel();
this.button = new System.Windows.Forms.Button();
this.SuspendLayout();
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._003;
this.panel.Location = new System.Drawing.Point(8, 43);
this.panel.Name = "panel1";
this.panel.Size = new System.Drawing.Size(100, 200);
this.panel.TabIndex = 0;
this.panel.Click += new System.EventHandler(this.panel1_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._001;
this.panel.Location = new System.Drawing.Point(108, 43);
this.panel.Name = "panel2";
this.panel.Size = new System.Drawing.Size(200, 200);
this.panel.TabIndex = 0;
this.panel.Click += new System.EventHandler(this.panel2_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._004;
this.panel.Location = new System.Drawing.Point(308, 43);
this.panel.Name = "panel3";
this.panel.Size = new System.Drawing.Size(100, 200);
this.panel.TabIndex = 1;
this.panel.Click += new System.EventHandler(this.panel3_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._005;
this.panel.Location = new System.Drawing.Point(8, 243);
this.panel.Name = "panel4";
this.panel.Size = new System.Drawing.Size(100, 200);
this.panel.TabIndex = 2;
this.panel.Click += new System.EventHandler(this.panel4_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._002;
this.panel.Location = new System.Drawing.Point(108, 243);
this.panel.Name = "panel5";
this.panel.Size = new System.Drawing.Size(200, 100);
this.panel.TabIndex = 3;
this.panel.Click += new System.EventHandler(this.panel5_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._006;
this.panel.Location = new System.Drawing.Point(308, 243);
this.panel.Name = "panel6";
this.panel.Size = new System.Drawing.Size(100, 200);
this.panel.TabIndex = 4;
this.panel.Click += new System.EventHandler(this.panel6_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._007;
this.panel.Location = new System.Drawing.Point(108, 343);
this.panel.Name = "panel7";
this.panel.Size = new System.Drawing.Size(100, 100);
this.panel.TabIndex = 5;
this.panel.Click += new System.EventHandler(this.panel7_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._008;
this.panel.Location = new System.Drawing.Point(208, 343);
this.panel.Name = "panel8";
this.panel.Size = new System.Drawing.Size(100, 100);
this.panel.TabIndex = 6;
this.panel.Click += new System.EventHandler(this.panel8_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._009;
this.panel.Location = new System.Drawing.Point(8, 443);
this.panel.Name = "panel9";
this.panel.Size = new System.Drawing.Size(100, 100);
this.panel.TabIndex = 7;
this.panel.Click += new System.EventHandler(this.panel9_Click);
this.panel.BackgroundImage = global::HuaRongDao.Properties.Resources._010;
this.panel.Location = new System.Drawing.Point(308, 443);
this.panel.Name = "panel10";
this.panel.Size = new System.Drawing.Size(100, 100);
this.panel.TabIndex = 8;
this.panel.Click += new System.EventHandler(this.panel10_Click);
this.button.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button.ForeColor = System.Drawing.Color.White;
this.button.Location = new System.Drawing.Point(11, 9);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(75, 25);
this.button.TabIndex = 0;
this.button.Text = "新游戏";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Teal;
this.ClientSize = new System.Drawing.Size(, 550);
this.Controls.Add(this.button);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.Controls.Add(this.panel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.KeyPreview = true;
this.MaximizeBox = false;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "华容道";
this.Load += new System.EventHandler(this.Form_Load);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form_KeyUp);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Button button;
}