XNA游戏开发之(五)——XNA实现组件复用

摘要:
Alex在XNA游戏开发过程中,OPP思想尤为重要,它能实现组件的复用。将特定部分从程序主体中分离出来。游戏开发与普通应用程序开发不同,每个游戏对象在运行过程中都会被更新Update或重绘Draw。因此分离出的对象必须要有自己的初始化方法,LoadContent,Update,Draw,UnLoadContent方法。这样做的目的是可将一个或多个新类的实例添加到Components集合中。每次游戏主体类如图AlexGame的Update方法被调用后,GameComponent的Update方法也会自动被调用。如果需要绘制其他某些东西,还可以将新类继承DrawableGameComponent类。

【原创】Alex

在XNA游戏开发过程中,OPP思想尤为重要,它能实现组件的复用。将特定部分从程序主体中分离出来。

游戏开发与普通应用程序开发不同,每个游戏对象在运行过程中都会被更新Update或重绘Draw。因此分离出的对象必须要有自己的初始化方法(Initialize),LoadContent,Update,Draw,UnLoadContent方法。

在定义新类的最好方法是继承GameComponent类。这样做的目的是可将一个或多个新类的实例添加到Components集合中。每次游戏主体类如图AlexGame的Update方法被调用后,GameComponent的Update方法也会自动被调用。

如果需要绘制其他某些东西,还可以将新类继承DrawableGameComponent类。该类包含Draw方法。同样的游戏主体的Draw方法被调用后新类的Draw也被调用。

XNA游戏开发之(五)——XNA实现组件复用第1张

士兵类

代码
1 usingSystem;
2 usingSystem.Collections.Generic;
3 usingSystem.Linq;
4 usingSystem.Text;
5 usingMicrosoft.Xna.Framework;
6 usingMicrosoft.Xna.Framework.Audio;
7 usingMicrosoft.Xna.Framework.Content;
8 usingMicrosoft.Xna.Framework.GamerServices;
9 usingMicrosoft.Xna.Framework.Graphics;
10 usingMicrosoft.Xna.Framework.Input;
11 usingMicrosoft.Xna.Framework.Media;
12 usingMicrosoft.Xna.Framework.Net;
13 usingMicrosoft.Xna.Framework.Storage;
14 15 namespaceAlex
16 {
17 classSoldier:GameComponent
18 {
19 publicSoldier(Game game):base(game)
20 {
21 22 }
23 publicoverridevoidInitialize()
24 {
25 base.Initialize();
26 }
27 publicoverridevoidUpdate(GameTime gameTime)
28 {
29 base.Update(gameTime);
30 }
31 32 }
33 }
34

英雄类

代码
1 usingSystem;
2 usingSystem.Collections.Generic;
3 usingSystem.Linq;
4 usingSystem.Text;
5 usingMicrosoft.Xna.Framework;
6 usingMicrosoft.Xna.Framework.Audio;
7 usingMicrosoft.Xna.Framework.Content;
8 usingMicrosoft.Xna.Framework.GamerServices;
9 usingMicrosoft.Xna.Framework.Graphics;
10 usingMicrosoft.Xna.Framework.Input;
11 usingMicrosoft.Xna.Framework.Media;
12 usingMicrosoft.Xna.Framework.Net;
13 usingMicrosoft.Xna.Framework.Storage;
14 namespaceAlex
15 {
16 classHero:GameComponent
17 {
18 publicHero(Game game):base(game)
19 {
20 }
21 publicoverridevoidInitialize()
22 {
23 24 base.Initialize();
25 }
26 publicoverridevoidUpdate(GameTime gameTime)
27 {
28 base.Update(gameTime);
29 }
30 }
31 }

游戏主体类

