C#編寫拼圖遊戲的圖文代碼分享(上)

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了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; }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.