【原创】Alex
在XNA游戏开发过程中,OPP思想尤为重要,它能实现组件的复用。将特定部分从程序主体中分离出来。
游戏开发与普通应用程序开发不同,每个游戏对象在运行过程中都会被更新Update或重绘Draw。因此分离出的对象必须要有自己的初始化方法(Initialize),LoadContent,Update,Draw,UnLoadContent方法。
在定义新类的最好方法是继承GameComponent类。这样做的目的是可将一个或多个新类的实例添加到Components集合中。每次游戏主体类如图AlexGame的Update方法被调用后,GameComponent的Update方法也会自动被调用。
如果需要绘制其他某些东西,还可以将新类继承DrawableGameComponent类。该类包含Draw方法。同样的游戏主体的Draw方法被调用后新类的Draw也被调用。
士兵类
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父类。其运行过程大家可设置相应的断点进行调试。