StarField.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Asteroid_Belt_Assault
  8. {
  9. class StarField
  10. {
  11. private List<Sprite> stars = new List<Sprite>();
  12. private int screenWidth = 800;
  13. private int screenHeight = 600;
  14. private Random rand = new Random();
  15. private Color[] colors = { Color.White, Color.Yellow,
  16. Color.Wheat, Color.WhiteSmoke,
  17. Color.SlateGray };
  18. public StarField(
  19. int screenWidth,
  20. int screenHeight,
  21. int starCount,
  22. Vector2 starVelocity,
  23. Texture2D texture,
  24. Rectangle frameRectangle)
  25. {
  26. this.screenWidth = screenWidth;
  27. this.screenHeight = screenHeight;
  28. for (int x = 0; x < starCount; x++)
  29. {
  30. stars.Add(new Sprite(
  31. new Vector2(rand.Next(0, screenWidth),
  32. rand.Next(0, screenHeight)),
  33. texture,
  34. frameRectangle,
  35. starVelocity));
  36. Color starColor = colors[rand.Next(0, colors.Count())];
  37. starColor *= (float)(rand.Next(30, 80) / 100f); stars[stars.Count() - 1].TintColor = starColor;
  38. }
  39. }
  40. public void Update(GameTime gameTime)
  41. {
  42. foreach (Sprite star in stars)
  43. {
  44. star.Update(gameTime);
  45. if (star.Location.Y > screenHeight)
  46. {
  47. star.Location = new Vector2(
  48. rand.Next(0, screenWidth), 0);
  49. }
  50. }
  51. }
  52. public void Draw(SpriteBatch spriteBatch)
  53. {
  54. foreach (Sprite star in stars)
  55. {
  56. star.Draw(spriteBatch);
  57. }
  58. }
  59. }
  60. }