用C#写的6只青蛙过河
作者:互联网
以前流传的一个excel内嵌flash的6只青蛙过河游戏,在我换了office2019后,彻底不能用了,网上搜遍了也找不到纯净的可收藏版本,找来找去只找到一个网页版的,凑合能玩,不过资源可以为我所用(如果侵权,请联系我删除),试试自己编一个吧
时间精力有限,没有搞动画效果和音效,答案是可以演示步骤的,本想上传全部文件的,不知怎么上传不了,可能是没权限。
using System; using System.Drawing; using System.Threading.Tasks; using System.Windows.Forms; namespace 青蛙过河 { public partial class Form1 : Form { PictureBox[] picStones; PictureBox[] picFrogs; Frog[] frogs; readonly Size StoneSize = new Size(70, 40); readonly Size FrogSize = new Size(70, 68); const int Interval = 10; Step[] steps; public Form1() { InitializeComponent(); picStones = new PictureBox[7]; picFrogs = new PictureBox[7]; for (int i = 0; i < 7; i++) { picStones[i] = new PictureBox(); picStones[i].Image = Properties.Resources.qingwa_dock; picStones[i].SizeMode = PictureBoxSizeMode.CenterImage; picStones[i].Size = StoneSize; picStones[i].Left = (StoneSize.Width + Interval) * i; picStones[i].Top = FrogSize.Height; picStones[i].BackColor = Color.Transparent; picStones[i].Parent = panel1; picFrogs[i] = new PictureBox(); Bitmap bitmap = new Bitmap(70, 70); Graphics g = Graphics.FromImage(bitmap); g.DrawImage(Properties.Resources.qingwa, new Rectangle(0, 0, 70, 70), new Rectangle(0, (i < 3 ? 0 : 70), 70, 70), GraphicsUnit.Pixel); if (i != 3) picFrogs[i].Image = bitmap; //if (i == 3) picFrogs[i].Visible = false; picFrogs[i].SizeMode = PictureBoxSizeMode.Normal; picFrogs[i].Size = FrogSize; picFrogs[i].Left = (FrogSize.Width + Interval) * i; picFrogs[i].Top = 2; picFrogs[i].BackColor = Color.Transparent; picFrogs[i].Parent = panel1; picFrogs[i].BringToFront(); picFrogs[i].Tag = i; picFrogs[i].Click += new EventHandler(Frog_Click); } frogs = new Frog[7]; frogs[0] = new Frog(0, Direction.向右, 0, false); frogs[1] = new Frog(1, Direction.向右, 1, true); frogs[2] = new Frog(2, Direction.向右, 2, true); frogs[3] = new Frog(3, Direction.中立, 3, false); // 空位 frogs[4] = new Frog(4, Direction.向左, 4, true); frogs[5] = new Frog(5, Direction.向左, 5, true); frogs[6] = new Frog(6, Direction.向左, 6, false); steps = new Step[] { new Step(){frogID=2, destPosition=3}, new Step(){frogID=4, destPosition=2}, new Step(){frogID=5, destPosition=4}, new Step(){frogID=2, destPosition=5}, new Step(){frogID=1, destPosition=3}, new Step(){frogID=0, destPosition=1}, new Step(){frogID=4, destPosition=0}, new Step(){frogID=5, destPosition=2}, new Step(){frogID=6, destPosition=4}, new Step(){frogID=2, destPosition=6}, new Step(){frogID=1, destPosition=5}, new Step(){frogID=0, destPosition=3}, new Step(){frogID=5, destPosition=1}, new Step(){frogID=6, destPosition=2}, new Step(){frogID=0, destPosition=4} }; } private void Frog_Click(object sender, EventArgs e) { PictureBox picFrog = sender as PictureBox; Frog frog = frogs[(int)picFrog.Tag]; if (!frog.canJump) return; int newEmptyPositon = frog.position; frog.position = frogs[3].position; frogs[3].position = newEmptyPositon; // 空位 picFrog.Left = (FrogSize.Width + Interval) * frog.position; picFrogs[3].Left = (FrogSize.Width + Interval) * frogs[3].position; for (int i = 0; i < 7; i++) { frogs[i].canJump = false; if (frogs[i].direction == Direction.向右 && frogs[i].position < newEmptyPositon && frogs[i].position > newEmptyPositon - 3) frogs[i].canJump = true; if (frogs[i].direction == Direction.向左 && frogs[i].position > newEmptyPositon && frogs[i].position < newEmptyPositon + 3) frogs[i].canJump = true; Console.WriteLine("Frog_Click: {0} {1} {2}", frogs[i].frogID, frogs[i].position, frogs[i].canJump); } if (IsWin()) { Console.WriteLine("You Win!"); Graphics g = this.CreateGraphics(); g.DrawString("Great", new Font("微软雅黑", 40), Brushes.Red, 220, 180); } } private bool IsWin() { for (int i = 0; i < 3; i++) if (frogs[i].position <= 3) return false; for (int i = 4; i < 7; i++) if (frogs[i].position >= 3) return false; return true; } private void Reset_Click(object sender, EventArgs e) { for (int i = 0; i < 7; i++) { frogs[i].position = i; picFrogs[i].Left = (FrogSize.Width + Interval) * i; if (i == 1 || i == 2 || i == 4 || i == 5) frogs[i].canJump = true; } this.Refresh(); } private void Label_MouseEnter(object sender, EventArgs e) { Label label = sender as Label; label.BackColor = Color.Yellow; label.ForeColor = Color.Blue; } private void Label_MouseLeave(object sender, EventArgs e) { Label label = sender as Label; label.BackColor = Color.LightGray; label.ForeColor = Color.Black; } private async void lblResult_Click(object sender, EventArgs e) { lblResult.Enabled = false; lblReset.Enabled = false; Graphics g = this.CreateGraphics(); this.Refresh(); g.DrawString("演示开始", new Font("微软雅黑", 40), Brushes.Red, 190, 180); //picFrogs[3].Visible = false; // 防止被移动到下层被覆盖 for (int i = 0; i < 7; i++) // 确保复位 { frogs[i].canJump = false; frogs[i].position = i; picFrogs[i].Left = (FrogSize.Width + Interval) * i; } await Task.Delay(1000); for (int i = 0; i < steps.Length; i++) { frogs[steps[i].frogID].position = steps[i].destPosition; picFrogs[steps[i].frogID].BorderStyle = BorderStyle.FixedSingle; await Task.Delay(1000); picFrogs[steps[i].frogID].Left = (FrogSize.Width + Interval) * steps[i].destPosition; picFrogs[steps[i].frogID].BringToFront(); // 防止被移动到下层被覆盖 await Task.Delay(1000); picFrogs[steps[i].frogID].BorderStyle = BorderStyle.None; } this.Refresh(); g.DrawString("演示完毕", new Font("微软雅黑", 40), Brushes.Red, 190, 180); lblResult.Enabled = true; lblReset.Enabled = true; } } enum Direction { 中立, 向左, 向右 }; class Frog // 青蛙类 { public int frogID; // 青蛙编号 public Direction direction; // 青蛙跳动的方向 public int position; // 青蛙位置 public bool canJump; // 是否可以跳 public Frog(int frogID, Direction direction, int positon, bool canJump) { this.frogID = frogID; this.direction = direction; this.position = positon; this.canJump = canJump; } } struct Step { public int frogID; // 青蛙编号 public int destPosition; // 目标位置 } }
namespace 青蛙过河 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.lblHelp = new System.Windows.Forms.Label(); this.lblReset = new System.Windows.Forms.Label(); this.lblResult = new System.Windows.Forms.Label(); this.SuspendLayout(); // // panel1 // this.panel1.BackColor = System.Drawing.Color.Transparent; this.panel1.Location = new System.Drawing.Point(25, 314); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(550, 110); this.panel1.TabIndex = 0; // // lblHelp // this.lblHelp.AutoSize = true; this.lblHelp.BackColor = System.Drawing.Color.Transparent; this.lblHelp.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblHelp.Location = new System.Drawing.Point(29, 25); this.lblHelp.Name = "lblHelp"; this.lblHelp.Size = new System.Drawing.Size(217, 70); this.lblHelp.TabIndex = 1; this.lblHelp.Text = "游戏说明\r\n 1:用鼠标点青蛙,它会向前跳;\r\n 2:青蛙只能按原方向向前跳;\r\n 3:它最多只能跳过一个青蛙;\r\n 4:此题肯定有解,不要怀疑。"; // // lblReset // this.lblReset.BackColor = System.Drawing.Color.LightGray; this.lblReset.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lblReset.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblReset.Location = new System.Drawing.Point(438, 465); this.lblReset.Name = "lblReset"; this.lblReset.Size = new System.Drawing.Size(74, 26); this.lblReset.TabIndex = 2; this.lblReset.Text = "复位"; this.lblReset.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.lblReset.Click += new System.EventHandler(this.Reset_Click); this.lblReset.MouseEnter += new System.EventHandler(this.Label_MouseEnter); this.lblReset.MouseLeave += new System.EventHandler(this.Label_MouseLeave); // // lblResult // this.lblResult.BackColor = System.Drawing.Color.LightGray; this.lblResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lblResult.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblResult.Location = new System.Drawing.Point(88, 465); this.lblResult.Name = "lblResult"; this.lblResult.Size = new System.Drawing.Size(74, 26); this.lblResult.TabIndex = 3; this.lblResult.Text = "答案"; this.lblResult.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.lblResult.Click += new System.EventHandler(this.lblResult_Click); this.lblResult.MouseEnter += new System.EventHandler(this.Label_MouseEnter); this.lblResult.MouseLeave += new System.EventHandler(this.Label_MouseLeave); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = global::青蛙过河.Properties.Resources.bg; this.ClientSize = new System.Drawing.Size(600, 500); this.Controls.Add(this.lblResult); this.Controls.Add(this.lblReset); this.Controls.Add(this.lblHelp); this.Controls.Add(this.panel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "Form1"; this.Text = "青蛙过河"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Label lblHelp; private System.Windows.Forms.Label lblReset; private System.Windows.Forms.Label lblResult; } }
标签:过河,frogID,C#,青蛙,System,frogs,new,picFrogs,Drawing 来源: https://www.cnblogs.com/chengyb/p/13844257.html