百度首页 | 百度空间
 
查看文章
 
第一次翻译一个简单的教程,Your First Game: Microsoft XNA Game Studio in 2D
2008-02-25 17:01
This article leads you through the steps necessary to create a simple sprite-based game by using XNA Game Studio.
The Complete Sample
这篇文章介绍了如何使用XNA工具,通过以下几步创建一个简单的游戏,

Step 1: Install Your Software
Step 2: Create a New Project
Step 3: View the Code
Step 4: Add a Sprite
Step 5: Make the Sprite Move and Bounce
Step 6: Explore!
1,安装软件,
2,创建一个项目
3,查看生成的代码
4,添加精灵
5,让精灵动起来,遇到边界自动反弹
6,运行游戏

The Complete Sample
The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.
完整的例子
文章中的代码实例讲解了这些技术,你可以下载到完整的代码,包括例子中用到的所有文件,
Download MyFirstGame_Tutorial_Sample.zip.

Step 1: Install Your Software
Before you begin, make sure that you have installed all the necessary software, including a supported version of Microsoft Visual Studio tools and XNA Game Studio. See Required Software for a list of required programs.
第一步,安装软件
开始之前,请确认你已经安装了所有必需的软件,包括Micorsoft Visual Studio和XNA Game Studio受支持的版本,点击此处查看要求的软件列表

Step 2: Create a New Project From the Start menu, click All Programs, then the Microsoft XNA Game Studio 2.0 folder, and finally click your supported version of Microsoft Visual Studio tools. When the Start Page appears, click the File menu, and then click New Project. A dialog box will appear with a tree list on the left pane, marked Project Types. Select the XNA Game Studio 2.0 tree node underneath the Visual C# node. A set of available projects will appear in the right pane.
第二步,创建项目
从开始菜单,点击所有程序,选择Microsoft XNA Game Studio 2.0,最后选择受支持的版本的Micorsoft Visual Studio,
出现开始界面时,点击菜单中的文件,点击新项目,这时出现一个对话框,左侧是各种项目,选择C#节点下边的XNA Game Studio 2.0项目,一套可用的项目将会在对话框的右边列出,

In the right pane of the dialog box that appears, choose Windows Game (2.0) and type a title for your project (such as "MyFirstGame") in the Name box. Type a path where you'd like to save your project in the Location box. Then click OK.After creating a new project, you'll be presented with the code view of your game.
在右边的对话框中选择Windows Game (2.0),在项目名称栏中输入项目的标题(如"MyFirstGame"),在存储位置中输入项目保存的地址,点击确定.项目创建完毕后,你可以看到默认打开的代码页

Step 3: View the Code
Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager will handle setting up your screen size and rendering a blank screen. Your game will run and update all by itself. It's up to you to insert your own code to make the game more interesting.
第三步,查看代码
一些重复性的工作已经自动创建好了,此时如果你编译运行的话,GraphicsDeviceManager会设置你的屏幕的大小并沉浸一个空窗口,这个窗口将运行并自动更新,是插入你自己的代码的时候了,让它更有趣一些.

Much of the code to start and run your game has already been written for you, and all have places for you to insert your code.
运行它的代码大部分已经写好,并且都留出了添加你自己代码的地方,

    * The Initialize method is where you can initialize any assets that do not require a GraphicsDevice to be initialized.
    * The LoadContent method is where you load any game assets your game needs, such as models and textures.
    * The UnloadContent method is where any game assets can be released. Generally, no extra code is required here, as assets will be released automatically when they are no longer needed.
    * The Update loop is the best place to update your game logic: move objects around, take player input, decide the outcome of collisions between objects, and so on.
    * The Draw loop is the best place to render all of your objects and backgrounds on the screen.

    * Initialize方法里,你可以初始化所有不需要GraphicsDevice的东东,
    * LoadContent方法里,可以加载游戏中的资源,如模型和纹理等
    * UnloadContent方法与LoadContent刚好相反,是释放这这些资源的地方,一般情况下,这里不需要额外的代码,如果这些资源长时间不使用的话会自动释放.
    * Update循环方法是放置游戏逻辑的最好的地方,移动物体,接收玩家的输入,检测到两个物体碰撞时的输出等,
    * Draw循环方法是渲染所有物体和背景到屏幕上的地方,


