用C#写的6只青蛙过河
这是以前流传的一个excel内嵌flash的6只青蛙过河游戏,在我换了office2019后,彻底不能用了,网上搜遍了也找不到纯净的可收藏版本,找来找去只找到一个网页版的,凑合能玩,不过资源可以为我所用(如果侵权,请联系我删除),下面是我自己做的。
时间精力有限,没有搞动画效果和音效,答案是可以演示步骤的。
点我下载:
其中包含的“青蛙过河解法”非本人原创,只是精简了一下,忘了是哪位高人做的了,如果侵权请联系。
对于非编程专业只想要exe可执行文件的同学,压缩包里有,bin\Release目录下,请随便玩。
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; // 目标位置 } }
——看完了文章,无论觉得写的不错的还是觉的辣鸡的,可以点赞或给点意见,鼓励或批评对我都很有用!