亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實(shí)現(xiàn)拼圖游戲

 更新時(shí)間:2021年07月23日 14:32:20   作者:Cooooooooco  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)拼圖游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#實(shí)現(xiàn)拼圖游戲的具體代碼,供大家參考,具體內(nèi)容如下

(一)需求:(這個(gè)需求書寫較為簡(jiǎn)單)

  • 圖片:有圖
  • 切割:拼圖不是一個(gè)圖,我們需要把一個(gè)整圖它切割成N*N的小圖
  • 打亂:把這N*N的小圖打亂順序,才能叫拼圖qwq
  • 判斷:判斷拼圖是否成功
  • 交互:選擇鼠標(biāo)點(diǎn)擊拖動(dòng)的方式
  • 展示原圖:拼不出來可以看看
  • 更換圖片:膩了可以從本地選擇一張你喜歡的來拼圖
  • 選擇難度:除了2×2還可以選擇切割成3×3或者4×4,看你喜歡qwq

(二)設(shè)計(jì):

使用VS的c#來實(shí)現(xiàn)

界面設(shè)計(jì):picturebox控件來顯示圖片,button控件來實(shí)現(xiàn)按鈕點(diǎn)擊的各類事件:圖片重排、換圖、查看原圖等,使用numericUpDown控件來控制切割的邊數(shù)。如下圖:

把要拼的圖片放進(jìn)resource文件里
設(shè)計(jì)函數(shù),使用cutpicture類來切割圖片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace 拼圖游戲
{
    class CutPicture
    {
        public static string picturePath = "";
        public static List<Bitmap> BitMapList = null;
        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;
        }
        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;
            }
        }
    }
}

Form_Main函數(shù)為主函數(shù)

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;
using System.IO;

namespace 拼圖游戲
{
    public partial class Form_Main : Form
    {
        PictureBox[] picturelist = null;
        SortedDictionary<string, Bitmap> pictureLocationDict = new SortedDictionary<string, Bitmap>();
        Point []pointlist=null;
        SortedDictionary<string, PictureBox > pictureBoxLocationDict = new SortedDictionary<string, PictureBox>();
        
