iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 5
0
自我挑戰組

暫時停止遊戲,寫遊戲!系列 第 5

[Day05] 剪刀石頭布猜拳遊戲Part1.繪出遊戲場景


每一個遊戲設計都會從資源載入及繪製起始畫面開始,
這裡我們專以一篇文章來介紹如何進行,在之後的遊戲就不會再贅述此過程。

目標

  • 載入遊戲所需資源檔案
  • 繪出場景,顯示開始畫面

可學到的東西

  • 載入遊戲資源
  • 繪圖函數 sprite

資源

目標: 載入遊戲資源

在遊戲直接調用資源都要先載入到開發環境中,不然有有可能出現錯誤,請特別注意。
這裡我們先載入開始畫面以及之後要用到的圖片。

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // 替每張圖片宣告一個變數
        Texture2D image1, image2, image3, imageGameStart, imagebackgroud;
  • 在 LoadContent() 執行階段載入資源
    把上面宣告的變數,一一對應檔案名稱後載入,
    注意! 檔案名稱是不需要副檔名的
            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
            image1 = Content.Load<Texture2D>("Scissor400x600");
            image2 = Content.Load<Texture2D>("Rock400x600");
            image3 = Content.Load<Texture2D>("Paper400x600");
            imageGameStart = Content.Load<Texture2D>("GameStart1024x768");
            imageBackgroud = Content.Load<Texture2D>("background1024x768");   
        }

目標: 繪出場景

在遊戲中要繪製圖型、字型等,只需要XNA提到的Sprite 即可,
不只可顯示於畫面上,還可以對圖片做放大、縮小等功能。
此 Sprite 是遊戲專案內建中,就是一開始就幫我們宣告的 SpriteBatch spriteBatch;

  • 先在 Initialize() 執行階段初始化畫面大小
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.ApplyChanges();

            base.Initialize();
        }
  • 在 Draw() 執行階段使用 Sprite 進行畫圖
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            // sprite.draw 的使用方法很多種,這裡載入的只有
            // 參數1表示圖片,參數2表示顯示圖片的起始位置,參數3表示填色,White表示不填色
            spriteBatch.Draw(imageGameStart,Vector2.Zero,Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
  • 按下功能表的開始,執行看看是否成功

上一篇
[Day04] 剪刀石頭布猜拳遊戲
下一篇
[Day06] 剪刀石頭布猜拳遊戲Part2.玩家出拳
系列文
暫時停止遊戲,寫遊戲!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言