"Demo" Puzzle games WinForm (a) simple drag puzzle

Source: Internet
Author: User

Simple demo, can be used to learn image processing, WinForm Drag events, can also be used for the general students of the party's work (in fact, this is to help students to solve the work of the party, but then adjusted the next ...) ), because it is a demo, so the code is a little bit, the following is the run, get a sister to watch the eye point, the game is to shuffle the pictures to the puzzle area, if the location is consistent, when the mouse is released, the picture will be drawn to the puzzle area, while the randomly scrambled image to remove the area

The first is the picture-related parts: Picture and other proportional scaling + segmentation, the following is the relevant code

    public static class Helpers {//<summary>///Get Picture of scaled (aspect the most middle part when inconsistent)///&LT;/SU mmary>//<param name= "FromImage" ></param>///<param Name= "width" > width to get </param&gt        ; <param name= "height" > Height to get </param>//<returns></returns> public static Image Adjimagetofitsize (this Image fromimage, int width, int height) {Bitmap Bitmap = new Bitmap (width, heigh            T);            Graphics graphics = graphics.fromimage (bitmap);            Point point = new Point (0, 0);            Point point2 = new Point (width, 0);            Point point3 = new Point (0, height);            point[] destpoints = new point[] {point, Point2, point3};            Rectangle rect = Getimagerectangle (fromimage); Graphics.            DrawImage (FromImage, destpoints, rect, graphicsunit.pixel); Image image = Image.fromhbitmap (bitmap.            Gethbitmap ()); Bitmap. Dispose ();           Graphics.            Dispose ();        return image;            } private static Rectangle Getimagerectangle (Image fromimage) {//center position get int x = 0;            int y = 0;            int height = fromimage.height;            int width = fromimage.width;                if (Fromimage.height > Fromimage.width) {Height = Fromimage.width;            y = (fromimage.height-fromimage.width)/2;                } else {width = fromimage.height;            x = (fromimage.width-fromimage.height)/2;        } return new Rectangle (x, y, width, height); }///<summary>///To cut the picture into small pictures, the picture order is first horizontal after the vertical///</summary>//<param name= "FromI Mage "></param>//<param name=" CX ">x direction cut number </param>//<param name=" Cy ">y direction cut number &lt ;/param>//<returns></returns> public static image[] SplittosmaLlimages (this Image fromimage, int cx, int cy) {image[] IMGs = new IMAGE[CX * CY];            int nwidth = FROMIMAGE.WIDTH/CX;            int nheight = Fromimage.height/cy;            Bitmap image = New Bitmap (nwidth, nheight);            Graphics graphics = graphics.fromimage (image);                    for (int i = 0, i < cy; i++) {for (int j = 0; J < CX; J + +) { Graphics.                    DrawImage (fromimage, 0, 0, New Rectangle (J * nwidth, I * nheight, nwidth, nheight), GraphicsUnit.Pixel); Image img = Image.fromhbitmap (image.                    Gethbitmap ());                    int idx = j + i * CX; Img.                    Tag = idx;                IMGS[IDX] = img;        }} return IMGs; }    }
Then a variety of clicks, drag the interaction is not much to say, you can download the code after the view, because the picture is split into a one-dimensional array, here is how to get the corresponding two-dimensional array according to the index number, the following is the relevant code fragment, the above code in the cutting of the picture has been used before the horizontal vertical way, That is, according to the index to get the two-dimensional position is as long as this, that is, the horizontal position is calculated as index number% per row of the number of split blocks, the vertical position is calculated as index number/Line split block number, of course, the following code is not indexed to get X, y for the corresponding index
            int idx = (int) e.data.getdata (typeof (int));            Point P = this.pnl_DragOn.PointToClient (new Point (e.x, e.y));            int x = P.x/this. sidelength;//calculates the position of the X-direction            int y = p.y/this. sidelength;//calculates the position in the Y direction            if (idx = = x + this. Imgnumbers * y)//Determine if the image position is consistent with the calculated position            {                Graphics graphics = This.pnl_DragOn.CreateGraphics ();                Graphics. DrawImage (this._dragpic.image, X * this. Sidelength, Y * this. Sidelength);                This._dragpic.image = null;//The image is placed, remove the image if on box                (!this._splitpictureboxes.any (pb + Pb). Image! = null)                {                    MessageBox.Show ("Congratulations, picture is spelled out");}            }

SOURCE download

"Demo" Puzzle games WinForm (a) simple drag puzzle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.