        PictureBox currentpicturebox = null;
        PictureBox havetopicturebox = null;
        Point oldlocation = Point.Empty;
        Point newlocation = Point.Empty;
        Point mouseDownPoint = Point.Empty;
        Rectangle rect = Rectangle.Empty;
        bool isDrag = false;
        public string originalpicpath;
        private int Imgnubers
        {
            get
            {
                return (int)this.numericUpDown1.Value;
            }
        }
        private int sidelength
        {
            get { return 600 / this.Imgnubers; }
        }
        public void InitRandomPictureBox()
        {
            pnl_Picture.Controls.Clear();
            picturelist = new PictureBox[Imgnubers * Imgnubers];
            pointlist = new Point [Imgnubers * Imgnubers];
           
            for (int i = 0; i < this.Imgnubers; i++)
            {
                for (int j = 0; j < this.Imgnubers; j++)
                {
                    PictureBox pic = new PictureBox();
                    pic.Name = "picturebox" + (j + i * Imgnubers + 1).ToString();
                    pic.Location = new Point(j * sidelength, i * sidelength);
                    pic.Size = new Size(sidelength, sidelength);
                    pic.Visible = true;
                    pic.BorderStyle = BorderStyle.FixedSingle;
                    pic.MouseDown += new MouseEventHandler(picture_MouseDown);
                    pic.MouseMove += new MouseEventHandler(picture_MouseMove);
                    pic.MouseUp += new MouseEventHandler(picture_MouseUp);
                    pnl_Picture.Controls.Add(pic);
                    picturelist[j + i * Imgnubers] = pic;
                    pointlist[j + i * Imgnubers] = new Point(j * sidelength, i * sidelength);
                }
            }
        }
        public void Flow(string path, bool disorder)
        {
            InitRandomPictureBox();
            Image bm = CutPicture.Resize(path, 600, 600);
            CutPicture.BitMapList = new List<Bitmap>();
            for(int y=0;y<600;y+=sidelength )
            {
                for (int x = 0; x < 600; x += sidelength)
                {
                    Bitmap temp = CutPicture.Cut(bm, x, y, sidelength, sidelength);
                    CutPicture.BitMapList.Add(temp);
                }
            }
                ImporBitMap(disorder );
        }
        public void ImporBitMap(bool disorder)
        {
            try
            {
                int i=0;
                foreach (PictureBox item in picturelist )
                {
                    Bitmap temp = CutPicture.BitMapList[i];
                    item.Image = temp;
                    i++;
                }
                if(disorder )ResetPictureLoaction();
            }
            catch (Exception exp)
            {
                Console .WriteLine (exp.Message );
            }
        }
        public void ResetPictureLoaction()
        {
            Point[] temp = DisOrderLocation();
            int i = 0;
            foreach (PictureBox item in picturelist)
            {
                item.Location = temp[i];
                i++;
            }
        }
        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;
        }  
        private void Form_Main_Load(object sender, EventArgs e)
        {
        
        }
        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://Resources"))
            {
                Directory.CreateDirectory(Application.StartupPath.ToString() + "http://Resources");
                Properties.Resources._0.Save(Application.StartupPath.ToString() + "http://Resources//0.jpg");
                Properties.Resources._1.Save(Application.StartupPath.ToString() + "http://Resources//1.jpg");
                Properties.Resources._2.Save(Application.StartupPath.ToString() + "http://Resources//2.jpg");
                Properties.Resources._3.Save(Application.StartupPath.ToString() + "http://Resources//3.jpg");
                Properties.Resources._4.Save(Application.StartupPath.ToString() + "http://Resources//4.jpg");
            }
            Random r=new Random ();
            int i=r.Next (5);
            originalpicpath = Application.StartupPath.ToString() + "http://Resources//" + i.ToString() + ".jpg";
            Flow(originalpicpath ,true );
        }
        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 + sidelength > x && item.Location.Y + sidelength > y)
                { pic = item; }
            }
            return pic;
        }
        public PictureBox GetPictureBoxByHashCode(string hascode)
        {
            PictureBox pic = null;
            foreach (PictureBox item in picturelist)
            {
                if (hascode == item.GetHashCode().ToString())
                {
                    pic = item;
                }
            }
            return pic;
        }
        private void picture_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 picture_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 Point getPointToForm(Point p)
        {
            return this.PointToClient(pictureBox1 .PointToScreen (p));
        }
        private void reset()
        {
            mouseDownPoint = Point.Empty;
            rect = Rectangle.Empty;
            isDrag = false;
        }

        private void picture_MouseUp(object sender, MouseEventArgs e)
        {
            oldlocation = new Point(currentpicturebox .Location .X ,currentpicturebox .Location .Y );
            if (oldlocation.X + e.X > 600 || oldlocation.Y + e.Y > 600 || 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())
            {
                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 bool Judge()//判斷是否成功
        {
            bool result=true;
            int i=0;
            foreach (PictureBox item in picturelist)
            {
                if (item.Location != pointlist[i])
                { result = false; }
                i++;
            }
            return result;
        }
        public void ExchangePictureBox(MouseEventArgs e)
        { }
        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;
        }  
        public Form_Main()
        {
            InitializeComponent();
            initgame();
        }

        private void pnl_Picture_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btn_import_Click(object sender, EventArgs e)
        {
            if (new_picture.ShowDialog() == DialogResult.OK)
            {
                originalpicpath = new_picture.FileName;
                CutPicture.picturePath = new_picture.FileName;
                Flow(CutPicture.picturePath, true);
            }
            
        }
 
        private void btn_changepic_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            int i = r.Next(5);
            originalpicpath = Application.StartupPath.ToString() + "http://Resources//" + i.ToString() + ".jpg";
            Flow(originalpicpath, true);
        }

        private void btn_Reset_Click(object sender, EventArgs e)
        {
            Flow(originalpicpath, true);
        }

        private void btn_originalpic_Click(object sender, EventArgs e)
        {
            Form_Original original = new Form_Original();
            original.picpath = originalpicpath;
            original.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            timer1.Enabled = true;
            timer1.Interval = 10000;
           
           


        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Judge())
            { MessageBox.Show("挑戰(zhàn)成功"); timer1.Stop(); }
            else { MessageBox.Show("挑戰(zhàn)失敗"); timer1.Stop(); }
        }
    }
}

