eptaxia-轻巧的心灵游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/eptaxia-a-lightweight-mind-game-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3708 个词 阅读量 0eptaxia-轻巧的心灵游戏(译文)
原文地址:https://www.codeproject.com/Articles/15353/eptaxia-A-Lightweight-Mind-Game
原文作者:Bilgin Esme
译文由本站 robot-v1.0 翻译
前言
A cute and yet challenging game written in C#. Also hints on graphics, sound, and simple class structures.
一个用C#编写的可爱又具有挑战性的游戏.还提示图形,声音和简单的类结构.
介绍(Introduction)
当我还是一名高中生时,我的一个朋友向我展示了如何玩这款游戏.顺便说一句,我不知道它的真名.从发布到本文的消息中,我得知其真实名称是"骑士之旅".我在那些学年里都玩过,特别是在无聊的课堂上.实际上,您可以简单地用纸和铅笔玩.(When I was a high-school student, one of my friends showed me how to play this game. By the way, I didn’t know its real name. And from a message posted to this article, I learned that its real name is “a Knights Tour”. I played it all those school years, especially in boring classes. In fact, you can play it simply with paper and pencil.) 在学习C#的过程中,我想到了创建此思维游戏程序的想法.(While I was in the process of learning C#, I came up with the idea of creating a program of this mind game.) 首先,我应该承认我只是C#游戏编程的初学者.所以我想分享我在途中学到的东西.我尽力呈现出简洁的代码和简洁的设计,并且我渴望获得各种反馈来改进它.(First of all, I should admit that I’m only a beginner in game programming in C#. So I wanted to share what I’ve learned on the way. I tried my best to present a clean code and clean design, and I’m eager for all kinds of feedback to improve it.) 本文的目标读者是希望提高C#游戏编程技能的初学者或中级程序员.我不认为自己是专家,而是一个乐于助人的程序员,他想分享通过这种方式学到的东西.(The target audience of this article is Beginner or Intermediate level programmers who wish to develop some skills in C# game programming. I do not consider myself as an expert, but a hearty programmer who wants to share what have been learned through the way.) 感谢您的关注.(Thanks for your interest.)
背景(Background)
Eptaxia是一个简单的游戏,但获胜很难.(Eptaxia is a simple game, but winning is hard.) 规则很简单.从网格的任何一个单元开始,然后从上一个单元进行" L"形移动(如国际象棋中的马运动).然后像这样,一个接一个地移动,直到填满所有单元格,或者被卡住.在下图中,您可以看到在第一步之后的可能步伐.(The rules are simple. You start from any cell of the grid, and then you make an “L” shaped move (like the horse movement in chess) from the previous one. And then it goes like this, move after move, until you fill all the cells, or you’re stuck. In the figure below, you see the possible moves after the first move.)
当我开始玩游戏时,我花了数周的练习才能在7x7格中做出49.所有网格尺寸都可以解决.默认情况下,游戏以7x7网格开始.(When I started to play it, it took me weeks of practice to make 49 out of a 7x7 grid. All the grid dimensions are possible to solve. The game starts as a 7x7 grid by default.) 要获得更多帮助,可以参考软件包中的帮助文件.或只是在游戏中按"帮助"按钮.(For further help, you can refer to the Help file in the package; or simply press the Help button in the game.)
使用代码(Using the code)
在此游戏程序中,您可以开发自己的能力,例如:(In this game program, you can develop your abilities such as:)
- 表单处理,从其他表单收集信息(Form handling, gathering information from another form)
- 图形能力,绘图线,矩形和图像(Graphic abilities, drawing lines, rectangles, and images)
- 图形中的双缓冲(Double buffering in graphics)
- 自定义鼠标事件处理(Custom mouse event handling)
- 播放声音(Playing sound) 而且,该游戏程序为图形应用程序提供了干净的类结构.请注意,示例代码是用Visual Studio 2005编写的,并且该演示程序在.NET Framework 2.0中可以正常工作.(Also, this game program enables a clean class structure for graphical applications. Please note that, the sample code is written in Visual Studio 2005, and the demo is functional with .NET Framework 2.0.)
类结构(The Class Structure)
游戏基于三层架构:Windows窗体,(The game is based on a three-layer architecture: Windows Forms, the) EptaxiaGame
class-主机,以及为(class - the main engine, and the classes that serve the) EptaxiaGame
类,如下图所示.(class, as seen in the figure below.)
EptaxiaGame
是游戏的主要引擎.此类的一个实例((is the main engine of the game. An instance of this class () theGame
)是在() is created in) frmMain
. frmMain的事件处理用作玩家与游戏的交互.播放器使用单击按钮进行移动和修改设置.这些动作将传递给(. Event handling of frmMain is used as player-game interaction. The player uses the button clicks for making moves and to modify settings. These actions are passed to the) EptaxiaGame
类.(class.)
所有的绘图都由(All the drawing is done by) EptaxiaGraphics
类.图形控制器(class. The graphics controller) dc
是从(is passed from the) OnPaint
的方法(method of) frmMain
到(to the) ReDraw
的方法(method of) EptaxiaGame
,然后(, and then to) EptaxiaGraphics
.(.)
还有一个子类(Also, there’s a subclass) Grid
.此类仅负责绘制网格和光标.中的绘制方法(. This class is solely responsible for drawing the grid and the cursor. The drawing methods in) ExptaxiaGraphics
都很简单例如,用于绘制网格的代码如下:(are all very simple. For example, the code for drawing the Grid is as follows:)
public void DrawGrid(Graphics dc)
{
Pen BlackPen = new Pen(Color.Black, lineThickness);
for (int i = 0; i < dimX + 1; i++)
{
dc.DrawLine(BlackPen, posX + gridSpace * i, posY,
posX + gridSpace * i, posY + dimY * gridSpace);
}
for (int j = 0; j < dimY + 1; j++)
{
dc.DrawLine(BlackPen, posX, posY + gridSpace * j,
posX + dimX * gridSpace, posY + gridSpace * j);
}
}
EptaxiaMoves
是处理游戏中动作的类.(is the class that handles the moves made in the game.) EptaxiaMessages
处理提供给播放器的消息,以及(handles the messages given to the player, and) EptaxiaSound
处理声音.(handles playing the sound.)
另外,有两个常见的类:(Also, there are two common classes:) EptaxiaLocations
提供网格上位置的向量空间模型,而(provides a vector space model for locations on the grid, while) EptaxiaButtons
处理游戏的动作按钮(如"设置",“新游戏”,“退出"等).(handles the action buttons of the game (as Settings, New Game, Exit, etc.).)
算法(The Algorithm)
游戏规则很简单;您应该填充网格的所有单元格.棘手的部分是"动作"的规则.从网格的任何一个单元开始,然后从上一个单元进行” L"形移动(如国际象棋中的马运动).然后像这样,一个接一个地移动,直到填满所有单元格,或者被卡住.(The rule of the game is simple; you should fill all the cells of the grid. The tricky part is the rules of the “moves”. You start from any cell of the grid, and then you make an “L” shaped move (like the horse movement in chess) from the previous one. And then it goes like this, move after move, until you fill all the cells, or you’re stuck.)
为了了解算法,您可以分析(In order to understand the algorithm, you can analyze the) AddNewMove
方法.它会将您引向适当的方向.有一些方法可以帮助玩家,例如,突出显示了可能的移动位置,但这只是装饰.该算法非常简单.(method. It directs you to the appropriate directions. There are methods that help the player, for example, the possible move locations are highlighted, but these are only cosmetics. The algorithm is really simple.)
public bool AddNewMove(EptaxiaLocation ptMove)
{
if (CheckInCanvas(ptMove)
&& CheckAvailability(ptMove)
&& CheckValidity(ptMove))
{
locations[count] = ptMove;
count++;
return true;
}
else
{
return false;
}
}
对于每次移动,当玩家点击鼠标时,点击位置都会转换为在(For each move, when the player makes a mouse click, the click locations are converted to a vector state defined in) EptaxiaLocations
.检查此位置的三个条件:(. This location is checked for three conditions:)
CheckInCanvas
;它在网格画布中吗?(; Is it in the grid canvas?)CheckAvailability
;是可用的么?如果之前已填写,则无法使用.(; Is it available? If filled before, then it’s not available.)CheckValidity
;这真的是" L"字形的法律举动吗?(; Is it really an “L” shaped legal move?) 如果同时满足所有三个条件,则将新的"移动"添加到(If all three conditions are met, the new “move” is added to the)locations[]
数组.游戏状态由(array. The state of the game is controlled by the)Controller
中的方法(method in the)EptaxiaGame
类.您也可以检查它以进行进一步分析.(class. You can also check it for further analysis.)
兴趣点(Points of Interest)
我喜欢编程.在开始游戏编程之后,我了解到,这比开发客户端服务器程序或任何业务应用程序要困难得多.这只是一个简单的游戏程序,但是我遇到了困难,尤其是在实现按钮和按钮单击引擎方面.还不完美.(I love programming. And after I’ve started game programming, I’ve learned that it’s much harder than developing client-server programs or any business applications. This is just a simple game program, but I had hard times, especially implementing the buttons and button clicking engine. It’s yet not perfect.) 屏幕图形设计的特别注意事项:这是维多利亚时代风格的书饰.我已经在我的Tablet PC上绘制了它.我知道这并不完美.特别是我对图标不满意.(A special note on the screen graphics design: it’s a Victorian style book ornament. I’ve drawn it on my Tablet PC. I know it’s not perfect. Especially I’m not happy with the icons.)
参考文献(References)
多年来,CodeProject一直是我工作的参考库.无数次,我从这里借用了示例代码和方法.因此,现在是我做出贡献的时候了.(For years, CodeProject has been my reference library for my work. Countless times, I’ve borrowed sample codes and methods from here. So it’s my time to contribute, if appropriate.) 对于这项工作,我还使用了一些CodeProject贡献.例如,为了播放声音,我从文章中借用了MeterMan的代码:(For this work also, I’ve used some of the CodeProject contributions. For example, for playing sounds, I’ve borrowed the code of MeterMan, from the article:) 一个简单的类,可让您在C#中玩Wavs(A simple class to allow you to play Wavs in C#) .(.) 另外,对于图形显示的双重缓冲,我使用了Gil_Schmidt在他的文章中的解决方案.(Also, for double buffering of graphics display, I’ve used the solution of Gil_Schmidt from his article;) 不要忽悠!双缓冲!(Don’t Flicker! Double Buffer!) .(.) 感谢那些热爱分享和成长的人们.(Thanks for those who love to share and grow.)
历史与未来发展(History & Future Development)
目前,高分功能不起作用.下一个版本将涵盖此问题.(For now, the high-score feature is not functional. The next version will cover this issue.)
-
2006年8月28日:(08-28-2006:)
- 字体问题已修复(Font problem fixed)
- 另外,表单上的一些小问题(Also, some minor fixes on the forms)
-
2006年8月27日:(08-27-2006:)
- 来源文章(Original article)
结论(Conclusion)
编写Eptaxia的代码对我来说很有趣.如果您觉得它对您有用,我将非常高兴.我的主要目的是分享我在途中学到的东西.(Writing the code of Eptaxia was lots of fun for me. And I’ll be very glad, if you find it somehow useful for you. My main purpose was to share what I learned on the way.) 我想收到有关这项工作的反馈.另外,请从我的网站检查代码更新,(I’d like to receive feedback on this work. Also, check for code update, from my site,) 这里(here) .(.) 谢谢.(Thanks.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET Windows Visual-Studio Dev 新闻 翻译