代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace PinTu2
{
public partial class MainForm : Form
{
int a;
public MainForm()
{
InitializeComponent();
}
Label[,] arrLbl = new Label[3, 3];
int unRow = 0, unCol = 0;
bool playing = false;
private void MainForm_Load(object sender, EventArgs e)
{
this.SuspendLayout();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Label lbl = new Label();
lbl.Text = "";
lbl.AutoSize = false;
lbl.Size = new Size(80, 80);
lbl.Location = new Point(j * 80, i * 80);
lbl.ImageList = imageList1;
lbl.Click += new System.EventHandler(this.lblPic_Click);
panel1.Controls.Add(lbl);
arrLbl[i, j] = lbl;
}
}
this.ResumeLayout();
} private void btnPlay_Click(object sender, EventArgs e)
{
a = 0;
arrLbl[unRow, unCol].Visible = true;
int[] arrNum ={ 0, 1, 2, 3, 4, 5, 6, 7, 8 };
Random rm = new Random();
for (int i = 0; i < 8; i++)
{
int rmNum = rm.Next(i, 9);
int temp = arrNum[i];
arrNum[i] = arrNum[rmNum];
arrNum[rmNum] = temp; }
for (int i = 0; i < 9; i++)
{
arrLbl[i / 3, i % 3].ImageIndex = arrNum[i];
arrLbl[i / 3, i % 3].BorderStyle = BorderStyle.FixedSingle;
}
int cover = rm.Next(0, 9);
unRow = cover / 3;
unCol = cover % 3;
arrLbl[unRow, unCol].Visible = false;
playing = true;
}
private void lblPic_Click(object sender, EventArgs e)
{
a++;
if (!playing)
{
return;
}
int row = ((Label)sender).Top / 80;
int col = ((Label)sender).Left / 80;
if (Math.Abs(row - unRow) + Math.Abs(col - unCol) == 1)
{
int temp = arrLbl[unRow, unCol].ImageIndex;
arrLbl[unRow, unCol].ImageIndex = arrLbl[row, col].ImageIndex;
arrLbl[row, col].ImageIndex = temp;
arrLbl[unRow, unCol].Visible = true;
arrLbl[row, col].Visible = false;
unRow = row;
unCol = col;
}
for (int i = 0; i < 9; i++)
{
if (arrLbl[i / 3, i % 3].ImageIndex != i)
{
break;
}
if (i == 8)
{
arrLbl[unRow, unCol].Visible = true;
foreach (Label lbl in arrLbl)
{
lbl.BorderStyle = BorderStyle.None;
}
playing = false;
MessageBox.Show("恭喜你通过了游戏!", "烧煤小姐",
MessageBoxButtons.OK, MessageBoxIcon.Information); }
}
label2.Text = a.ToString();
} }
}
//我想在完成一关之后再重新的到一张图片的拼图~也就是的到新的一关~不知道怎么做~希望大家指点!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace PinTu2
{
public partial class MainForm : Form
{
int a;
public MainForm()
{
InitializeComponent();
}
Label[,] arrLbl = new Label[3, 3];
int unRow = 0, unCol = 0;
bool playing = false;
private void MainForm_Load(object sender, EventArgs e)
{
this.SuspendLayout();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Label lbl = new Label();
lbl.Text = "";
lbl.AutoSize = false;
lbl.Size = new Size(80, 80);
lbl.Location = new Point(j * 80, i * 80);
lbl.ImageList = imageList1;
lbl.Click += new System.EventHandler(this.lblPic_Click);
panel1.Controls.Add(lbl);
arrLbl[i, j] = lbl;
}
}
this.ResumeLayout();
} private void btnPlay_Click(object sender, EventArgs e)
{
a = 0;
arrLbl[unRow, unCol].Visible = true;
int[] arrNum ={ 0, 1, 2, 3, 4, 5, 6, 7, 8 };
Random rm = new Random();
for (int i = 0; i < 8; i++)
{
int rmNum = rm.Next(i, 9);
int temp = arrNum[i];
arrNum[i] = arrNum[rmNum];
arrNum[rmNum] = temp; }
for (int i = 0; i < 9; i++)
{
arrLbl[i / 3, i % 3].ImageIndex = arrNum[i];
arrLbl[i / 3, i % 3].BorderStyle = BorderStyle.FixedSingle;
}
int cover = rm.Next(0, 9);
unRow = cover / 3;
unCol = cover % 3;
arrLbl[unRow, unCol].Visible = false;
playing = true;
}
private void lblPic_Click(object sender, EventArgs e)
{
a++;
if (!playing)
{
return;
}
int row = ((Label)sender).Top / 80;
int col = ((Label)sender).Left / 80;
if (Math.Abs(row - unRow) + Math.Abs(col - unCol) == 1)
{
int temp = arrLbl[unRow, unCol].ImageIndex;
arrLbl[unRow, unCol].ImageIndex = arrLbl[row, col].ImageIndex;
arrLbl[row, col].ImageIndex = temp;
arrLbl[unRow, unCol].Visible = true;
arrLbl[row, col].Visible = false;
unRow = row;
unCol = col;
}
for (int i = 0; i < 9; i++)
{
if (arrLbl[i / 3, i % 3].ImageIndex != i)
{
break;
}
if (i == 8)
{
arrLbl[unRow, unCol].Visible = true;
foreach (Label lbl in arrLbl)
{
lbl.BorderStyle = BorderStyle.None;
}
playing = false;
MessageBox.Show("恭喜你通过了游戏!", "烧煤小姐",
MessageBoxButtons.OK, MessageBoxIcon.Information); }
}
label2.Text = a.ToString();
} }
}
//我想在完成一关之后再重新的到一张图片的拼图~也就是的到新的一关~不知道怎么做~希望大家指点!!
要是不想点按钮生成的话,可以调用Button.PerformClick()方法