XNA Space Invaders flicker
Hej. Er igang med at lave et spaceinvaders spil. Men både mine skud og mine asteroider "flicker", som om at det "lagger". Nogen der har en ide?namespace SpaceInvaders
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D Tex_Ship;
Vector2 Vec_Ship = new Vector2(450.0f, 660.0f);
Texture2D Tex_Rock;
Vector2 Vec_Rock = new Vector2(0.0f,0.0f);
Texture2D Tex_Shot;
LinkedList<Vector2> List_Rock = new LinkedList<Vector2>();
LinkedList<Vector2> List_Shot = new LinkedList<Vector2>();
Random randomizer = new Random();
int Tim_Shot = 0;
int Tim_Rock = 0;
SpriteFont spr_fon;
int ui = 0;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;
graphics.IsFullScreen = true;
graphics.ApplyChanges();
Window.Title = "Space Invaders";
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Tex_Ship = Content.Load<Texture2D>("ship");
Tex_Shot = Content.Load<Texture2D>("shot");
Tex_Rock = Content.Load<Texture2D>("rock");
spr_fon = Content.Load<SpriteFont>("SpriteFont1");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) this.Exit();
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right)) Vec_Ship.X += 5.0f;
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left)) Vec_Ship.X -= 5.0f;
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Space))
{
if (Tim_Shot == 0)
{
List_Shot.AddLast(new Vector2(Vec_Ship.X + 69.0f, Vec_Ship.Y - 12.0f));
Tim_Shot = 1;
}
}
if(Tim_Shot > 0) Tim_Shot++;
if (Tim_Shot == 20)
{
Tim_Shot = 0;
}
if (Tim_Rock == 0)
{
Vec_Rock.X = randomizer.Next(900)+50;
Vec_Rock.Y = 0.0f;
List_Rock.AddLast(Vec_Rock);
Tim_Rock = 1;
}
if (Tim_Rock > 0) Tim_Rock++;
if (Tim_Rock == 100)
{
Tim_Rock = 0;
}
CheckColl();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(Tex_Ship, Vec_Ship, Color.White);
MoveShots(gameTime);
MoveRocks(gameTime);
spriteBatch.DrawString(spr_fon, List_Rock.Count.ToString(), new Vector2(50.0f, 50.0f), Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
void CheckColl()
{
if (Vec_Ship.X <= 0) Vec_Ship.X = 1;
if (Vec_Ship.X >= 881) Vec_Ship.X = 880;
for (int i = 0; i < List_Shot.Count; i++)
{
for (ui = 0; ui < List_Rock.Count; ui++)
{
if (List_Rock.ElementAt(ui).Y > List_Shot.ElementAt(i).Y)
{
if (List_Rock.ElementAt(ui).Y < List_Shot.ElementAt(i).Y + 50)
{
if (List_Rock.ElementAt(ui).X > List_Shot.ElementAt(i).X - 50)
{
if (List_Rock.ElementAt(ui).X < List_Shot.ElementAt(i).X)
{
List_Rock.Remove(List_Rock.ElementAt(ui));
break;
}
}
}
}
}
if (List_Shot.ElementAt(i).Y < 0)
{
List_Shot.Remove(List_Shot.ElementAt(i));
}
}
for (int u = 0; u < List_Rock.Count; u++)
{
if (List_Rock.ElementAt(u).Y > 700)
{
List_Rock.Remove(List_Rock.ElementAt(u));
}
}
}
void MoveShots(GameTime gameTime)
{
for (int i = 0; i < List_Shot.Count; i++)
{
Vector2 vec = List_Shot.ElementAt(i);
List_Shot.Remove(vec);
vec.Y -= 5;
List_Shot.AddLast(vec);
spriteBatch.Draw(Tex_Shot, vec, Color.White);
}
}
void MoveRocks(GameTime gameTime)
{
for (int i = 0; i < List_Rock.Count; i++)
{
Vector2 vec = List_Rock.ElementAt(i);
List_Rock.Remove(List_Rock.ElementAt(i));
vec.Y += randomizer.Next(2);
List_Rock.AddLast(vec);
spriteBatch.Draw(Tex_Rock, vec, Color.White);
}
}
}
}