C# 拼图魔方小游戏
作者:[Stephen-kzx] 时间:2023-10-27 20:41:22
工作闲暇之余去逛了逛CodeProject,刚好现有项目主要用到就是winform,浏览了下照片,找到上周带着蛋挞打疫苗回家的照片,于是新生一记,如何把这些图片玩起来~
80后应该都有印象,小时候有种玩具,叫做拼图魔方,90后00后的世界这种玩具应该早已灭绝了。一个塑料小板,上面分隔了很多小图框,通过移动这些小图框,最后拼接成完整的图片
话不多说开始吧~ 先上一张原图
代码也很简单,主要就是通过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();
}
}
具体效果如下:
代码有很多已知的可以优化的地方,后面有闲暇时间会处理,如果大家有更好的建议,不妨在下方评论区告知,在此感谢~
【点击下载源码】
来源:https://www.cnblogs.com/axing/p/12376325.html
标签:C#,拼图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析Java中线程的创建和启动
2022-12-29 17:37:41
![](https://img.aspxhome.com/file/2023/1/89871_0s.jpg)
java模拟hibernate一级缓存示例分享
2023-06-18 08:43:55
SpringCloud Edgware.SR3版本中Ribbon的timeout设置方法
2023-03-07 10:45:46
Java基础学习之方法的重载知识总结
2023-10-27 01:45:15
![](https://img.aspxhome.com/file/2023/2/96132_0s.png)
10分钟学会VS NuGet包私有化部署
2022-04-23 02:28:31
![](https://img.aspxhome.com/file/2023/7/116597_0s.png)
关于多线程常用方法以及对锁的控制(详解)
2022-02-02 08:16:42
Java list如何根据id获取子节点
2023-09-04 09:42:37
springboot+gradle 构建多模块项目的步骤
2023-02-19 00:33:31
![](https://img.aspxhome.com/file/2023/0/61050_0s.jpg)
Java Spring AOP源码解析之事务实现原理
2023-09-10 02:39:46
![](https://img.aspxhome.com/file/2023/5/66945_0s.png)
详解SpringBoot修改启动端口server.port的四种方式
2022-03-10 20:28:48
对Java中JSON解析器的一些见解
2023-02-05 20:53:15
从C#程序中调用非受管DLLs的方法
2023-03-23 08:43:41
java中Vector类的常用方法详解
2023-11-26 10:29:07
![](https://img.aspxhome.com/file/2023/4/60234_0s.png)
解读List list=new ArrayList()是怎么回事
2022-04-17 12:28:22
![](https://img.aspxhome.com/file/2023/5/61145_0s.png)
C#操作目录与文件的方法步骤
2023-11-23 20:45:52
java使用Filter实现自动登录的方法
2022-09-09 15:46:47
springboot配置https访问的方法
2022-12-11 16:17:37
ConcurrentMap.putIfAbsent(key,value)用法实例
2023-03-14 06:45:43
![](https://img.aspxhome.com/file/2023/9/72419_0s.png)
springboot配置mysql数据库spring.datasource.url报错的解决
2023-10-04 12:49:26
Java中Stream流中map和forEach的区别详解
2022-07-09 12:24:43
![](https://img.aspxhome.com/file/2023/8/108428_0s.jpg)