• 手把手教用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.   停止调试并回到应用程序编辑状态。

     

    在个章节,你创建了新的主游戏类,并重写了游戏基类的功能

  • 相关阅读:
    开心菜鸟系列学习笔记------javascript(5)
    开心菜鸟系列学习笔记-------javascript(3)
    开心菜鸟学习系列-----javascript(2)
    开心菜鸟学习系列笔记-----Javascript(1)
    开心菜鸟系列----函数作用域(javascript入门篇)
    开心菜鸟系列----变量的解读(javascript入门篇)
    开心菜鸟笔记系列---正则表达式笔记(入门篇)
    开心菜鸟笔记系列---博客开篇贴!(求支持)
    在开启bin-log日志下Mysql报错
    开心菜鸟学习系列学习笔记------------nodejs util公共函数
  • 原文地址:https://www.cnblogs.com/zouyuntao/p/1874267.html
Copyright © 2020-2023  润新知