ps:挑戰(zhàn)模式貌似有點(diǎn)小問題,沒法顯示倒數(shù)的時(shí)間在頁面上,體驗(yàn)感覺不好。

接下來是設(shè)計(jì)顯示原圖的頁面,只需要一個(gè)picturebox即可,代碼如下:

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 Form_Original : Form
    {
        public string picpath;
        public Form_Original()
        {
            InitializeComponent();
        }

        private void pic_Original_Click(object sender, EventArgs e)
        {

        }

        private void Form_Original_Load(object sender, EventArgs e)
        {
            pic_Original.Image = CutPicture.Resize(picpath, 600, 600);
        }
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享

    C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享

    這篇文章主要介紹了C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享,小編親測(cè)可用,需要的朋友可以參考下
    2014-09-09
  • C# TabControl控件中TabPage選項(xiàng)卡切換時(shí)的觸發(fā)事件問題

    C# TabControl控件中TabPage選項(xiàng)卡切換時(shí)的觸發(fā)事件問題

    這篇文章主要介紹了C# TabControl控件中TabPage選項(xiàng)卡切換時(shí)的觸發(fā)事件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • C# Email郵件發(fā)送功能 找回或重置密碼功能

    C# Email郵件發(fā)送功能 找回或重置密碼功能

    這篇文章主要為大家詳細(xì)介紹了C# Email郵件發(fā)送功能,找回或重置密碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#打印繪圖的實(shí)現(xiàn)方法

    C#打印繪圖的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#打印繪圖的實(shí)現(xiàn)方法,涉及C#針對(duì)圖片的繪制與打印相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • c# 判斷指定文件是否存在的簡(jiǎn)單實(shí)現(xiàn)

    c# 判斷指定文件是否存在的簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要介紹了c# 判斷指定文件是否存在的簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下
    2014-02-02
  • C#操作DataTable的實(shí)現(xiàn)步驟

    C#操作DataTable的實(shí)現(xiàn)步驟

    本文主要介紹了C#操作DataTable的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • C#中實(shí)現(xiàn)Json序列化與反序列化的幾種方式

    C#中實(shí)現(xiàn)Json序列化與反序列化的幾種方式

    C#中實(shí)現(xiàn)Json的序列化與反序列化也算是個(gè)老話題,那么在這篇文章中我們將老話重提,本文中將會(huì)學(xué)到如何使用C#,來序列化對(duì)象成為Json格式的數(shù)據(jù),以及如何反序列化Json數(shù)據(jù)到對(duì)象。有需要的朋友們可以參考借鑒,下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-12-12
  • C#刪除文件目錄或文件的解決方法

    C#刪除文件目錄或文件的解決方法

    本篇文章是對(duì)C#中如何刪除文件目錄或文件的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C# BitArray點(diǎn)陣列的使用

    C# BitArray點(diǎn)陣列的使用

    本文主要介紹了C# BitArray點(diǎn)陣列的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • C#?md5?算法實(shí)現(xiàn)代碼

    C#?md5?算法實(shí)現(xiàn)代碼

    相對(duì)C#來說,md5算法就相對(duì)簡(jiǎn)單很多,因?yàn)?System.Security.Cryptography;?已經(jīng)包含了md5算法。所以我們只需創(chuàng)建MD5類對(duì)象即可實(shí)現(xiàn)md5算法,今天通過本文給大家介紹C#?md5?算法實(shí)現(xiàn),感興趣的朋友一起看看吧
    2022-11-11

最新評(píng)論