本文设想了C#拼图游戏递次,供人人参考,具体内容以下
功用形貌:
1.用户自定义上传图片
2.游戏难度挑选:简朴(3*3)、平常(5*5)、难题(9*9)三个级别
3.记载完成步数
模块:
1.拼图类
2.设置类
3.游戏菜单窗口
4.游戏运转窗口
代码文件VS2013版本:
下载链接: 拼图游戏
--------------------------------------------------我叫支解线---------------------------------------------------------------
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 = 3; node=new Node[3,3]; break; case Diff.ordinary: //平常则为5*5 this.N = 5; node = new Node[5, 5]; break; case Diff.difficulty: //难题则为9*9 this.N = 9; node = new Node[9, 9]; break; } //支解图片构成各单位保留在数组中 int Count = 0; for (int x = 0; x < this.N; x++) { for (int y = 0; 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 = 0; x < this.N; x++) { for (int y = 0; y < this.N; y++) { Graphics newGra = Graphics.FromImage(node[x, y].Img); newGra.DrawLine(new Pen(Color.White), new Point(0, 0), new Point(0, this.Width / this.N)); newGra.DrawLine(new Pen(Color.White), new Point(0, 0), new Point(this.Width / this.N, 0)); newGra.DrawLine(new Pen(Color.White), new Point(this.Width / this.N, this.Width / this.N), new Point(this.Width / this.N, 0)); newGra.DrawLine(new Pen(Color.White), new Point(this.Width / this.N, this.Width / this.N), new Point(0,this.Width / this.N)); } } //(末了一项为空零丁处置惩罚) node[N - 1, N - 1].Img = Image.FromFile("Image\\end.PNG"); Graphics newGra2 = Graphics.FromImage(node[N - 1, N - 1].Img); newGra2.DrawLine(new Pen(Color.Red), new Point(1, 1), new Point(1, this.Width / this.N - 1)); newGra2.DrawLine(new Pen(Color.Red), new Point(1, 1), new Point(this.Width / this.N - 1, 1)); newGra2.DrawLine(new Pen(Color.Red), new Point(this.Width / this.N - 1, this.Width / this.N - 1), new Point(this.Width / this.N - 1, 1)); newGra2.DrawLine(new Pen(Color.Red), new Point(this.Width / this.N - 1, this.Width / this.N - 1), new Point( 1,this.Width / this.N - 1)); //打乱拼图 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 = 0; int y = 0; int sX = fromImage.Width - width; int sY = fromImage.Height - height; if (sX > 0) { x = sX > spaceX ? spaceX : sX; } else { width = fromImage.Width; } if (sY > 0) { y = sY > spaceY ? spaceY : sY; } else { height = fromImage.Height; } //建立新图位图 Bitmap bitmap = new Bitmap(width, height); //建立作图地区 Graphics graphic = Graphics.FromImage(bitmap); //截取原图响应地区写入作图区 graphic.DrawImage(fromImage, 0, 0, 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 + 1 != N && node[x + 1, y].Num == N * N - 1) { Swap(new Point(x + 1, y), new Point(x, y)); return true; } if (y + 1 != N && node[x, y + 1].Num == N * N - 1) { Swap(new Point(x, y + 1), new Point(x, y)); return true; } if (x - 1 != -1 && node[x - 1, y].Num == N * N - 1) { Swap(new Point(x - 1, y), new Point(x, y)); return true; } if (y - 1 != -1 && node[x, y - 1].Num == N * N - 1) { Swap(new Point(x, y - 1), 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=0; for (int x = 0; x < this.N; x++) { for (int y = 0; 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 = 0; x < this.N; x++) for (int y = 0; 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 = 100000; if (this._gameDif == Diff.simple) sum = 10000; //if (this._gameDif == Diff.ordinary) sum = 100000; Random ran = new Random(); for (int i = 0, x = N - 1, y = N - 1; i < sum; i++) { long tick = DateTime.Now.Ticks; ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)|ran.Next()); switch (ran.Next(0, 4)) { case 0: if (x + 1 != N) { Move(x + 1, y); x = x + 1; } break; case 1: if (y + 1 != N) { Move(x, y + 1); y = y + 1; } break; case 2: if (x - 1 != -1) { Move(x - 1, y); x = x - 1; } break; case 3: if (y - 1 != -1) { Move(x, y - 1); y = y - 1; } break; } } } } }
2、设置类:
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(600,600,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=0; private Image img; private void Form1_Load(object sender, EventArgs e) { img = GamePage.img; pictureBox2.Image = img.GetThumbnailImage(120,120, new Image.GetThumbnailImageAbort(delegate { return false; }), IntPtr.Zero); puzzle = new Puzzle(img, 600, 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 = 0; puzzle.Upset(); pictureBox1.Image = puzzle.Display(); } else { Num = 0; 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#编写拼图游戏的图文代码分享(上)的细致内容,更多请关注ki4网别的相干文章!