这篇文章主要介绍了C# 拼图魔方小游戏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
工作闲暇之余去逛了逛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();
}
}
具体效果如下:
代码有很多已知的可以优化的地方,后面有闲暇时间会处理,如果大家有更好的建议,不妨在下方评论区告知,在此感谢~
【点击下载源码】
到此这篇关于C# 拼图魔方小游戏的文章就介绍到这了,更多相关C# 拼图魔方内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
本文标题为:C# 拼图魔方小游戏
- 如何使用C# 捕获进程输出 2023-03-10
- Unity3D实现渐变颜色效果 2023-01-16
- Oracle中for循环的使用方法 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- c# 模拟线性回归的示例 2023-03-14
- .NET CORE DI 依赖注入 2023-09-27
- Unity Shader实现模糊效果 2023-04-27
- user32.dll 函数说明小结 2022-12-26