Step 4: Add a Sprite
The next step is to add a graphic that can be drawn on the screen. Use a small graphics file, such as a small .bmp or .jpg file. Be creative—you can even make your own.
第四步,添加精灵
下一步将添加一个图片并输出到屏幕上,使用一个小图片文件,如.bmp,.jpg,你也可以自己制作一张

    * You can even skip ahead a bit and make a sprite that "hides" parts that should not be seen (such as edges or corners) so that it looks even better.
    * 你甚至可以忽略精灵的前景色,这样使精灵的一部分不可见,如边缘或边角部分,这样看上去更好看一些
    * Once you have a graphic picked out on your computer, follow these steps.
    * 当你选择一张图片的时候,按照下边的步骤
    * Make sure you can see the Solution Explorer for your project on the right side of the window. If you cannot see it, click the View menu, and then click Solution Explorer. When it appears, you will see files associated with your project in a tree structure.
    * 确认你现在可以看到项目右边的解决方案资源管理器,如果你看不到它,点击菜单中的查看,点击解决方案资源管理器,这时就会出现了,你可以在树型列表中看到这些已经声明过的文件
    * Inside the tree, you will see a node named Content. Right-click this node, click Add, and then click Existing Item. Browse to your graphic. If you can't see any files, make sure you change the Files of type selection box to read Texture Files. Click the graphic file, then click Add. An entry for the graphic file will appear in Solution Explorer.
    * 在这个树型列表里,你可以看到一个叫做Content的节点,右键这个节点,点击添加,添加现有项,找到你的图片,如果你看不到文件,请确认对话框中的文件类型,选择你的图片文件,点击确定添加,图片文件将会出现在解决方案的列表中
    * When you add a graphic file, it is automatically added to the XNA Framework Content Pipeline, which will allow you to quickly and easily load the graphic into your game.
    * 当你添加图片的时候,它会自动添加一个XNA Framework Content Piptline,这样你就可以快速方便的将图片加载到你的游戏中了,
    * Click the entry for the graphic in the Solution Explorer. If you do not see the entry, ensure the Content node is expanded by clicking the small plus sign (+) to the left of the node, then click on the entry that appears underneath the Content node.
    * In the Properties window below Solution Explorer, look for the "Asset Name" property. Note the name; you'll use it in your code to load the graphic so it can be displayed in your game. If the Properties window is not visible, press F4, or click the View menu, and then click Properties Window.
    * 在解决方案资源管理器中点击这张图片,如果你看不到它请确认Content节点是展开的,点击它左边的小加号(+)就可以,这时点击Content节点下边的这张图片,在解决方案资源管理器的下边有一个属性窗口,找到"Asset Name"属性,给它命名,然后就可以在程序加载它并在你的游戏里边显示了.如果你找不到属性窗口,按F4键,或者点击查看菜单,选择属性窗口,
    * Now, you must write code that loads and displays the sprite on the screen. You can use the instructions in How To: Draw a Sprite, or follow along here.
    * 好了,你现在必须写一些代码,加载这个精灵并让它显示在屏幕上,你可以查看"How To"中的Draw a Sprite,或者继续往下看


Back in the Code view of your game, find the LoadContent method, and add the following lines in and above the method so it looks similar to this:
回到游戏的代码页,找到LoadContent方法,添加代码如下所示
C#
// This is a texture we can render.
Texture2D myTexture;

// Set the coordinates to draw the sprite at.
Vector2 spritePosition = Vector2.Zero;

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    myTexture = Content.Load<Texture2D>("mytexture");
}


    Make sure the call to ContentManager.Load is using the "Asset Name" you saw in the Properties window in the previous step.
    This code will load and prepare your graphic to be drawn, and will reload your graphic if the graphics device is reset (such as in the case of the game window being resized).
    确认ContentManager.Load方法使用的参数是你上一步在"Asset Name"中命名的资源,这些代码将加载你的图片并准备画到屏幕上去,在你的图片设备重置(如当游戏窗口改变大小后)时会自动重新加载


