亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何利用C#編寫拼圖游戲

發布時間:2020-07-16 11:39:56 來源:億速云 閱讀:387 作者:Leah 欄目:編程語言

如何利用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;

 }
}

看完上述內容,你們掌握如何利用C#編寫拼圖游戲的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

巴彦县| 崇左市| 洛南县| 阜宁县| 香格里拉县| 南澳县| 大同县| 德保县| 安阳市| 孟州市| 伊金霍洛旗| 邵武市| 广西| 新龙县| 巫山县| 唐山市| 龙里县| 大丰市| 望谟县| 嘉义市| 南靖县| 丹巴县| 谢通门县| 万荣县| 周宁县| 宾阳县| 达日县| 抚松县| 建德市| 青田县| 镶黄旗| 汤原县| 岫岩| 邛崃市| 龙门县| 井冈山市| 栾川县| 灵寿县| 涟源市| 河西区| 云南省|