程序環境: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(); //spriteBatch.Draw(dog, Vector2.Zero, null, Color.White); spriteBatch.Draw(dog, beginPosition, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0); spriteBatch.End(); base.Draw(gameTime); }
這就是一個簡單的XNA圖片運動的程序
在wpf中,可以通過Animation來指定開始和結束進行動畫,和XNA能做到相同的效果。
只是筆者個人做3D的時候遇到一些瓶頸,wpf有些滿足不了想法,所以試着學一下XNA看看能不能解決,以后會繼續看下去,僅僅是個人愛好
若有相似想法,可以討論
源代碼:files.cnblogs.com/fish124423/XNA.rar