Now, add code to the Draw loop so it looks like this:
在Draw循环中添加以下代码,
C#
protected override void Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
   
    // Draw the sprite.
    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
    spriteBatch.Draw(myTexture, spritePosition, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}


    This code draws the sprite on the screen each frame.
    这段代码在屏幕上显示这个精灵,


    * Build and run your game. The sprite appears. Now, it's time to give it some motion.
    * 编译运行你的游戏,这个精灵显示出来了,好了,现在是时候让它动起来了,


Step 5: Make the Sprite Move and Bounce Change the lines of code in and above the Update method to read this way:
第五步,让精灵动起来并自动反弹,将代码切换到Update方法,添加以下函数,
C#
// Store some information about the sprite's motion.
Vector2 spriteSpeed = new Vector2( 50.0f, 50.0f );

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

    // Move the sprite around.
    UpdateSprite(gameTime);

    base.Update(gameTime);
}

void UpdateSprite(GameTime gameTime)
{
    // Move the sprite by speed, scaled by elapsed time.
    spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

    int MaxX = graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
    int MinX = 0;
    int MaxY = graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
    int MinY = 0;

    // Check for bounce.
    if (spritePosition.X > MaxX)
    {
        spriteSpeed.X *= -1;
        spritePosition.X = MaxX;
    }

    else if (spritePosition.X < MinX)
    {
        spriteSpeed.X *= -1;
        spritePosition.X = MinX;
    }

    if (spritePosition.Y > MaxY)
    {
        spriteSpeed.Y *= -1;
        spritePosition.Y = MaxY;
    }

    else if (spritePosition.Y < MinY)
    {
        spriteSpeed.Y *= -1;
        spritePosition.Y = MinY;
    }
}


This adds a little bit of logic that will move the sprite around each frame and cause the sprite to change direction if it hits the edges of the game window.
这里添加了一些很小的逻辑判断,精灵在每侦中移动,并在碰到窗口的边缘时自动更改方向

Step 6: Explore!
From here, you can do just about anything. If you're ready to jump into a more complex example, including 3D graphics, input, and audio, see Going Beyond: XNA Game Studio in 3D.
第六步,运行
在这里,你基本没有什么工作要做了,如果你准备去看一些复杂些的例子,包括3D图形,输入和声音,可以看:XNA Game Studio in 3D.

Here are some more ideas to extend this sample:
这里有一些方法去扩展这个例子

Experiment with the call to SpriteBatch.Draw. Change the Color parameter to tint the sprite. See How To: Tint a Sprite. Add a second sprite, and use BoundingBox objects to allow the sprites to collide with one another. (See How To: Detect Whether Two Models Collide.)
试着用SpriteBatch.Draw方法,改变精灵的颜色参数,参考How To: Tint a Sprite.
添加两个精灵,使用BoundingBox让两个精灵互相碰撞,参考Detect Whether Two Models Collide.

Use Keyboard, Mouse, or GamePad to make the sprite respond to movements of an input device. (See Input Overview.)
Create some audio events so that the sprite makes sounds as it moves. (See How To: Add a Sound File to Your Game Using XACT and How To: Play a Sound.)
使用键盘,鼠标或者GamePad使精灵响应输入操作,参考 Input Overview
创建一些声音事件,当它运行时制造一些声音,参考Add a Sound File to Your Game Using XACT and How To: Play a Sound.
Instead of a sprite, use a 3D primitive that moves around in 3D space. (See How To: Draw Points, Lines, and Other 3D Primitives.)
使用3D取代精灵,并在3D空间中移动

Get more ideas and resources at XNA Creators Club Online.
更多的方法和资源请访问XNA Creators Club Online.


//第一次翻译外文并发表,翻译得不好,献丑了

类别:探索 | 浏览() | 评论 (3)
 
网友评论:
1
2008-02-26 14:56
强,慢慢欣赏
 
2
2008-02-29 19:11
WA ,翻译外文书籍。

看起来很深呀

看来学习英语的细胞又再迅速增长啦,哈哈

加油~好方法。
 
3
2008-02-29 20:30
现在又卡在3DMAX这儿了,郁闷,
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu