手把手教用XNA开发winphone7游戏(五)大结局
/// </summary>
/// <param name="elapsed"></param>
void UpdateBullets(float elapsed)
{
for (int i = 0; i < playerBullets.Count; ++i)
{
if (playerBullets[i].IsAlive == false)
continue;
playerBullets[i].Position += playerBullets[i].Velocity * elapsed;
if (playerBullets[i].Position.Y < -32)
{
playerBullets[i].IsAlive = false;
hitStreak = 0;
}
}
for (int i = 0; i < alienBullets.Count; ++i)
{
if (alienBullets[i].IsAlive == false)
continue;
alienBullets[i].Position += alienBullets[i].Velocity * elapsed;
if (alienBullets[i].Position.Y > worldBounds.Height - groundTexture.Height - laserTexture.Height)
alienBullets[i].IsAlive = false;
}
}
/// <summary>
/// Moves the aliens and performs their "thinking" by determining if they
/// should shoot and where.
/// </summary>
/// <param name="elapsed">The elapsed time since UpdateAliens was called last.</param>
private void UpdateAliens(float elapsed)
{
// See if it's time to spawn an alien;
alienSpawnTimer -= elapsed;
if (alienSpawnTimer <= 0.0f)
{
SpawnAlien();
alienSpawnTimer += alienSpawnRate;
}
for (int i = 0; i < aliens.Count; ++i)
{
if (aliens[i].IsAlive == false)
continue;
aliens[i].Position += aliens[i].Velocity * elapsed;
if ((aliens[i].Position.X < -aliens[i].Width - 64 && aliens[i].Velocity.X < 0.0f) ||
(aliens[i].Position.X > worldBounds.Width + 64 && aliens[i].Velocity.X > 0.0f))
{
aliens[i].IsAlive = false;
continue;
}
aliens[i].FireTimer -= elapsed;
if (aliens[i].FireTimer <= 0.0f && aliens[i].FireCount > 0)
{
if (player.IsAlive)
{
Bullet bullet = CreateAlienBullet();
bullet.Position.X = aliens[i].Position.X + aliens[i].Width / 2 - laserTexture.Width / 2;
bullet.Position.Y = aliens[i].Position.Y + aliens[i].Height;
if ((float)random.NextDouble() <= aliens[i].Accuracy)
{
bullet.Velocity = Vector2.Normalize(player.Position - aliens[i].Position) * 64.0f;
}
else
{
bullet.Velocity = new Vector2(-8.0f + 16.0f * (float)random.NextDouble(), 64.0f);
}
alienFired.Play();
}
aliens[i].FireCount--;
}
}
}
/// <summary>
/// Performs all bullet and player/alien collision detection. Also handles game logic
/// when a hit occurs, such as killing something, adding score, ending the game, etc.
/// </summary>
void CheckHits()
{
if (gameOver)
return;
for (int i = 0; i < playerBullets.Count; ++i)
{
if (playerBullets[i].IsAlive == false)
continue;
for (int a = 0; a < aliens.Count; ++a)
{
if (aliens[a].IsAlive == false)
continue;
if ((playerBullets[i].Position.X >= aliens[a].Position.X && playerBullets[i].Position.X <= aliens[a].Position.X + aliens[a].Width) && (playerBullets[i].Position.Y >= aliens[a].Position.Y && playerBullets[i].Position.Y <= aliens[a].Position.Y + aliens[a].Height))
{
playerBullets[i].IsAlive = false;
aliens[a].IsAlive = false;
hitStreak++;
player.Score += aliens[a].Score * (hitStreak / 5 + 1);
if (player.Score > highScore)
highScore = player.Score;
if (player.Score > nextLife)
{
player.Lives++;
nextLife += nextLife;
}
levelKillCount--;
if (levelKillCount <= 0)
AdvanceLevel();
particles.CreateAlienExplosion(new Vector2(aliens[a].Position.X + aliens[a].Width / 2, aliens[a].Position.Y + aliens[a].Height / 2));
alienDied.Play();
}
}
}
if (player.IsAlive == false)
return;
for (int i = 0; i < alienBullets.Count; ++i)
{
if (alienBullets[i].IsAlive == false)
continue;
if ((alienBullets[i].Position.X >= player.Position.X + 2 && alienBullets[i].Position.X <= player.Position.X + player.Width - 2) && (alienBullets[i].Position.Y >= player.Position.Y + 2 && alienBullets[i].Position.Y <= player.Position.Y + player.Height))
{
alienBullets[i].IsAlive = false;
player.IsAlive = false;
hitStreak = 0;
player.RespawnTimer = 3.0f;
particles.CreatePlayerExplosion(new Vector2(player.Position.X + player.Width / 2, player.Position.Y + player.Height / 2));
playerDied.Play();
if (player.Lives <= 0)
{
gameOver = true;
}
}
}
}
/// <summary>
/// Advances the difficulty of the game one level.
/// </summary>
void AdvanceLevel()
{
baseLevelKillCount += 5;
levelKillCount = baseLevelKillCount;
alienScore += 25;
alienSpawnRate -= 0.3f;
alienMaxAccuracy += 0.1f;
if (alienMaxAccuracy > 0.75f)
alienMaxAccuracy = 0.75f;
alienSpeedMin *= 1.35f;
alienSpeedMax *= 1.35f;
if (alienSpawnRate < 0.33f)
alienSpawnRate = 0.33f;
if (transitionFactor == 1.0f)
{
transitionRate = -0.5f;
}
else
{
transitionRate = 0.5f;
}
}
14. 在GameplayScreen 类中添加如下代码:
Note: 下面的代码片断还增加了大量的帮助器方法。 他们的目的是,如下所示: CreateAlienBullet: 创建的异形的子弹,将用于异形向玩家开火。
SpawnAlien: 初始化一个新的异形实例,设置了的初始位置、速度,选择颜色纹理等。
CreateAlien: 创建新异形的实例,并将其添加到异形集合
(Code Snippet – Game Development with XNA – Gameplay Screen – Helper aliens methods)
C#
/// <summary>
/// Returns an instance of a usable alien bullet. Prefers reusing an existing (dead)
/// bullet over creating a new instance.
/// </summary>
/// <returns>A bullet ready to place into the world.</returns>
Bullet CreateAlienBullet()
{
Bullet b = null;
for (int i = 0; i < alienBullets.Count; ++i)
{
if (alienBullets[i].IsAlive == false)
{
b = alienBullets[i];
break;
}
}
if (b == null)
{
b = new Bullet();
alienBullets.Add(b);
}
b.IsAlive = true;
return b;
}
/// <summary>
/// Creates an instance of an alien, sets the initial state, and places it into the world.
/// </summary>
private void SpawnAlien()
{
Alien newAlien = CreateAlien();
if (random.Next(2) == 1)
{
newAlien.Position.X = -64.0f;
newAlien.Velocity.X = random.Next((int)alienSpeedMin, (int)alienSpeedMax);
}
else
{
newAlien.Position.X = worldBounds.Width + 32;
newAlien.Velocity.X = -random.Next((int)alienSpeedMin, (int)alienSpeedMax);
}
newAlien.Position.Y = 24.0f + 80.0f * (float)random.NextDouble();
// Aliens
if (transitionFactor > 0.0f)
{
switch (random.Next(4))
{
case 0:
newAlien.Texture = badguy_blue;
break;
case 1:
newAlien.Texture = badguy_red;
break;
case 2:
newAlien.Texture = badguy_green;
break;
case 3:
newAlien.Texture = badguy_orange;
break;
}
}
else
{
newAlien.Texture = alienTexture;
}
newAlien.Width = newAlien.Texture.Width;
newAlien.Height = newAlien.Texture.Height;
newAlien.IsAlive = true;
newAlien.Score = alienScore;
float duration = screenHeight / newAlien.Velocity.Length();
newAlien.FireTimer = duration * (float)random.NextDouble();
newAlien.FireCount = 1;
newAlien.Accuracy = alienMaxAccuracy;
}
/// <summary>
/// Returns an instance of a usable alien instance. Prefers reusing an existing (dead)
/// alien over creating a new instance.
/// </summary>
/// <returns>An alien ready to place into the world.</returns>
Alien CreateAlien()
{
Alien b = null;
for (int i = 0; i < aliens.Count; ++i)
{
if (aliens[i].IsAlive == false)
{
b = aliens[i];
break;
}
}
if (b == null)
{
b = new Alien();
aliens.Add(b);
}
b.IsAlive = true;
return b;
}
15. .导航到Draw方法,并添加下面的蓝色突出显示的代码段,在Screen.SpriteBatch.Begin() 和 Screen.SpriteBatch.End() 之间:
这样更改方法方法将呼调用helper方法以在屏幕上绘制变化后的内容。
(Code Snippet – Game Development with XNA – Gameplay Screen – Draw method update)
C#
public override void Draw(GameTime gameTime)
{
float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
ScreenManager.SpriteBatch.Begin();
DrawBackground(elapsedTime);
DrawAliens();
DrawPlayer();
DrawBullets();
particles.Draw();
DrawForeground(elapsedTime);
DrawHud();
ScreenManager.SpriteBatch.End();
}
16. 在Draw 方法后面添加如下方法:
Note: 下面的代码段中添加大量的绘图相关的Helper方法。 方法用途如下: DrawPlayer: 绘制玩家的坦克。
DrawAliens: 绘制所有异形。
DrawBullets: 绘制所有的子弹 (玩家的和异形的)。
DrawForeground: 作为前台绘制云,并将其移动。
DrawBackground: 绘制草、 丘陵、 山地和太阳/月亮。 此外处理白天和黑夜之间转换。
DrawHud: 绘制分数、剩余的生命值和" GAME OVER "需要的时候。
drawString: 绘制隐藏的文本的通用方法。
(Code Snippet – Game Development with XNA – Gameplay Screen – Draw methods)
C#
/// <summary>
/// Draws the player's tank
/// </summary>
void DrawPlayer()
{
if (!gameOver && player.IsAlive)
{
ScreenManager.SpriteBatch.Draw(tankTexture, player.Position, Color.White);
}
}
/// <summary>
/// Draws all of the aliens.
/// </summary>
void DrawAliens()
{
for (int i = 0; i < aliens.Count; ++i)
{
if (aliens[i].IsAlive) ScreenManager.SpriteBatch.Draw(aliens[i].Texture, new Rectangle((int)aliens[i].Position.X, (int)aliens[i].Position.Y, (int)aliens[i].Width, (int)aliens[i].Height), Color.White);
}
}
/// <summary>
/// Draw both the player and alien bullets.
/// </summary>
private void DrawBullets()
{
for (int i = 0; i < playerBullets.Count; ++i)
{
if (playerBullets[i].IsAlive)
ScreenManager.SpriteBatch.Draw(bulletTexture, playerBullets[i].Position, Color.White);
}
for (int i = 0; i < alienBullets.Count; ++i)
{
if (alienBullets[i].IsAlive)
ScreenManager.SpriteBatch.Draw(laserTexture, alienBullets[i].Position, Color.White);
}
}
/// <summary>
/// Draw the foreground, which is basically the clouds. I think I had planned on one point
/// having foreground grass that was drawn in front of the tank.
/// </summary>
/// <param name="elapsedTime">The elapsed time since last Draw</param>
private void DrawForeground(float elapsedTime)
{
// Move the clouds. Movement seems like an Update thing to do, but this animations
// have no impact over gameplay.
cloud1Position += new Vector2(24.0f, 0.0f) * elapsedTime;
if (cloud1Position.X > screenWidth)
cloud1Position.X = -cloud1Texture.Width * 2.0f;
cloud2Position += new Vector2(16.0f, 0.0f) * elapsedTime;
if (cloud2Position.X > screenWidth)
cloud2Position.X = -cloud1Texture.Width * 2.0f;
ScreenManager.SpriteBatch.Draw(cloud1Texture, cloud1Position, Color.White);
ScreenManager.SpriteBatch.Draw(cloud2Texture, cloud2Position, Color.White);
}
/// <summary>
/// Draw the grass, hills, mountains, and sun/moon. Handle transitioning
/// between day and night as well.
/// </summary>
/// <param name="elapsedTime">The elapsed time since last Draw</param>
private void DrawBackground(float elapsedTime)
{
transitionFactor += transitionRate * elapsedTime;
if (transitionFactor < 0.0f)
{
transitionFactor = 0.0f;
transitionRate = 0.0f;
}
if (transitionFactor > 1.0f)
{
transitionFactor = 1.0f;
transitionRate = 0.0f;
}
Vector3 day = Color.White.ToVector3();
Vector3 night = new Color(80, 80, 180).ToVector3();
Vector3 dayClear = Color.CornflowerBlue.ToVector3();
Vector3 nightClear = night;
Color clear = new Color(Vector3.Lerp(dayClear, nightClear, transitionFactor));
Color tint = new Color(Vector3.Lerp(day, night, transitionFactor));
// Clear the background, using the day/night color
ScreenManager.Game.GraphicsDevice.Clear(clear);
// Draw the mountains
ScreenManager.SpriteBatch.Draw(mountainsTexture, new Vector2(0, screenHeight - mountainsTexture.Height), tint);
// Draw the hills
ScreenManager.SpriteBatch.Draw(hillsTexture, new Vector2(0, screenHeight - hillsTexture.Height), tint);
// Draw the ground
ScreenManager.SpriteBatch.Draw(groundTexture, new Vector2(0, screenHeight - groundTexture.Height), tint);
// Draw the sun or moon (based on time)
ScreenManager.SpriteBatch.Draw(sunTexture, sunPosition, new Color(255, 255, 255, (byte)(255.0f * (1.0f - transitionFactor))));
ScreenManager.SpriteBatch.Draw(moonTexture, sunPosition, new Color(255, 255, 255, (byte)(255.0f * transitionFactor)));
}
/// <summary>
/// Draw the hud, which consists of the score elements and the GAME OVER tag.
/// </summary>
void DrawHud()
{
float scale = 2.0f;
if (gameOver)
{
Vector2 size = menuFont.MeasureString("GAME OVER");
DrawString(menuFont, "GAME OVER", new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.Width / 2 - size.X, ScreenManager.Game.GraphicsDevice.Viewport.Height / 2 - size.Y / 2), new Color(255, 64, 64), scale);
}
else
{
int bonus = 100 * (hitStreak / 5);
string bonusString = (bonus > 0 ? " (" + bonus.ToString(System.Globalization.CultureInfo.CurrentCulture) + "%)" : "");
// Score
DrawString(scoreFont, "SCORE: " + player.Score.ToString(System.Globalization.CultureInfo .CurrentCulture) + bonusString, new Vector2(leftOffset, topOffset), Color.Yellow, scale);
string text = "LIVES: " + player.Lives.ToString(System.Globalization.CultureInfo .CurrentCulture);
Vector2 size = scoreFont.MeasureString(text);
size *= scale;
// Lives
DrawString(scoreFont, text, new Vector2(screenWidth - leftOffset - (int)size.X, topOffset), Color.Yellow, scale);
DrawString(scoreFont, "LEVEL: " + (((baseLevelKillCount - 5) / 5) + 1).ToString(System.Globalization.CultureInfo.CurrentCulture), new Vector2(leftOffset, screenHeight - bottomOffset), Color.Yellow, scale);
text = "HIGH SCORE: " + highScore.ToString(System.Globalization.CultureInfo
.CurrentCulture);
size = scoreFont.MeasureString(text);
DrawString(scoreFont, text, new Vector2(screenWidth - leftOffset - (int)size.X * 2, screenHeight - bottomOffset), Color.Yellow, scale);
}
}
/// <summary>
/// A simple helper to draw shadowed text.
/// </summary>
void DrawString(SpriteFont font, string text,
Vector2 position, Color color)
{
ScreenManager.SpriteBatch.DrawString(font, text, new Vector2(position.X + 1, position.Y + 1), Color.Black);
ScreenManager.SpriteBatch.DrawString(font, text, position, color);
}
/// <summary>
/// A simple helper to draw shadowed text.
/// </summary>
void DrawString(SpriteFont font, string text,
Vector2 position, Color color, float fontScale)
{
ScreenManager.SpriteBatch.DrawString(font, text, new Vector2(position.X + 1, position.Y + 1), Color.Black, 0, new Vector2(0, font.LineSpacing / 2), fontScale, SpriteEffects.None, 0);
ScreenManager.SpriteBatch.DrawString(font, text, position, color, 0, new Vector2(0, font.LineSpacing / 2), fontScale, SpriteEffects.None, 0);
}
17. 找到 LoadContent 方法,在base.LoadContent() 调用后面体检如下代码:
C#
public override void LoadContent()
{
...
player.Width = tankTexture.Width;
player.Height = tankTexture.Height;
base.LoadContent();
LoadHighscore();
Start();
}
18. 找到 UnloadContent 方法并在“particles = null;”之前的位置添加下面的代码段:
C#
public override void UnloadContent()
{
SaveHighscore();
particles = null;
base.UnloadContent();
}
19. 创建一个区域loading/unloading high scores logic。我们需要通过Isolated Storage来读取Windows Phone 文件系统中的数据。用如下代码在GameplayScreen类中创建logic:
(Code Snippet – Game Development with XNA – Gameplay Screen – Highscore storage methods)
C#
#region Highscore loading/saving logic
/// <summary>
/// Saves the current highscore to a text file. The StorageDevice was selected during screen loading.
/// </summary>
private void SaveHighscore()
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("highscores.txt", FileMode.Create, isf))
{
using (StreamWriter writer = new StreamWriter(isfs))
{
writer.Write(highScore.ToString(System.Globalization.CultureInfo.InvariantCulture));
writer.Flush();
writer.Close();
}
}
}
}
/// <summary>
/// Loads the high score from a text file. The StorageDevice was selected during the loading screen.
/// </summary>
private void LoadHighscore()
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isf.FileExists("highscores.txt"))
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("highscores.txt", FileMode.Open, isf))
{
using (StreamReader reader = new StreamReader(isfs))
{
try
{
highScore = Int32.Parse(reader.ReadToEnd(), System.Globalization.CultureInfo.InvariantCulture);
}
catch (FormatException)
{
highScore = 10000;
}
finally
{
if (reader != null)
reader.Close();
}
}
}
}
}
}
#endregion
20. 在GameplayScreen 类中创建Start方法来开始一个新游戏:
(Code Snippet – Game Development with XNA – Gameplay Screen – Start method)
C#
/// <summary>
/// Starts a new game session, setting all game states to initial values.
/// </summary>
void Start()
{
if (gameOver)
{
player.Score = 0;
player.Lives = 3;
player.RespawnTimer = 0.0f;
gameOver = false;
aliens.Clear();
alienBullets.Clear();
playerBullets.Clear();
Respawn(0.0f);
}
transitionRate = 0.0f;
transitionFactor = 0.0f;
levelKillCount = 5;
baseLevelKillCount = 5;
alienScore = 25;
alienSpawnRate = 1.0f;
alienMaxAccuracy = 0.25f;
alienSpeedMin = 24.0f;
alienSpeedMax = 32.0f;
alienSpawnRate = 2.0f;
alienSpawnTimer = alienSpawnRate;
nextLife = 5000;
}
21. 打开 ParticleSystem.cs文件
22. 添加如下命名申明:
(Code Snippet – Game Development with XNA – ParticleSystem – using statement)
C#
using AlienGame;
23. 在这个步骤,你将添加两个方法。一个用来创建玩家控制坦克时泥土/灰尘的效果。一个用来创建玩家发射子弹时,火焰的效果。添加如下方法到ParticleSystem 类中:
(Code Snippet – Game Development with XNA – ParticleSystem – Helper player effects methods)
C#
/// <summary>
/// Creates the mud/dust effect when the player moves.
/// </summary>
/// <param name="position">Where on the screen to create the effect.</param>
public void CreatePlayerDust(Player player)
{
for (int i = 0; i < 2; ++i)
{
Particle p = CreateParticle();
p.Texture = smoke;
p.Color = new Color(125, 108, 43);
p.Position.X = player.Position.X + player.Width * (float)random.NextDouble();
p.Position.Y = player.Position.Y + player.Height - 3.0f * (float)random.NextDouble();
p.Alpha = 1.0f;
p.AlphaRate = -2.0f;
p.Life = 0.5f;
p.Rotation = 0.0f;
p.RotationRate = -2.0f + 4.0f * (float)random.NextDouble();
p.Scale = 0.25f;
p.ScaleRate = 0.5f;
p.Velocity.X = -4 + 8.0f * (float)random.NextDouble();
p.Velocity.Y = -8 + 4.0f * (float)random.NextDouble();
}
}
/// <summary>
/// Creates the effect for when the player fires a bullet.
/// </summary>
/// <param name="position">Where on the screen to create the effect.</param>
public void CreatePlayerFireSmoke(Player player)
{
for (int i = 0; i < 8; ++i)
{
Particle p = CreateParticle();
p.Texture = smoke;
p.Color = Color.White;
p.Position.X = player.Position.X + player.Width / 2;
p.Position.Y = player.Position.Y;
p.Alpha = 1.0f;
p.AlphaRate = -1.0f;
p.Life = 1.0f;
p.Rotation = 0.0f;
p.RotationRate = -2.0f + 4.0f * (float)random.NextDouble();
p.Scale = 0.25f;
p.ScaleRate = 0.25f;
p.Velocity.X = -4 + 8.0f * (float)random.NextDouble();
p.Velocity.Y = -16.0f + -32.0f * (float)random.NextDouble();
}
}
24. 编译并运行该应用程序。 选择"开始游戏"菜单项,并享受你自己编写的游戏吧。
Note: 在模拟器中我们使用桌面的键盘移动车辆,您需要首先按PAUSE /BREAK 键。 这将开启或者关闭仿真程序软件输入面板 (SIP) 或者桌面的键盘。因为键盘不能同时作为仿真程序软件输入面板 (SIP)和桌面的键盘,这个问题将在后面的版本里解决。

图1
完工的游戏
恭喜你,你终于完成了一个属于你自己的winphone7手机程序。
在今天的部分的过程中,您创建包括玩家和异形的移动计算、 命中的检测、 屏幕绘制,和其他一些的 AlienGame 逻辑。
虽然这个游戏完成了,但是我想大家可能有更多的疑问和想改进的地方,那让我们进入
手把手教用XNA开发winphone7游戏中级教程