【原创】Alex

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

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

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

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

士兵类

复制代码
代码
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using Microsoft.Xna.Framework;
6  using Microsoft.Xna.Framework.Audio;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.GamerServices;
9 using Microsoft.Xna.Framework.Graphics;
10 using Microsoft.Xna.Framework.Input;
11 using Microsoft.Xna.Framework.Media;
12 using Microsoft.Xna.Framework.Net;
13 using Microsoft.Xna.Framework.Storage;
14
15 namespace Alex
16 {
17 class Soldier:GameComponent
18 {
19 public Soldier(Game game):base(game)
20 {
21
22 }
23 public override void Initialize()
24 {
25 base.Initialize();
26 }
27 public override void Update(GameTime gameTime)
28 {
29 base.Update(gameTime);
30 }
31
32 }
33 }
34
复制代码

 

 

英雄类

复制代码
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Audio;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.GamerServices;
9 using Microsoft.Xna.Framework.Graphics;
10 using Microsoft.Xna.Framework.Input;
11 using Microsoft.Xna.Framework.Media;
12 using Microsoft.Xna.Framework.Net;
13 using Microsoft.Xna.Framework.Storage;
14 namespace Alex
15 {
16 class Hero:GameComponent
17 {
18 public Hero(Game game):base(game)
19 {
20 }
21 public override void Initialize()
22 {
23
24 base.Initialize();
25 }
26 public override void Update(GameTime gameTime)
27 {
28 base.Update(gameTime);
29 }
30 }
31 }
复制代码

游戏主体类

1
代码   1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using Microsoft.Xna.Framework;  5 using Microsoft.Xna.Framework.Audio;  6 using Microsoft.Xna.Framework.Content;  7 using Microsoft.Xna.Framework.GamerServices;  8 using Microsoft.Xna.Framework.Graphics;  9 using Microsoft.Xna.Framework.Input; 10 using Microsoft.Xna.Framework.Media; 11 using Microsoft.Xna.Framework.Net; 12 using Microsoft.Xna.Framework.Storage; 13  14 namespace Alex 15 { 16     /// <summary> 17     /// This is the main type for your game 18     /// </summary> 19     public class AlexGame : Microsoft.Xna.Framework.Game 20     { 21         GraphicsDeviceManager graphics; 22         SpriteBatch spriteBatch; 23        24         public AlexGame() 25         { 26             graphics = new GraphicsDeviceManager(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         protected override void Initialize() 40         { 41             // TODO: Add your initialization logic here 42             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         protected override void LoadContent() 51         { 52             // Create a new SpriteBatch, which can be used to draw textures. 53             spriteBatch = new SpriteBatch(GraphicsDevice); 54  55             // TODO: use this.Content to load your game content here 56         } 57  58         /// <summary> 59         /// UnloadContent will be called once per game and is the place to unload 60         /// all content. 61         /// </summary> 62         protected override void UnloadContent() 63         { 64             // TODO: Unload any non ContentManager content here 65         } 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         protected override void Update(GameTime gameTime) 73         { 74             // Allows the game to exit 75             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 here 85  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         protected override void Draw(GameTime gameTime) 94         { 95             GraphicsDevice.Clear(Color.CornflowerBlue); 96             // TODO: Add your drawing code here 97  98             base.Draw(gameTime); 99         }100     }101 }102 103 104
1
在士兵类和英雄类中只是继承GameComponment父类。其运行过程大家可设置相应的断点进行调试。
posted on   AlexCheng  阅读(296)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示