原理详解请参考博客中

拼图游戏(下)原理篇 http://www.cnblogs.com/labixiaohei/p/6713761.html

功能描述:

  1.用户自定义上传图片

  2.游戏难度选择:简单(3*3)、一般(5*5)、困难(9*9)三个级别

  3.纪录完成步数

模块:

  1.拼图类

  2.配置类

  3.游戏菜单窗口

  4.游戏运行窗口

代码文件VS2013版本:

游戏:        百度网盘链接:http://pan.baidu.com/s/1boQFZqf

--------------------------------------------------我叫分割线---------------------------------------------------------------

1.拼图类

  方法:

  1.构造函数:传图片并分割成一个一个小图片

  2.交换方法

  3.大图中截取小单元方法

  4.移动单元的方法

  5.打乱单元顺序方法

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 拼图
{
public class Puzzle
{
public enum Diff //游戏难度
{
simple,//简单
ordinary,//普通
difficulty//困难
}
private struct Node //拼图单元格结构体
{
public Image Img;
public int Num;
}
private Image _img; //拼图图片
public int Width; //拼图边长
private Diff _gameDif; //游戏难度
private Node[,] node; //单元格数组
public int N; //单元格数组行列数 /// <summary>
/// 构造函数
/// </summary>
/// <param name="Img">拼图大图</param>
/// <param name="GameDif">游戏难度,该类下结构体Diff</param>
public Puzzle(Image Img,int Width, Diff GameDif)
{
this._gameDif = GameDif;
this._img = Img;
this.Width = Width;
switch(this._gameDif)
{
case Diff.simple:    //简单则单元格数组保存为3*3的二维数组
this.N = ;
node=new Node[,];
break;
case Diff.ordinary:   //一般则为5*5
this.N = ;
node = new Node[, ];
break;
case Diff.difficulty:  //困难则为9*9
this.N = ;
node = new Node[, ];
break;
} //分割图片形成各单元保存在数组中
int Count = ;
for (int x = ; x < this.N; x++)
{
for (int y = ; y < this.N; y++)
{ node[x, y].Img = CaptureImage(this._img, this.Width / this.N, this.Width / this.N, x * (this.Width / this.N), y * (this.Width / this.N));
node[x, y].Num = Count;
Count++;
}
} for (int x = ; x < this.N; x++)
{
for (int y = ; y < this.N; y++)
{ Graphics newGra = Graphics.FromImage(node[x, y].Img);
newGra.DrawLine(new Pen(Color.White), new Point(, ), new Point(, this.Width / this.N));
newGra.DrawLine(new Pen(Color.White), new Point(, ), new Point(this.Width / this.N, ));
newGra.DrawLine(new Pen(Color.White), new Point(this.Width / this.N, this.Width / this.N), new Point(this.Width / this.N, ));
newGra.DrawLine(new Pen(Color.White), new Point(this.Width / this.N, this.Width / this.N), new Point(,this.Width / this.N));
}
}
//(最后一项为空单独处理)
node[N - , N - ].Img = Image.FromFile("Image\\end.PNG");
Graphics newGra2 = Graphics.FromImage(node[N - , N - ].Img);
newGra2.DrawLine(new Pen(Color.Red), new Point(, ), new Point(, this.Width / this.N - ));
newGra2.DrawLine(new Pen(Color.Red), new Point(, ), new Point(this.Width / this.N - , ));
newGra2.DrawLine(new Pen(Color.Red), new Point(this.Width / this.N - , this.Width / this.N - ), new Point(this.Width / this.N - , ));
newGra2.DrawLine(new Pen(Color.Red), new Point(this.Width / this.N - , this.Width / this.N - ), new Point( ,this.Width / this.N - ));
//打乱拼图
this.Upset(); } /// <summary>
/// 由图片fromImage中截图并返回
/// </summary>
/// <param name="fromImage">原图片</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="spaceX">起始X坐标</param>
/// <param name="spaceY">起始Y坐标</param>
/// <returns></returns>
public Image CaptureImage(Image fromImage, int width, int height, int spaceX, int spaceY)
{
int x = ;
int y = ;
int sX = fromImage.Width - width;
int sY = fromImage.Height - height;
if (sX > )
{
x = sX > spaceX ? spaceX : sX;
}
else
{
width = fromImage.Width;
}
if (sY > )
{
y = sY > spaceY ? spaceY : sY;
}
else
{
height = fromImage.Height;
} //创建新图位图
Bitmap bitmap = new Bitmap(width, height);
//创建作图区域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原图相应区域写入作图区
graphic.DrawImage(fromImage, , , new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
//从作图区生成新图
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
return saveImage;
}
/// <summary>
/// 移动坐标(x,y)拼图单元
/// </summary>
/// <param name="x">拼图单元x坐标</param>
/// <param name="y">拼图单元y坐标</param>
public bool Move(int x,int y)
{
//MessageBox.Show(" " + node[2, 2].Num);
if (x + != N && node[x + , y].Num == N * N - )
{
Swap(new Point(x + , y), new Point(x, y));
return true;
}
if (y + != N && node[x, y + ].Num == N * N - )
{
Swap(new Point(x, y + ), new Point(x, y));
return true;
}
if (x - != - && node[x - , y].Num == N * N - )
{
Swap(new Point(x - , y), new Point(x, y));
return true;
}
if (y - != - && node[x, y - ].Num == N * N - )
{
Swap(new Point(x, y - ), new Point(x, y));
return true;
}
return false; }
//交换两个单元格
private void Swap(Point a, Point b)
{
Node temp = new Node();
temp = this.node[a.X, a.Y];
this.node[a.X, a.Y] = this.node[b.X, b.Y];
this.node[b.X, b.Y] = temp;
}
public bool Judge()
{
int count=;
for (int x = ; x < this.N; x++)
{
for (int y = ; y < this.N; y++)
{
if (this.node[x, y].Num != count)
return false;
count++;
}
}
return true;
}
public Image Display()
{
Bitmap bitmap = new Bitmap(this.Width, this.Width);
//创建作图区域
Graphics newGra = Graphics.FromImage(bitmap);
for (int x = ; x < this.N; x++)
for (int y = ; y < this.N; y++)
newGra.DrawImage(node[x, y].Img, new Point(x * this.Width / this.N, y * this.Width / this.N));
return bitmap;
}
/// <summary>
/// 打乱拼图
/// </summary>
public void Upset()
{
int sum = ;
if (this._gameDif == Diff.simple) sum = ;
//if (this._gameDif == Diff.ordinary) sum = 100000;
Random ran = new Random();
for (int i = , x = N - , y = N - ; i < sum; i++)
{
long tick = DateTime.Now.Ticks;
ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> )|ran.Next());
switch (ran.Next(, ))
{
case :
if (x + != N)
{
Move(x + , y);
x = x + ;
} break;
case :
if (y + != N)
{
Move(x, y + );
y = y + ;
}
break;
case :
if (x - != -)
{
Move(x - , y);
x = x - ;
}
break;
case :
if (y - != -)
{
Move(x, y - );
y = y - ;
}
break;
} }
} }
}

