西部世界-第4部分(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/westworld-part-4-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 7 分钟阅读 - 3083 个词 阅读量 0西部世界-第4部分(译文)
原文地址:https://www.codeproject.com/Articles/1011185/WestWorld-Part-4
原文作者:Terence Wallace
译文由本站 robot-v1.0 翻译
前言
A demonstration of Finite State Machines (FSM) where agents inhabit an Old West style gold mining town called West World.
有限状态机(FSM)的演示,代理商在其中居住于一个古老的西式金矿开采镇,名为"西部世界".
- 下载源代码(RogueSharp.Dll)-63.5 KB(Download source code (RogueSharp.Dll) - 63.5 KB)
- 下载源代码(WestWorld.exe)-103.1 KB(Download source code (WestWorld.exe) - 103.1 KB)
注意:(Note:)这是CodeProject文章系列的第4部分,标题为(This is Part 4 of a CodeProject Article Series entitled)
WestWorld
.要获取本文和第1-3部分的更多背景信息,请点击以下链接(. To get more background information for this article and for Parts 1-3, follow this link for)西部世界(WestWorld).(.)
介绍(Introduction)
如何创建利用有限状态机的代理的实际示例.在本文中,我将尝试解释如何使用WinForms和GDI +实现FSM代码.我们将要研究一个游戏环境,在该环境中,特工居住在一个名为West World的古老西式金矿开采镇.西部世界有两个居民-一个名叫Miner Bob的金矿工和他的妻子Amanda.(A practical example of how to create agents that utilize finite state machines. In this article, I will attempt to explain how to implement FSM code using WinForms and GDI+. We are going to look at a game environment where agents inhabit an Old West-style gold mining town named West World. There are two inhabitants of WestWorld — a gold miner named Miner Bob and his Wife, Amanda.)
WestWorld1
-FSM基础(- Basics of FSM)WestWorld2
-有两名特工的FSM(矿工和妻子)(- FSM with two agents (Miner & Wife))WestWorld3
-包括FSM和代理之间的消息传递(- Includes FSM with Messaging between Agents)WestWorld4
((()本文(this article))-使用RogueSharp引入WindowsForms GDI +() - Introduces WindowsForms GDI+ using RogueSharp)
背景(Background)
本教程的目的是向您展示如何从头开始创建简单的游戏,而无需使用诸如XNA或DirectX之类的高级API来帮助您自动完成一半的过程.我们将使用的是Windows Form和GDI +进行一些基本的绘制,并为一些额外的便利提供一些Form的事件.(The aim of this tutorial is to show you how to create a simple game, from scratch, without the help of higher level APIs like XNA or DirectX which automate half the process for you. All we will be using is a Windows Form and GDI+ to do some basic drawing, and for a little extra convenience, a few of the Form’s events.)
地图生成(Map Generation)
WestWorld
使用修改后的VB.NET版本(uses a modified VB.NET version of) 盗贼(RogueSharp) .地图生成会生成随机创建的唯一地图.您的地图可能与显示的地图有所不同.(. Map Generation generates a randomly created unique map. Your map may appear different than the map shown.)
RogueSharp是一个用C#编写的免费库,可帮助流氓类开发人员抢先开发他们的游戏. RogueSharp提供了许多实用程序功能来处理地图生成,视场计算,路径查找,随机数生成等.它还包含制作房间和类似洞穴的地图的策略.(RogueSharp is a free library written in C# to help roguelike developers get a head start on their game. RogueSharp provides many utility functions for dealing with map generation, field-of-view calculations, path finding, random number generation and more. It also contains strategies for making room and cave-like maps.)
与RogueSharp的大多数互动都是基于"(Most interactions with RogueSharp is based around the concept of a) Map
这是一个矩形网格(which is a rectangular grid of) Cells
.(.)
每(Each) Cell
在一个(in a) Map
具有以下属性:(has the following properties:)
-
IsTransparent
:(:)true
如果能见度延伸到(if visibility extends through the)Cell
-
IsWalkable
:(:)true
如果一个(if a the)Cell
可能被玩家遍历(may be traversed by the player) -
IsExplored
:(:)true
如果玩家曾经视线(if the player has ever had line-of-sight to the)Cell
-
IsInFov
:(:)true
如果(if the)Cell
当前处于玩家视野内(is currently in the player’s field-of-view) 实例化一个新的(To instantiate a new)Map
,您可以使用其宽度和高度的构造函数,并使用所有的属性创建这些尺寸的新地图(, you can use its constructor which takes a width and height and will create a new map of those dimensions with the properties of all)Cells
设置(set to)false
.(.) 请注意(Notice that the)ToString()
运算符在(operator is overridden on the)Map
类,以提供地图的简单视觉表示.可选的(class to provide a simple visual representation of the map. An optional)bool
可以提供给参数(parameter can be provided to)ToString()
指示您是否要使用视野.如果未提供参数,则默认为(to indicate if you want to use the field-of-view or not. If the parameter is not given, it defaults to)false
.(.) 使用的符号如下:(The symbols used are as follows:) -
%
:(:)Cell
不在视野范围内(is not in field-of-view) -
.
:(:)Cell
透明,可步行且在视野内(is transparent, walkable, and in field-of-view) -
s
:(:)Cell
可步行且视野范围内(但不透明)(is walkable and in field-of-view (but not transparent)) -
o
:(:)Cell
是透明的并且在视野内(但不可步行)(is transparent and in field-of-view (but not walkable)) -
#
:(:)Cell
在视野内(但不透明或不可走动)(is in field-of-view (but not transparent or walkable)) 创建地图的一种更有趣的方法是使用(A more interesting way to create a map is to use the)Map
类'(class')static
方法(method)Create
这需要一个(which takes an)IMapCreationStrategy
.一些简单的类实现(. Some simple classes implementing)IMapCreationStrategy
随RogueSharp一起提供,但是可以通过创建自己的实现该策略的类轻松地对其进行扩展.(are provided with RogueSharp but this is easily extended by creating your own class that implements the strategy.) 对于RogueSharp的原始版本,可以从以下位置获取NuGet软件包:(For the original version of RogueSharp, a NuGet package is available at) https://www.nuget.org/packages/RogueSharp(https://www.nuget.org/packages/RogueSharp) .(.)
精灵渲染(Sprite Rendering)
制作动画(To animate) WestWorld
,我们只需绘制图像,然后移至下一帧,然后绘制序列中的下一幅图像.因此,我们需要跟踪动画的帧,动画中的帧数,单个子画面的宽度和高度以及旋转方向.为了动画化我们的图像,我们在(, we simply draw an image, and then move to the next frame, then we draw the next image in the sequence. So, we need to keep track of what frame of the animation we’re on, how many frames are in the animation, the width and height of an individual sprite, and the direction in which to turn. To animate our images, we draw the image at) (0, 32)
,然后在下一帧上绘制图像(, and then on the next frame, draw the image at) (33, 65)
,然后重复.(, and repeat.)
Dim sizeOfSprites As Integer = 32
Dim scale As Double = 0.5F
Dim rectPosition As New System.Drawing.Rectangle_
(New Point(X * sizeOfSprites, Y * sizeOfSprites)...
buffer.DrawImage(_Image, rectPosition, imageX, imageY, _Width, _Height, GraphicsUnit.Pixel)
图像太大,无法显示在屏幕上.因此,让我们将所有子图缩小到1/2大小,即16 x 16像素.使用(The image is too large to fit on the screen. Therefore, let’s just scale all of our sprites down to 1/2 size, 16 x 16 pixels. Using the) DrawImage
上的方法(method on the) Bitmap
class允许我们根据子图形的当前位置设置要绘制的子图形的比例.(class allows us to set the scale of the sprite to draw based upon its current position.)
地图图(Map Drawing)
要实际查看我们的地图,我们需要向其中添加一些代码(To actually see our map, we’re going to need to add some code to the) Draw()
方法中(method in)World.vb(World.vb).我们设置一个变量,该变量与用于地板和墙单元的精灵大小相对应.如果使用提供的图形,则这些图形为64像素正方形.该值将用于计算绘制每个精灵的位置.接下来,我们称(. We set a variable corresponding to the size of the sprites we are using for our floor and wall Cells. If you used the graphics provided, these are 64 pixels square. This value will be used to calculate where to draw each sprite. Next, we call the) GetAllCells
()对我们新创建的方法(() method on our newly created) _map
.这返回一个(. This returns an) IEnumerable
我们可以使用进行迭代的地图中所有单元的( of all Cells in the map that we can iterate through using a) For Each
循环.当我们遍历每个(loop. As we iterate through each) Cell
,我们检查(, we check the) IsWalkable
属性.这将决定我们是否应该铺地板.我们通过查看相应单元格的位置来计算绘制精灵的位置(property. This will determine if we should draw a floor. We calculate the position of where we should draw the sprite by looking at the corresponding Cell’s) X
和(and) Y
属性并将其乘以我们设置的变量.这是我们实际致电给(properties and multiplying it by the variable we set. This is where we make the actual calls to the) Draw
我们的方法(method on our) Bitmap
buffer
变量,并提供地板或墙壁纹理以及我们之前计算的位置.(variable and provide either the floor or wall texture and the position we calculated earlier.)
Public Sub Draw(ByVal g As Graphics)
Dim sizeOfSprites As Integer = 64
Dim scale As Double = 0.5F
Dim multiplier As Integer = CInt(sizeOfSprites * scale)
Dim myBrush As Brush = New SolidBrush(Color.Yellow)
For Each Cell As Cell In _map.GetAllCells
Dim Text As String = String.Format("X:{0}, Y:{1}", Cell.X, Cell.Y)
If Cell.IsWalkable And Cell.IsTransparent Then
Dim position As New System.Drawing.Point(CInt(Cell.X * multiplier), CInt(Cell.Y * multiplier))
g.DrawImage(_Grass, position)
Else
Dim position As New System.Drawing.Point(CInt(Cell.X * multiplier), CInt(Cell.Y * multiplier))
g.DrawImage(_Wall, position)
End If
Next
历史(History)
学分(Credits)
- 盗贼(RogueSharp)
- 安东尼`穆什罗(Anthony Mushrow)
- Telerik代码转换器(Telerik Code Converter)
- 开发人员融合(DeveloperFusion)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VB12.0 .NET Win7 .NET4.5 Windows VS2013 GDI+ Dev 新闻 翻译