将通用队列队列<t>类更改为非通用队列类</t>

时间:2011-04-18 14:54:13

标签: c#

嘿伙计们。我这里的代码完全正常工作.....它目前正在使用System.Collections.Generic;
但我想将其更改为使用非通用队列类 我在掌握这个概念方面遇到了很多麻烦。非常感谢任何帮助和方向! 我在网上举例的尝试:
//编辑代码

    Queue Letters = new Queue(); //new code
    FallingCharacter myFallingCharacter = new FallingCharacter();//new code
    Letters.Enqueue(myFallingCharacter); //new code

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

原始代码是:

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

}
}

1 个答案:

答案 0 :(得分:0)

使用非泛型集合时,您需要做的就是在使用它们之前将返回的对象强制转换为FallingCharacter。否则,它应该基本相同,包括EnqueueDequeuePeek等。

E.g:

String.Compare( ( Letters.Peek() as FallingCharacter ).character.ToString(), sName )

foreach可以保持不变,因为只要您指定FallingCharacter作为类型,他们就会隐式进行类型转换。

如果您想要更安全,请在盲目假设对象为FallingCharacter之前添加一些类型检查。