XNA Game Studio游戏循环
在这部分中您将重点两剩余部分的游戏——重写Update和Draw功能。有些大大可能看过相关微软的训练包,我这里主要是帮一些初学者。希望各位大大包含,毕竟文章发出来还是有工作量的。大家觉得有用就好,要是没有耽误时间给大家道个歉。(感谢http://winphone.us/)
1.打开BackgroundScreen.cs文件。
2.重写基类Update方法如下:
(Code Snippet –Game Development with XNA – Background Screen Update method)
C#
publicoverridevoidUpdate(GameTimegameTime,boolotherScreenHasFocus,
boolcoveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus,false);
}
3.重写基类方法绘制。绘图方法将绘制图形设备上使用Microsoft.Xna.Framewok.Graphics命名空间中的SpriteBatch类。一组sprites被绘制的时候使用同样的设置。改变Draw方法来匹配下面的代码段:
(Code Snippet –Game Development with XNA– Background Screen Draw method)
C#
publicoverridevoidDraw(GameTimegameTime)
{
SpriteBatchspriteBatch = ScreenManager.SpriteBatch;
// Make the menu slide into place during transitions, using a
// power curve to make things look more interesting (this makes
// the movement slow down as it nears the end).
floattransitionOffset = (float)Math.Pow(TransitionPosition, 2);
spriteBatch.Begin();
// Draw Background
spriteBatch.Draw(background,newVector2(0, 0),
newColor(255, 255, 255, TransitionAlpha));
// Draw Title
spriteBatch.Draw(title,newVector2(60, 55),
newColor(255, 255, 255, TransitionAlpha));
spriteBatch.End();
}
4.按F5编译并运行该应用程序。
图1
修改了updatae和Draw后的运行效果
5.停止调试(SHIFT + F5),并返回到编辑应用程序。
6.将一个附加类添加到应用程序,并将其名称设置为GameplayScreen。
Note:要创建一个新的类,在解决方案资源管理器中右键单击AlienGame项目并选择Add | Class.
7.添加以下使用申明到新类:
(Code Snippet –Game Development with XNA – Gameplay Screen using statements)
C#
usingAlienGameSample;
usingMicrosoft.Xna.Framework;
usingMicrosoft.Xna.Framework.Graphics;
usingMicrosoft.Xna.Framework.Audio;
usingSystem.IO.IsolatedStorage;
usingSystem.IO;
8.从GameScreen派生:
C#
classGameplayScreen :GameScreen
{
}
9.添加以下类变量(将在比赛中使用它们)。后面我们使用这些变量,处理游戏逻辑、用户输入和绘图:
(Code Snippet –Game Development with XNA – Gameplay Screen variables)
C#
//
// Game Play Members
//
RectangleworldBounds;
boolgameOver;
intbaseLevelKillCount;
intlevelKillCount;
floatalienSpawnTimer;
floatalienSpawnRate;
floatalienMaxAccuracy;
floatalienSpeedMin;
floatalienSpeedMax;
intalienScore;
intnextLife;
inthitStreak;
inthighScore;
Randomrandom;
//
// Rendering Members
//
Texture2Dcloud1Texture;
Texture2Dcloud2Texture;
Texture2DsunTexture;
Texture2DmoonTexture;
Texture2DgroundTexture;
Texture2DtankTexture;
Texture2DalienTexture;
Texture2Dbadguy_blue;
Texture2Dbadguy_red;
Texture2Dbadguy_green;
Texture2Dbadguy_orange;
Texture2DmountainsTexture;
Texture2DhillsTexture;
Texture2DbulletTexture;
Texture2DlaserTexture;
SpriteFontscoreFont;
SpriteFontmenuFont;
Vector2cloud1Position;
Vector2cloud2Position;
Vector2sunPosition;
// Level changes, nighttime transitions, etc
floattransitionFactor;// 0.0f == day, 1.0f == night
floattransitionRate;// > 0.0f == day to night
ParticleSystemparticles;
//
// Audio Members
//
SoundEffectalienFired;
SoundEffectalienDied;
SoundEffectplayerFired;
SoundEffectplayerDied;
//Screen dimension consts
constfloat screenHeight = 800.0f;
constfloat screenWidth = 480.0f;
constint leftOffset = 25;
constint topOffset = 50;
constint bottomOffset = 20;
10.游戏类构造函数定义(在游戏屏幕和其他屏幕在游戏中的)之间的屏幕转换的速度和大小——在处理游戏的所有操作的地方。添加此类构造函数,如下所示::
(Code Snippet –Game Development with XNA – Gameplay Screen Constructor)
C#
publicGameplayScreen()
{
random =newRandom();
worldBounds =newRectangle(0, 0, (int)screenWidth, (int)screenHeight);
gameOver =true;
TransitionOnTime =TimeSpan.FromSeconds(0.0);
TransitionOffTime =TimeSpan.FromSeconds(0.0);
}
11.现在让我们来创建内容的加载和卸载功能。重写基类的LoadContent和UnloadContent的方法。
添加LoadContent代码段::
(Code Snippet –Game Development with XNA – Gameplay Screen LoadContent method)
C#
publicoverridevoidLoadContent()
{
cloud1Texture = ScreenManager.Game.Content.Load<Texture2D>("cloud1");
cloud2Texture = ScreenManager.Game.Content.Load<Texture2D>("cloud2");
sunTexture = ScreenManager.Game.Content.Load<Texture2D>("sun");
moonTexture = ScreenManager.Game.Content.Load<Texture2D>("moon");
groundTexture = ScreenManager.Game.Content.Load<Texture2D>("ground");
tankTexture = ScreenManager.Game.Content.Load<Texture2D>("tank");
mountainsTexture = ScreenManager.Game.Content.Load<Texture2D>("mountains_blurred");
hillsTexture = ScreenManager.Game.Content.Load<Texture2D>("hills");
alienTexture = ScreenManager.Game.Content.Load<Texture2D>("alien1");
badguy_blue = ScreenManager.Game.Content.Load<Texture2D>("badguy_blue");
badguy_red = ScreenManager.Game.Content.Load<Texture2D>("badguy_red");
badguy_green = ScreenManager.Game.Content.Load<Texture2D>("badguy_green");
badguy_orange = ScreenManager.Game.Content.Load<Texture2D>("badguy_orange");
bulletTexture = ScreenManager.Game.Content.Load<Texture2D>("bullet");
laserTexture = ScreenManager.Game.Content.Load<Texture2D>("laser");
alienFired = ScreenManager.Game.Content.Load<SoundEffect>("Tank_Fire");
alienDied = ScreenManager.Game.Content.Load<SoundEffect>("Alien_Hit");
playerFired = ScreenManager.Game.Content.Load<SoundEffect>("Tank_Fire");
playerDied = ScreenManager.Game.Content.Load<SoundEffect>("Player_Hit");
scoreFont = ScreenManager.Game.Content.Load<SpriteFont>("ScoreFont");
menuFont = ScreenManager.Game.Content.Load<SpriteFont>("MenuFont");
cloud1Position =newVector2(224 - cloud1Texture.Width, 32);
cloud2Position =newVector2(64, 80);
sunPosition =newVector2(16, 16);
particles =newParticleSystem(ScreenManager.Game.Content, ScreenManager.SpriteBatch);
base.LoadContent();
}
12.添加UnloadContent代码段:
(Code Snippet –Game Development with XNA – Gameplay Screen Unload method)
C#
publicoverridevoidUnloadContent()
{
particles =null;
base.UnloadContent();
}
13.重写基类Update功能:
Note:我们将在做游戏逻辑的时候再来修改他。
(Code Snippet –Game Development with XNA – Gameplay Screen Update method)
C#
///<summary>
///Runs one frame of update for the game.
///</summary>
///<param name="gameTime">Provides a snapshot of timing values.</param>
publicoverridevoidUpdate(GameTimegameTime,
boolotherScreenHasFocus,bool coveredByOtherScreen)
{
floatelapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
}
14.重写基类绘图功能,当下的“游戏世界”是每秒30次。
(Code Snippet –Game Development with XNA – Gameplay Screen Draw region)
C#
///<summary>
///Draw the game world, effects, and HUD
///</summary>
///<param name="gameTime">The elapsed time since last Draw</param>
publicoverridevoidDraw(GameTimegameTime)
{
floatelapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
ScreenManager.SpriteBatch.Begin();
ScreenManager.SpriteBatch.End();
}
Note:The GameTime could be used to calculate the drawing locations of various game items.
15.打开MainMenuScreen.cs,找到StartGameMenuEntrySelected方法,现在是空的,我们将以下代码添加进去。这段代码的作用是当用户点击“START GAME”按钮时,将GameplayScreen添加到ScreenManager:
(Code Snippet –Game Development with XNA– MainMenu Screen – GameMenuEntrySelected handler)
C#
voidStartGameMenuEntrySelected(object sender,EventArgse)
{
ScreenManager.AddScreen(newGameplayScreen());
}
16.编译并运行该应用程序。单击"开始游戏"菜单项,可以看到主菜单从屏幕的下方滚动上来。
图2
运行效果
Note:现在游戏的场景你还看不到,不过不要紧,明天我们就开始了,加油!!
17.停止调试并回到应用程序编辑状态。
在个章节,你创建了新的主游戏类,并重写了游戏基类的功能。