Windows Phone 7范例游戏Platformer实战1——5大平台支持
Windows Phone 7范例游戏Platformer实战2——游戏设计初步
Windows Phone 7范例游戏Platformer实战3——游戏资源和内容管道
Windows Phone 7范例游戏Platformer实战4——冲突检测的实现
Windows Phone 7范例游戏Platformer实战5——多点触控编程
Windows Phone 7范例游戏Platformer实战6——加速度传感器解读
1 public class Game1 : Microsoft.Xna.Framework.Game
2 {
3 GraphicsDeviceManager graphics;
4 SpriteBatch spriteBatch;
5
6 public Game1()
7 {
8 graphics = new GraphicsDeviceManager(this);
9 Content.RootDirectory = "Content";
10
11 // Frame rate is 30 fps by default for Windows Phone.
12 TargetElapsedTime = TimeSpan.FromTicks(333333);
13 }
14
15 ///
16 /// Allows the game to perform any initialization it needs to before starting to run.
17 /// This is where it can query for any required services and load any non-graphic
18 /// related content. Calling base.Initialize will enumerate through any components
19 /// and initialize them as well.
20 ///
21 protected override void Initialize()
22 {
23 // TODO: Add your initialization logic here
24
25 base.Initialize();
26 }
27
28 ///
29 /// LoadContent will be called once per game and is the place to load
30 /// all of your content.
31 ///
32 protected override void LoadContent()
33 {
34 // Create a new SpriteBatch, which can be used to draw textures.
35 spriteBatch = new SpriteBatch(GraphicsDevice);
36
37 // TODO: use this.Content to load your game content here
38 }
39
40 ///
41 /// UnloadContent will be called once per game and is the place to unload
42 /// all content.
43 ///
44 protected override void UnloadContent()
45 {
46 // TODO: Unload any non ContentManager content here
47 }
48
49 ///
50 /// Allows the game to run logic such as updating the world,
51 /// checking for collisions, gathering input, and playing audio.
52 ///
53 /// Provides a snapshot of timing values.
54 protected override void Update(GameTime gameTime)
55 {
56 // Allows the game to exit
57 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
58 this.Exit();
59
60 // TODO: Add your update logic here
61
62 base.Update(gameTime);
63 }
64
65 ///
66 /// This is called when the game should draw itself.
67 ///
68 /// Provides a snapshot of timing values.
69 protected override void Draw(GameTime gameTime)
70 {
71 GraphicsDevice.Clear(Color.CornflowerBlue);
72
73 // TODO: Add your drawing code here
74
75 base.Draw(gameTime);
76 }
77 }
XNA模板自动为我们提供了数个成员变量,此外还有一个构造函数和5个方法。轩辕有必要对这些变量和方法逐一进行介绍。
相比之下,游戏程序由事件轮询(polling for events)驱动,而不是等待并监听是否有事件被激发。取而代之,游戏程序会主动询问系统鼠标是否被移动,同时程序会一直运行,不管有没有用户输入。 在同一时间,不管用户是否与系统进行交互,XNA游戏中的一切仍在进行。我们不可能在游戏中为每个对象注册一个事件,以便被动去响应。游戏的很多对象都应该按照自己的设定模式进行运动或者存在,无需任何外界输入的驱动。比如说Platformer中的僵尸怪,它会按照自己的方法在平台上来回跑动,无需用户的输入和操作。 这就是XNA使用游戏循环的一个主要原因:它提供了一种途径让游戏始终运行着而不管玩家在做什么。
1 using System;
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using Microsoft.Xna.Framework.Audio;
5
6 namespace Platformer
7 {
8 ///
9 /// A valuable item the player can collect.
10 ///
11 class Gem
12 {
13 private Texture2D texture;
14 private Vector2 origin;
15 private SoundEffect collectedSound;
16
17 public const int PointValue = 30;
18 public readonly Color Color = Color.Yellow;
19
20 // The gem is animated from a base position along the Y axis.
21 private Vector2 basePosition;
22 private float bounce;
23
24 public Level Level
25 {
26 get { return level; }
27 }
28 Level level;
29
30 ///
31 /// Gets the current position of this gem in world space.
32 ///
33 public Vector2 Position
34 {
35 get
36 {
37 return basePosition + new Vector2(0.0f, bounce);
38 }
39 }
40
41 ///
42 /// Gets a circle which bounds this gem in world space.
43 ///
44 public Circle BoundingCircle
45 {
46 get
47 {
48 return new Circle(Position, Tile.Width / 3.0f);
49 }
50 }
51
52 ///
53 /// Constructs a new gem.
54 ///
55 public Gem(Level level, Vector2 position)
56 {
57 this.level = level;
58 this.basePosition = position;
59
60 LoadContent();
61 }
62
63 ///
64 /// Loads the gem texture and collected sound.
65 ///
66 public void LoadContent()
67 {
68 texture = Level.Content.Load("Sprites/Gem");
69 origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
70 collectedSound = Level.Content.Load("Sounds/GemCollected");
71 }
72
73 ///
74 /// Bounces up and down in the air to entice players to collect them.
75 ///
76 public void Update(GameTime gameTime)
77 {
78 // Bounce control constants
79 const float BounceHeight = 0.18f;
80 const float BounceRate = 3.0f;
81 const float BounceSync = -0.75f;
82
83 // Bounce along a sine curve over time.
84 // Include the X coordinate so that neighboring gems bounce in a nice wave pattern.
85 double t = gameTime.TotalGameTime.TotalSeconds * BounceRate + Position.X * BounceSync;
86 bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;
87 }
88
89 ///
90 /// Called when this gem has been collected by a player and removed from the level.
91 ///
92 ///
93 /// The player who collected this gem. Although currently not used, this parameter would be
94 /// useful for creating special powerup gems. For example, a gem could make the player invincible.
95 ///
96 public void OnCollected(Player collectedBy)
97 {
98 collectedSound.Play();
99 }
100
101 ///
102 /// Draws a gem in the appropriate color.
103 ///
104 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
105 {
106 spriteBatch.Draw(texture, Position, null, Color, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
107 }
108 }
109 }
110
宝石实现完成了,下一节轩辕开始介绍僵尸怪的动画实现。僵尸怪的行为包括只有跑动一种,和宝石的动画不一样的是,僵尸怪是由一个连贯的精灵图片实现的,这里需要对精灵图片进行解析,将整个图片解析为细分的动作。如下图所示: