รู้จักกับ XNA Game Studio Project for Windows Phone
รู้จักกับ XNA Game Studio Project for Windows Phone หลังจากที่ติดตั้ง Windows Phone SDK และทำการสร้าง Project บน Windows Phone จะมี Project Type ชื่อว่า XNA Game Studio ซึ่งเป็น Project ไว้สำหรับการสร้าง Game ที่จะถูกใช้กับ Windows Phone โดยการเขียน Application ที่เป็น XNA Game จะใช้รูปแบบคำสั่ง Library จาก .NET Framework เช่นเดียว สามารถเขียนได้ทั้ง VB.NET และ C#
ทดสอบโปรแกรม XNA Game Project for Windows Phone
เลือก File -> New -> Project
เลือกเป็น XNA Game Studio 4.0 และเลือกเป็น Windows Phone Game (4.0) และกำหนดชื่อโปรเจคด้านล่าง
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace WindowsPhoneGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
string text = "Hello, Windows Phone 7!";
SpriteFont segoe14;
Vector2 textPosition;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
// Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
/// <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();
}
/// <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
segoe14 = this.Content.Load<SpriteFont>("Segoe14");
Vector2 textSize = segoe14.MeasureString(text);
Viewport viewport = this.GraphicsDevice.Viewport;
textPosition = new Vector2((viewport.Width - textSize.X) / 2,
(viewport.Height - textSize.Y) / 2);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <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.DrawString(segoe14, text, textPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Game1.vb (VB.NET)
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Input.Touch
Imports Microsoft.Xna.Framework.Media
Namespace WindowsPhoneGame
''' <summary>
''' This is the main type for your game
''' </summary>
Public Class Game1
Inherits Microsoft.Xna.Framework.Game
Private graphics As GraphicsDeviceManager
Private spriteBatch As SpriteBatch
Private text As String = "Hello, Windows Phone 7!"
Private segoe14 As SpriteFont
Private textPosition As Vector2
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Content.RootDirectory = "Content"
' Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333)
' Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1)
End Sub
''' <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 Overrides Sub Initialize()
' TODO: Add your initialization logic here
MyBase.Initialize()
End Sub
''' <summary>
''' LoadContent will be called once per game and is the place to load
''' all of your content.
''' </summary>
Protected Overrides Sub 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
segoe14 = Me.Content.Load(Of SpriteFont)("Segoe14")
Dim textSize As Vector2 = segoe14.MeasureString(text)
Dim viewport As Viewport = Me.GraphicsDevice.Viewport
textPosition = New Vector2((viewport.Width - textSize.X) / 2, (viewport.Height - textSize.Y) / 2)
End Sub
''' <summary>
''' UnloadContent will be called once per game and is the place to unload
''' all content.
''' </summary>
Protected Overrides Sub UnloadContent()
' TODO: Unload any non ContentManager content here
End Sub
''' <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 Overrides Sub Update(gameTime As GameTime)
' Allows the game to exit
If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
Me.[Exit]()
End If
' TODO: Add your update logic here
MyBase.Update(gameTime)
End Sub
''' <summary>
''' This is called when the game should draw itself.
''' </summary>
''' <param name="gameTime">Provides a snapshot of timing values.</param>
Protected Overrides Sub Draw(gameTime As GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
' TODO: Add your drawing code here
spriteBatch.Begin()
spriteBatch.DrawString(segoe14, text, textPosition, Color.White)
spriteBatch.[End]()
MyBase.Draw(gameTime)
End Sub
End Class
End Namespace
สำหรับข้อมูลการเขียนโปรแกรม XNA Game บน Windows Phone นั้นยังถือว่าน้อยมาก แต่ก็เห็นมีอยู่หลายคลิปในเว็บไซต์ของยูทูป เช่น