Engine.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.IO;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.GamerServices;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. using MonoMac.Foundation;
  8. namespace Tetris
  9. {
  10. /// <summary>
  11. /// This is the main type for your game
  12. /// </summary>
  13. public class Engine : Microsoft.Xna.Framework.Game
  14. {
  15. // Graphics
  16. GraphicsDeviceManager graphics;
  17. SpriteBatch spriteBatch;
  18. Texture2D tetrisBackground, tetrisTextures;
  19. SpriteFont gameFont;
  20. readonly Rectangle[] blockRectangles = new Rectangle[7];
  21. // Game
  22. Board board;
  23. Score score;
  24. bool pause = false;
  25. // Input
  26. KeyboardState oldKeyboardState = Keyboard.GetState ();
  27. public Engine ()
  28. {
  29. graphics = new GraphicsDeviceManager (this);
  30. //Content.RootDirectory = "Content";
  31. Content.RootDirectory = "Content";
  32. // Create sprite rectangles for each figure in texture file
  33. // O figure
  34. blockRectangles [0] = new Rectangle (312, 0, 24, 24);
  35. // I figure
  36. blockRectangles [1] = new Rectangle (0, 24, 24, 24);
  37. // J figure
  38. blockRectangles [2] = new Rectangle (120, 0, 24, 24);
  39. // L figure
  40. blockRectangles [3] = new Rectangle (216, 24, 24, 24);
  41. // S figure
  42. blockRectangles [4] = new Rectangle (48, 96, 24, 24);
  43. // Z figure
  44. blockRectangles [5] = new Rectangle (240, 72, 24, 24);
  45. // T figure
  46. blockRectangles [6] = new Rectangle (144, 96, 24, 24);
  47. }
  48. /// <summary>
  49. /// Allows the game to perform any initialization it needs to before starting to run.
  50. /// This is where it can query for any required services and load any non-graphic
  51. /// related content. Calling base.Initialize will enumerate through any components
  52. /// and initialize them as well.
  53. /// </summary>
  54. protected override void Initialize ()
  55. {
  56. Window.Title = "MonoGame XNA Tetris 2D";
  57. graphics.PreferredBackBufferHeight = 600;
  58. graphics.PreferredBackBufferWidth = 800;
  59. graphics.ApplyChanges();
  60. this.TargetElapsedTime = TimeSpan.FromSeconds (1.0f / 10.0f);
  61. // Try to open file if it exists, otherwise create it
  62. using (FileStream fileStream = File.Open ("record.dat", FileMode.OpenOrCreate)) {
  63. fileStream.Close ();
  64. }
  65. base.Initialize ();
  66. }
  67. /// <summary>
  68. /// LoadContent will be called once per game and is the place to load
  69. /// all of your content.
  70. /// </summary>
  71. protected override void LoadContent ()
  72. {
  73. // Create a new SpriteBatch, which can be used to draw textures.
  74. spriteBatch = new SpriteBatch (GraphicsDevice);
  75. // Add the SpriteBatch service
  76. Services.AddService (typeof(SpriteBatch), spriteBatch);
  77. //Load 2D textures
  78. tetrisBackground = Content.Load<Texture2D> ("background");
  79. tetrisTextures = Content.Load<Texture2D> ("tetris");
  80. // Load game font
  81. //gameFont = Content.Load<SpriteFont> ("font");
  82. gameFont = Content.Load<SpriteFont> ("Arial");
  83. // Create game field
  84. board = new Board (this, ref tetrisTextures, blockRectangles);
  85. board.Initialize ();
  86. Components.Add (board);
  87. // Save player's score and game level
  88. score = new Score (this, gameFont);
  89. score.Initialize ();
  90. Components.Add (score);
  91. // Load game record
  92. using (StreamReader streamReader = File.OpenText ("record.dat")) {
  93. string player = null;
  94. if ((player = streamReader.ReadLine ()) != null)
  95. score.RecordPlayer = player;
  96. int record = 0;
  97. if ((record = Convert.ToInt32 (streamReader.ReadLine ())) != 0)
  98. score.RecordScore = record;
  99. }
  100. }
  101. /// <summary>
  102. /// Allows the game to run logic such as updating the world,
  103. /// checking for collisions, gathering input, and playing audio.
  104. /// </summary>
  105. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  106. protected override void Update (GameTime gameTime)
  107. {
  108. // Allows the game to exit
  109. KeyboardState keyboardState = Keyboard.GetState ();
  110. if (keyboardState.IsKeyDown (Keys.Escape))
  111. this.Exit ();
  112. // Check pause
  113. bool pauseKey = (oldKeyboardState.IsKeyDown (Keys.P) && (keyboardState.IsKeyUp (Keys.P)));
  114. oldKeyboardState = keyboardState;
  115. if (pauseKey)
  116. pause = !pause;
  117. if (!pause) {
  118. // Find dynamic figure position
  119. board.FindDynamicFigure ();
  120. // Increase player score
  121. int lines = board.DestroyLines ();
  122. if (lines > 0) {
  123. score.Value += (int)((5.0f / 2.0f) * lines * (lines + 3));
  124. board.Speed += 0.005f;
  125. }
  126. score.Level = (int)(10 * board.Speed);
  127. // Create new shape in game
  128. if (!board.CreateNewFigure ())
  129. GameOver ();
  130. else {
  131. // If left key is pressed
  132. if (keyboardState.IsKeyDown (Keys.Left))
  133. board.MoveFigureLeft ();
  134. // If right key is pressed
  135. if (keyboardState.IsKeyDown (Keys.Right))
  136. board.MoveFigureRight ();
  137. // If down key is pressed
  138. if (keyboardState.IsKeyDown (Keys.Down))
  139. board.MoveFigureDown ();
  140. // Rotate figure
  141. if (keyboardState.IsKeyDown (Keys.Up) || keyboardState.IsKeyDown (Keys.Space))
  142. board.RotateFigure ();
  143. // Moving figure
  144. if (board.Movement >= 1) {
  145. board.Movement = 0;
  146. board.MoveFigureDown ();
  147. } else
  148. board.Movement += board.Speed;
  149. }
  150. }
  151. base.Update (gameTime);
  152. }
  153. private void GameOver ()
  154. {
  155. if (score.Value > score.RecordScore) {
  156. score.RecordScore = score.Value;
  157. pause = true;
  158. Record record = new Record ();
  159. //record.ShowDialog ();
  160. score.RecordPlayer = record.Player;
  161. using (StreamWriter writer = File.CreateText ("record.dat")) {
  162. writer.WriteLine (score.RecordPlayer);
  163. writer.WriteLine (score.RecordScore);
  164. }
  165. pause = false;
  166. }
  167. board.Initialize ();
  168. score.Initialize ();
  169. }
  170. /// <summary>
  171. /// This is called when the game should draw itself.
  172. /// </summary>
  173. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  174. protected override void Draw (GameTime gameTime)
  175. {
  176. spriteBatch.Begin ();
  177. spriteBatch.Draw (tetrisBackground, Vector2.Zero, Color.White);
  178. base.Draw (gameTime);
  179. spriteBatch.End ();
  180. }
  181. }
  182. }