程式環境:VS2010和XNA Game Studio 4.0外掛程式,具體可以下載
http://xbox.create.msdn.com/zh-CN/resources/downloads 這個是筆者的
1.建立一個項目
選中Windows Game(4.0),建立
會有兩個工程,上面一個工程負責代碼實現,下面的content則包含了項目所有資源,圖片聲音模型
首先,先看一下One(move)這個工程,它下面有個Game1,program這些檔案,開啟Game1
/// <summary> /// 所有的game都繼承自Microsoft.Xna.Framework.Game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { //它提供了訪問PC、Xbox360以及wp7圖形裝置的途徑 GraphicsDeviceManager graphics; //繪製精靈,可以理解為一個2D,3D映像 SpriteBatch spriteBatch; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// 初始化各種資料和方法,和.net的Initialize()差不多的意義,這裡是程式執行的第一步 /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); } /// <summary> /// 這裡載入各種資源 /// </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 } /// <summary> /// 釋放資源 /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// 這裡會進行一個重新整理動作,預設每秒鐘60次,這樣就相當於介面在動態 /// </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> /// 這裡是和Update相同的,動態重新整理,但是盡量在這裡少做處理,所有的處理,邏輯,最好放在Update中 /// </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的生命週期是:Initialize()->LoadContent()->Update()和Draw()迴圈->UnLoadContet()
接著我加入一張圖片,你可以右鍵添加,也可以直接複製,然後包含在項目中
載入圖片
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); dog = Content.Load<Texture2D>(@"Image/dog"); // TODO: use this.Content to load your game content here }
在Draw函數中,讓載入後的圖片畫出來
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(dog, Vector2.Zero, null, Color.White); spriteBatch.End(); base.Draw(gameTime); }
運行,就能看到圖片了
然後,我們要讓圖片動起來,我們在程式裡定義一個圖片初始位置和一個圖片的運行速度
Vector2 beginPosition = Vector2.Zero; float speed = 3f;
在Update中加入判斷
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 beginPosition.X += speed; if (beginPosition.X > Window.ClientBounds.Width - dog.Width || beginPosition.X < 0) speed *= -1; base.Update(gameTime); }
Draw函數中
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();