0

上部の数字と一致するものをクリックすると、数字のグリッドがランダム化されるゲームを作成しようとしています。

マウス入力のコードはまだありませんが、繰り返しがなく、そのうちの少なくとも 1 つが一番上の数字と一致するように、数字のグリッドをランダム化するにはどうすればよいですか?

また、マウスがそれらのいずれかをクリックしたときにのみ数値が変化し、1/60秒ごとに変化しないようにするにはどうすればよいですか?

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 numbergame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class NumberGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D hpBar, oneTex, twoTex, threeTex, fourTex, fiveTex, sixTex, sevenTex, eightTex, nineTex;
        Rectangle one, two, three, four, five, six, seven, eight, nine, ten;
        SpriteFont font;
        Vector2 score;
        float currentHealth = 100;
        Texture2D[] numGrid = new Texture2D[9];

    System.Random rng = new System.Random();

    private Texture2D GetRandomTexture()
    {
        return numGrid[rng.Next(0,9)];
    }

    public NumberGame()
    {
        graphics = new GraphicsDeviceManager(this);
        IsMouseVisible = true;
        graphics.PreferredBackBufferHeight = 500;
        graphics.PreferredBackBufferWidth = 600;
        Window.Title = "Number Game";
        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()
    {
        score = new Vector2(470,0);

        one = new Rectangle(170, 220, 60, 60);
        two = new Rectangle(270, 220, 60, 60);
        three = new Rectangle(370, 220, 60, 60);

        four = new Rectangle(170, 320, 60, 60);
        five = new Rectangle(270, 320, 60, 60);
        six = new Rectangle(370, 320, 60, 60);

        seven = new Rectangle(170, 420, 60, 60);
        eight = new Rectangle(270, 420, 60, 60);
        nine = new Rectangle(370, 420, 60, 60);

        ten = new Rectangle(270, 100, 60, 60);

        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);
        hpBar = Content.Load<Texture2D>("HealthBar2");
        font = Content.Load<SpriteFont>("SpriteFont1");

        numGrid[0] = Content.Load<Texture2D>("one");
        numGrid[1] = Content.Load<Texture2D>("two");
        numGrid[2] = Content.Load<Texture2D>("three");
        numGrid[3] = Content.Load<Texture2D>("four");
        numGrid[4] = Content.Load<Texture2D>("five");
        numGrid[5] = Content.Load<Texture2D>("six");
        numGrid[6] = Content.Load<Texture2D>("seven");
        numGrid[7] = Content.Load<Texture2D>("eight");
        numGrid[8] = Content.Load<Texture2D>("nine");

        oneTex = numGrid[0];
        twoTex = numGrid[1];
        threeTex = numGrid[2];
        fourTex = numGrid[3];
        fiveTex = numGrid[4];
        sixTex = numGrid[5];
        sevenTex = numGrid[6];
        eightTex = numGrid[7];
        nineTex = numGrid[8];

    }

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

        float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

        if (currentHealth > 0)
        {
            currentHealth = Math.Max(0f, currentHealth - deltaTime * 25);
        }

        base.IsFixedTimeStep = true;
        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);

        spriteBatch.Begin();
        spriteBatch.DrawString(font, "Score:", score, Color.Black);
        spriteBatch.Draw(hpBar, new Rectangle(this.Window.ClientBounds.Width / 2 - hpBar.Width / 2, 30, hpBar.Width, 44), new Rectangle(0, 45, hpBar.Width, 44), Color.Gray);
        spriteBatch.Draw(hpBar, new Rectangle(this.Window.ClientBounds.Width / 2 - hpBar.Width / 2, 30, (int)(hpBar.Width * ((double)currentHealth/ 100)), 44), new Rectangle(0, 45, hpBar.Width, 44), Color.Red);
        spriteBatch.Draw(hpBar, new Rectangle(this.Window.ClientBounds.Width / 2 - hpBar.Width / 2, 30, hpBar.Width, 44), new Rectangle(0, 0, hpBar.Width, 44), Color.White);

        spriteBatch.Draw(oneTex, one, Color.White);
        spriteBatch.Draw(GetRandomTexture(), two, Color.White);
        spriteBatch.Draw(GetRandomTexture(), three, Color.White);
        spriteBatch.Draw(GetRandomTexture(), four, Color.White);
        spriteBatch.Draw(GetRandomTexture(), five, Color.White);
        spriteBatch.Draw(GetRandomTexture(), six, Color.White);
        spriteBatch.Draw(GetRandomTexture(), seven, Color.White);
        spriteBatch.Draw(GetRandomTexture(), eight, Color.White);
        spriteBatch.Draw(GetRandomTexture(), nine, Color.White);


        spriteBatch.Draw(GetRandomTexture(), ten, Color.LightSteelBlue);

        spriteBatch.End();
        base.Draw(gameTime);
    }
}

http://i.stack.imgur.com/oCS7h.gif

4

1 に答える 1

0

n 個の数値の配列が必要です。グリッドを初期化するたびSystem.Randomに、一番上の数値として ( を使用して) ランダムに数値を選択し、配列から削除し、グリッドにもコピーします。グリッドの他の数字についても同じことができます。グリッドに数字を追加するたびに数字を削除すると、数字が繰り返されなくなります。

もう 1 つの問題については、マウスが数字の 1 つをクリックしたときにのみ数字が変化するようにしたい場合は、サイクルごとに行うのではなく、マウス クリックを検出したときに構造を更新するだけで済みます。

于 2013-09-20T20:17:30.163 に答える