C#拼图游戏编写代码

 更新时间:2020年6月25日 11:20  点击:2736

本文设计了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;

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25