【问题标题】:how to change the queue to List?如何将队列更改为列表?
【发布时间】:2011-05-15 03:21:16
【问题描述】:

原代码为:

partial class Game1 : Microsoft.Xna.Framework.Game
{

    public enum GameState
    {
        StateMainMenu,
        StatePlaying,
        StateGameOver,
        StateHelp,
    }


    GameState currentState = GameState.StateMainMenu;

    KeyboardState kbState;
    Keys[] pressedKeys;


    Queue<FallingCharacter> Letters = new Queue<FallingCharacter>();

    float nextLetterTime = 0.6f;
    int NextSpeedup = 20; // in 20 points lets speed it up
    int iSpeed = 2;

    int Score = 0;

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteFont spriteFont;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 600;
        graphics.ApplyChanges();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        spriteFont = Content.Load<SpriteFont>("GameFont");

        base.LoadContent();
    }


    public void OnKeyPressed(Keys k)
    {

        if ((currentState == GameState.StateMainMenu ||
            currentState == GameState.StateGameOver)
            && k == Keys.Enter)
        {
            currentState = GameState.StatePlaying;

            nextLetterTime = 0.0f;
            NextSpeedup = 20; // in 20 points lsets speed it up
            iSpeed = 1;
            Score = 0;
        }
        else if (currentState == GameState.StatePlaying)
        {
            string sName = Enum.GetName(typeof(Keys), k);
            if (Letters.Count > 0 &&
                    String.Compare(Letters.Peek().character.ToString(), sName) == 0)
            {
                Score += 100;
                Letters.Dequeue();

                NextSpeedup--;

                if (NextSpeedup <= 0)
                {
                    iSpeed++;

                    NextSpeedup = 20;
                }
            }
        }

        if (k == Keys.Escape)
        {
            this.Exit();
        }
    }


    protected override void Update(GameTime gameTime)
    {
        // The time since Update was called last
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;


        kbState = Keyboard.GetState();

        Keys[] newKeys = (Keys[])kbState.GetPressedKeys().Clone();

        if (pressedKeys != null)
        {
            foreach (Keys k in newKeys)
            {
                bool bFound = false;

                foreach (Keys k2 in pressedKeys)
                {
                    if (k == k2)
                    {
                        bFound = true;
                        break;
                    }
                }

                if (!bFound)
                {
                    OnKeyPressed(k);
                }
            }
        }

        pressedKeys = newKeys;

        if (currentState == GameState.StatePlaying)
        {

            foreach (FallingCharacter fc in Letters)
            {
                if (fc.Pos.Y + 50 > graphics.PreferredBackBufferHeight)
                {
                    currentState = GameState.StateGameOver;

                    Letters.Clear();
                    break;
                }
                else
                {
                    fc.Pos.Y += (elapsed * (float)(iSpeed * 40));
                }
            }


            nextLetterTime -= elapsed;

            if (nextLetterTime <= 0 && currentState != GameState.StateGameOver)
            {
                nextLetterTime = 0.75f - (iSpeed * .03f);
                Random r = new Random();

                Letters.Enqueue(new FallingCharacter(
                    r.Next(graphics.PreferredBackBufferWidth - 30), -30,
                    Color.LightGreen, (char)((int)'A' + r.Next(25))));

            }
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); 

        switch (currentState)
        {
            case GameState.StateMainMenu:
                {
                    spriteBatch.DrawString(spriteFont,
                        "Press Enter to begin", 
                        new Vector2(graphics.PreferredBackBufferWidth / 4,
                        graphics.PreferredBackBufferHeight / 2), 
                        Color.White);
                }
                break;
            case GameState.StatePlaying:
                {
                    spriteBatch.DrawString(spriteFont, 
                        "Score: " + Score.ToString(), 
                        new Vector2(10, 10), Color.White);

                    foreach (FallingCharacter fc in Letters)
                    {
                        fc.Render(spriteBatch, spriteFont);
                    }
                }
                break;
            case GameState.StateGameOver:
                {
                    spriteBatch.DrawString(spriteFont, 
                        "Score: " + Score.ToString(), 
                        new Vector2(10, 10), Color.White);
                    spriteBatch.DrawString(spriteFont, 
                        "Game Over", 
                        new Vector2(graphics.PreferredBackBufferWidth / 3, 
                                    graphics.PreferredBackBufferHeight / 2),
                        Color.LightBlue);
                    spriteBatch.DrawString(spriteFont, 
                        "Press Return to Continue", 
                        new Vector2(graphics.PreferredBackBufferWidth / 6, 
                            graphics.PreferredBackBufferHeight / 2 + 50), 
                        Color.LightBlue);
                }
                break;
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }

}
}

我需要将 quque 更改为 list()。

在我更改了 Queue Letters = new Queue(); 列出字母 = new List(); 谁能帮我下一步需要改变什么。 我对此感到非常困惑,非常感谢。

【问题讨论】:

  • 您应该编辑它并用您使用的任何语言对其进行标记。它将帮助您从懂适当语言的人那里获得答案。

标签: c# list xna queue


【解决方案1】:

如果要保持队列的相同行为(先进先出),则必须进行以下更改(更改为粗体):


if (Letters.Count > 0 && String.Compare(Letters.Peek().character.ToString(), sName) == 0)

更改为

if (Letters.Count > 0 && String.Compare(Letters.First().character.ToString(), sName) == 0)

保持 Peek 返回第一个字符的行为。


Letters.Dequeue();

更改为

字母。RemoveAt(0);

Dequeue 那样删除第一个元素。 请注意,您可以进行检查以确保列表不为空。


Letters.Enqueue(new FallingCharacter( r.Next(graphics.PreferredBackBufferWidth - 30), -30, Color.LightGreen, (char)((int)'A' + r.Next(25))));

更改为

Letters.添加(new FallingCharacter( r.Next(graphics.PreferredBackBufferWidth - 30), -30, Color.LightGreen, (char)((int)'A' + r.Next(25))));

FallingCharacter 附加到列表的末尾。

【讨论】:

  • +1 Letters.First() 有效,但由于它是一个列表,我们可以通过 Letters[0] 来利用可用的索引器。
  • 当然,我个人更喜欢 First(),因为我觉得它更明确,但更喜欢 Letters[0] 有什么特别的优势吗?我只是好奇。
  • This questionElementAt 与索引器以及底层代码如何实际使用索引器(如果可用)进行了类似的讨论。就性能而言,几乎可以忽略不计。在处理 LINQ 查询和 IEnumerable&lt;T&gt; 对象时,如果我不能使用索引器,我倾向于使用 Enumerable 类型的扩展方法。但是,我确实认为在某些情况下Enumerable 方法可能有助于提高可读性。你去吧,一个非常开放的回应:)
  • @Ahmad Mageed 非常感谢! :)
猜你喜欢
  • 2021-06-06
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
相关资源
最近更新 更多