【WP7进阶】——XNA游戏初探

之前一直在做Android 的软件应用,包括现在学习Wp7 开发也一直未接触过游戏方面的开发。这几天对XNA开发粗略的看了一下,也找了相关的文档,觉得对于学习XNA方式开发WP7游戏应用应该是一件很酷的事。也是对自己本身职业技能的提高吧。

学习XNA开发游戏需要的工具

 

  • Visual Studio 2010 前提是安装了Developer Tools 套件 或Express for Windows Phone
  • Microsoft Cross-Platform Audio Creation Tool 3 (XACT3):XACT 是一个由 Microsoft 开发的跨平台的音效建立工具,可以协助您建立游戏程序需要使用的音效资源。
  • XNA Framework Remote Performance Monitor:XNA Framework Remote Performance Monitor 是一个可以在游戏运行时间分析游戏效能的工具,协助程序设计师分析游戏程序的执行效能。

XNA 游戏程序架构的属性和方法

 

      使用IDE创建一个Windows Phone Game (4.0)的解决方案。这里主要学习的是Game1.cs 这个文件,该文件默认为游戏的程序入口,而且该类默认继承自 Microsoft.Xna.Framework.Game,该模板会预设为我们重写几个游戏运行需要的方法,方法列表如下:

  • public Game1() 构造函数
  •  protected override void Initialize() 负责初始化动作
  •  protected override void LoadContent()  负责加载资源
  •  protected override void UnloadContent() 负责释放资源动作
  •  protected override void Update(GameTime gameTime)负责更新游戏的状态,但不负责更新游戏的画面
  • protected override void Draw(GameTime gameTime)跟update 相反。Draw 负责更新游戏的画面

其中Game1 还有两个比较常用的方法没有默认为我们重写,这两个方法也是比较常用,分别为:

  • protected override bool BeginDraw()     开始绘图的动作
  • protected override void EndDraw()   绘图动作结束

Game1比较常用的属性,可见下表:

 

属性名称说明
Components管理所有 GameComponent 的集合
Content取得 ContentManager 对象的属性
GraphicsDevice取得图型装置对象的属性
IsActive判断游戏程序的窗口目前是否在作用中
IsFixedTimeStep控制游戏程序要使用固定更新模式或是可变更新模式
TargetElapsedTime当 IsFixedTimeStep 属性的内容值为 true 时,控制 Update 方法被呼叫的频率的属性


继续查看Game1 文件生成的代码,最上方默认声明了两个变量:

 

 

// 以XNA为基础的游戏程序必须在初始化的时候声明GraphicsDeviceManager的对象,并设定游戏界面的高度与宽度
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch; 
// SpriteBatch 主要显示2D图像,包括游戏背景、游戏人物、游戏的状态和菜单

 

在Gamer1 的构造函数中可以通过如下代码创建一个GraphicsDeviceManager 对象

 

  public  Game1()
        {
            graphics 
=   new  GraphicsDeviceManager( this );
            Content.RootDirectory 
=   " Content " ;
            graphics.PreferredBackBufferHeight 
=   480 ;   // 创建一个画面高度为480的屏幕
            graphics.PreferredBackBufferWidth  =   800 ;     // 创建一个画面宽度为800的屏幕
            
// 负责控制游戏程序更新状态及显示内容的频率
            TargetElapsedTime  =  TimeSpan.FromTicks( 333333 );  // 设定游戏每秒更新30次,这里并不一定适合PC机或XBOX
            
        }

GraphicsDeviceManager 对象常用的属性可见下表:

 

 

属性名称说明
IsFullScreen控制游戏程序的窗口是否要以全屏幕的方式显示
PreferredBackBufferFormat屏幕缓冲区的格式
PreferredBackBufferHeight屏幕缓冲区的高度
PreferredBackBufferWidth屏幕缓冲区的宽度

 

 

GraphicsDeviceManager 对象常用的方法:

 

方法名称说明
ToggleFullScreen在窗口模式和全屏幕模式中切换

 

Demo 演示

下面通过一个小DEMO来了解XNA for Windows Phone 的游戏框架,先看下图演示:

整个过程的代码量并不多,大部分都由XNA模板为我们生成,全部代码如下:

 

  // 以XNA为基础的游戏程序必须在初始化的时候声明GraphicsDeviceManager的对象,并设定游戏界面的高度与宽度
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch; 
// SpriteBatch 主要显示2D图像,包括游戏背景、游戏人物、游戏的状态和菜单

        
public  Game1()
        {
            graphics 
=   new  GraphicsDeviceManager( this );
            Content.RootDirectory 
=   " Content " ;
            graphics.PreferredBackBufferHeight 
=   480 ;   // 创建一个画面高度为480的屏幕
            graphics.PreferredBackBufferWidth  =   800 ;     // 创建一个画面宽度为800的屏幕
            
// 负责控制游戏程序更新状态及显示内容的频率
            TargetElapsedTime  =  TimeSpan.FromTicks( 333333 );  // 设定游戏每秒更新30次,这里并不一定适合PC机或XBOX
            
        }

        
