Game1.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. #if ANDROID
  4. using Android.App;
  5. #endif
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Storage;
  11. using Microsoft.Xna.Framework.Audio;
  12. namespace Microsoft.Xna.Samples.Sound
  13. {
  14. /// <summary>
  15. /// This is the main type for your game
  16. /// </summary>
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. KeyboardState oldSate;
  21. SoundEffect sound;
  22. SpriteBatch spriteBatch;
  23. SoundEffectInstance soundInst;
  24. SpriteFont font;
  25. public Game1 ()
  26. {
  27. graphics = new GraphicsDeviceManager (this);
  28. Content.RootDirectory = "Content";
  29. graphics.PreferMultiSampling = true;
  30. graphics.IsFullScreen = false;
  31. Window.AllowUserResizing = false;
  32. graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.PortraitUpsideDown;
  33. }
  34. /// <summary>
  35. /// Allows the game to perform any initialization it needs to before starting to run.
  36. /// This is where it can query for any required services and load any non-graphic
  37. /// related content. Calling base.Initialize will enumerate through any components
  38. /// and initialize them as well.
  39. /// </summary>
  40. protected override void Initialize ()
  41. {
  42. // TODO: Add your initialization logic here
  43. base.Initialize ();
  44. }
  45. /// <summary>
  46. /// LoadContent will be called once per game and is the place to load
  47. /// all of your content.
  48. /// </summary>
  49. protected override void LoadContent ()
  50. {
  51. // Create a new SpriteBatch, which can be used to draw textures.
  52. spriteBatch = new SpriteBatch(GraphicsDevice);
  53. sound = Content.Load<SoundEffect>("Explosion1");
  54. soundInst = sound.CreateInstance();
  55. font = Content.Load<SpriteFont>("spriteFont1");
  56. }
  57. /// <summary>
  58. /// Allows the game to run logic such as updating the world,
  59. /// checking for collisions, gathering input, and playing audio.
  60. /// </summary>
  61. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  62. protected override void Update (GameTime gameTime)
  63. {
  64. KeyboardState ks = Keyboard.GetState();
  65. #if ANDROID
  66. if (soundInst.State != SoundState.Playing)
  67. {
  68. soundInst.Volume = 1f;
  69. soundInst.IsLooped = true;
  70. soundInst.Play();
  71. }
  72. #endif
  73. if (ks[Keys.Escape] == KeyState.Down)
  74. base.Exit();
  75. if (ks.IsKeyDown(Keys.A) && oldSate.IsKeyUp(Keys.A))
  76. soundInst.Play();
  77. if (ks.IsKeyDown(Keys.B) && oldSate.IsKeyUp(Keys.B))
  78. soundInst.Stop();
  79. if (ks.IsKeyDown(Keys.C) && oldSate.IsKeyUp(Keys.C))
  80. soundInst.Pause();
  81. if (ks.IsKeyDown(Keys.D) && oldSate.IsKeyUp(Keys.D))
  82. soundInst.IsLooped = !soundInst.IsLooped;
  83. if (ks.IsKeyDown(Keys.E) && oldSate.IsKeyUp(Keys.E))
  84. soundInst.Stop(true);
  85. if (ks.IsKeyDown(Keys.X))
  86. soundInst.Volume = MathHelper.Clamp(soundInst.Volume + 0.01f, 0f, 1f);
  87. else if (ks.IsKeyDown(Keys.Z))
  88. soundInst.Volume = MathHelper.Clamp(soundInst.Volume - 0.01f, 0f, 1f);;
  89. oldSate = ks;
  90. base.Update (gameTime);
  91. }
  92. /// <summary>
  93. /// This is called when the game should draw itself.
  94. /// </summary>
  95. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  96. protected override void Draw (GameTime gameTime)
  97. {
  98. graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
  99. spriteBatch.Begin();
  100. spriteBatch.DrawString(font, "A: play\nB: stop\nC: pause\nD: toggle looping\nE: immediate stop\nX/Z volume\nStatus: " +
  101. soundInst.State.ToString() + "\nLooping: " +
  102. soundInst.IsLooped.ToString() + "\nVolume: " +
  103. soundInst.Volume.ToString()
  104. , Vector2.Zero, Color.White);
  105. base.Draw(gameTime);
  106. spriteBatch.End();
  107. }
  108. }
  109. }