手把手教用XNA开发winphone7游戏(三)
XNA Game Studio 游戲循環
在這部分中您將重點兩剩余部分的游戲 — — 重寫Update 和 Draw 功能。有些大大可能看過相關微軟的訓練包,我這里主要是幫一些初學者。希望各位大大包含,畢竟文章發出來還是有工作量的。大家覺得有用就好,要是沒有耽誤時間給大家道個歉。(感謝http://winphone.us/)
1.?????? 打開 BackgroundScreen.cs文件。
2.?????? 重寫基類Update 方法如下:
(Code Snippet – Game Development with XNA – Background Screen Update method)
C#
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
???????????????? ?bool coveredByOtherScreen)
{
?? base.Update(gameTime, otherScreenHasFocus, false);
}
?
3.?????? 重寫基類方法繪制。 繪圖方法將繪制圖形設備上使用 Microsoft.Xna.Framewok.Graphics 命名空間中的 SpriteBatch 類。一組sprites被繪制的時候使用同樣的設置。改變 Draw 方法來匹配下面的代碼段:
(Code Snippet – Game Development with XNA – Background Screen Draw method)
C#
public override void Draw(GameTime gameTime)
{
?? SpriteBatch spriteBatch = 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).
?? float transitionOffset = ???? (float)Math.Pow(TransitionPosition, 2);
?
?? spriteBatch.Begin();
?
?? // Draw Background
?? spriteBatch.Draw(background, new Vector2(0, 0),
???? new Color(255, 255, 255, TransitionAlpha));
?
?? // Draw Title
?? spriteBatch.Draw(title, new Vector2(60, 55),
???? new Color(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#
using AlienGameSample;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using System.IO.IsolatedStorage;
using System.IO;
?
8.?????? 從 GameScreen 派生:
C#
class GameplayScreen : GameScreen
{
}
?
9.?????? 添加以下類變量 (將在比賽中使用它們)。 后面我們使用這些變量,處理游戲邏輯、 用戶輸入和繪圖:
(Code Snippet – Game Development with XNA – Gameplay Screen variables)
C#
//
// Game Play Members
//
Rectangle worldBounds;
bool gameOver;
int baseLevelKillCount;
int levelKillCount;
float alienSpawnTimer;
float alienSpawnRate;
float alienMaxAccuracy;
float alienSpeedMin;
float alienSpeedMax;
int alienScore;
int nextLife;
int hitStreak;
int highScore;
Random random;
?
//
// Rendering Members
//
Texture2D cloud1Texture;
Texture2D cloud2Texture;
Texture2D sunTexture;
Texture2D moonTexture;
Texture2D groundTexture;
Texture2D tankTexture;
Texture2D alienTexture;
Texture2D badguy_blue;
Texture2D badguy_red;
Texture2D badguy_green;
Texture2D badguy_orange;
Texture2D mountainsTexture;
Texture2D hillsTexture;
Texture2D bulletTexture;
Texture2D laserTexture;
?
SpriteFont scoreFont;
SpriteFont menuFont;
?
Vector2 cloud1Position;
Vector2 cloud2Position;
Vector2 sunPosition;
?
// Level changes, nighttime transitions, etc
float transitionFactor; // 0.0f == day, 1.0f == night
float transitionRate; // > 0.0f == day to night
?
ParticleSystem particles;
?
//
// Audio Members
//
SoundEffect alienFired;
SoundEffect alienDied;
SoundEffect playerFired;
SoundEffect playerDied;
?
//Screen dimension consts
const float screenHeight = 800.0f;
const float screenWidth = 480.0f;
const int leftOffset = 25;
const int topOffset = 50;
const int bottomOffset = 20;
?
10.?? 游戲類構造函數定義 (在游戲屏幕和其他屏幕在游戲中的) 之間的屏幕轉換的速度和大小—— 在處理游戲的所有操作的地方。 添加此類構造函數,如下所示::
(Code Snippet – Game Development with XNA – Gameplay Screen Constructor)
C#
public GameplayScreen()
{
?? random = new Random();
?
?? worldBounds = new Rectangle(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#
public override void LoadContent()
{
??? 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 = new Vector2(224 - cloud1Texture.Width, 32);
??? cloud2Position = new Vector2(64, 80);
?
??? sunPosition = new Vector2(16, 16);
?
??? particles = new ParticleSystem(ScreenManager.Game.Content, ScreenManager.SpriteBatch);
?
??? base.LoadContent();
}
?
12.?? 添加UnloadContent代碼段:
(Code Snippet – Game Development with XNA – Gameplay Screen Unload method)
C#
public override void UnloadContent()
{
??? 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>
public override void Update(GameTime gameTime,
bool otherScreenHasFocus, bool coveredByOtherScreen)
{
?? float elapsed = ?(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>
public override void Draw(GameTime gameTime)
{
?? float elapsedTime = (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#
void StartGameMenuEntrySelected(object sender, EventArgs e)
{
??? ScreenManager.AddScreen(new GameplayScreen());
}
?
16.?? 編譯并運行該應用程序。 單擊"開始游戲"菜單項,可以看到主菜單從屏幕的下方滾動上來。
圖2
運行效果?
Note: 現在游戲的場景你還看不到,不過不要緊,明天我們就開始了,加油!!
?
17.?? 停止調試并回到應用程序編輯狀態。
?
在個章節,你創建了新的主游戲類,并重寫了游戲基類的功能。
?
轉載于:https://www.cnblogs.com/zouyuntao/archive/2010/11/10/1874267.html
總結
以上是生活随笔為你收集整理的手把手教用XNA开发winphone7游戏(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 熊猫“大虾”-03/07/2011开始修
- 下一篇: RoseHA集群:RHEL+RoseMi