跟我一起學XNA(1)讓物體動起來①(附源碼)

來源:互聯網
上載者:User

程式環境: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();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.