国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#拼圖游戲編寫代碼

2019-10-29 21:11:39
字體:
來源:轉載
供稿:網友

本文設計了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,然后拼圖對象構造時讀取配置類

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; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚州市| 靖边县| 阜南县| 嵊州市| 汾阳市| 武鸣县| 安化县| 西吉县| 余江县| 青川县| 高雄市| 璧山县| 体育| 芷江| 中西区| 洛扎县| 安龙县| 钦州市| 太和县| 托里县| 贵定县| 汕尾市| 平乡县| 卢氏县| 富川| 诏安县| 巴东县| 炉霍县| 阿荣旗| 金寨县| 庆云县| 双峰县| 当雄县| 甘肃省| 温宿县| 饶平县| 铜川市| 安康市| 托克逊县| 东城区| 永吉县|