XNA Game Studio游戏循环
在这部分中您将重点两剩余部分的游戏——重写Update和Draw功能。有些大大可能看过相关微软的训练包,我这里主要是帮一些初学者。希望各位大大包含,毕竟文章发出来还是有工作量的。大家觉得有用就好,要是没有耽误时间给大家道个歉。(感谢http://winphone.us/)
1.打开BackgroundScreen.cs文件。
2.重写基类Update方法如下:
(Code Snippet –Game Development with XNA – Background Screen Update method)
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)
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)
usingAlienGameSample;
usingMicrosoft.Xna.Framework;
usingMicrosoft.Xna.Framework.Graphics;
usingMicrosoft.Xna.Framework.Audio;
usingSystem.IO.IsolatedStorage;
usingSystem.IO;
8.从GameScreen派生:
classGameplayScreen :GameScreen
{
}
9.添加以下类变量(将在比赛中使用它们)。后面我们使用这些变量,处理游戏逻辑、用户输入和绘图:
(Code Snippet –Game Development with XNA – Gameplay Screen variables)
//
// 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)
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)
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)
publicoverridevoidUnloadContent()
{
particles =null;
base.UnloadContent();
}
13.重写基类Update功能:
(Code Snippet –Game Development with XNA – Gameplay Screen Update method)
///<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)
///<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)
voidStartGameMenuEntrySelected(object sender,EventArgse)
{
ScreenManager.AddScreen(newGameplayScreen());
}
16.编译并运行该应用程序。单击"开始游戏"菜单项,可以看到主菜单从屏幕的下方滚动上来。
图2
运行效果
Note:现在游戏的场景你还看不到,不过不要紧,明天我们就开始了,加油!!
17.停止调试并回到应用程序编辑状态。
在个章节,你创建了新的主游戏类,并重写了游戏基类的功能。
分享到:
相关推荐
wp7 xna开发最强教程,让你最快的学习xna开发,能够写出一个小的游戏。
XNA平台游戏开发中文教程,对于入门来说是不可多得的靓文.
Microsoft 的 XNA 游戏开发 资料。
Direct 3D与XNA游戏开发源码,很优秀的书,很优秀的源代码。
xna游戏的开发介绍以及编程代码 是学习xna开发的必读之书
Direct 3D与XNA游戏开发光盘资源(6卷).part3
基于XNA框架开发的连连看小游戏,使用C#语言
本游戏是使用C#语言结合XNA技术开发的基于VS2010平台的小游戏,游戏有完整的游戏界面,不同难度的游戏关卡,音效几分功能齐全,视觉效果良好,希望大家能够喜欢
Direct.3D和XNA游戏开发基础(C#语言版)
该游戏是基于xna4.0开发的小游戏,属于简单的AI游戏, 含源代码。
XNA 游戏开发 C# 安装包
介绍了Direct_3D的基础知识与XNA游戏开发基础
XNA游戏开发SDK安装包,安装后在Visual Studio中可以做XNA游戏项目
《Windows Phone 7游戏高级编程:使用XNA Game Studio 4》包含了大量真实示例,对WindowsPhone 7游戏开发做了深入探索,是手边必备的一本全面教程。本书首先介绍了Windows Phone 7的特性,然后讲解了游戏开发过程会...
国内第一本XNA教程中文《XNA入门指南》,经《A Beginners Guild to XNA》原作者 Mike Fleischauer 书面授权中国XNA开发网翻译!书中如有错误请访问www.ChinaXNA.com 或者 www.XNADev.cn
基于XNA开发的3D游戏源码,适合初学者!
本系统共享全部源代码,对XNA游戏开发有兴趣的读者或是专业人士均有一定的参考价值。
用XNA开发的,类似雷电的射击小游戏。可以看一下,有游戏开发兴趣的人不能错过。
采用VS2008,C#语言,利用XNA游戏框架开发的小游戏