グラディウスのオプション



オプションといったらこれ!
シューティングゲームの金字塔「グラディウス」のオプションを作ってみましょう。

・・・といっても、ツインビーのオプションとほぼ同じです。何が違うのか?



ツインビーの場合、自機が動かないときもオプションが動き続け、じっとしているとオプションが自機の位置に戻ってきます。
一方、グラディウスの場合、自機が動かないときはオプションも動きません。

個人的にはグラディウスのオプションのほうが好きです。好きな場所にオプションを配置しておくことができるので、敵の出現パターンや地形を先読みしてあらかじめオプションを配置しておくという戦略性が生まれるからです。




サンプルコード

まあ・・・ツインビーのときとほぼ一緒です・・・

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;

namespace Gradius
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        // プレイヤー移動速度
        const float PlayerSpeed = 4.0f;
        // プレイヤーの位置の履歴の個数
        const int HistorySize = 40;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // ぱっちぃのテクスチャ
        Texture2D texturePacchi;
        // かまトゥのテクスチャ
        Texture2D textureKamatoo;
        // プレイヤーの位置
        Vector2 playerPosition = new Vector2(200, 200);
        // プレイヤーの位置の履歴
        List<Vector2> positionHistory = new List<Vector2>();
        // オプションの位置
        Vector2 option0Position;
        Vector2 option1Position;
        Vector2 option2Position;
        Vector2 option3Position;

        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

            // 履歴を初期化。とりあえず現在の位置で埋めておく。
            for (int i = 0; i < HistorySize; i++)
            {
                positionHistory.Add(playerPosition);
            }

            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

            // 画像を読み込む
            texturePacchi = Content.Load<Texture2D>("pacchi_32");
            textureKamatoo = Content.Load<Texture2D>("kamatoo_32");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific 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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            // 移動前の位置をとっておく
            Vector2 prePosition = playerPosition;

            // 移動したか? 移動したらtrue
            bool moved = false;

            // 上下左右が押されたらプレイヤーを移動させる
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                playerPosition.X -= PlayerSpeed;
                moved = true;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                playerPosition.X += PlayerSpeed;
                moved = true;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                playerPosition.Y -= PlayerSpeed;
                moved = true;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                playerPosition.Y += PlayerSpeed;
                moved = true;
            }

            // 移動が行われたときだけ、履歴を更新する
            if (moved)
            {
                // プレイヤーの位置(1フレーム前のやつ)を履歴の先頭に挿入する
                positionHistory.Insert(0, prePosition);

                // 最も古い履歴を除去する
                positionHistory.RemoveAt(positionHistory.Count - 1);
            }

            // オプションの位置を更新する
            option0Position = positionHistory[9];
            option1Position = positionHistory[19];
            option2Position = positionHistory[29];
            option3Position = positionHistory[39];

            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();

            // オプションを描画
            spriteBatch.Draw(textureKamatoo, option3Position, Color.White);
            spriteBatch.Draw(textureKamatoo, option2Position, Color.White);
            spriteBatch.Draw(textureKamatoo, option1Position, Color.White);
            spriteBatch.Draw(textureKamatoo, option0Position, Color.White);

            // プレイヤーを描画
            spriteBatch.Draw(texturePacchi, playerPosition, Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}







解説

ツインビーのときとほぼ同じなので、解説はそちらを参照してください!



色んなオプションを作る目次へ戻る


0 件のコメント:

コメントを投稿