"Demo" Puzzle Games WinForm (ii) can only move the puzzle in the space area

Source: Internet
Author: User

As in the first article, the same is a simple demo, this in the beginning to do, think is to drag the PictureBox to another PictureBox, if PictureBox no picture, will be the image passed, but the idea is simple, The fact is complex: first PictureBox does not support the DragEnter and DragDrop events, so the image is replaced by the carrier of the panel; second, the picture is completely random upset no, the last picture of the first line is not random, Must be a specific picture (of course, this is because the demo is all small images exist, in fact, can also be made to randomly cancel a piece of the puzzle game, as long as the demo to make some adjustments); Finally, there are questions about how to confirm that the panel is allowed to drag. The game is to play in the small picture on the mouse to drag to the blank area (only a few blocks near the white space to allow drag, as long as the source code in a line to cancel the cancellation, you can cancel this limit), the following is the run, also got a sister to see the eye point

Compared with the previous demo, now split the small picture carrier panel to accept the MouseDown event, but also accept the DragEnter and DragDrop events, in the MouseDown event to confirm whether the picture is allowed to drag, The logic of whether or not to allow dragging is whether the tag of the panel currently clicked is not NULL (this is used to determine whether the blank area is empty), and whether or not the Panel has a blank area or boundary for the upper and lower four blocks, as to how the index is converted into a two-dimensional array position as in the first rule. The following is the code that executes for the MouseDown event

      private void Panel_mousedown (object sender, MouseEventArgs e) {panel panel = (panel) sender; if (panel.                Tag! = NULL) {bool allowed; if (panel. Name.contains ('_')) {//non-emptypanel int idx = Int. Parse (panel.                    Name.split ('_') [1]); int x = idx% this. The imgnumbers;//is converted into the X-direction of the first few blocks of int y = idx/this. imgnumbers;//converted to the number of blocks in the Y direction//If a panel is allowed to drag in the upper and lower left and right four directions, you can drag allowed = this. Allowdragdrop (x-1, y) | | This. Allowdragdrop (x + 1, y) | | This. Allowdragdrop (x, y-1) | | This.                Allowdragdrop (x, y + 1); } else {//emptypanel only need to determine if the N-1 block is empty allowed = this._splitpanels[this. Specialindex].                Tag = = NULL; }//allowed = true;//Uncomment can be adjusted anywhere in the puzzle if (allowed) {This._d                  Ragpanel = Panel;  Panel. DoDragDrop (This._dragpanel.tag, DragDropEffects.Move |                Dragdropeffects.copy); }            }        }
For Allowdragdrop, here is a separate description: If you determine whether the X, Y can be obtained to the corresponding panel, if not obtained, the representative has been out of the critical range, must not be dragged, if you can get to the panel, there are two cases, One is the additional panel for the first line to display a particular picture, and a panel that is normally split, and if the panel corresponds to a tag that is null, the panel is a blank area
//<        Summary>///Determine if a specific panel is allowed to drag//</summary>//<param name= "x" ></param>  <param name= "y" ></param>///<returns></returns> private bool Allowdragdrop (int            x, int y) {bool allowed = true; if (x < 0 | | Y < 0 | | y >= this. Imgnumbers | | (x >= this.)            Imgnumbers && y > 0)) {//not present panel allowed = false; } else {if (x >= this.                Imgnumbers && y = = 0) {allowed = This._emptypanel.tag = = NULL; } else {allowed = this._splitpanels[x + y * this. Imgnumbers].                Tag = = NULL;        }} return allowed; }
The last DragDrop event here is not much to say, as the beginning of the article, just pass the image and fix the tag, while correcting the AllowDrop property
      void Panel_dragdrop (object sender, DragEventArgs e)        {            panel panel = (panel) sender;            Panel. Tag = This._dragpanel.tag;            Panel. BackgroundImage = this._splitimages[(int) panel. TAG];            Panel. AllowDrop = false;            This._dragpanel.tag = null;            This._dragpanel.allowdrop = true;            This._dragpanel.backgroundimage = null;            if (This._emptypanel.tag = = null &&!this._splitpanels.any ((p) = = string. Format ("pnl_{0}", P.tag)! = p.name))            {//empty panel no longer contains tag, and panel corresponds to tag order correctly                This._emptypanel.allowdrop = False ;                MessageBox.Show ("Congratulations, picture is spelled out");}        }
Source download, in Program.cs inside Application.Run (New RandomPictureForm2 ()) run that this blog corresponding to the demo


"Demo" Puzzle Games WinForm (ii) can only move the puzzle in the space area

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.