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; //單元格數組行列數 /// <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" />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; } }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。