游戏开发框架(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/the-game-development-framework-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2757 个词 阅读量 0游戏开发框架(译文)
原文地址:https://www.codeproject.com/Articles/82769/The-Game-Development-Framework
原文作者:CaptainSeeSharp
译文由本站 robot-v1.0 翻译
前言
Simple Game Dev Fx in C# (Maps, Graphics, Path-finding AI)
C#中的简单游戏开发Fx(地图,图形,寻路AI)
- 下载源426.78 KB(Download source - 426.78 KB)
- 下载示例程序-82.8 KB(Download sample program - 82.8 KB) 与VS 2008解决方案相比,所有代码均已完整记录,并带有XML注释,以实现智能感知.详细描述了所有变量,方法和类型.(VS 2008 Solution, all code is thoroughly documented with XML comments for intellisense. All variables, methods, and types are described in detail.)
介绍(Introduction)
游戏开发框架((The Game Development Framework () GameFX
)只是一组库,可以用作任何基于2D瓦片的简单游戏的基础.它可以用来构建诸如PacMan,Snake,Tetris甚至Chess之类的游戏.() is simply a set of libraries to be used as the foundation for any simple 2D tile-based game. It can be used to build games such as PacMan, Snake, Tetris, and even Chess.)
点击这里(Click here) 如果您想阅读完整的教程,我们将逐步进行该项目的开发.我们将从这个想法开始,然后,我们将在设计程序的过程中,仔细考虑所有程序员必须考虑的所有重要细节.最后,我们将敲定所有必需的代码以使其起作用.(if you wish to review a complete tutorial where we will go through the step-by-step development of this project. We will start with the idea, after that we will go through the process of designing the program, thinking through all of the important details that all programmers must think about. Finally, we will hammer out all of the necessary code to make it work.)
背景(Background)
构建和重用框架大大简化了实际开发游戏的任务.您不必考虑所有的贴图,图形和AI繁琐的工作,因为它们已被分离和封装.(Building and reusing a framework greatly simplifies the task of actually developing the game. You don’t have to think about all of the tedium of mapping, graphics, and AI because it has been separated and encapsulated.) 这就像建立一个带有按钮,旋钮和开关的盒子一样.我们将构建一个盒子,其中包含处理地图,绘制地图图形和查找路径的逻辑的所有机制.在该框的外部,我们将使用假设的按钮,开关和旋钮来操纵地图,探路者和图形引擎.这样做可以使它变得更加简单易用.如果我们不这样做,我们将为我们开发的每个游戏在其中进行修补.(It is like building a box with buttons, knobs, and switches. We are going build a box that has all of the mechanisms inside that deal with the logic of handling maps, drawing map graphics, and finding paths. On the outside of that box, we will have the hypothetical buttons, switches, and knobs to manipulate the maps, pathfinder, and graphics engine. Doing it that way makes it much simpler and easier to use. If we didn’t do it, we would be tinkering around inside of it for every game that we build.)
使用代码(Using the Code)
GameFX将处理基于图块的地图,图形和寻路,因此我们可以将GameFX分为这三个主要类别.(GameFX will handle tile-based maps, graphics, and pathfinding, so we can divide GameFX into those three main categories.) 让我们从图块映射开始.贴图映射的最高功能单元当然是TileMap. TileMap的基本单位是使用坐标的Tile.(Let’s start with tile-mapping. The highest-level functioning unit of tile-mapping is of course the TileMap. The basic unit of a TileMap is a Tile which uses Coordinates.) 好的,我们已经将第一类(平铺贴图)分解为三个功能组件,可以将其组合在一起以执行平铺贴图的功能.(Good, we have broken down the first category (tile-mapping) into three functional components that can be put together to perform the functions of tile-mapping.)
-
瓦(Tile)((()平铺地图的基本单位(the basic unit of a tile map))())
-
瓷砖地图(TileMap)((()可在以下位置操作的图块的2D地图(a 2D map of Tiles that can be operated on))())
-
坐标(Coordinate)((()一个简单的数据类型,表示TileMap上的X,Y值(a simple data type that represents X,Y values on a TileMap))()) 这里有图形引擎.(Here we have the Graphics Engine.)
-
图形引擎(GraphicsEngine)((()以图形方式渲染TileMap的机制(the mechanism that graphically renders a TileMap))()) 我们可以将寻路类别分为这三个部分.(We can break down the pathfinding category into these three components.)
-
节点(Node)((()一种有趣的机制,用于查找从一个坐标到另一个坐标的路径(an interesting mechanism used in finding a path from one coordinate to another))())
-
节点堆(NodeHeap)((()排序存储机制,这是A 寻路算法的关键(a sorted storage mechanism that is key to the A pathfinding algorithm))())
-
SimplePathFinder(SimplePathFinder)((()在TileMap上找到从一个坐标到另一个坐标的路径的机制(the mechanism that finds the path from one coordinate to another on a TileMap))()) 这是最简单的工作示例(Here is the simplest working example of)
GameFX
.该代码仅创建了一个类型为Tiles的10x10地图(. The code simply creates a 10x10 map of Tiles of type)Byte
. 0表示地板,1表示路径,2表示墙.图形引擎会将地图绘制到(. 0 represents the floor, 1 represents the path, and 2 represents a wall. The Graphics Engine will draw the map to)Form1
,对地图的所有更改都会实时显示.地图中间有一堵墙.探路者将找到从左上角到右下角的路径.(, all changes to the map will be rendered in real-time. There is a wall in the middle of the map. The Pathfinder will find the path from the upper left corner to the bottom right corner.)
using HenizeSoftware.GameFX.PathFinding;
using HenizeSoftware.GameFX.TileMap;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
TileMap<byte> myMap;
GraphicsEngine<byte> gfx;
SimplePathFinder<byte> path;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myMap = new TileMap<byte>(10, 10);
gfx = new GraphicsEngine<byte>(myMap, this.CreateGraphics(),
10, 10, RenderMode.OnChanged);
path = new SimplePathFinder<byte>(myMap);
path.ResistanceDictionary.Add(2, 255);
path.Start = new Coordinate(0, 0);
path.End = new Coordinate(9, 9);
gfx.ColorDictionary.Add(0, Color.Black);
gfx.ColorDictionary.Add(1, Color.Pink);
gfx.ColorDictionary.Add(2, Color.White);
gfx.DrawAllTiles();
for (ushort i = 8; i > 0; i--) //draw wall down the middle
myMap[4, i].Value = 2;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if(gfx != null)
gfx.DrawAllTiles();
}
private void button2_Click(object sender, EventArgs e)
{
foreach (Coordinate c in path.GetFullPath())
myMap[c].Value = 1;
}
}
}
兴趣点(Points of Interest)
我尚未发现的代码中可能存在一些小错误.但是,我开发了一个测试程序,该程序可让您创建任何大小的地图,并在计算机上将尽可能多的蛇掉入地图中.蛇在地图上跑来跑去争夺食物.您可以在地图运行时在其上绘制.如果选择绘制图形,它还允许您使用位图图形.速度也可以调节.(There may be some minor bugs in the code that I have not yet discovered. However I have developed a test program that allows you to create maps of any size, and drop as many snakes as your computer can handle onto the map. The snakes run around on the map in competition for food. You can draw on the map while it’s running. It also allows you to use bitmapped graphics if you choose to draw them. The speed can also be adjusted.) 首先,您必须先单击"新建地图",然后才能执行任何操作.您也无法删除最后一条蛇.(First you must click New Map before you can do anything. You also cannot remove the last snake.) 该代码是很容易解释的,但是我将介绍示例程序的核心,该部分将其全部打勾.(The code is pretty self explanatory, however I will go over the core of the sample program, the part that makes it all tick.)
//Each snake has its own pathfinder. For each snake on the map,
//find the path to the food and increment the snake toward it.
private void timer_Tick(object sender, EventArgs e)
{
//Go through all of the snakes that are currently on the map
for (int i = 0; i < snakeList.Count; i++)
{
//If the snake being processed has eaten the food then find a
//new place to drop food, and then tell all the other snakes
//the new location of the food.
if (snakeList[i].HeadCoordinate == pathFinderList[i].End)
{
//Drop new food on clear area of map
while (true)
{
food = new Coordinate((ushort)rnd.Next(0, map.Width - 1),
(ushort)rnd.Next(0, map.Height - 1));
if (map[food].Value == SLThTile.Floor) break;
}
//Tell the other snakes the new location of the food.
foreach(SimplePathFinder<SLThTile> p in pathFinderList)
p.End = food;
map[food].Value = SLThTile.Food;
if (btnAutoGrow.Checked == true)
snakeList[i].Grow(1);
}
Coordinate next = new Coordinate(); //default 0,0 incase Snake is trapped.
//Try to find the path to the food.
try
{
next = pathFinderList[i].GetNextMove();
}
catch (PathNotFoundException)
{
snakeList[i].Reverse(); //if no path found then make the snake reverse.
}
//Figure out the general direction the snake needs to go.
if (next.X == snakeList[i].HeadCoordinate.X &&
next.Y < snakeList[i].HeadCoordinate.Y)
snakeList[i].Direction = SnakeDirection.Up;
else if (next.X == snakeList[i].HeadCoordinate.X &&
next.Y > snakeList[i].HeadCoordinate.Y)
snakeList[i].Direction = SnakeDirection.Down;
else if (next.X > snakeList[i].HeadCoordinate.X &&
next.Y == snakeList[i].HeadCoordinate.Y)
snakeList[i].Direction = SnakeDirection.Right;
else if (next.X < snakeList[i].HeadCoordinate.X &&
next.Y == snakeList[i].HeadCoordinate.Y)
snakeList[i].Direction = SnakeDirection.Left;
snakeList[i].IncrementForward();
pathFinderList[i].Start = snakeList[i].HeadCoordinate;
}
的能力(The capabilities of) GameFX
远远超出了此处显示的示例.位图图形和许多配置可通过简单地初始化和配置属性来获得.不要害怕检查出来,它可能对您的一个项目有用.(far exceed the example shown here. Bitmapped graphics, and many configurations are available by simply initiating and configuring properties. Don’t be afraid to check it out, it might be useful for one of your projects.)
如果您需要更详细的文章,(If you would like a more detailed article,) 点击这里(click here) .(.)
这是一个类图(Here is a class diagram for) GameFX
.(.)
## 历史(History)
- 2010年5月20日,星期四:首次公开发布(Thursday, May 20, 2010: First public release)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 Win2K WinXP Win2003 Vista .NET2.0 Dev Design Architect 新闻 翻译