Game1.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Audio;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Media;
  9. namespace SoundTest
  10. {
  11. public class Game1 : Microsoft.Xna.Framework.Game
  12. {
  13. GraphicsDeviceManager graphics;
  14. SpriteBatch spriteBatch;
  15. Texture2D tExplosion;
  16. SoundEffect sExplosion;
  17. SoundEffectInstance eExplosion;
  18. float move = 11f;
  19. Random rnd = new Random ();
  20. class explosion
  21. {
  22. public Vector2 Position;
  23. public float Size;
  24. public explosion (Vector2 pos)
  25. {
  26. Position = pos;
  27. Size = 1f;
  28. }
  29. }
  30. List<explosion> Explosions = new List<explosion> ();
  31. TimeSpan timer = TimeSpan.Zero;
  32. int Interval = 1000; //Milliseconds. The duration of the sound is about 1.5 sec.
  33. public Game1 ()
  34. {
  35. graphics = new GraphicsDeviceManager (this);
  36. Content.RootDirectory = "Content";
  37. graphics.PreferredBackBufferWidth = 1024;
  38. ;
  39. graphics.PreferredBackBufferHeight = 768;
  40. graphics.IsFullScreen = false;
  41. Explosions.Add (new explosion (new Vector2 (1025, 384)));
  42. }
  43. protected override void Initialize ()
  44. {
  45. base.Initialize ();
  46. }
  47. /// <summary>
  48. /// LoadContent will be called once per game and is the place to load
  49. /// all of your content.
  50. /// </summary>
  51. protected override void LoadContent ()
  52. {
  53. spriteBatch = new SpriteBatch (GraphicsDevice);
  54. sExplosion = Content.Load<SoundEffect> ("ExplosionSound");
  55. tExplosion = Content.Load<Texture2D> ("Explosion");
  56. eExplosion = sExplosion.CreateInstance ();
  57. }
  58. protected override void Update (GameTime gameTime)
  59. {
  60. base.Update (gameTime);
  61. /*
  62. //update explosions
  63. for (int i = 0; i<Explosions.Count;i++)
  64. {
  65. Explosions[i].Size -= 0.01f;
  66. if (Explosions[i].Size < 0.1f)
  67. {
  68. Explosions.RemoveAt(i);
  69. i--;
  70. }
  71. }
  72. //Check for next explosion
  73. timer += gameTime.ElapsedGameTime;
  74. if (timer.TotalMilliseconds > Interval)
  75. {
  76. timer = TimeSpan.Zero;
  77. float x = rnd.Next(24,1000);
  78. Explosions.Add(new explosion(new Vector2(x, rnd.Next(50,700))));
  79. sExplosion.Play(1f, 1f, (x / 512f) -1);
  80. }
  81. */
  82. float pan = 0;
  83. if (Explosions [0].Position.X > 1024 || Explosions [0].Position.X < 0) {
  84. move = -move;
  85. pan = MathHelper.Clamp ((Explosions [0].Position.X / 512f) - 1, -1, 1);
  86. eExplosion.Pan = pan;
  87. eExplosion.Play ();
  88. }
  89. Explosions [0].Position.X += move;
  90. pan = MathHelper.Clamp ((Explosions [0].Position.X / 512f) - 1, -1, 1);
  91. eExplosion.Pan = pan;
  92. //Console.WriteLine (pan);
  93. //Check for exit
  94. KeyboardState state = new KeyboardState ();
  95. state = Keyboard.GetState ();
  96. if (state.IsKeyDown (Keys.Escape) || state.IsKeyDown (Keys.Space) || state.IsKeyDown (Keys.Enter))
  97. this.Exit ();
  98. }
  99. protected override void Draw (GameTime gameTime)
  100. {
  101. GraphicsDevice.Clear (Color.Black);
  102. base.Draw (gameTime);
  103. spriteBatch.Begin ();
  104. //Draw explosions
  105. foreach (explosion e in Explosions)
  106. spriteBatch.Draw (tExplosion, e.Position, null, Color.White, 0, Vector2.Zero, e.Size, SpriteEffects.None, 1);
  107. spriteBatch.End ();
  108. }
  109. }
  110. }