iT邦幫忙

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

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

[Day07] 剪刀石頭布猜拳遊戲Part3.電腦出拳

  • 分享至 

  • xImage
  •  


玩家出完拳,再來就要處理電腦出拳的部份了,
從這裡到下一篇的判斷輸贏,都是在程式開發上了,小心不要睡著啦~

目標: 電腦出拳

可學到的東西: 無比的耐心

資源

目標實作: 電玩出拳

電腦出拳分面,首先和玩家一樣,要思考一下需要儲存什麼資訊以利後面判斷。
接著比較不一樣的地方在於,玩家使用鍵盤出拳,而電腦使用隨機的方式來出拳。

  • 宣告電腦出拳的變數
// 紀錄玩家出拳,0=初始值,1=剪刀,2=石頭,3=布
int computer = 0;
// 宣告隨機的變數,之後再產生數字
Random random = new Random();
  • 在Update()執行階段中,產生數字給電腦的出拳
    隨機的第一個參數是最小值,第二個參數是最大值,產生的值會介於最小值到最大值-1的整數
// 隨機產生1-3數字
computer = random.Next(1, 4);
  • 在Draw()執行階段將電腦隨機之拳繪出
    與玩家出拳貼圖在不一樣的座標,不要忘記改了!
// 繪製電腦出拳的貼圖
if (computer == 1)
{
    spriteBatch.Draw(image1, new Vector2(50, 100), Color.White);
}
if (computer == 2)
{
    spriteBatch.Draw(image2, new Vector2(50, 100), Color.White);
}
if (computer == 3)
{
    spriteBatch.Draw(image3, new Vector2(50, 100), Color.White);
}
  • 按下功能表的開始,執行看看是否成功

Source Cde

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 GuessFingerMoocs
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
 
        // 替每張圖片宣告一個變數
        Texture2D image1, image2, image3, imageGameStart, imageBackgroud;
 
        // 紀錄玩家出拳,0=初始值,1=剪刀,2=石頭,3=布
        int player = 0;
        // 紀錄場景,0=開始畫面,1=出拳畫面
        int gameState = 0;
        // 紀錄玩家出拳,0=初始值,1=剪刀,2=石頭,3=布
        int computer = 0;
        // 宣告隨機的變數,之後再產生數字
        Random random = new Random();
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
 
        /// <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
            // 設定起始畫面大小
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.ApplyChanges();
 
            base.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
            // 載入變數,並對應每個圖片變數
            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");   
        }
 
        /// <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
        }
 
        /// <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 exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
 
            // TODO: Add your update logic here
            // 取得鍵盤按下的狀態
            KeyboardState newState = Keyboard.GetState();
            // 按下ESC 可以離開遊戲
            if (newState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            // 按下數字鍵1,把玩家出拳的變數值修改為1,代表剪刀
            if (newState.IsKeyDown(Keys.D1))
            {
                player = 1;
            }
            // 按下數字鍵1,把玩家出拳的變數值修改為2,代表石頭
            if (newState.IsKeyDown(Keys.D2))
            {
                player = 2;
            }
            // 按下數字鍵1,把玩家出拳的變數值修改為3,代表布
            if (newState.IsKeyDown(Keys.D3))
            {
                player = 3;
            }
 
            if (newState.IsKeyDown(Keys.D1) || newState.IsKeyDown(Keys.D2) || newState.IsKeyDown(Keys.D3))
            {
                gameState = 1;
            }
 
            // 隨機產生1-3數字
            computer = random.Next(1, 4);
 
                base.Update(gameTime);
        }
 
        /// <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
            spriteBatch.Begin();
            // sprite.draw 的使用方法很多種,這裡載入的只有3個參數
            // 參數1表示要顯示的圖片,參數2表示顯示圖片的起始位置,參數3表示填色,White表示不填色
            // 原本繪出開始畫面也要更改,只有當還沒出拳時,才會繪製開始畫面
            if (gameState == 0)
            {
                spriteBatch.Draw(imageGameStart, Vector2.Zero, Color.White);
            }
            if (gameState == 1)
            {
                // 繪製出拳後的遊戲背景
                spriteBatch.Draw(imageBackgroud, Vector2.Zero, Color.White);
                // 繪製玩家出拳的貼圖
                if (player == 1)
                {
                    spriteBatch.Draw(image1, new Vector2(550,100), Color.White);
                }
                if (player == 2)
                {
                    spriteBatch.Draw(image2, new Vector2(550, 100), Color.White);
                }
                if (player == 3)
                {
                    spriteBatch.Draw(image3, new Vector2(550, 100), Color.White);
                }
 
 
                // 繪製電腦出拳的貼圖
                if (computer == 1)
                {
                    spriteBatch.Draw(image1, new Vector2(50, 100), Color.White);
                }
                if (computer == 2)
                {
                    spriteBatch.Draw(image2, new Vector2(50, 100), Color.White);
                }
                if (computer == 3)
                {
                    spriteBatch.Draw(image3, new Vector2(50, 100), Color.White);
                }
            }
            spriteBatch.End();
 
            base.Draw(gameTime);
        }
    }
}

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

尚未有邦友留言

立即登入留言