十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
如何利用C#編寫拼圖游戲?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
創(chuàng)新互聯(lián)是專業(yè)的泉港網(wǎng)站建設(shè)公司,泉港接單;提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行泉港網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!


功能描述:
1.用戶自定義上傳圖片
2.游戲難度選擇:簡單(3*3)、一般(5*5)、困難(9*9)三個(gè)級別
3.紀(jì)錄完成步數(shù)
模塊:
1.拼圖類
2.配置類
3.游戲菜單窗口
4.游戲運(yùn)行窗口
代碼文件VS2013版本:
下載鏈接: 拼圖游戲
--------------------------------------------------我叫分割線---------------------------------------------------------------
1.拼圖類
方法:
1.構(gòu)造函數(shù):傳圖片并分割成一個(gè)一個(gè)小圖片
2.交換方法
3.大圖中截取小單元方法
4.移動(dòng)單元的方法
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 //拼圖單元格結(jié)構(gòu)體
{
public Image Img;
public int Num;
}
private Image _img; //拼圖圖片
public int Width; //拼圖邊長
private Diff _gameDif; //游戲難度
private Node[,] node; //單元格數(shù)組
public int N; //單元格數(shù)組行列數(shù)
///
/// 構(gòu)造函數(shù)
///
/// 拼圖大圖
/// 游戲難度,該類下結(jié)構(gòu)體Diff
public Puzzle(Image Img,int Width, Diff GameDif)
{
this._gameDif = GameDif;
this._img = Img;
this.Width = Width;
switch(this._gameDif)
{
case Diff.simple: //簡單則單元格數(shù)組保存為3*3的二維數(shù)組
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;
}
//分割圖片形成各單元保存在數(shù)組中
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));
}
}
//(最后一項(xiàng)為空單獨(dú)處理)
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();
}
///
/// 由圖片fromImage中截圖并返回
///
/// 原圖片
/// 寬
/// 高
/// 起始X坐標(biāo)
/// 起始Y坐標(biāo)
///
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;
}
//創(chuàng)建新圖位圖
Bitmap bitmap = new Bitmap(width, height);
//創(chuàng)建作圖區(qū)域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原圖相應(yīng)區(qū)域?qū)懭胱鲌D區(qū)
graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
//從作圖區(qū)生成新圖
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
return saveImage;
}
///
/// 移動(dòng)坐標(biāo)(x,y)拼圖單元
///
/// 拼圖單元x坐標(biāo)
/// 拼圖單元y坐標(biāo)
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;
}
//交換兩個(gè)單元格
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);
//創(chuàng)建作圖區(qū)域
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;
}
///
/// 打亂拼圖
///
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,然后拼圖對象構(gòu)造時(shí)讀取配置類

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)
{
}
}
}游戲運(yùn)行窗口:
1.注冊鼠標(biāo)點(diǎn)擊事件來獲得輸入消息
2.顯示功能
3.pictureBox1用來展示游戲拼圖情況
4.pictureBox2展示完整拼圖的縮略圖(當(dāng)鼠標(biāo)移至pictureBox2時(shí),發(fā)送消息,使pictureBox1顯示完整拼圖)
5.Numlabel來顯示移動(dòng)步數(shù)(通過變量Num的累加來計(jì)算)

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("恭喜過關(guān)", "是否重新玩一把", 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;
}
}看完上述內(nèi)容,你們掌握如何利用C#編寫拼圖游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!