Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of C # Jigsaw puzzle

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of C#jigsaw puzzle for everyone. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

I. Project Analysis

Project analysis briefly describes the main user requirements, design ideas and module division of the project.

1. User demand analysis

In today's society, people's life pressure is getting bigger and bigger, and there is not much time to play large-scale games, so some simple Mini games are needed to relieve everyone's work and study pressure. Lianliankan is a simple, easy to understand, most people will play Mini games, time consuming is not too long, so specially designed to meet the needs of users to relax mood.

2. System design ideas

(1) Registration and login of game users;

(2) Import pictures first and cut them

(3) Randomly discard a small cut graph and scramble the order

(3) The basic function selection column of the form needs to be realized;

(4) It can determine whether the game is won or lost, and when it is combined, it will prompt success;

(5) Click on the picture to be able to move;

(6) There should be a rearranged display box on the right side to facilitate the completion of the player's game.

3. System module division

Block 1: Register users and log in.

Block 2: Import pictures.

Block 3: Set the difficulty level you can choose.

Jigsaw puzzles should have level options of 33, 44, 5 *5, etc. Setting up multiple levels can make the user experience more challenging.

Section 4: Judging whether the puzzle is complete.

Whether the game is won or not is judged by judging whether the numbers of the corresponding structures of the pictures are arranged in complete order.

Block 5: Cutting pictures.

Block 6: Disassemble the puzzle.

Block 7: rearrange the picture to reflect the current state of the puzzle.

Plate 8: Cut into small pictures after moving.

This process is the core process of the whole game, and it is the most important process. The user moves the picture by clicking on the picture. If the picture is close to the white block, the picture moves to the white block position; otherwise, it does not move.

II. Project Design

The design method and core technology of each sub-module of the project are introduced in detail.

1. Design method of each sub-module Block 1: Register users and log in.

The steps to register for login are completed through two forms. Registration mainly uses file reading methods:

StreamReader reader = new StreamReader("name.txt");

And write the user registration name through the write function:

StreamWriter writer = new StreamWriter("name.txt", true);writer.WriteLine(textBox1.Text);MessageBox.Show("User registration successful, please login! ");writer.Dispose();

The registration is the same.

Block 2: Import pictures.

Import pictures to use

OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog 1.ShowDialog ();//Open the folder and select pictureBox1.Image = Image.FromFile(openFileDialog 1.FileName).GetThumbnailImage(250, 250,new Image.GetThumbnailImageAbort(delegate { return false; }),IntPtr.Zero);//Make the size of the selected picture the desired size

After opening the image file and initial its size, it is convenient for later cutting and user experience.

Block 3: Set the difficulty level you can choose.

Add difficulty settings to combox

comboBox1.Items.Add("1");comboBox1.Items.Add("2");comboBox1.Items.Add("3");comboBox1.Items.Add("4");

Then use switch to correspond to each difficulty

int Diffcult = int.Parse(comboBox1.Text);//initialize array according to difficulty and assign switch (Diffcult) to n { case 1: node = new Node[3, 3]; n = 3; break; case 2: node = new Node[4, 4]; n = 4; break; case 3: node = new Node[5, 5]; n = 5; break; case 4: node = new Node[9, 9]; n = 9; Block 4: Judge whether the puzzle is complete.

By judging the function to determine whether the digital record of the structure of each picture is combined or not

public bool JudgePicture(){ int count = 0; for (int x = 0; x

< n; x++) { for (int y = 0; y < n; y++) { if (this.node[x, y].num != count) return false; count++; } } return true;}板块五:切割图片。// 传入图片;单个图片宽,高;拼图界面的x,y长度public Image Cutpicture(Image fImage,int width,int height,int spacex,int spacey){ // 创建新图片的位图 Bitmap bitmap = new Bitmap(width, height); // 创建绘制区域 Graphics graphics = Graphics.FromImage(bitmap); // 截取所需原图对应区域写入作图区域 graphics.DrawImage(fImage, 0, 0, new Rectangle(spacex, spacey, width, height), GraphicsUnit.Pixel); // 从作图区域生成新图 Image fgImage = Image.FromHbitmap(bitmap.GetHbitmap()); return fgImage; // 返回分割后的图片} 通过创建绘制区域,来截取所需的小图块,然后通过调用切图函数将切后的图片全部传入结构体图片数组中。 板块六:打乱拼图。// 定义随机数用于打乱切后的图Random lx = new Random();Random ly = new Random();int x = lx.Next(0, n);int y = ly.Next(0, n); 再调用交换函数进行来进行两图片交换,进行400000次达到打乱的目的 板块七:重排图片,反映现在拼图的状况。 每次图片的点击移动事件都会进行pictureBox2.Image = BackImage();语句来存储移动后的图片,当点击button3时就会调用这个函数 // 按钮重排后再picturebox3中显示private void button3_Click(object sender, EventArgs e){ // 在pictureBox3中显示被移动后的拼图 pictureBox3.Image = BackImage();} 是的picturebox3的图片显示现在图片的状态 板块八:切成图后的小图片移动。 先读取鼠标点击的位置,用于判断点击的图片位置 int X = e.X / (width / n); // 鼠标x位置整型只能取 1到n-1int Y = e.Y / (width / n); 一共九种情况:图片在中间,四角,四边,分别判断来确定是否移动:如下面是在中间点击图片的运行代码,其他情况类似: if (node[X + 1, Y].num == index_m){ Swap(new Point(X + 1, Y), new Point(X, Y));}else if (node[X - 1, Y].num == index_m){ Swap(new Point(X - 1, Y), new Point(X, Y));}else if (node[X, Y + 1].num == index_m){ Swap(new Point(X, Y + 1), new Point(X, Y));}else if (node[X, Y - 1].num == index_m){ Swap(new Point(X, Y - 1), new Point(X, Y));}2、设计核心技术 用文件流的方法设置登录注册界面,并保存用户名信息 使用GDI+的方法对图片进行切割 打乱图片的时候运用大量随机数 每个图片有自己的对应结构体数字,为了判断是否拼图成功 当所点击图片与空白图片交换时,分九种情况:四角,四边,中间,按情况来写入交换方法 当拼成功时,按照图片对应的结构体数字并调用判断函数判断 三、项目测试 简要介绍采用的测试方法和测试要点。 点击图中按钮后选择一个图片文件,将图片显示到窗口的左边 然后将图片切为指定的数目(如 3x3)的小图片,从生成的小图片中随机选择一张丢弃,将小图片按照一定规律打乱,并将显示到窗口的中间。 用户可以用鼠标点击空白方块周围的图片,被点击的图片移动到原来空白的位置,被点击图片的位置变为空白。 另设登录,注册界面

About "C#jigsaw puzzle example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 285

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report