动态滑动拼图生成器(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/dynamic-sliding-puzzle-generator-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 4 分钟阅读 - 1736 个词 阅读量 0动态滑动拼图生成器(译文)
原文地址:https://www.codeproject.com/Articles/172517/Dynamic-Sliding-Puzzle-Generator
原文作者:Krishnachytanya Ayyagari
译文由本站 robot-v1.0 翻译
前言
A dynamic sliding puzzle generator involving LINQ and photo scaling functionalities.
一个动态滑拼图生成器,涉及LINQ和照片缩放功能.
介绍(Introduction)
根据Wiki,滑动拼图,滑块拼图或滑动瓷砖拼图向玩家挑战,使它们通常沿某些路线(通常在板上)滑动扁平的棋子,以建立某种最终配置.但是在这里,我将尝试一个简单(程序复杂)的滑动拼图原型,玩家可以灵活地根据自己选择的图像生成拼图.(As per Wiki, a sliding puzzle, sliding block puzzle, or sliding tile puzzle challenges a player to slide usually flat pieces along certain routes (usually on a board) to establish a certain end-configuration. But here I would go for a simple (programmatically complex) prototype of sliding puzzle where the players have the flexibility to generate a puzzle from an image of their choice.)
背景(Background)
最近,我一直在尝试各种实现滑动拼图的方法(实际上,甚至还有很多方法).这个版本的游戏非常简单而且非常(Of late, I have been going through various ways of implementing the sliding puzzle (in fact, there are numerous ways even). This version of game here is pretty simple and very)动态(dynamic)在自然界.此处的每一行都以用户完全灵活的方式设置图片的各个部分的大小以及每行的单元数.(in nature. Each and every line here is written in such a way that the user has complete flexibility to set the size of the individual pieces of a picture as well as the number of cells per row.) 实施游戏的主要功能包括:(The key features in implementing the game are:)
- 在行数和每个单元格的大小方面极为动态.(Extremely dynamic with respect to the number of rows and the size of each individual cell.)
- 动态加载图像.(Loads image pieces dynamically.)
- 将图像缩放到拼图的长度.(Scales images to the length of the puzzle.)
- 使用事件来实现逻辑.(Uses events to implement the logic.)
使用代码(Using the code)
这里的逻辑是动态构造图像并将其添加到Windows窗体.功能(The logic here is to construct and add images dynamically to the Windows form. The function) AssignEventsToButtons()
做到这一点的核心功能.(does the core functionality of this.)
private void AssignEventsToButtons()
{
PictureBox temp = null;
int x = -1;
for (int i = 0; i <= MaxSeed; i++)
{
temp = new PictureBox
{
TabIndex = ++x,
SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage,
Size = new System.Drawing.Size(SideLength, SideLength),
Name = string.Concat("pictureBox", x.ToString()),
AllowDrop = true,
Location = new Point(0, 0)
};
temp.MouseDown += new MouseEventHandler(button_MouseDown);
temp.DragDrop += new DragEventHandler(button_DragDrop);
temp.DragEnter += new DragEventHandler(button_DragEnter);
temp.CreateControl();
flowLayoutPanel1.Controls.Add(temp);
}
flowLayoutPanel1.Controls.Add(this.groupBox1);
flowLayoutPanel1.PerformLayout();
}
处理鼠标事件在这里至关重要.每个块都必须处理所有三个事件,即(Handling mouse events is crucial here. Every piece of block has to be handling all the three events, namely) MouseDown
,(,) DragDrop
和(, and) DragEnter
.最初考虑将一块移到空白空间,处理当前块的鼠标按下事件以将图片向前发送.(. Initially consider a piece to be moved to white space, the mouse down event of the current piece is handled to send the picture forward.)
private void button_MouseDown(object sender, MouseEventArgs e)
{
Cursor = Cursors.Hand;
_dragSource = (PictureBox)sender;
_dragSource.DoDragDrop(_dragSource.Image,
DragDropEffects.Copy | DragDropEffects.Move);
}
事件(The event) DragEnter
基本上是为了处理移动图片所需的效果.(is basically to handle the desired effect of moving the picture.)
private void button_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(DataFormats.Bitmap) ?
DragDropEffects.Move : DragDropEffects.None;
}
游戏的逻辑部分是检查有效举动.存储拖动部件的信息,并与目标部件的信息进行比较,步骤如下:(The logic part of the game is to check for a valid move. The dragging piece’s information is stored and is compared with the destination piece’s, with the following steps:)
- 目标位置应为可移动的有效位置(不可交叉移动).(Destination place should be a valid position to move (no cross movements).)
- 图像不应允许自己拖放.(An image should not allow drag and drop on itself.)
- 检查完成后,将交换图像.(Once checks are done, the images are swapped.)
private void button_DragDrop(object sender , DragEventArgs e)
{
var dest = (PictureBox) sender;
if(dest.Image.Equals(_dragSource.Image))
return;
if( !new List<int>
{
_dragSource.TabIndex + 1,
_dragSource.TabIndex - 1,
_dragSource.TabIndex + RowCount,
_dragSource.TabIndex - RowCount
}.Contains(dest.TabIndex))
{
MessageBox.Show(Resources.DynPuzzleform_button_DragDrop_Invalid_move);
return;
}
if (!ImageCompareArray(dest.Image as Bitmap,Resources._17))
{
MessageBox.Show(Resources.DynPuzzleform_button_DragDrop_Invalid_move);
return;
}
_dragSource.Image = dest.Image;
dest.Image = e.Data.GetData(DataFormats.Bitmap) as Image;
//checks state
CheckState(_dragSource.TabIndex, dest.TabIndex);
}
这里的功能(Here the function) CheckState
检查每一步的游戏状态,并使用(checks for the state of the game on every step, and is checked with) _successState
以便确认游戏的完成.当您按F3时,可以看到此游戏中另一个有趣的功能.是的,您可以加载您选择的图像.图像会按比例缩小到播放器配置的大小,而不会失真.的(so as to confirm the game’s completion. Another interesting feature in this game can be seen when you press F3. Yes, you can load an image of your choice. The image is scaled down to the size the player configured, without distortion. The) ResizeImage
函数为您完成此操作.(function does this for you.)
注意:检查设置(Note: Check for the settings) SideLength
和(and) RowCount
在(in)DynSlidingPuzzle.exe.config(DynSlidingPuzzle.exe.config).请为此指定有效值.(. Please assign valid values for the same.)
结论(Conclusion)
这样就完成了一个简单而又动态的游戏应用程序的制作,该应用程序演示了.NET的易用性.请仔细阅读代码,让我知道您的评论.提前致谢.(This concludes the making of a simple yet dynamic game application that demonstrates the ease of using .NET. Please go through the code and let me know your comments. Thanks in advance.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C#3.5 .NET .NET3.5 LINQ Dev 新闻 翻译