代码
1 usingSystem;
2 usingSystem.Collections.Generic;
3 usingSystem.Linq;
4 usingMicrosoft.Xna.Framework;
5 usingMicrosoft.Xna.Framework.Audio;
6 usingMicrosoft.Xna.Framework.Content;
7 usingMicrosoft.Xna.Framework.GamerServices;
8 usingMicrosoft.Xna.Framework.Graphics;
9 usingMicrosoft.Xna.Framework.Input;
10 usingMicrosoft.Xna.Framework.Media;
11 usingMicrosoft.Xna.Framework.Net;
12 usingMicrosoft.Xna.Framework.Storage;
13 14 namespaceAlex
15 {
16 ///<summary>17 ///This is the main type for your game
18 ///</summary>19 publicclassAlexGame : Microsoft.Xna.Framework.Game
20 {
21 GraphicsDeviceManager graphics;
22 SpriteBatch spriteBatch;
23 24 publicAlexGame()
25 {
26 graphics =newGraphicsDeviceManager(this);
27 Content.RootDirectory ="Content";
28 //游戏类初始化阶段将新类添加到组件中29 this.Components.Add(new Hero(this));
30 this.Components.Add(new Soldier(this
));
31 }
32 33 ///<summary>34 ///Allows the game to perform any initialization it needs to before starting to run.
35 ///This is where it can query for any required services and load any non-graphic
36 ///related content. Calling base.Initialize will enumerate through any components
37 ///and initialize them as well.
38 ///</summary>39 protectedoverridevoidInitialize()
40 {
41 //TODO: Add your initialization logic here42 this.TargetElapsedTime =TimeSpan.FromSeconds(1.0f/1000.0f);
43 base.Initialize();
44 }
45 46 ///<summary>47 ///LoadContent will be called once per game and is the place to load
48 ///all of your content.
49 ///</summary>50 protectedoverridevoidLoadContent()
51 {
52 //Create a new SpriteBatch, which can be used to draw textures.53 spriteBatch =newSpriteBatch(GraphicsDevice);
54 55 //TODO: use this.Content to load your game content here56 }
57 58 ///<summary>59 ///UnloadContent will be called once per game and is the place to unload
60 ///all content.
61 ///</summary>62 protectedoverridevoidUnloadContent()
63 {
64 //TODO: Unload any non ContentManager content here65 }
66 67 ///<summary>68 ///Allows the game to run logic such as updating the world,
69 ///checking for collisions, gathering input, and playing audio.
70 ///</summary>71 ///<param name="gameTime">Provides a snapshot of timing values.</param>72 protectedoverridevoidUpdate(GameTime gameTime)
73 {
74 //Allows the game to exit75 if(GamePad.GetState(PlayerIndex.One).Buttons.Back ==ButtonState.Pressed)
76 {
77 this.Exit();
78 }
79 if(Keyboard.GetState().IsKeyDown(Keys.Escape))
80 {
81 this.Exit();
82 }
83 84 //TODO: Add your update logic here85 86 base.Update(gameTime);
87 }
88 89 ///<summary>90 ///This is called when the game should draw itself.
91 ///</summary>92 ///<param name="gameTime">Provides a snapshot of timing values.</param>93 protectedoverridevoidDraw(GameTime gameTime)
94 {
95 GraphicsDevice.Clear(Color.CornflowerBlue);
96 //TODO: Add your drawing code here97 98 base.Draw(gameTime);
99 }
100 }
101 }
102 103 104
在士兵类和英雄类中只是继承GameComponment父类。其运行过程大家可设置相应的断点进行调试。

免责声明:文章转载自《XNA游戏开发之(五)——XNA实现组件复用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇朴素贝叶斯常见面试题Quartus中代码字体大小的调整方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

当我读《体验引擎:游戏设计全景探秘》时我在想什么

写在前面 这本书长这样 当看到它的中文书名时,我以为是讲引擎的书,但并不是。书的内容可见它的英文书名,直译过来是“游戏设计:设计体验入门书”。 我把它归到“不用敲代码也能读下去”类别。 一些感想 这本书所说的“体验引擎”,用我的话概括就是“抽象来说游戏是什么”。这本书按“游戏是什么”,“用什么填充了游戏”,“游戏制作流程中一些重点”依次来组织内容,...

【Unity3D入门教程】Unity3D播放音频和视频

前言 在游戏开发的过程中,我们经常会用到播放音频和视频,我们今天给大家带来一种简单的播放音频和视频的方法,使用的是Unity自带的函数。本文的内容参考了宣雨松所写的《Unity3D游戏开发》一书,特此致谢。   1 播放音频 首先,Unity支持的音频格式有wav、mp3、ogg等。在场景中创建一个空物体GameObject,并为其添加AudioSourc...

【转】推荐给大家的7本游戏开发书

1、游戏设计的 100 个原理 世界知名的游戏设计界权威的见解和阐释,深刻揭示原理对游戏设计的启发和价值。 本书首次将游戏设计师的想法中最好的一部分变成一个工具包。作者在书中整合了众多游戏设计秘笈,概括并阐释了 100 条重要的方法、原理和设计哲学。这些原理是从游戏设计的各个流派中收集而来的,并且从创新、创作、平衡和解决问题这 4 个角度来组织并加以...

XNA游戏开发之(六)——保存游戏数据

[原创]Alex 游戏过程中,通常需要存储游戏状态。在下次启动游戏时,重新载入游戏,继续游戏。 实现过程非常简单,首先建立一个结构体定义需要保存的游戏参数,然后通过XmlSerializer类将其保存为一个XML文档。 1interfaceGameData2{3intGameID;4stringPlayName;5DateTimeTime;6} XNA...

【Visual C++】游戏开发笔记之一——API函数、DirectX的关键系统

本系列文章由zhmxy555(毛星云)编写,转载请注明出处。 http://blog.csdn.net/zhmxy555/article/details/7318264 作者:毛星云邮箱:happylifemxy@qq.com 在从第一节开始看这个笔记系列的话,大家会发现,一上来就开始讲DirectX相关的内容,但是写了几节之后,又开始讲...

unity3D游戏开发十四之NGUI一

转:http://blog.csdn.net/kuloveyouwei/article/details/23676039 在Unity中,可以使用代码控制其自身所携带的GUI来实现图形界面的搭建。但是这些组件的搭建效率很低,而且已经无法满足现在市场对图形用户界面美感的要求,所以,引入了NGUI来增加所要开发的图形用户界面的美感。NGUI是一个功能强大的UI...