配置类:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 拼图
{
public static class GamePage
{
public static Puzzle.Diff Dif; //游戏难度
public static Image img; //拼图图案
}
}

游戏菜单:

 通过菜单,上传图片至配置类img,并选择难度上传至配置类Dif,然后拼图对象构造时读取配置类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 拼图
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
GamePage.img =Image.FromFile(@"Image\\拼图.jpg");
Control.CheckForIllegalCrossThreadCalls = false;
} private void button1_Click(object sender, EventArgs e)
{
GamePage.Dif = Puzzle.Diff.simple;
this.Hide();
Form1 ff = new Form1();
ff.closefather+=new 拼图.Form1.childclose(this.closethis);
ff.Show(); } private void button2_Click(object sender, EventArgs e)
{
GamePage.Dif = Puzzle.Diff.ordinary;
this.Hide();
Form1 ff = new Form1();
ff.closefather += new 拼图.Form1.childclose(this.closethis);
ff.Show(); } private void button3_Click(object sender, EventArgs e)
{
GamePage.Dif = Puzzle.Diff.difficulty;
this.Hide();
Form1 ff = new Form1();
ff.closefather += new 拼图.Form1.childclose(this.closethis);
ff.Show(); } public void closethis()
{
this.Show();
} private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
GamePage.img = Image.FromFile(ofd.FileName).GetThumbnailImage(,,new Image.GetThumbnailImageAbort(delegate { return false; }), IntPtr.Zero); }
private void Menu_Load(object sender, EventArgs e)
{
} }
}

游戏运行窗口:

1.注册鼠标点击事件来获得输入消息

2.显示功能

3.pictureBox1用来展示游戏拼图情况

4.pictureBox2展示完整拼图的缩略图(当鼠标移至pictureBox2时,发送消息,使pictureBox1显示完整拼图)

5.Numlabel来显示移动步数(通过变量Num的累加来计算)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 拼图
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Puzzle puzzle;
private int Num=;
private Image img;
private void Form1_Load(object sender, EventArgs e)
{
img = GamePage.img;
pictureBox2.Image = img.GetThumbnailImage(,, new Image.GetThumbnailImageAbort(delegate { return false; }), IntPtr.Zero);
puzzle = new Puzzle(img, , GamePage.Dif);
pictureBox1.Image =puzzle.Display();
} private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (puzzle.Move(e.X / (puzzle.Width / puzzle.N), e.Y / (puzzle.Width / puzzle.N)))
{
Num++;
pictureBox1.Image = puzzle.Display();
if (puzzle.Judge())
{
if (MessageBox.Show("恭喜过关", "是否重新玩一把", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Num = ;
puzzle.Upset();
pictureBox1.Image = puzzle.Display(); }
else
{
Num = ;
closefather();
this.Close();
} } }
NumLabel.Text = Num.ToString();
} private void pictureBox2_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Image = img;
} private void pictureBox2_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = puzzle.Display();
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
closefather();
}
public delegate void childclose();
public event childclose closefather; }
}

程序设计 之 C#实现《拼图游戏》 (上)代码篇的更多相关文章

  1. 练手小游戏(代码篇之敌人AI

    诶呀~又没有更新微博,去拔牙了,疼死了,休息了几天过来接着写代码~ 首先是Base.写了一个框架,照别人扒的. Base分三部分,AILocomotion(AI移动),Steering(行为基类),V ...

  2. JavaScript写一个拼图游戏

    拼图游戏的代码400行, 有点多了, 在线DEMO的地址是:打开: 因为使用canvas,所以某些浏览器是不支持的: you know: 为什么要用canvas(⊙o⊙)?  因为图片是一整张jpg或 ...

  3. 微信小程序开发的游戏《拼图游戏》

    微信小程序开发的游戏<拼图游戏> 代码直接考进去就能用 pintu.js // pintu.js Page({ /** * 页面的初始数据 */ data: { }, initGame: ...

  4. 程序设计 之 C#实现《拼图游戏》 (下) 原理篇

    前言:在 http://www.cnblogs.com/labixiaohei/p/6698887.html 程序设计 之 C#实现<拼图游戏>(上),上传了各模块代码,而在本文中将详细剖 ...

  5. 程序设计 之 C#实现《拼图游戏》

    功能描述: 1.用户自定义上传图片 2.游戏难度选择:简单(3*3).一般(5*5).困难(9*9)三个级别 3.纪录完成步数 模块: 1.拼图类 2.配置类 3.游戏菜单窗口 4.游戏运行窗口 -- ...

  6. C#实现拼图游戏

    C#实现<拼图游戏> (下) 原理篇   前言:在 http://www.cnblogs.com/labixiaohei/p/6698887.html 程序设计 之 C#实现<拼图游 ...

  7. 利用Vue.js实现拼图游戏

    之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/luozhihao/p/5 ...

  8. JavaScript拼图游戏

    今天是2016年最后一天上班了.最近几天都比较休闲,有时间空闲下来写写文档之类的. 2016过得真是快.感觉没做什么就过去了.想到之前想坚持每个月写一写博客都没坚持到.希望2017年可以吧. 无聊之余 ...

  9. atitit.html5 拼图游戏的解决之道.

    atitit.html5 拼图游戏的解决之道. 1. 拼图游戏的操作(点击法and 拖动法) 1 1. 支持键盘上.下.左.右键移动: 1 2. 支持点击空白模块中的上下左右箭头移动: 1 3. 支持 ...

随机推荐

  1. 打开一个网站中的不同页面时,相同的js文件会被重复加载吗?

    作者:JasonYang链接:https://www.zhihu.com/question/41184156/answer/135195798来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  2. Python之Flask框架使用

    Flask和Django.Bottle号称Python中的强大又简单的Web框架. Flask是一个使用Python编写的轻量级Web应用框架.基于Werkzeug WSGI工具箱和Jinja2 模板 ...

  3. Angular动态表单生成(五)

    动态表单生成之布局 到上面的篇章为止,我们已经把表单比较完整的生成出来了,也实现了一些验证功能,可以说,我们截止这里,就已经可以满足我们的大部分表单生成需求了~ 但是: 目前来说,我们对于表单的布局只 ...

  4. sublime 一些常用功能和快捷键

    Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)Ctrl+G 跳转到相应的行Ctrl+J 合并行(已选择需要合并的多行时)Ctrl+L 选择整行(按住-继续选择下 ...

  5. Sql server 查看锁和Kill 死锁进程

    死锁的概念 死锁就是两个或多个会话(SPID)相互请求对方持有的锁资源,导致循环等待的情况.下面两种方法都是用来粗暴的解决死锁的. # 已知阻塞进程ID KILL ID SELECT blocking ...

  6. Cloudera Manager Server CDH 5.15部署

    安装前准备 主机和系统 Host OS Memory IP bigdata001-dev Cent OS 7.4 x64 32G 192.168.1.1 bigdata002-dev Cent OS ...

  7. 一个简单python爬虫的实现——爬取电影信息

    最近在学习网络爬虫,完成了一个比较简单的python网络爬虫.首先为什么要用爬虫爬取信息呢,当然是因为要比人去收集更高效. 网络爬虫,可以理解为自动帮你在网络上收集数据的机器人. 网络爬虫简单可以大致 ...

  8. 关于IRAM和IFLASH启动模式,重映射remap 整理中

    工程基于NXP LPC2468 1 为什么试用IRAM MODE 2 设置Program algorithm 编程算法的作用是什么 3 IRAM和FLASH 模式下IROM和IRAM的地址为什么不一样 ...

  9. exLucas学习笔记

    exLucas学习笔记 Tags:数学 写下抛硬币和超能粒子炮改 洛谷模板代码如下 #include<iostream> #define ll long long using namesp ...

  10. mfc 类静态成员

    知识点 类静态数据成员 类静态成员函数 一.类静态数据成员 静态成员的提出是为了解决数据共享的问题.实现共享有许多方法,如:设置全局性的变量或对象是一种方法.但是,全局变量或对象是有局限性的.这一课里 ...