This article is mainly for everyone to introduce the C # puzzle game coding, with a certain reference value, interested in small partners can refer to
This article designed the C # puzzle game program, for your reference, the specific content as follows
Function Description:
1. User-defined upload image
2. Game Difficulty Selection: Simple (3*3), General (5*5), difficult (9*9) three levels
3. Record the number of steps completed
Module:
1. Puzzle Class
2. Configuration Classes
3. Game Menu Window
4. Game Run window
code file VS2013 version:
Download Link: Puzzle game
--------------------------------------------------My name is split line---------------------------------------------------------------
1. Puzzle Class
Method:
1. Constructor: Pass the picture and split it into a small picture
2. Method of Exchange
3. Capturing small cell methods in larger images
4. How to move the unit
5. Scrambling the unit sequence method
Using system;using system.collections.generic;using system.drawing;using system.linq;using System.Text;using System.threading.tasks;using System.windows.forms;namespace Puzzle {public class Puzzle {public enum Diff//game difficulty {simple,//Jane Single ordinary,//common difficulty//difficulty} private struct Node//puzzle cell struct Body {public Image Img; public int Num;} private Image _img; Puzzle picture public int Width; Puzzle Edge Long Private Diff _gamedif; Game difficulty Private node[,] Node; Cell array public int N; Number of cell arrays///<summary>///constructors////</summary>//<param name= "IMG" > Puzzle big Picture </param>//<param Name= "Gamedif" > difficulty of the game, under the structure diff</param> public Puzzle (Image img,int Width, Diff gamedif) {this._gamedif = gamed If This._img = img; This. width = width; Switch (THIS._GAMEDIF) {case Diff.simple://Simple then the cell array is saved as a two-dimensional array of 3*3 this. N = 3; Node=new node[3,3]; Break Case Diff.ordinary://General is 5*5 this. N = 5; node = new node[5, 5]; Break Case Diff.difficulty://The difficulty is 9*9 this. N = 9; node = new node[9, 9]; BReak; }//split the picture to form each unit saved in an array of 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), and new Point (0, this. Width/this. N)); Newgra.drawline (New Pen (Color.White), new Point (0, 0), and 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)); }}//(The last item is empty separately processing) 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), and new Point (1, this. Width/this. N-1)); Newgra2.drawline (New Pen (color.red), New Point (1, 1), and 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)); Upset puzzle this. Upset (); }///<summary>///from the picture FromImage and return to///</summary>/<param name= "FromImage" > Original image </param>/// Lt;param name= "width" > Width </param>//<param name= "height" > High </param>//<param name= "SpaceX" > start x Coordinate </param>//<param name= "SpaceY" > Start y coordinate </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;}//Create bitmap Bitmap Bitmap = new Bitmap (width, height); Create a Graphics area graphic = graphics.fromimage (bitmap); The corresponding area of the captured original is written to the plot area graphic. DrawImage (fromimage, 0, 0, New Rectangle (x, y, width, height), graphicsunit.pixel); Generate a new image from the drawing area image saveimage = Image.fromhbitmap (bitmap. Gethbitmap ()); return saveimage; }///<summary>///mobile coordinates (x, y) puzzle unit///</summary>/<param name= "x" > Puzzle unit x Coordinate </param>//<para M name= "y" > Puzzle unit y-coordinate </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 &&Amp 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; }//Exchange two cells 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); Create a Graphics area 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>//scrambled puzzles///</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 >> +) |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. Configuration class:
Using system;using system.collections.generic;using system.drawing;using system.linq;using System.Text;using System.threading.tasks;namespace Puzzle {public static class Gamepage {public static Puzzle.diff Dif;//game difficulty public static Im Age img; Jigsaw Pattern}}
Game Menu:
Through the menu, upload images to the Configuration class IMG, and select the difficulty to upload to the configuration class dif, and then the puzzle object constructs when reading the configuration class
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 jigsaw Puzzle {public Partial class Menu:form {public Menu () {InitializeComponent (); Gamepage.img =image.fromfile (@ "image\\ jigsaw puzzle. jpg"); Control.checkforillegalcrossthreadcalls = false; private void Button1_Click (object sender, EventArgs e) {gamepage.dif = Puzzle.Diff.simple; Hide (); Form1 ff = new Form1 (); Ff.closefather+=new Jigsaw Puzzles. Form1.childclose (This.closethis); Ff. Show (); private void Button2_Click (object sender, EventArgs e) {gamepage.dif = Puzzle.Diff.ordinary; Hide (); Form1 ff = new Form1 (); Ff.closefather + = new jigsaw puzzle. Form1.childclose (This.closethis); Ff. Show (); private void Button3_Click (object sender, EventArgs e) {gamepage.dif = Puzzle.Diff.difficulty; Hide (); Form1 ff = new Form1 (); Ff.closefather + = new jigsaw puzzle. 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) {}}}
Game Run window:
1. Register the mouse click event to get the input message
2. Display function
3.picturebox1 is used to show the game puzzle situation
4.picturebox2 display thumbnails of the complete puzzle (when the mouse is moved to PictureBox2, send a message to make the PictureBox1 show the complete puzzle)
5.Numlabel to show the number of moving steps (calculated by adding the variable 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 Puzzle {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, +, GAMEPAGE.DIF); pictureBox1.Image =puzzle. Display (); The 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 ("Congratulations Pass", "whether to play again", 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_mo Useleave (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; }}