C#编写拼图游戏的图文代码分享(上)

这篇文章主要为大家详细介绍了c#拼图游戏的编写代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文设计了C#拼图游戏程序,供大家参考,具体内容如下

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; //单元格数组行列数 ///  /// 构造函数 ///  /// 拼图大图 /// 游戏难度,该类下结构体Diff 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  /// 由图片fromImage中截图并返回 ///  /// 原图片 /// 宽 /// 高 /// 起始X坐标 /// 起始Y坐标 ///  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; } ///  /// 移动坐标(x,y)拼图单元 ///  /// 拼图单元x坐标 /// 拼图单元y坐标 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  /// 打乱拼图 ///  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 > 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,然后拼图对象构造时读取配置类

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;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的累加来计算)

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;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#编写拼图游戏的图文代码分享(上)的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2443781.html

(0)
上一篇 2025年3月3日 14:16:32
下一篇 2025年3月3日 14:17:02

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 详细介绍C#中的常用方法

    本篇文章介绍了,c#中方法的详细说明。需要的朋友参考下 1.让方法返回多个参数 1.1在方法体外定义变量保存结果  代码如下: using System; using System.Collections.Generic; using Sy…

    编程技术 2025年3月3日
    200
  • 深入讲解C#中委托的+=和-=

    这篇文章主要介绍了c#中委托的+=和-=深入研究,本文深入研究+=和-=在执行时都做了哪些事情,加深对c#委托的理解和使用,需要的朋友可以参考下 写在前面 为什么会突然想说说委托?原因吗,起于一个同事的想法,昨天下班的路上一直在想这个问题,…

    2025年3月3日 编程技术
    200
  • 教你用C#制作最简单的登录界面

    我们在使用c#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。 首先我们来看一个简单的制作过程 打开visual 2010,新建窗体,既然是登录窗口…

    2025年3月3日 编程技术
    200
  • C#中把Image无损转换为Icon的实例详解

    这篇文章主要为大家详细介绍了c#无损转换image为icon的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 如题,市面上常见的方法是: var handle = bmp.GetHicon();  //得到图标句柄return Ic…

    编程技术 2025年3月3日
    200
  • FormDragger在C#中实现窗体拖拽器的方法详解

    这篇文章主要为大家详细介绍了c#组件formdragger窗体拖拽器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 适用:.net2.0+ winform项目 介绍: 类似QQ、迅雷等讲究UI体验的软件,都支持在窗口内多处地方拖动窗口,…

    2025年3月3日
    200
  • 关于C#中方法的阐述

    前言: C#中的方法在C#编程中经常会用到,那么什么是方法,方法怎么用呢?在这里总结一下,希望大家能指出我的缺点。 1 什么是方法    方法是C#中将一堆代码进行重用的一种机制。方法就是一段代码,这段代码可能有输入的值,可能会返回值。记得…

    编程技术 2025年3月3日
    200
  • 关于.NET(C#)正确读取中文编码文件的实例教程

    首先如果读者对编码或者bom还不熟悉的话,推荐先读这篇文章:.net(c#):字符编码(encoding)和字节顺序标记(bom)。中文编码基本可以分成两大类:1. ansi编码的扩展集合:比如gbk, gb2312, gb18030等,这…

    编程技术 2025年3月3日
    200
  • log4Net 高性能写入和CSV格式的实例详解

    最近在使用log4net,在使用之前我们必须知道文件流是如何操作的,否则就是盲人摸向。。。,在FileAppender.cs文件里面有LockingModelBase来控制流的锁,默认有3个子类 ExclusiveLock:默认的,Hold…

    2025年3月3日
    200
  • ANTS Performance Profiler(.NET性能调优教程)

    .NET性能调优系列文章 系列文章索引 .NET性能调优之一:ANTS Performance Profiler的使用 .NET性能调优之二:使用Visual Studio进行代码度量 .net性能调优之三:yslow相关规则的调优工具和方…

    2025年3月3日 编程技术
    200
  • .NET实现WebSocket服务端即时通信的实例详解

    本篇文章主要介绍了.net实现即时通信,websocket服务端实例 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 即时通信常用手段 1.第三方平台 谷歌、腾讯 环信等多如牛毛,其中谷歌即时通信是免费的,但免…

    编程技术 2025年3月3日
    200

发表回复

登录后才能评论