1. 加载图片
(1)加载图片,生成图片格
int[] changeway; //记录图片自动打乱的轨迹
int[] check; //检查图片是否排序成功的匹配数组
PictureBox[] picturebox; //图片格
//动态生成图片格
public void loadpicture()
{
picturebox = new PictureBox[modeselect];
for (int i = 0; i < modeselect; i++)
{
picturebox[i] = new PictureBox();
picturebox[i].SizeMode = PictureBoxSizeMode.StretchImage;
}
}
//加载图片
private void 打开图片0ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "jpg图片|*.jpg|bmp图片|*.bmp"; //设定图片格式
DialogResult requset = openFileDialog1.ShowDialog();
if (requset == DialogResult.OK)
{
picture = openFileDialog1.FileName.ToString();
mainpicutre.Image = Image.FromFile(picture);
…//省略
}
}
//确定每个图片格的位置,和显示模式,初始图片格内的图片
public void initial(int left ,int top ,int mode)
{
int changetime = 0; //帮助确定切割原始图片的切割点
int width = (int.Parse( Math.Sqrt(mode).ToString())) % 3 * 85; //根据给定的mode 确定图片格宽度是基本宽度(85)的多少倍,如果是16格的,则图片的宽度是基本宽度的1倍,如果是4就为2倍 ,这里巧妙的用整除3解决了倍数问题
int modemiddle =int.Parse ( Math.Sqrt(mode).ToString()); //用以确定原始图片水平或竖直需要分成几个块
int picturewidth = mainpicutre.Image .Width / modemiddle;//设置图片的宽度基数
int pictureheight=mainpicutre.Image .Height /modemiddle;//设置图片的高度基数
Image b=(Image ) Image .FromFile (picture );
for (int i = 0; i < mode; i++) //初始化图片格
{
Point point = new Point(left + width * (changetime % modemiddle), top + width * (changetime / modemiddle));
picturebox[i].Name = i.ToString();
picturebox[i].Width = width; //确定生成的图片格的宽度和高度
picturebox[i].Height = width;
picturebox[i].Location = point;
picturebox[i].BorderStyle = BorderStyle.FixedSingle;;//设定图片框的模式
picturebox[i].Click += new EventHandler(this.pictureclick); // 装箱 技术 将实体类型转换为参考 ,将点击图片与某个函数相关联
Bitmap bmp = new Bitmap(width ,width );
Graphics f = Graphics.FromImage(bmp);
picturebox[i].BackColor = Color.Red;
f.DrawImage(b, new Rectangle(0, 0, width , width ),new Rectangle ( picturewidth * (changetime % modemiddle), pictureheight * (changetime / modemiddle), picturewidth , pictureheight), GraphicsUnit.Pixel);//切割原始图片 , f填充新图片的区域是Rectangle(0, 0, width , width ) ,填充的数据来之图片源 b,其区域为 Rectangle ( picturewidth * (changetime % modemiddle), pictureheight * (changetime / modemiddle), picturewidth , pictureheight)
picturebox[i].Image = bmp; // 将每个图片格内填入图片
changetime++;
this.Controls.Add(picturebox[i]);
}
picturebox[0].Image = null; // 拼图 起始点
changeway = new int[modeselect];
check = new int[modeselect];
for (int i = 1; i < modeselect; i++) // 初始化两个数组,一个用于核对图片是否拼图成功,一个用来记录交换轨迹
{
changeway[i] = i;
check[i] = i;
}
changeway[0] = -1;
check[0] = -1;
}
试验中的效果如图8。
图8 初始化拼图游戏
(2)生成随机路径
//生成打乱轨迹核心代码
public void changethepictureorder() //根据给定的困难程度grade,产生图片打乱的轨迹
{
int time = DateTime.Now.Second;
Random random = new Random(time );
int checkway= 0; //标记上次去定交换图片的图片格
int last = 0;
contrail = new int[grade ]; //记录生成的打乱轨迹
contrail[0] = 0;
int biaoji=0;
int[] selectin = new int[4];
for (int i = 0; i < 4; i++) //初始化数组,并将每次所有可以交换图片的位置,放在数组中
{
selectin[i] = 0;
}
do
{
int n = 0;
int down = checkway + int.Parse(Math.Sqrt(modeselect ).ToString()); //可以和下方的交换
if (down < modeselect & down != last)
{
selectin[n] = down;
n = n + 1;
}
int up = checkway - int.Parse(Math.Sqrt(modeselect).ToString());
if (up > -1 & up != last) //可以和上方的交换 {
selectin[n] = up;
n = n + 1;
}
int right = checkway + 1;
if ((right % int.Parse(Math.Sqrt(modeselect).ToString())) != 0 & right != last) //可以和右方的交换
{
selectin[n] = right;
n = n + 1;
}
int left = checkway - 1;
if ((left > -1) & (checkway % int.Parse(Math.Sqrt(modeselect).ToString()) != 0) & left != last) //可以和左方的交换
{
selectin[n] = left;
n = n + 1;
}
int getchangenumber = selectin[random.Next(0, n)]; //随机选择本次可以进行交换的图片格
checkway = getchangenumber; //记录本次的交换图片格,下次从该图片格开始寻找下个图片格
biaoji++;
contrail[biaoji] = getchangenumber;
copychange[biaoji] = getchangenumber; //备份图片交换轨迹,为了自动排序
last = contrail[biaoji - 1]; //标记上次的交换的图片格 防止无用的图片交换(如图1到图2 再从 图2 到图1)
} while (biaoji != grade-1 );
// 根据生成的打乱轨迹,对图片进行初始打乱
for (int i = 0; i < grade-1; i++)
{
picturebox[contrail[i]].Image = picturebox[contrail[i + 1]].Image;
picturebox[contrail[i + 1]].Image = null;
changeway[contrail[i]] = changeway[contrail[i + 1]];
changeway[contrail[i + 1]] = -1;
}
…. // 省略
}
|