iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >C#怎么实现拼图游戏
  • 136
分享到

C#怎么实现拼图游戏

2023-06-20 17:06:19 136人浏览 薄情痞子
摘要

这篇文章主要介绍“C#怎么实现拼图游戏”,在日常操作中,相信很多人在C#怎么实现拼图游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么实现拼图游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!本文

这篇文章主要介绍“C#怎么实现拼图游戏”,在日常操作中,相信很多人在C#怎么实现拼图游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么实现拼图游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

本文实例为大家分享了C#实现拼图游戏的具体代码,供大家参考,具体内容如下

(一)需求:(这个需求书写较为简单)

  • 图片:有图

  • 切割:拼图不是一个图,我们需要把一个整图它切割成N*N的小图

  • 打乱:把这N*N的小图打乱顺序,才能叫拼图qwq

  • 判断:判断拼图是否成功

  • 交互:选择鼠标点击拖动的方式

  • 展示原图:拼不出来可以看看

  • 更换图片:腻了可以从本地选择一张你喜欢的来拼图

  • 选择难度:除了2×2还可以选择切割成3×3或者4×4,看你喜欢qwq

(二)设计:

使用VS的c#来实现

界面设计:picturebox控件来显示图片,button控件来实现按钮点击的各类事件:图片重排、换图、查看原图等,使用numericUpDown控件来控制切割的边数。如下图:

C#怎么实现拼图游戏

把要拼的图片放进resource文件里
设计函数,使用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() + "//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函数为主函数

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()        {            if (!Directory.Exists(Application.StartupPath.ToString() + "//Resources"))            {                Directory.CreateDirectory(Application.StartupPath.ToString() + "//Resources");                Properties.Resources._0.Save(Application.StartupPath.ToString() + "//Resources//0.jpg");                Properties.Resources._1.Save(Application.StartupPath.ToString() + "//Resources//1.jpg");                Properties.Resources._2.Save(Application.StartupPath.ToString() + "//Resources//2.jpg");                Properties.Resources._3.Save(Application.StartupPath.ToString() + "//Resources//3.jpg");                Properties.Resources._4.Save(Application.StartupPath.ToString() + "//Resources//4.jpg");            }            Random r=new Random ();            int i=r.Next (5);            originalpicpath = Application.StartupPath.ToString() + "//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() + "//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("挑战成功"); timer1.Stop(); }            else { MessageBox.Show("挑战失败"); timer1.Stop(); }        }    }}

ps:挑战模式貌似有点小问题,没法显示倒数的时间在页面上,体验感觉不好。

接下来是设计显示原图的页面,只需要一个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);        }    }}

到此,关于“C#怎么实现拼图游戏”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: C#怎么实现拼图游戏

本文链接: https://www.lsjlt.com/news/298637.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#怎么实现拼图游戏
    这篇文章主要介绍“C#怎么实现拼图游戏”,在日常操作中,相信很多人在C#怎么实现拼图游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么实现拼图游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!本文...
    99+
    2023-06-20
  • C#实现拼图游戏
    本文实例为大家分享了C#实现拼图游戏的具体代码,供大家参考,具体内容如下 (一)需求:(这个需求书写较为简单) 图片:有图 切割:拼图不是一个图,我们需要把一个整图...
    99+
    2024-04-02
  • 怎么用Android实现拼图游戏
    小编给大家分享一下怎么用Android实现拼图游戏,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!具体内容如下源码package packageName;import android.os.Bundle;...
    99+
    2023-06-29
  • java中拼图游戏怎么实现
    这篇文章主要介绍了java中拼图游戏怎么实现,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。直接上效果图:1.所需技术java基础java的GUI相关技术2.具体实现2.1&n...
    99+
    2023-06-29
  • Android实现拼图游戏
    本文实例为大家分享了Android实现拼图游戏的具体代码,供大家参考,具体内容如下 本人是用 android studio 完成的 源码 package packageName; ...
    99+
    2024-04-02
  • C语言如何实现拼图游戏
    本篇内容介绍了“C语言如何实现拼图游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!制作一款拼图小游戏#include <g...
    99+
    2023-06-08
  • C++语言实现拼图游戏详解
    目录开发环境:Visual Studio 2019,easyx图形库。游戏功能列表:游戏效果一.头文件和基本量二.封面三.数据初始化四.封面规则按钮五.构造拼图六.绘图函数七.背景音...
    99+
    2024-04-02
  • 怎么用Python实现拼图小游戏
    本篇内容主要讲解“怎么用Python实现拼图小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python实现拼图小游戏”吧!开发工具Python版本:...
    99+
    2024-04-02
  • iOS实现拼图小游戏
    本文实例为大家分享了iOS实现拼图小游戏的具体代码,供大家参考,具体内容如下 首先找到这8张图片,还需要一张空白的图片,自己随便剪一张吧。 定义三个属性:button可变数组,图片...
    99+
    2024-04-02
  • C++基于EasyX库实现拼图小游戏
    用C++的EasyX库做的拼图小游戏,供大家参考,具体内容如下   记录一下自己做的第一个项目,还有一些改进空间QWQ,可以支持难度升级,但是通关判断似乎有点...
    99+
    2024-04-02
  • C# 拼图游戏的实战(附demo)
    目录一、项目分析1、用户需求分析2、系统设计思路3、系统模块划分二、项目设计1、各个子模块的设计方法板块一:注册用户并进行登录。板块二:导入图片。板块三:设置关卡所能选的难度。版块四...
    99+
    2024-04-02
  • js实现拖拽拼图游戏
    本文实例为大家分享了js实现拖拽拼图游戏的具体代码,供大家参考,具体内容如下 该游戏主要使用了一些拖拽事件,以及对数据传递的应用,直接上代码,感兴趣的可以参考 html:代码 ...
    99+
    2024-04-02
  • Opencv开发实现拼图游戏
    本文实例为大家分享了Opencv开发实现拼图游戏的具体代码,供大家参考,具体内容如下 一、代码 #include<opencv2/opencv.hpp> #inclu...
    99+
    2024-04-02
  • 用js实现拼图小游戏
    本文实例为大家分享了js实现拼图小游戏的具体代码,供大家参考,具体内容如下 一、js拼图是什么? 用js做得小游戏 二、使用步骤 1、先创建div盒子 <style>...
    99+
    2024-04-02
  • js实现简单拼图游戏
    本文实例为大家分享了js实现简单拼图游戏的具体代码,供大家参考,具体内容如下 HTML仅有一个id为game的div,并且没有编写css样式,只需要在img文件夹中放置一个图片文件就...
    99+
    2024-04-02
  • android实现简单拼图游戏
    本文实例为大家分享了android实现简单拼图游戏的具体代码,供大家参考,具体内容如下 1. 2. //使用回调接口,首先初始化pintuview并绑定,实现回调接口的方法    ...
    99+
    2024-04-02
  • 怎么使用js编写实现拼图游戏
    这篇文章主要讲解了“怎么使用js编写实现拼图游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用js编写实现拼图游戏”吧!目标使用原生js编写一个拼图游戏,我这里写了两种拼图的方法。一...
    99+
    2023-07-02
  • java控制台实现拼图游戏
    本文实例为大家分享了java控制台实现拼图游戏的具体代码,供大家参考,具体内容如下 1、首先对原始的拼图进行多次无规律的移动,将拼图打乱 2、然后进行游戏,在游戏移动同时对拼图顺序进...
    99+
    2024-04-02
  • java实现九宫格拼图游戏
    本文实例为大家分享了java实现九宫格拼图游戏的具体代码,供大家参考,具体内容如下 设计步骤:  先将框架构思出来,首先将拼图游戏的雏形实现,即一个界面,九个按钮,按钮上的...
    99+
    2024-04-02
  • js实现简单拼图小游戏
    本文实例为大家分享了js实现简单拼图小游戏的具体代码,供大家参考,具体内容如下 游戏很简单,拼拼图,鼠标拖动一块去和另一块互换。语言是HTML+js,注释写的有不明白的可以留言问一下...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作