0

我正在尝试制作一个游戏,当您单击与顶部数字匹配的数字时,数字网格会随机化。

我还没有鼠标输入的代码但是我将如何随机化数字网格以便没有重复并且至少其中一个与顶部数字匹配?

另外,我将如何使数字仅在鼠标单击其中一个时才更改,以使它们不会每 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)作为顶部数字,然后将其从数组中删除,并将其复制到网格中。您可以对网格的其他数字执行相同的操作,每次在网格中添加一个数字时删除它,这将确保您不会有任何重复的数字。

对于另一个问题,如果您想让数字仅在鼠标单击其中一个时更改,您只需在检测到鼠标单击时更新您的结构,而不是每次循环都更新。

于 2013-09-20T20:17:30.163 回答