XNA基础(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/xna-basics-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 7 分钟阅读 - 3185 个词 阅读量 0XNA基础(译文)
原文地址:https://www.codeproject.com/Articles/670232/XNA-Basics
原文作者:Patchrick
译文由本站 robot-v1.0 翻译
前言
Learn the basics of game development in C# and XNA
了解C#和XNA游戏开发的基础知识
介绍(Introduction)
在本文中,您将学习如何使用C#作为编程语言来制作简单的2D XNA游戏.本教程的PC版本.(In this article, you will learn how to a simple 2D XNA game using C# as the programming language. A PC version for this tutorial.)
要求(Requirements)
如果您运行的是Windows 7(If you’re running Windows 7)
-
Microsoft Visual Studio 2010或Visual C#Express 2010((Microsoft Visual Studio 2010 or Visual C# Express 2010 () 链接(link) )())
-
Microsoft XNA Game Studio 4.0刷新((Microsoft XNA Game Studio 4.0 Refresh () 链接(link) )()) 如果您运行的是Windows 8或Windows 8.1(If you’re running Windows 8 or Windows 8.1)
-
Microsoft Visual Studio 2010或Visual C#Express 2010((Microsoft Visual Studio 2010 or Visual C# Express 2010 () 链接(link) )())
-
Windows Marketplace客户端游戏((Games for Windows Marketplace Client () 链接(link) )())
-
Microsoft XNA Game Studio 4.0刷新((Microsoft XNA Game Studio 4.0 Refresh () 链接(link) )())
概念(Concept)
一个游戏要运作,需要4种基本方法.(A game, to work, requires 4 basic methods.)
初始化();(Initialize();) LoadContent();(LoadContent();) Update();(Update();) 画();(Draw();)
初始化启动时所需的所有对象,变量和管理器.(Initializes all objects, variables and managers required at start up.) 预加载图像,声音,字体等并分配变量.(Pre-load images, sounds, fonts, etc. and assign variables.) 更新游戏LOGIC.(Updates the game LOGIC.) 在屏幕上绘制图像.(Draw the images on screen.)
游戏(Game)
游戏无限运行并由TIMER指定.(Game runs infinitely and specified by a TIMER.)
计时器测量一个周期中Update()和Draw()执行的时间.(Timer measures the time Update() and Draw() execute in a cycle.)
然后,我们对它的处理时间进行计时,并限制它以达到处理器的最大潜力,从而使游戏高效运行.(We then TIME it how much it processes and LIMIT that to reach the maximum potential of our processors thus making the game run efficiently.)
我们以纳米时间来衡量一个过程,并以每秒帧数(FPS)来限制它.(We measure a process by nano time and limit it by frames per second (FPS).)
Game.TargetElapsedTime = (60 / 1000); // Sample 60 FPS
此代码将以每秒60帧的速度运行游戏(This code will run the game 60 Frames Per Second)
游戏状态(Game State)
每个程序的状态为正在发生什么.(Every program has a status of WHAT is currently happening.)
- 介绍(Intro)
- 游戏菜单(Game Menu)
- 载入中(Loading)
- 赌博(Gaming)
- 游戏结束(Game Over)
程序必须知道屏幕上正在发生什么(当前状态).(The program must know what is going on the screen (current state).) 样例代码(sample code)
if(gameState == "Main Menu")
{
runMainMenu();
}
if(gameState == "In Game")
{
runGame();
}
if(gameState == "Settings")
{
configureSettings();
}
像"主菜单"一样,它等待用户输入用户想要在菜单中执行的操作.如果用户想玩游戏,则它现在进入"游戏中"状态,然后更新()和Draw()s玩游戏所需的特定资源.(Like the “Main Menu”, it waits for user input as to what the user wanted to do in the menu. If the user wanted to play a game, it now go to the state “In Game” then Update()s and Draw()s the specific resources required to play the game.) 这包括游戏逻辑,图像绘制,声音播放以及游戏要求的内容.(That includes game logic, drawing of images, playing of sounds and what ever the game requires it to.)
XNA游戏工作室(XNA Game Studio)
XNA Game Studio 4.0 Refresh是一种编程环境,使您可以使用Visual Studio创建适用于Windows Phone,Xbox 360和Windows的游戏.(XNA Game Studio 4.0 Refresh is a programming environment that allows you to use Visual Studio to create games for Windows Phone, Xbox 360, and Windows.)
XNA Game Studio包括XNA Framework,这是一组基于Microsoft .NET Framework设计用于游戏开发的托管库.(XNA Game Studio includes the XNA Framework, a set of managed libraries designed for game development based on the Microsoft .NET Framework.)
XNA Framework旨在遵循.NET Framework设计模式和习惯用法.借助XNA Game Studio,您可以使用XNA Framework和更通用的.NET Framework的功能进行游戏开发.(The XNA Framework is designed to follow .NET Framework design patterns and idioms. With XNA Game Studio, you can use the capabilities of both the XNA Framework and the more general .NET Framework for game development.)
** MSDN(MSDN) **
创建一个项目(Creating a Project)
当您创建XNA项目时. (Windows游戏)(When you create an XNA project. (Windows Game))
自动添加了2个项目(2 projects was added automatically)
- 游戏项目(The Game project)
- 内容项目(The Content project)
XNA自动管理添加到游戏中的内容.这些是作为"纹理"处理的文件.(XNA automatically manages Content added to the game. These are the files processed as Texture.)
我们看到了想要看到的东西,但我们并不关心程序如何执行.(We see what we want to see but we don’t care about how the program does it.) 让我们将该项目命名为(Let us name that project as)HelloXNA(HelloXNA)
步骤1:HelloXNA(Step 1 : HelloXNA)
我会告诉你如何(I will show you how to)第一(FIRST)在屏幕上显示图像.(display an image on the screen.)
将任何图像添加到Content项目,我的图像是" omnom.png".(Add any image to the Content project, my image is “omnom.png”.)
我使用的omnom.png图片(The omnom.png image I used)
步骤2:编码(Step 2: Coding)
全局声明(必须在任何方法之外声明)(Declare globally (must be declared outside any methods))
Texture2D omnom;
Vector2 position;
下一个(Next)
omnom = Content.Load<Texture2D>(“omnom”);
position = new Vector2(100f, 100f);
放进去(Put this inside)
protected override void LoadContent()
- 将omnom图像分配给Texture2D" omnom"(Assign omnom image to Texture2D “omnom”)
- 将任何Vector 2D位置分配给" position"(Assign any Vector 2D position to “position”)
程序员的观点(A programmer’s perspective)
程序员可以看到这样的坐标平面.(Programmers see the coordinate plane like this.) 原点在屏幕的最左上角(The point of origin is at the upper left most corner of the screen)
并且如上所述,我们主要使用坐标平面的第四象限.(And as described, we use the 4th quadrant of the coordinate plane mostly.) 我们将屏幕内的点定义为2D位置的Vector2,即XY坐标平面(We define points inside the screen as Vector2 for 2D position which is the XY coordinate plane)
在XNA中,我们将其称为视口,它包含(In XNA we call this the Viewport, it contains)
- 缓冲区高度(BufferHeight)
- 缓冲区宽度(BufferWidth)
步骤2继续(Step 2 continuation)
我们使用XNA的SpriteBatch绘制图像,管理在屏幕上绘制的图形.(We draw the image using XNA’s SpriteBatch, manages the graphics drawn on screen.)
spriteBatch.Begin();
spriteBatch.Draw(omnom, position, Color.White);
spriteBatch.End();
放进去(Put this inside)
protected override void Draw(GameTime gameTime)
它首先初始化精灵批处理开始绘制,绘制处理后的图像,然后刷新精灵批处理以恢复游戏的状态.(It first initialize the sprite batch to begin drawing, draws the processed image then flushes the sprite batch to restore the Game’s state.)
步骤3:运行游戏(Step 3: Run the game)
完成所有编码后,尝试运行游戏.您会在屏幕上看到绘制的图像.(After all coding try running the game. You’ll see the image drawn on screen.)
步骤4:运动(Step 4: Movement)
现在,让Omnom行动!(Now let’s make Omnom move!)
全局声明(在所有其他方法之外声明)(Declare globally (declare outside all other methods))
KeyboardState currentState;
下一个(Next) 现在,我们使用键盘的光标键更新了简单移动的逻辑.(We now update the logic of simple movement using the cursor keys of our keyboard.)
然后,我们从键盘的当前状态分配currentState(We then assign the currentState from the Keyboard’s current state)
currentState = Keyboard.GetState();
将此代码放入(Put this code inside)
protected override void Update(GameTime gameTime)
确保currentState在我们的游戏逻辑之上,以确保在执行任何操作之前先捕获用户输入.(Make sure currentState is on top of our game logic to ensure that user input is first captured before doing anything.) 下一个(Next) 现在,我们在点击键盘的光标键时会修改omnom的位置.(We now modify the position of omnom as we tap the cursor keys of our keyboard.)
protected override void Update(GameTime gameTime)
if(currentState.IsKeyDown(Keys.Up))
{ position.Y -= 5; }
if(currentState.IsKeyDown(Keys.Down))
{ position.Y += 5; }
if(currentState.IsKeyDown(Keys.Left))
{ position.X -= 5; }
if(currentState.IsKeyDown(Keys.Right))
{ position.X += 5; }
要向上移动,我们向Y减去一个数字以向上移动;(To move up we subtract a number to Y to move it UP;) 加到Y向下移动;(Add to Y to move DOWN;) 将数字减去X即可向左移动;(Subtract a number to X to move LEFT;) 并添加到X向右移动.(And add to X to move RIGHT.)
尝试运行游戏,看看Omnom的动作.(Try running the game and see how Omnom moves.)
现在,您已了解2D游戏编程的基础知识.尝试创建一个简单的游戏并将其发布在评论中.另外,如果您有任何疑问,请随时提问. =](Now you know the basics of 2D game programming. Try creating a simple game and post it in the comments. Also if you have questions, feel free to ask. =])
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# XNA 新闻 翻译