C# 如何将队列更改为列表?
原代码为:C# 如何将队列更改为列表?,c#,list,xna,queue,C#,List,Xna,Queue,原代码为: partial class Game1 : Microsoft.Xna.Framework.Game { public enum GameState { StateMainMenu, StatePlaying, StateGameOver, StateHelp, } GameState currentState = GameState.StateMainMenu; Keyb
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:Microsoft.Xna.Framework.Game
{
公共枚举博弈状态
{
StateMain菜单,
国家行为,
国家游戏结束,
国家援助,
}
GameState currentState=GameState.StateMain菜单;
键盘状态;
按键[]按按键;
队列字母=新队列();
浮动nextLetterTime=0.6f;
int-NextSpeedup=20;//在20点中让它加速
int-iSpeed=2;
智力得分=0;
图形管理器图形;
SpriteBatch SpriteBatch;
雪碧峰雪碧峰;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=800;
graphics.PreferredBackBufferHeight=600;
graphics.ApplyChanges();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
spriteFont=Content.Load(“GameFont”);
base.LoadContent();
}
按下公共无效ON键(k键)
{
如果((currentState==GameState.StateMain)菜单||
currentState==GameState.StateGameOver)
&&k==键。输入)
{
currentState=GameState.StatePlaying;
nextLetterTime=0.0f;
NextSpeedup=20;//在20点lsets中加速
iSpeed=1;
得分=0;
}
else if(currentState==GameState.StatePlaying)
{
字符串sName=Enum.GetName(typeof(Keys),k);
如果(字母数>0)&&
String.Compare(Letters.Peek().character.ToString(),sName)==0)
{
分数+=100分;
Letters.Dequeue();
下一页--;
if(NextSpeedup图形。首选BackBufferHeight)
{
currentState=GameState.StateGameOver;
字母。清除();
打破
}
其他的
{
fc.Pos.Y+=(经过时间*(浮动)(iSpeed*40));
}
}
nextLetterTime-=已用时间;
如果(nextLetterTime如果要保持队列的相同行为(先进先出),则必须执行以下更改(粗体更改):
如果(字母数>0)&&
String.Compare(字母.Peek().character.ToString(),
sName)==0)
更改为
如果(字母数>0)&&
String.Compare(字母。第一个().character.ToString(),
sName)==0)
保持Peek返回第一个字符的行为
字母。出列()
更改为
字母。删除(0)
像出列一样删除第一个元素。
请注意,您可以进行检查以确保列表不是空的
字母。排队(新的FallingCharacter(
r、 下一步(graphics.PreferredBackBufferWidth)
- 30), -30,
颜色。浅绿色,(char)(int'A'+r.Next(25))
更改为
字母。添加(新的下降字符(
r、 下一步(graphics.PreferredBackBufferWidth)
- 30), -30,
颜色。浅绿色,(char)(int'A'+r.Next(25))
若要将FallingCharacter追加到列表末尾。如果要保持队列的相同行为(先进先出),必须进行以下更改(粗体更改):
如果(字母数>0)&&
String.Compare(字母.Peek().character.ToString(),
sName)==0)
更改为
如果(字母数>0)&&
String.Compare(字母。第一个().character.ToString(),
sName)==0)
保持Peek返回第一个字符的行为
字母。出列()
更改为
字母。删除(0)
像出列一样删除第一个元素。
请注意,您可以进行检查以确保列表不是空的
字母。排队(新的FallingCharacter(
r、 下一步(graphics.PreferredBackBufferWidth)
- 30), -30,
颜色。浅绿色,(char)(int'A'+r.Next(25))
更改为
字母。添加(新的下降字符(
r、 下一步(graphics.PreferredBackBufferWidth)
- 30), -30,
颜色。浅绿色,(char)(int'A'+r.Next(25))
将FallingCharacter附加到列表末尾。您应该编辑此内容并用您使用的任何语言标记它。这将帮助您从了解相应语言的人那里获取答案。您应该编辑此内容并用您使用的任何语言标记它。这将帮助您从了解相应语言的人那里获取答案语言。+1Letters.First()
有效,但由于它是一个列表,我们可以通过Letters[0]
利用可用的索引器。当然,我个人更喜欢First(),因为我觉得它更明确,但喜欢字母[0]有什么特别的优势吗?我只是好奇。进入了关于ElementAt
与索引器的类似讨论,以及底层代码如何实际使用索引器(如果可用)。就性能而言,它几乎可以忽略不计。在处理LINQ查询和IEnumerable
对象时,我倾向于使用Enumerable
类型的扩展方法ts如果我不能使用索引器。但是,我确实认为在某些情况下,枚举
方法可能有助于提高可读性。现在,一个非常开放的回答:)@Ahmad Mageed非常感谢!:)+1字母。First()
可以工作,但由于它是一个列表,我们可以通过字母[0]利用可用的索引器
。当然,我个人更喜欢First(),因为我觉得它更明确,但是喜欢字母[0]有什么特别的好处吗?我只是好奇而已