///   <summary>
        
///  Allows the game to perform any initialization it needs to before starting to run.
        
///  This is where it can query for any required services and load any non-graphic
        
///  related content.  Calling base.Initialize will enumerate through any components
        
///  and initialize them as well.
         
///  负责游戏的初始化动作
        
///   </summary>
         protected   override   void  Initialize()
        {
            
//  TODO: Add your initialization logic here

            
base .Initialize();
        }
        Texture2D user 
=   null // 声明一个可加载图像素材的Texture2D
         ///   <summary>
        
///  LoadContent will be called once per game and is the place to load
        
///  all of your content.
        
///   </summary>
         protected   override   void  LoadContent()
        {
            
//  Create a new SpriteBatch, which can be used to draw textures.
            
            spriteBatch 
=   new  SpriteBatch(GraphicsDevice);

            
//  TODO: use this.Content to load your game content here
            user  =  Content.Load < Texture2D > ( " user " );    // 通过Content 项目将user的图片加载进来
            
        }

        
///   <summary>
        
///  UnloadContent will be called once per game and is the place to unload
        
///  all content.
        
///    负责执行释放资源的动作
        
///  以XNA为基础的应用程序只需要释放非 ContentManager 管理的资源即可,
        
///  即未通过ContentManager的Add 方法加入到ContentManager 进行管理的资源
        
///  才有需要在此方法执行翻译资源的动作
        
///   </summary>
         protected   override   void  UnloadContent()
        {
            
//  TODO: Unload any non ContentManager content here
        }

        
int  pos  =   0 ;   // 记录人物当前的位置  

        
// 负责更新游戏的状态,但不负责更新游戏的画面
         ///   <summary>
        
///  Allows the game to run logic such as updating the world,
        
///  checking for collisions, gathering input, and playing audio.
        
///   </summary>
        
///   <param name="gameTime"> Provides a snapshot of timing values. </param>
         protected   override   void  Update(GameTime gameTime)
        {
            
//  Allows the game to exit
            
// 如果在游戏中按下BACK键则结束游戏
             if  (GamePad.GetState(PlayerIndex.One).Buttons.Back  ==  ButtonState.Pressed)
                
this .Exit();

            
if  (pos  <=   300 )
            {
                pos
++ ;

            }
           
            
//  TODO: Add your update logic here
           
            
base .Update(gameTime);
        }

     

        
// Vector2 2维空间坐标
        
// 跟update 相反。Draw 负责更新游戏的画面
         ///   <summary>
        
///  This is called when the game should draw itself.
        
///   </summary>
        
///   <param name="gameTime"> Provides a snapshot of timing values. </param>
         protected   override   void  Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            
//  TODO: Add your drawing code here
            spriteBatch.Begin();    // 给spriteBatch下命令准备开始动作

            Vector2 vect 
=   new  Vector2(pos,  10 ); // 创建一个x为用户所在位置,y为10的Vector2对象
            
// 将user 按Vector2指定的位置开始画图
            spriteBatch.Draw(user, vect, Color.White);
           
            
// 停止时打印出stop
            spriteBatch.End(); // 给spriteBatch下命令结束动作
             base .Draw(gameTime);
        }
    }

2D与坐标

Texture2D是用来管理2D图形资源,常用属性为:

 

属性名称说明
Bounds代表图形资源的大小
Format代表图形资源的格式
GraphicsDevice取得游戏程序使用的绘图装置的属性
Height图形资源的高度(单位:pixel)
Width图形资源的宽度(单位:pixel)

 

Vector2是用来加载2D图像资源显示到游戏界面时定义的空间坐标

引用之Windows Phone 官网

Vector2 的结构数据成员为

 

数据成员名称说明
X代表坐标点的X轴的位置
Y代表坐标点的Y轴的位置

 

 

常用到的方法列表:

 

方法名称说明
Add对坐标点执行加法运算
Clamp限制坐标内容值必须落在指定的范围之间
Distance计算两个坐标点之间的距离
DistanceSquared计算两个坐标点之间的距离的平方
Divide对坐标点执行除法运算
Equals判断坐标点是否等于指定的坐标点
Lerp计算两个坐标点之间的线性内插
Max计算坐标点的最大值
Min计算坐标点的最小值
Multiply对坐标点执行乘法运算
Negate对坐标点执行反运算
Subtract对坐标点执行减法运算
Transform对坐标点执行转置 (Transform) 运算

 

 

总体上来说XNA游戏开发,可以同时向多个平台延伸,只要注意好尺寸的不同和微调下代码即可。

 

DEMO下载:源码

 

本文参考自Windows Phone 7 官方教程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值