2011-05-15 109 views
1

原始代碼是:如何將隊列更改爲List?

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列出()。

我改變之後Queue Letters = new Queue(); List List = new List(); 任何人都可以幫助我接下來需要改變什麼。 我對此感到非常困惑,非常感謝。

+1

你應該編輯這個標籤並用你正在使用的任何語言標記它。它會幫助你從知道適當語言的人那裏得到答案。 – 2011-05-15 03:25:37

回答

1

如果你想保持隊列(先入先出)相同的行爲,你必須做以下更改(以粗體顯示):


如果(快報。計數> 0 & & String.Compare(信函。皮克().character.ToString(), SNAME)== 0)

變爲

如果(Letters.Count> 0 & & String.Compare(信函。 首先().character.ToString(), SNAME)== 0)

保持其中皮克返回第一個字符的行爲。


字母。 Dequeue();

變化

快報。 RemoveAt(0);

刪除像Dequeue這樣的第一個元素。 請注意,您可以進行檢查以確保列表不爲空。


快報。 (字符)((int)'A'+ r.Next(25))));這個字符串可以是一個字符串,也可以是一個字符串,也可以是一個字符,也可以是一個字符。

變化

快報。 Add(new Falling Character( r.Next(graphics.PreferredBackBufferWidth - 30),-30, Color。LightGreen,(char)((int)'A'+ r.Next(25))));

FallingCharacter附加到列表的末尾。

+0

+1'Letters.First()'可以工作,但由於它是一個列表,我們可以通過'Letters [0]'利用可用的索引器。 – 2011-05-15 05:55:25

+0

當然,我個人更喜歡First(),因爲我覺得它更明確,但是有沒有特別的優勢來選擇Letters [0]?我只是好奇。 – 2011-05-15 13:33:19

+0

[這個問題](http://stackoverflow.com/q/5326874/59111)進入了一個關於'ElementAt'與索引器相似的討論,以及底層代碼如何使用索引器(如果可用)。在性能方面它幾乎可以忽略不計。我傾向於在處理LINQ查詢時使用'Enumerable'類型的擴展方法,如果我無法使用索引器,則傾向於使用'IEnumerable '對象。不過,我認爲有些情況下Enumerable方法可能有助於可讀性。你走了,一個非常開放的迴應:) – 2011-05-15 16:46:41

相關問題