XNA 4.0 簡介 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 XNA 簡介 XNA Framework...

Post on 29-Dec-2015

234 views 7 download

Transcript of XNA 4.0 簡介 靜宜大學資工系 蔡奇偉 副教授 © 2011. 大綱 XNA 簡介 XNA Framework...

XNA 4.0 簡介靜宜大學資工系蔡奇偉 副教授© 2011

大綱• XNA 簡介• XNA Framework• XNA Build• XNA Game Studio• 建立 XNA 4 的專案• 方案的目錄結構• XNA Game Studio 4.0 參考手冊• Hello, XNA• XNA Game Loop• Add a Sprite• 參考資料

XNA 簡介• Provided by Microsoft

• A set of tools with a managed runtime environment that facilitates computer game development and management.

• XNA:

• XNA is Not an Acronymed

• Xbox New Architecture

• cross(X)-platform, Next-generation Architecture

• Available on Windows, Xbox 360, and Windows Phone 7

System Requirement

XNA Framework

• Based on

• .NET Compact Framework 2.0 for Xbox 360 development

• .NET Framework 2.0

• Runs on a version of the Common Language Runtime that is optimized for gaming to provide a managed execution environment.

• Only supports C# programming language

• Integrates with a number of tools to aid in content creation

• Supports both 2D and 3D game creation

• Allows use of the Xbox 360 controllers and vibrations

XNA Build• A game asset pipeline describes the process by which

game content are modified to a form suitable for use by the gaming engine.

• XNA Build is a set of game asset pipeline management tools.

• Helps identify the pipeline dependencies.

• Provides API access to enable further processing of the dependency data.

• The dependency data can be analyzed to help reduce the size of a game by finding content that is not actually used.

XNA Game Studio

版本 發表日期 VC# & VS版本

XNA Game Studio Express 12/2006 2003

XNA Game Studio 2.0 12/2007 2005

XNA Game Studio 3.0 10/2008 2008

XNA Game Studio 3.1 06/2009 2008

XNA Game Studio 4.0 09/2010 2010

XNA Game Studio is an integrated development environment (IDE) for development of games.

XNA Indie Games

Xbox 360 games written in XNA Game Studio can be submitted to the Creators

Club community, for which premium membership is required, this costs US$49

for 4 months or US$99/year. All games submitted to the community are

subjected to peer review by other creators. If the game passes review then it is

listed on Xbox Live Marketplace. Creators can set a price of 80, 240 or 400

points for their game. The creator is paid 70% of the total revenue from their

game sales as a baseline. Microsoft originally planned to take an additional

percentage of revenue if they provided additional marketing for a game, but this

policy was rescinded in March 2009, leaving the flat rate intact regardless of

promotion.

新增專案

新增 XNA Windows Game 專案

指定專案名稱

專案名稱,也將是專案目錄、名稱空間與執行檔的名稱

設定方案目錄之所在

方案目錄所在位置

指定方案名稱

方案名稱,亦為方案目錄的名稱

為方案建立目錄

確定專案之建立

專案之 IDE 視窗

方案總管

程式專案

內容專案

方案的目錄結構

XNA Game Studio 4.0 參考手冊

Hello, XNA// File: Program.csusing System;

namespace xnatest{#if WINDOWS || XBOX static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } }#endif}

定義在 Game1.cs 檔案中的類別

// File: Game1.cs

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.Media;

namespace xnatest{ /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game

可能用到的系統類別庫

繼承並依需求改寫後頁所述的方法

錄目此為定設

graphics

Handles the configuration and management of the graphics device

spriteBatch

Enables a group of sprites to be drawn using the same settings.

/// <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();

}

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

}

LoadContent ()

UnloadContent ()

/// <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

}

Update ()

/// <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 exitif (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);}

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

base.Draw(gameTime);

}

XNA Game Loop

Initialize() LoadContent ()

Game quit?

Draw ()

UnLoadContent ()

Y

Update ()

N

Exit

Ga

me

Lo

op

Add a Sprite

• 把 sprite 所用的圖片檔放入 Content 目錄1. 在 Content 目錄中建立 Images 子目錄

2. 在 Images 目錄中加入圖片檔

• 載入 Sprite

• 繪製 Sprite

在 Content 目錄中建立 Images 子目錄 - I

按滑鼠右鍵

在 Content 目錄中建立 Images 子目錄 - II

在 Images 目錄中加入圖片檔 - I

按滑鼠右鍵

在 Images 目錄中加入圖片檔 - II

按滑鼠右鍵

圖片的屬性頁

定義一個 Texture2D 型態的資料成員

載入圖片並存入 texture 中

繪製 sprite

程式執行結果