C#利用控件拖拽技术制作拼图游戏
程序员文章站
2022-06-09 13:46:56
主要实现的功能:
1.程序附带多张拼图随机拼图。
2.可手动添加拼图。
3.游戏成功判断。
4.30秒超时判断。
puzzle.cs...
主要实现的功能:
1.程序附带多张拼图随机拼图。
2.可手动添加拼图。
3.游戏成功判断。
4.30秒超时判断。
puzzle.cs
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.io; namespace puzzle { public partial class puzzle : form { //图片列表 picturebox[] picturelist = null; //图片位置字典 sorteddictionary<string, bitmap> picturelocationdict = new sorteddictionary<string, bitmap>(); //location list point[] pointlist = null; //图片控件字典 sorteddictionary<string, picturebox> pictureboxlocationdict = new sorteddictionary<string, picturebox>(); //拼图时间 int second = 0; //所拖拽的图片 picturebox currentpicturebox = null; //*需要移动的图片 picturebox havetopicturebox = null; //原位置 point oldlocation = point.empty; //新位置 point newlocation = point.empty; //鼠标按下坐标(control控件的相对坐标) point mousedownpoint = point.empty; //显示拖动效果的矩形 rectangle rect = rectangle.empty; //是否正在拖拽 bool isdrag = false; public puzzle() { initializecomponent(); initgame(); } /// <summary> /// 初始化游戏资源 /// </summary> public void initgame() { picturelist = new picturebox[9] { picturebox1, picturebox2, picturebox3, picturebox4, picturebox5, picturebox6, picturebox7, picturebox8, picturebox9 }; pointlist = new point[9] { new point(0, 0), new point(100, 0), new point(200, 0), new point(0, 100), new point(100, 100), new point(200, 100), new point(0, 200), new point(100, 200), new point(200, 200) }; if (!directory.exists(application.startuppath.tostring() + "\\picture")) { directory.createdirectory(application.startuppath.tostring() + "\\picture"); properties.resources.默认.save(application.startuppath.tostring() + "\\picture\\1.jpg"); properties.resources._1.save(application.startuppath.tostring() + "\\picture\\2.jpg"); properties.resources._2.save(application.startuppath.tostring() + "\\picture\\3.jpg"); properties.resources._3.save(application.startuppath.tostring() + "\\picture\\4.jpg"); properties.resources._4.save(application.startuppath.tostring() + "\\picture\\5.jpg"); properties.resources.成功.save(application.startuppath.tostring() + "\\picture\\6.jpg"); properties.resources.欢呼.save(application.startuppath.tostring() + "\\picture\\7.jpg"); } random r = new random(); int i = r.next(7); flow(application.startuppath.tostring() + "\\picture\\"+i.tostring()+".jpg"); } private void puzzle_paint(object sender, painteventargs e) { if (rect != rectangle.empty) { if (isdrag) { e.graphics.drawrectangle(pens.white, rect); } else { e.graphics.drawrectangle(new pen(this.backcolor), rect); } } } /// <summary> /// 不好用 /// </summary> /// <returns></returns> public picturebox getpictureboxbylocation() { picturebox pic = null; if (this.activecontrol.name.contains("picturebox")) { pic = (picturebox)this.activecontrol; } return pic; } public picturebox getpictureboxbylocation(mouseeventargs e) { picturebox pic = null; foreach (picturebox item in picturelist) { if (e.location.x > item.location.x && e.location.y > item.location.y && item.location.x + 100 > e.location.x && item.location.y + 100 > e.location.x) { pic = item; } } return pic; } public picturebox getpictureboxbylocation(int x,int y) { picturebox pic = null; foreach (picturebox item in picturelist) { if (x> item.location.x && y > item.location.y && item.location.x + 100 > x && item.location.y + 100 > y) { pic = item; } } return pic; } /// <summary> /// 通过hashcode获取picture,用mouseeventargs之后获取相对于picture的坐标不是相对窗体 /// </summary> /// <param name="hascode"></param> /// <returns></returns> public picturebox getpictureboxbyhashcode(string hascode) { picturebox pic = null; foreach (picturebox item in picturelist) { if (hascode == item.gethashcode().tostring()) { pic = item; } } return pic; } private void picturebox_mousedown(object sender, mouseeventargs e) { oldlocation = new point(e.x, e.y); currentpicturebox = getpictureboxbyhashcode(sender.gethashcode().tostring()); mosedown(currentpicturebox, sender, e); } public void mosedown(picturebox pic, object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { oldlocation = e.location; rect = pic.bounds; } } private void picturebox_mousemove(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { isdrag = true; rect.location = getpointtoform(new point(e.location.x - oldlocation.x, e.location.y - oldlocation.y)); this.refresh(); } } private void reset() { mousedownpoint = point.empty; rect = rectangle.empty; isdrag = false; } private point getpointtoform(point p) { return this.pointtoclient(picturebox1.pointtoscreen(p)); } private void picturebox_mouseup(object sender, mouseeventargs e) { oldlocation = new point(currentpicturebox.location.x, currentpicturebox.location.y); if (oldlocation.x + e.x > 300 || oldlocation.y + e.y > 300||oldlocation.x + e.x < 0 || oldlocation.y + e.y < 0) { return; } havetopicturebox = getpictureboxbylocation(oldlocation.x + e.x, oldlocation.y + e.y); newlocation = new point(havetopicturebox.location.x, havetopicturebox.location.y); havetopicturebox.location = oldlocation; picturemouseup(currentpicturebox, sender, e); if ( judge()) { lab_result.text = "成功!"; //messagebox.show("恭喜拼图成功"); } } public void picturemouseup(picturebox pic, object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { if (isdrag) { isdrag = false; pic.location = newlocation; this.refresh(); } reset(); } } public void exchangepicturebox(mouseeventargs e) { } private void btn_sta_click(object sender, eventargs e) { messagebox.show(this.activecontrol.name); } /// <summary> /// 初始化 /// </summary> /// <param name="path"></param> public void flow(string path) { image bm = cutpicture.resize(path, 300, 300); cutpicture.bitmaplist = new list<bitmap>(); for (int y = 0; y < 300; y += 100) { for (int x = 0; x < 300; x += 100) { //string key = x + "-" + y; bitmap temp = cutpicture.cut(bm, x, y, 100, 100); //picturelocationdict.add(key, temp); cutpicture.bitmaplist.add(temp); } } importbitmap(); } /// <summary> /// 打乱数据 /// </summary> /// <param name="picturearray"></param> /// <returns></returns> public picturebox[] disorderarray(picturebox[] picturearray) { picturebox[] temparray = picturearray; for (int i = temparray.length - 1; i > 0; i--) { random rand = new random(); int p = rand.next(i); picturebox temp = temparray[p]; temparray[p] = temparray[i]; temparray[i] = temp; } return temparray; } /// <summary> /// 判断是否拼图成功 /// </summary> /// <returns></returns> public bool judge() { bool result = true; int i = 0; foreach (picturebox item in picturelist) { if (item.location != pointlist[i]) { result = false; } i++; } return result; } private void btn_import_click(object sender, eventargs e) { lab_result.text = ""; ofd_picture.showdialog(); cutpicture.picturepath = ofd_picture.filename; flow(cutpicture.picturepath); counttime(); } /// <summary> /// 计时 /// </summary> public void counttime() { lab_time.text = "0"; timer1.start(); } /// <summary> /// 给piturebox赋值 /// </summary> public void importbitmap() { try { int i = 0;// disorderarray(picturelist) foreach (picturebox item in picturelist) { bitmap temp = cutpicture.bitmaplist[i]; item.image = temp; i++; } resetpicturelocation(); } catch (exception exp) { console.writeline(exp.message); } } /// <summary> /// 打乱位置列表 /// </summary> /// <returns></returns> public point[] disorderlocation() { point[] temparray = (point[])pointlist.clone(); for (int i = temparray.length - 1; i > 0; i--) { random rand = new random(); int p = rand.next(i); point temp = temparray[p]; temparray[p] = temparray[i]; temparray[i] = temp; } return temparray; } /// <summary> /// 重新设置图片位置 /// </summary> public void resetpicturelocation() { point[] temp = disorderlocation(); int i = 0; foreach (picturebox item in picturelist) { item.location = temp[i]; i++; } } /// <summary> /// 计时,超过30秒停止计时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_tick(object sender, eventargs e) { second++; lab_time.text = second.tostring(); if (second == 30) { timer1.stop(); lab_result.text = "失败!"; } } private void btn_sta_click_1(object sender, eventargs e) { lab_result.text = ""; timer1.start(); } } }
cutpicture.cs
using system; using system.collections.generic; using system.linq; using system.text; using system.drawing; using system.drawing.imaging; using system.windows.forms; namespace puzzle { class cutpicture { public static string picturepath = ""; public static list<bitmap> bitmaplist = null; /// <summary> /// 剪切图片 /// </summary> /// <param name="b">图片</param> /// <param name="startx">x坐标</param> /// <param name="starty">y坐标</param> /// <param name="iwidth">宽</param> /// <param name="iheight">高</param> /// <returns></returns> public static bitmap cut(image b, int startx, int starty, int iwidth, int iheight) { if (b == null) { return null; } int w = b.width; int h = b.height; if (startx >= w || starty >= h) { return null; } if (startx + iwidth > w) { iwidth = w - startx; } if (starty + iheight > h) { iheight = h - starty; } try { bitmap bmpout = new bitmap(iwidth, iheight, pixelformat.format24bpprgb); graphics g = graphics.fromimage(bmpout); g.drawimage(b, new rectangle(0, 0, iwidth, iheight), new rectangle(startx, starty, iwidth, iheight), graphicsunit.pixel); g.dispose(); return bmpout; } catch { return null; } } /// <summary> /// 保存图片到根目录的pictures文件夹下 /// </summary> /// <param name="path">文件路径</param> /// <param name="iwidth">调整的宽</param> /// <param name="iheignt">调整的高</param> /// <returns></returns> public static image resize(string path, int iwidth, int iheignt) { image thumbnail = null; try { var img = image.fromfile(path); thumbnail = img.getthumbnailimage(iwidth, iheignt, null, intptr.zero); thumbnail.save(application.startuppath.tostring() + "\\picture\\img.jpeg"); } catch (exception exp) { console.writeline(exp.message); } return thumbnail; } } }
mouse_down
private void picturebox_mousedown(object sender, mouseeventargs e) { oldlocation = new point(e.x, e.y); currentpicturebox = getpictureboxbyhashcode(sender.gethashcode().tostring()); mosedown(currentpicturebox, sender, e); } public void mosedown(picturebox pic, object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { oldlocation = e.location; rect = pic.bounds; } }
mouse_move
private void picturebox_mousemove(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { isdrag = true; rect.location = getpointtoform(new point(e.location.x - oldlocation.x, e.location.y - oldlocation.y)); this.refresh(); } }
mouse_up
private void picturebox_mouseup(object sender, mouseeventargs e) { oldlocation = new point(currentpicturebox.location.x, currentpicturebox.location.y); if (oldlocation.x + e.x > 300 || oldlocation.y + e.y > 300||oldlocation.x + e.x < 0 || oldlocation.y + e.y < 0) { return; } havetopicturebox = getpictureboxbylocation(oldlocation.x + e.x, oldlocation.y + e.y); newlocation = new point(havetopicturebox.location.x, havetopicturebox.location.y); havetopicturebox.location = oldlocation; picturemouseup(currentpicturebox, sender, e); if ( judge()) { lab_result.text = "成功!"; //messagebox.show("恭喜拼图成功"); } } public void picturemouseup(picturebox pic, object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { if (isdrag) { isdrag = false; pic.location = newlocation; this.refresh(); } reset(); } }
reset
private void reset() { mousedownpoint = point.empty; rect = rectangle.empty; isdrag = false; }
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇: C#隐藏控制台键盘输入的方法