C#如何实现拼图魔方小游戏-创新互联
小编给大家分享一下C#如何实现拼图魔方小游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
东台ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!先上一张原图
代码也很简单,主要就是通过BitMap分隔现有(后面有时间可以优化下,让玩家自动上传图片,应该会更有意思)图片,然后Random随机打乱分割后图片的顺序,通过点击小方格来完成图片的拼图,为了更方便玩家,每个小方格添加了序号,玩家也可以不参考原图,按照小方格上的序号进行拼图
序号功能实现主要是类MyButton集成父类Button实现:
public class MyButton : Button { private int number; public int Number { get { return this.number; } set { this.Text = value.ToString(); this.number = value; } } public MyButton() { } }
随机分隔
Random r = new Random(); int[] a = new int[24]; int i = 0; int b; bool exist; while (i != a.Length) { exist = false; b = (r.Next(24) + 1); for (int j = 0; j < a.Length; j++) if (a[j] == b) exist = true; if (!exist) a[i++] = b; } for (int j = 0; j < a.Length; j++) ButtonArray[j].Number = a[j]; // set picture pieces as the background image int Number; int Row, Column; for (int k = 0; k < 5; k++) { for (int j = 0; j < 5; j++) { if (k == 4) if (j == 4) break; Number = ButtonArray[k * 5 + j].Number; //Get The Number Of Button Row = (Number - 1) / 5; Column = (Number - 1) - (Row * 5); ButtonArray[k * 5 + j].Image = CurrentBitmapImage.Clone(new Rectangle(new Point(Column * 75, Row * 75), new Size(75, 75)), System.Drawing.Imaging.PixelFormat.DontCare); } }
点击小方格,通过改变当前点击的小方格X,Y坐标来更新小方格的位置
private void myButton_LocationChanged(object sender, EventArgs e) { MyButton A = sender as MyButton; YouWin = true; int ButtonNumber; this.NumberOfMoves++; if (ButtonArray == null) { this.FrmMain_Load(sender, e); } for (int i = 0; i < 5; i++) { if (YouWin == false) break; else for (int j = 0; j < 5; j++) { ButtonNumber = i * 5 + j; if (i == 4 && j == 4) break; else if (GetNumber(ButtonArray[ButtonNumber].Location.X, ButtonArray[ButtonNumber].Location.Y) == ButtonArray[ButtonNumber].Number) continue; else { YouWin = false; break; } } } if (YouWin) { if (MessageBox.Show("You Win This Game in " + this.NumberOfMoves.ToString() + " Moves\n\rDo You Want To Play Another Game ?", "Congratulation", MessageBoxButtons.YesNo) == DialogResult.Yes) this.LoadNewGame(); else this.Close(); } }
private void myButton_LocationChanged(object sender, EventArgs e) { MyButton A = sender as MyButton; YouWin = true; int ButtonNumber; this.NumberOfMoves++; if (ButtonArray == null) { this.FrmMain_Load(sender, e); } for (int i = 0; i < 5; i++) { if (YouWin == false) break; else for (int j = 0; j < 5; j++) { ButtonNumber = i * 5 + j; if (i == 4 && j == 4) break; else if (GetNumber(ButtonArray[ButtonNumber].Location.X, ButtonArray[ButtonNumber].Location.Y) == ButtonArray[ButtonNumber].Number) continue; else { YouWin = false; break; } } } if (YouWin) { if (MessageBox.Show("You Win This Game in " + this.NumberOfMoves.ToString() + " Moves\n\rDo You Want To Play Another Game ?", "Congratulation", MessageBoxButtons.YesNo) == DialogResult.Yes) this.LoadNewGame(); else this.Close(); } }
具体效果如下:
以上是“C#如何实现拼图魔方小游戏”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都网站设计公司行业资讯频道!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:C#如何实现拼图魔方小游戏-创新互联
分享链接:http://myzitong.com/article/dohdij.html