Game1.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework.Input;
  9. using Microsoft.Xna.Framework.Media;
  10. using Tile_Engine;
  11. namespace Gemstone_Hunter
  12. {
  13. /// <summary>
  14. /// This is the main type for your game
  15. /// </summary>
  16. public class Game1 : Microsoft.Xna.Framework.Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. Player player;
  21. SpriteFont pericles8;
  22. Vector2 scorePosition = new Vector2(20, 580);
  23. enum GameState { TitleScreen, Playing, PlayerDead, GameOver };
  24. GameState gameState = GameState.TitleScreen;
  25. Vector2 gameOverPosition = new Vector2(350, 300);
  26. Vector2 livesPosition = new Vector2(600, 580);
  27. Texture2D titleScreen;
  28. float deathTimer = 0.0f;
  29. float deathDelay = 5.0f;
  30. public Game1()
  31. {
  32. graphics = new GraphicsDeviceManager(this);
  33. Content.RootDirectory = "Content";
  34. }
  35. /// <summary>
  36. /// Allows the game to perform any initialization it needs to before starting to run.
  37. /// This is where it can query for any required services and load any non-graphic
  38. /// related content. Calling base.Initialize will enumerate through any components
  39. /// and initialize them as well.
  40. /// </summary>
  41. protected override void Initialize()
  42. {
  43. // TODO: Add your initialization logic here
  44. this.graphics.PreferredBackBufferWidth = 800;
  45. this.graphics.PreferredBackBufferHeight = 600;
  46. this.graphics.ApplyChanges();
  47. base.Initialize();
  48. }
  49. /// <summary>
  50. /// LoadContent will be called once per game and is the place to load
  51. /// all of your content.
  52. /// </summary>
  53. protected override void LoadContent()
  54. {
  55. spriteBatch = new SpriteBatch(GraphicsDevice);
  56. TileMap.Initialize(
  57. Content.Load<Texture2D>(@"Textures\PlatformTiles"));
  58. TileMap.spriteFont =
  59. Content.Load<SpriteFont>(@"Fonts\Pericles8");
  60. pericles8 = Content.Load<SpriteFont>(@"Fonts\Pericles8");
  61. titleScreen = Content.Load<Texture2D>(@"Textures\TitleScreen");
  62. Camera.WorldRectangle = new Rectangle(0, 0, 160 * 48, 12 * 48);
  63. Camera.Position = Vector2.Zero;
  64. Camera.ViewPortWidth = 800;
  65. Camera.ViewPortHeight = 600;
  66. player = new Player(Content);
  67. LevelManager.Initialize(Content, player);
  68. }
  69. private void StartNewGame()
  70. {
  71. player.Revive();
  72. player.LivesRemaining = 3;
  73. player.WorldLocation = Vector2.Zero;
  74. LevelManager.LoadLevel(0);
  75. }
  76. /// <summary>
  77. /// UnloadContent will be called once per game and is the place to unload
  78. /// all content.
  79. /// </summary>
  80. protected override void UnloadContent()
  81. {
  82. // TODO: Unload any non ContentManager content here
  83. }
  84. /// <summary>
  85. /// Allows the game to run logic such as updating the world,
  86. /// checking for collisions, gathering input, and playing audio.
  87. /// </summary>
  88. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  89. protected override void Update(GameTime gameTime)
  90. {
  91. KeyboardState keyState = Keyboard.GetState();
  92. // Allows the game to exit
  93. if (keyState.IsKeyDown(Keys.Escape)
  94. || GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  95. ButtonState.Pressed)
  96. this.Exit();
  97. GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
  98. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  99. if (gameState == GameState.TitleScreen)
  100. {
  101. if (keyState.IsKeyDown(Keys.Space) ||
  102. gamepadState.Buttons.A == ButtonState.Pressed)
  103. {
  104. StartNewGame();
  105. gameState = GameState.Playing;
  106. }
  107. }
  108. if (gameState == GameState.Playing)
  109. {
  110. player.Update(gameTime);
  111. LevelManager.Update(gameTime);
  112. if (player.Dead)
  113. {
  114. if (player.LivesRemaining > 0)
  115. {
  116. gameState = GameState.PlayerDead;
  117. deathTimer = 0.0f;
  118. }
  119. else
  120. {
  121. gameState = GameState.GameOver;
  122. deathTimer = 0.0f;
  123. }
  124. }
  125. }
  126. if (gameState == GameState.PlayerDead)
  127. {
  128. player.Update(gameTime);
  129. LevelManager.Update(gameTime);
  130. deathTimer += elapsed;
  131. if (deathTimer > deathDelay)
  132. {
  133. player.WorldLocation = Vector2.Zero;
  134. LevelManager.ReloadLevel();
  135. player.Revive();
  136. gameState = GameState.Playing;
  137. }
  138. }
  139. if (gameState == GameState.GameOver)
  140. {
  141. deathTimer += elapsed;
  142. if (deathTimer > deathDelay)
  143. {
  144. gameState = GameState.TitleScreen;
  145. }
  146. }
  147. base.Update(gameTime);
  148. }
  149. /// <summary>
  150. /// This is called when the game should draw itself.
  151. /// </summary>
  152. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  153. protected override void Draw(GameTime gameTime)
  154. {
  155. GraphicsDevice.Clear(Color.Black);
  156. spriteBatch.Begin(
  157. SpriteSortMode.BackToFront,
  158. BlendState.AlphaBlend);
  159. if (gameState == GameState.TitleScreen)
  160. {
  161. spriteBatch.Draw(titleScreen, Vector2.Zero, Color.White);
  162. }
  163. if ((gameState == GameState.Playing) ||
  164. (gameState == GameState.PlayerDead) ||
  165. (gameState == GameState.GameOver))
  166. {
  167. TileMap.Draw(spriteBatch);
  168. player.Draw(spriteBatch);
  169. LevelManager.Draw(spriteBatch);
  170. spriteBatch.DrawString(
  171. pericles8,
  172. "Score: " + player.Score.ToString(),
  173. scorePosition,
  174. Color.White);
  175. spriteBatch.DrawString(
  176. pericles8,
  177. "Lives Remaining: " + player.LivesRemaining.ToString(),
  178. livesPosition,
  179. Color.White);
  180. }
  181. if (gameState == GameState.PlayerDead)
  182. {
  183. }
  184. if (gameState == GameState.GameOver)
  185. {
  186. spriteBatch.DrawString(
  187. pericles8,
  188. "G A M E O V E R !",
  189. gameOverPosition,
  190. Color.White);
  191. }
  192. spriteBatch.End();
  193. base.Draw(gameTime);
  194. }
  195. }
  196. }