0

我对编码完全陌生,只练习了几个星期,我被分配了一项看似简单的任务却遇到了绊脚石

我在屏幕上绘制了 4 个精灵,但我必须每次游戏开始时,精灵必须在 1 个精灵或另一个精灵之间随机选择,并且在 2 个精灵中,每个精灵必须至少有一个在屏幕上。

我的导师建议我使用一个数组来存储纹理,然后对其进行编码,以便每次随机选择要绘制的纹理

namespace GamesProgrammingAssement1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        KeyboardState keys;
        KeyboardState oldKeys;
        GamePadState Pad1;
        GamePadState oldPad1;
        Texture2D gnome;
        Texture2D troll;
        Rectangle sprRect1;
        Rectangle sprRect2;
        Rectangle sprRect3;
        Rectangle sprRect4;
        SpriteFont Run;
        SpriteFont Score;
        int scoreNum = 0;
        int runNum = 0;
        Vector2 scorePos;
        Vector2 runPos;
        Texture2D[] sprite = new Texture2D[2];

        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()
        {
            sprRect1 = new Rectangle(375, 100, 64, 64);
            sprRect2 = new Rectangle(375, 300, 64, 64);
            sprRect3 = new Rectangle(225, 200, 64, 64);
            sprRect4 = new Rectangle(525, 200, 64, 64);

            scorePos = new Vector2(5, 400);
            runPos = new Vector2(5, 425);

            sprite[0] = gnome;
            sprite[1] = troll;

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

            gnome = Content.Load<Texture2D>("Gnome");
            troll = Content.Load<Texture2D>("Troll");
            Score = Content.Load<SpriteFont>("Score");
            Run = Content.Load<SpriteFont>("Run");
        }

        /// <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)
        {

            KeyboardState keys = Keyboard.GetState();
            KeyboardState oldkeys = keys;
            if (keys.IsKeyDown(Keys.Escape)) this.Exit();

            // TODO: Add your update logic here

            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.Draw(gnome,sprRect1,Color.White);
            spriteBatch.Draw(troll, sprRect2,Color.White);
            spriteBatch.Draw(troll, sprRect3, Color.White);
            spriteBatch.Draw(troll, sprRect4, Color.White);

            spriteBatch.DrawString(Score, "SCORE : "+ scoreNum, scorePos, Color.Black);
            spriteBatch.DrawString(Run, "RUN OF TROLL : " + runNum, runPos, Color.Black);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

任何帮助都会很棒,因为我不知道我是否正确存储了数组,或者我是否正确地随机操作

4

2 回答 2

0

您想选择四个精灵,因此您应该使用可以包含四个元素的数组(或类似结构):

Texture2D[] sprites = new Texture2D[4];

在初始化 Array 时,关于每个 sprite 将存在多少个可能存在三种情况:

  • 1-3(一个巨魔,三个侏儒)
  • 2-2(每人两个)
  • 3-1(三个巨魔,一个侏儒)

因此,首先,您必须选择以下发行版之一:

var rnd = new Random();
var trolls = rnd.Next(1, 3);
var gnomes = 4 - trolls;

然后,您可以填充数组:

for(int i = 0; i < 4; ++i)
{
    if(gnomes == 0)
    {
        //choose the troll
        sprites[i] = troll; 
        --trolls;
    }
    else if(trolls == 0)
    {
        //choose the gnome
        sprites[i] = gnome;
        --gnomes;
    }
    else
    {
        //choose randomly
        if(rnd.Next(2) < 1)
        {
            sprites[i] = troll; 
            --trolls;
        }
        else
        {
            sprites[i] = gnome;
            --gnomes;
        }
    }
}

你画他们喜欢

spriteBatch.Draw(sprites[0], sprRect1,Color.White);
spriteBatch.Draw(sprites[1], , sprRect2,Color.White);
spriteBatch.Draw(sprites[2], , sprRect3, Color.White);
spriteBatch.Draw(sprites[3], , sprRect4, Color.White);
于 2012-11-19T16:40:32.430 回答
0

我看到您将纹理存储在数组中。我在您的示例中没有看到任何 Random 代码。

话虽如此,请考虑以下功能:

Random rand = new Random();
private Texture2D GetRandomTexture()
{
    return sprite[rand.Next(0, 2)];
}

调用此函数,将返回包含在“精灵”数组中的随机纹理。将此函数调用插入到您的绘图中可能类似于以下内容:

spriteBatch.Draw(GetRandomTexture(), sprRect1, Color.White);

既然你说你是初学者,我尽量不讲太多细节。但是,您应该考虑创建一个新类“Sprite”,该类将包含每个精灵的 Texture2D、Position、Rectangle 值。然后,您可以存储 Sprite 对象的数组(或列表),而不是存储纹理数组。

于 2012-11-19T16:30:04.530 回答