C#拼图游戏编写代码

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

 }
}

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

(0)

相关推荐

  • C#在Unity游戏开发中进行多线程编程的方法

    在这之前,有很多人在质疑Unity支不支持多线程,事实上Unity是支持多线程的.而提到多线程就要提到Unity非常常用的协程,然而协程并非真正的多线程.协程其实是等某个操作完成之后再执行后面的代码,或者说是控制代码在特定的时机执行.而多线程在Unity渲染和复杂逻辑运算时可以高效的使用多核CPU,帮助程序可以更高效的运行.本篇主要介绍在Unity中如何使用多线程. 首先引入C#中使用多线程的类库 using System.Threading; 创建线程实例的四种方式 一.线程执行无参方法 构造

  • C#实现的24点游戏实例详解

    本文实例分析了C#实现的24点游戏.分享给大家供大家参考.具体如下: 1. 24点游戏规则及算法 规则:给出4个自然数,找出能够求出24的四则运算式,要求数字不能重复使用 分析: 本算法是一种暴力求解法: 给出任意两个数字,可以进行6种四则运算,求出最多6个值.以数字a和b为例,有: 加(a+b).减(a-b).被减(b-a).乘以(a*b).除以(a/b)和除(b/a) abcd共计四个数,如果顺序固定,则有5种计算顺序(★代表上面6种四则运算中的一种): ((a★b)★c)★d.(a★b)★

  • C#实现简单的井字游戏实例

    本文实例讲述了C#实现简单的井字游戏.分享给大家供大家参考.具体如下: /* * Created using: SharpDevelop * Created by: Tony Misner * Date: 1/2/2007 * Time: 2:34 PM * */ using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace TicTacToe

  • C#实现的算24点游戏算法实例分析

    本文实例讲述了C#实现的算24点游戏算法.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Calc24Points { public class Cell { public enum Type { Number, Signal } public int Number; public ch

  • C#实现洗牌游戏实例

    棋牌类游戏是目前比较火的游戏之一.今天本文就以实例形式实现洗牌游戏.本文实例所采用的算法是:遍历每个位置上的牌,然后与随机位置上的牌交换. 运行结果如下图所示: 对于牌来讲,2个关键的因素是面值和类型(如红桃.梅花等). 代码如下: public class Card { private string mianzhi; private string leixin; public Card(string m, string l) { mianzhi = m; leixin = l; } publi

  • C#十五子游戏编写代码

    本文实例为大家分享了C#十五子游戏的具体代码,供大家参考,具体内容如下 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;

  • C#利用控件拖拽技术制作拼图游戏

    主要实现的功能: 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

  • C#拼图游戏编写代码(2)

    前言:在C#拼图游戏编写代码程序设计 之 C#实现<拼图游戏>(上),上传了各模块代码,而在本文中将详细剖析原理,使读者更容易理解并学习,程序有诸多问题,欢迎指出,共同学习成长! 正文: 拼图是一个非常经典的游戏,基本每个人都知道他的玩法,他的开始,运行,结束.那么,当我们想要做拼图的时候如何入手呢?答案是:从现实出发,去描述需求(尽量描述为文档),当我们拥有了全面的需求,就能够提供可靠的策略,从而在代码中实现,最终成为作品! (一)需求: (这个需求书写较为潦草,为广大小白定制,按照最最最普

  • 基于C#实现俄罗斯方块游戏

    最近在看C#,写了一个很水的方块游戏练手. 代码: namespace game { class Square { public Square() { state = 0; positionY = 0; positionX = 0; } public Square(int InitShapeCnt, int InitState) { state = InitState; positionY = 0; positionX = 0; InitShape(InitShapeCnt); } public

  • C#面向对象编程之猜拳游戏实现方法

    本文实例讲述了C#面向对象编程之猜拳游戏实现方法.分享给大家供大家参考.具体实现方法如下: 1.需求 现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢. 2.需求分析 根据需求,来分析一下对象,可分析出:玩家对象(Player).计算机对象(Computer).裁判对象(Judge). 玩家出拳由用户控制,使用数字代表:1石头.2剪子.3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢. 3.类对象的实现 ①.玩家类示例代码: 复制

随机推荐