亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#利用控件拖拽技術制作拼圖游戲

2020-01-24 01:47:52
字體:
來源:轉載
供稿:網友

主要實現的功能:

1.程序附帶多張拼圖隨機拼圖。
2.可手動添加拼圖。
3.游戲成功判斷。
4.30秒超時判斷。

 Puzzle.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO; namespace Puzzle{  public partial class Puzzle : Form  {    //圖片列表    PictureBox[] pictureList = null;    //圖片位置字典    SortedDictionary<string, Bitmap> pictureLocationDict = new SortedDictionary<string, Bitmap>();    //Location List     Point[] pointList = null;    //圖片控件字典    SortedDictionary<string, PictureBox> pictureBoxLocationDict = new SortedDictionary<string, PictureBox>();    //拼圖時間    int second = 0;    //所拖拽的圖片    PictureBox currentPictureBox = null;    //被迫需要移動的圖片    PictureBox haveToPictureBox = null;    //原位置    Point oldLocation = Point.Empty;    //新位置    Point newLocation = Point.Empty;    //鼠標按下坐標(control控件的相對坐標)     Point mouseDownPoint = Point.Empty;    //顯示拖動效果的矩形     Rectangle rect = Rectangle.Empty;    //是否正在拖拽     bool isDrag = false;     public Puzzle()    {      InitializeComponent();      InitGame();    }     /// <summary>    /// 初始化游戲資源    /// </summary>    public void InitGame()    {      pictureList = new PictureBox[9] { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9 };      pointList = new Point[9] { new Point(0, 0), new Point(100, 0), new Point(200, 0), new Point(0, 100), new Point(100, 100), new Point(200, 100), new Point(0, 200), new Point(100, 200), new Point(200, 200) };      if (!Directory.Exists(Application.StartupPath.ToString() + "http://Picture"))      {        Directory.CreateDirectory(Application.StartupPath.ToString() + "http://Picture");        Properties.Resources.默認.Save(Application.StartupPath.ToString() + "http://Picture//1.jpg");        Properties.Resources._1.Save(Application.StartupPath.ToString() + "http://Picture//2.jpg");        Properties.Resources._2.Save(Application.StartupPath.ToString() + "http://Picture//3.jpg");        Properties.Resources._3.Save(Application.StartupPath.ToString() + "http://Picture//4.jpg");        Properties.Resources._4.Save(Application.StartupPath.ToString() + "http://Picture//5.jpg");        Properties.Resources.成功.Save(Application.StartupPath.ToString() + "http://Picture//6.jpg");        Properties.Resources.歡呼.Save(Application.StartupPath.ToString() + "http://Picture//7.jpg");      }      Random r = new Random();      int i = r.Next(7);      Flow(Application.StartupPath.ToString() + "http://Picture//"+i.ToString()+".jpg");    }     private void Puzzle_Paint(object sender, PaintEventArgs e)    {      if (rect != Rectangle.Empty)      {        if (isDrag)        {          e.Graphics.DrawRectangle(Pens.White, rect);        }        else        {          e.Graphics.DrawRectangle(new Pen(this.BackColor), rect);        }      }    }        /// <summary>    /// 不好用    /// </summary>    /// <returns></returns>    public PictureBox GetPictureBoxByLocation()    {      PictureBox pic = null;      if (this.ActiveControl.Name.Contains("pictureBox"))      {        pic = (PictureBox)this.ActiveControl;      }      return pic;     }     public PictureBox GetPictureBoxByLocation(MouseEventArgs e)    {      PictureBox pic = null;      foreach (PictureBox item in pictureList)      {        if (e.Location.X > item.Location.X && e.Location.Y > item.Location.Y && item.Location.X + 100 > e.Location.X && item.Location.Y + 100 > e.Location.X)        {          pic = item;        }      }      return pic;    }     public PictureBox GetPictureBoxByLocation(int x,int y)    {      PictureBox pic = null;      foreach (PictureBox item in pictureList)      {        if (x> item.Location.X && y > item.Location.Y && item.Location.X + 100 > x && item.Location.Y + 100 > y)        {          pic = item;        }      }      return pic;    }     /// <summary>    /// 通過hashcode獲取picture,用mouseeventargs之后獲取相對于picture的坐標不是相對窗體    /// </summary>    /// <param name="hascode"></param>    /// <returns></returns>    public PictureBox GetPictureBoxByHashCode(string hascode)    {      PictureBox pic = null;      foreach (PictureBox item in pictureList)      {        if (hascode == item.GetHashCode().ToString())        {          pic = item;        }      }      return pic;    }     private void pictureBox_MouseDown(object sender, MouseEventArgs e)    {      oldLocation = new Point(e.X, e.Y);      currentPictureBox = GetPictureBoxByHashCode(sender.GetHashCode().ToString());      MoseDown(currentPictureBox, sender, e);    }     public void MoseDown(PictureBox pic, object sender, MouseEventArgs e)    {      if (e.Button == MouseButtons.Left)      {        oldLocation = e.Location;        rect = pic.Bounds;      }    }     private void pictureBox_MouseMove(object sender, MouseEventArgs e)    {      if (e.Button == MouseButtons.Left)      {        isDrag = true;        rect.Location = getPointToForm(new Point(e.Location.X - oldLocation.X, e.Location.Y - oldLocation.Y));        this.Refresh();       }    }          private void reset()    {      mouseDownPoint = Point.Empty;      rect = Rectangle.Empty;      isDrag = false;    }     private Point getPointToForm(Point p)    {      return this.PointToClient(pictureBox1.PointToScreen(p));    }     private void pictureBox_MouseUp(object sender, MouseEventArgs e)    {      oldLocation = new Point(currentPictureBox.Location.X, currentPictureBox.Location.Y);      if (oldLocation.X + e.X > 300 || oldLocation.Y + e.Y > 300||oldLocation.X + e.X < 0 || oldLocation.Y + e.Y < 0)      {        return;      }      haveToPictureBox = GetPictureBoxByLocation(oldLocation.X + e.X, oldLocation.Y + e.Y);      newLocation = new Point(haveToPictureBox.Location.X, haveToPictureBox.Location.Y);      haveToPictureBox.Location = oldLocation;      PictureMouseUp(currentPictureBox, sender, e);      if ( Judge())      {        lab_result.Text = "成功!";        //MessageBox.Show("恭喜拼圖成功");      }    }     public void PictureMouseUp(PictureBox pic, object sender, MouseEventArgs e)    {      if (e.Button == MouseButtons.Left)      {        if (isDrag)        {          isDrag = false;          pic.Location = newLocation;          this.Refresh();        }        reset();      }    }     public void ExchangePictureBox(MouseEventArgs e)    { }     private void btn_sta_Click(object sender, EventArgs e)    {      MessageBox.Show(this.ActiveControl.Name);    }     /// <summary>    /// 初始化    /// </summary>    /// <param name="path"></param>    public void Flow(string path)    {      Image bm = CutPicture.Resize(path, 300, 300);      CutPicture.BitMapList = new List<Bitmap>();      for (int y = 0; y < 300; y += 100)      {        for (int x = 0; x < 300; x += 100)        {          //string key = x + "-" + y;          Bitmap temp = CutPicture.Cut(bm, x, y, 100, 100);          //pictureLocationDict.Add(key, temp);          CutPicture.BitMapList.Add(temp);        }      }      ImportBitMap();    }     /// <summary>    /// 打亂數據    /// </summary>    /// <param name="pictureArray"></param>    /// <returns></returns>    public PictureBox[] DisOrderArray(PictureBox[] pictureArray)    {      PictureBox[] tempArray = pictureArray;      for (int i = tempArray.Length - 1; i > 0; i--)      {        Random rand = new Random();        int p = rand.Next(i);        PictureBox temp = tempArray[p];        tempArray[p] = tempArray[i];        tempArray[i] = temp;      }      return tempArray;    }     /// <summary>    /// 判斷是否拼圖成功    /// </summary>    /// <returns></returns>    public bool Judge()    {      bool result = true;      int i = 0;      foreach (PictureBox item in pictureList)      {        if (item.Location != pointList[i])        {          result = false;        }        i++;      }      return result;    }     private void btn_import_Click(object sender, EventArgs e)    {      lab_result.Text = "";      ofd_picture.ShowDialog();      CutPicture.PicturePath = ofd_picture.FileName;      Flow(CutPicture.PicturePath);      CountTime();    }     /// <summary>    /// 計時    /// </summary>    public void CountTime()    {      lab_time.Text = "0";      timer1.Start();    }     /// <summary>    /// 給piturebox賦值    /// </summary>    public void ImportBitMap()    {      try      {         int i = 0;// DisOrderArray(pictureList)        foreach (PictureBox item in pictureList)        {          Bitmap temp = CutPicture.BitMapList[i];          item.Image = temp;          i++;        }        ResetPictureLocation();      }      catch (Exception exp)      {        Console.WriteLine(exp.Message);      }           }     /// <summary>    /// 打亂位置列表    /// </summary>    /// <returns></returns>    public Point[] DisOrderLocation()    {      Point[] tempArray = (Point[])pointList.Clone();      for (int i = tempArray.Length - 1; i > 0; i--)      {        Random rand = new Random();        int p = rand.Next(i);        Point temp = tempArray[p];        tempArray[p] = tempArray[i];        tempArray[i] = temp;      }      return tempArray;    }     /// <summary>    /// 重新設置圖片位置    /// </summary>    public void ResetPictureLocation()    {      Point[] temp = DisOrderLocation();      int i = 0;      foreach (PictureBox item in pictureList)      {        item.Location = temp[i];        i++;      }    }     /// <summary>    /// 計時,超過30秒停止計時    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void timer1_Tick(object sender, EventArgs e)    {      second++;      lab_time.Text = second.ToString();      if (second == 30)      {        timer1.Stop();        lab_result.Text = "失?。?;      }    }     private void btn_sta_Click_1(object sender, EventArgs e)    {      lab_result.Text = "";      timer1.Start();    }     }}

CutPicture.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using System.Drawing.Imaging;using System.Windows.Forms; namespace Puzzle{  class CutPicture  {    public static string PicturePath = "";    public static List<Bitmap> BitMapList = null;    /// <summary>    /// 剪切圖片    /// </summary>    /// <param name="b">圖片</param>    /// <param name="StartX">X坐標</param>    /// <param name="StartY">Y坐標</param>    /// <param name="iWidth">寬</param>    /// <param name="iHeight">高</param>    /// <returns></returns>    public static Bitmap Cut(Image b, int StartX, int StartY, int iWidth, int iHeight)    {      if (b == null)      {        return null;      }      int w = b.Width;      int h = b.Height;      if (StartX >= w || StartY >= h)      {        return null;      }      if (StartX + iWidth > w)      {        iWidth = w - StartX;      }      if (StartY + iHeight > h)      {        iHeight = h - StartY;      }      try      {        Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);        Graphics g = Graphics.FromImage(bmpOut);        g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);        g.Dispose();        return bmpOut;      }      catch      {        return null;      }    }     /// <summary>    /// 保存圖片到根目錄的Pictures文件夾下    /// </summary>    /// <param name="path">文件路徑</param>    /// <param name="iWidth">調整的寬</param>    /// <param name="iHeignt">調整的高</param>    /// <returns></returns>    public static Image Resize(string path, int iWidth, int iHeignt)    {      Image thumbnail = null;      try      {        var img = Image.FromFile(path);        thumbnail = img.GetThumbnailImage(iWidth, iHeignt, null, IntPtr.Zero);        thumbnail.Save(Application.StartupPath.ToString() + "http://Picture//img.jpeg");      }      catch (Exception exp)      {        Console.WriteLine(exp.Message);      }      return thumbnail;    }       }}

mouse_down

private void pictureBox_MouseDown(object sender, MouseEventArgs e)    {      oldLocation = new Point(e.X, e.Y);      currentPictureBox = GetPictureBoxByHashCode(sender.GetHashCode().ToString());      MoseDown(currentPictureBox, sender, e);    }     public void MoseDown(PictureBox pic, object sender, MouseEventArgs e)    {      if (e.Button == MouseButtons.Left)      {        oldLocation = e.Location;        rect = pic.Bounds;      }    }

mouse_move

private void pictureBox_MouseMove(object sender, MouseEventArgs e)    {      if (e.Button == MouseButtons.Left)      {        isDrag = true;        rect.Location = getPointToForm(new Point(e.Location.X - oldLocation.X, e.Location.Y - oldLocation.Y));        this.Refresh();       }    }

mouse_up

private void pictureBox_MouseUp(object sender, MouseEventArgs e)    {      oldLocation = new Point(currentPictureBox.Location.X, currentPictureBox.Location.Y);      if (oldLocation.X + e.X > 300 || oldLocation.Y + e.Y > 300||oldLocation.X + e.X < 0 || oldLocation.Y + e.Y < 0)      {        return;      }      haveToPictureBox = GetPictureBoxByLocation(oldLocation.X + e.X, oldLocation.Y + e.Y);      newLocation = new Point(haveToPictureBox.Location.X, haveToPictureBox.Location.Y);      haveToPictureBox.Location = oldLocation;      PictureMouseUp(currentPictureBox, sender, e);      if ( Judge())      {        lab_result.Text = "成功!";        //MessageBox.Show("恭喜拼圖成功");      }    }     public void PictureMouseUp(PictureBox pic, object sender, MouseEventArgs e)    {      if (e.Button == MouseButtons.Left)      {        if (isDrag)        {          isDrag = false;          pic.Location = newLocation;          this.Refresh();        }        reset();      }    }

reset

private void reset()   {     mouseDownPoint = Point.Empty;     rect = Rectangle.Empty;     isDrag = false;   }

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
www日韩中文字幕在线看| 精品亚洲aⅴ在线观看| 日韩av观看网址| 欧美www视频在线观看| 国产亚洲欧洲黄色| 91精品啪在线观看麻豆免费| 欧美日韩中文在线| 日本aⅴ大伊香蕉精品视频| 精品日韩视频在线观看| 91久久国产婷婷一区二区| 国产成人jvid在线播放| 国产精品久久久久不卡| 91高清免费在线观看| 亚洲欧美中文字幕在线一区| 国产69精品久久久| 国产亚洲视频在线| 38少妇精品导航| 亚洲色图av在线| 亚洲黄色www| 久久精品中文字幕一区| 日韩在线观看你懂的| 热久久这里只有| 欧美日韩中国免费专区在线看| 在线a欧美视频| 日韩在线观看免费av| 97精品一区二区视频在线观看| 成人激情免费在线| 国产精品wwwwww| 亚洲国产精品99| 欧美日韩精品在线视频| 欧美黄色www| 欧美综合在线观看| www.欧美三级电影.com| 欧洲一区二区视频| 97涩涩爰在线观看亚洲| 日韩大陆欧美高清视频区| 欧美黑人性生活视频| 久久久久久久久久婷婷| 久久国产精品久久精品| 热99在线视频| 欧美日韩免费观看中文| 欧洲亚洲妇女av| 久热99视频在线观看| 97不卡在线视频| 91精品国产免费久久久久久| 亚洲老司机av| 亚洲精品永久免费精品| 97精品一区二区视频在线观看| 岛国av一区二区在线在线观看| 日韩成人网免费视频| 国产精品最新在线观看| 欧美日韩免费观看中文| 欧美激情国内偷拍| 欧美性高跟鞋xxxxhd| 日韩成人在线播放| 亚洲精品永久免费精品| 欧美视频二区36p| www.99久久热国产日韩欧美.com| 国产精品wwww| 久久精品亚洲国产| 国产午夜精品视频| 久久精彩免费视频| 一区二区成人精品| 国产女人18毛片水18精品| 欧美在线视频网| 疯狂欧美牲乱大交777| 精品久久香蕉国产线看观看gif| 日韩欧美大尺度| 欧美中在线观看| 亚洲欧美日韩一区在线| 精品无人区乱码1区2区3区在线| 国产精品久久久一区| 欧美国产日产韩国视频| 91美女片黄在线观| 亚洲精品国产精品国自产观看浪潮| 国产亚洲美女精品久久久| 精品偷拍各种wc美女嘘嘘| 中文字幕日韩精品在线观看| 国产精品福利在线| 亚洲石原莉奈一区二区在线观看| 久久久亚洲欧洲日产国码aⅴ| 久久精品青青大伊人av| 尤物yw午夜国产精品视频| 57pao成人国产永久免费| 国产精品99导航| 国产精品入口福利| 在线观看日韩欧美| 国产69精品99久久久久久宅男| 欧美高清电影在线看| 一道本无吗dⅴd在线播放一区| 91色视频在线导航| 久久久精品美女| 久久久久久久一区二区| 亚洲国产精品久久久久秋霞不卡| 久久久欧美一区二区| 久久综合国产精品台湾中文娱乐网| 久久精品人人做人人爽| 国产欧美精品日韩精品| 国语自产偷拍精品视频偷| 亚洲自拍另类欧美丝袜| 国产精品视频在线观看| 国产精品久久在线观看| 亚洲欧美国产va在线影院| 国产国产精品人在线视| 欧美丰满少妇xxxxx| 久久久久久高潮国产精品视| 国产精品高精视频免费| 国产精品视频区1| 精品国产拍在线观看| 欧美日韩在线第一页| 成人xxxxx| 亚洲欧美一区二区三区情侣bbw| 色中色综合影院手机版在线观看| 亚洲精品一二区| 国产精品视频一区国模私拍| 国产精品美女久久久久av超清| 九九精品视频在线| 欧美激情在线观看视频| 亚洲天堂男人的天堂| 91在线免费网站| 91精品国产九九九久久久亚洲| 高清视频欧美一级| 国产精品九九久久久久久久| 久久99精品国产99久久6尤物| 中文字幕日韩精品有码视频| 91精品国产自产91精品| 精品久久久999| 日本国产欧美一区二区三区| 久久99久久久久久久噜噜| 欧美激情中文字幕乱码免费| 欧美性猛交xxxx偷拍洗澡| 国产精品视频99| 国产一区二区三区在线播放免费观看| 91精品国产自产在线观看永久| 97在线视频免费看| 人人澡人人澡人人看欧美| www.色综合| 日本乱人伦a精品| 亚洲国产成人精品女人久久久| xxxxx91麻豆| 国产亚洲欧美视频| 久久综合久中文字幕青草| 国产精品99久久久久久www| 亚洲国产精品悠悠久久琪琪| 欧美性猛交xxxx乱大交极品| 亚洲欧美另类人妖| 国产一区二中文字幕在线看| 成人免费网站在线观看| 日韩免费在线免费观看| 97成人超碰免| 国产精品老女人精品视频| 亚洲欧洲成视频免费观看| 成人自拍性视频| 国内精品久久久久伊人av| 久久人人爽人人爽人人片av高清| 91精品国产91久久久久| 91tv亚洲精品香蕉国产一区7ujn| 欧美亚洲成人xxx| 欧美性猛交99久久久久99按摩| 亚洲精品一区中文| 欧美高清第一页| 成人精品视频99在线观看免费| 欧美性在线视频| 高清一区二区三区日本久|