C# 小游戏-拼图魔方【Game Puzzle】
2021-03-18 02:23
标签:bre The 应该 ant 很多 建议 pixel current dep 序号功能实现主要是类MyButton集成父类Button实现: 随机分隔 点击小方格,通过改变当前点击的小方格X,Y坐标来更新小方格的位置 具体效果如下: 代码有很多已知的可以优化的地方,后面有闲暇时间会处理,如果大家有更好的建议,不妨在下方评论区告知,在此感谢~ 【点击下载源码】 【打个小广告】最近申请了个微信公众号,主要用于个人随笔记录,工作/生活中项目中用到的技术或娱乐的小游戏,俗话说得好好记性不如烂键盘!后续娱乐的小游戏源码也会分享到公众号中(部分会同步到博客园)。 又有俗话说了, 您的支持就是我写作的动力,公众号点个关注什么的也是举手之劳,还请您高抬贵手~ C# 小游戏-拼图魔方【Game Puzzle】 标签:bre The 应该 ant 很多 建议 pixel current dep 原文地址:https://www.cnblogs.com/axing/p/12376325.html
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 )
if (a[j] == b) exist = true;
if (!exist) a[i++] = b;
}
for (int j = 0; 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);
}
}
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();
}
}