PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Blocks/Tutorials.cs

#
C# | 106 lines | 80 code | 3 blank | 23 comment | 18 complexity | 225cc19e153e0bd541bccefd71d4cc01 MD5 | raw file
Possible License(s): Apache-2.0
  1. using Delta.Engine;
  2. using Delta.InputSystem;
  3. using Delta.Multimedia;
  4. using Delta.Rendering.Basics.Fonts;
  5. using Delta.Rendering.Basics.Materials;
  6. using Delta.Rendering.Enums;
  7. using Delta.Utilities.Datatypes;
  8. namespace Blocks
  9. {
  10. /// <summary>
  11. /// Tutorial for the Blocks game in just 50 lines of code (plus some comment
  12. /// lines). Shows up in the SampleBrowser since this file is named Tutorials.
  13. /// </summary>
  14. public class Tutorials
  15. {
  16. #region Tutorial
  17. /// <summary>
  18. /// Tutorial: Blocks game tutorial
  19. /// Difficulty: Medium
  20. /// Description: This is just the Tutorial for Blocks in 50 lines of code
  21. /// (70 with comments). The full game is in the Game class.
  22. /// Image: http://DeltaEngine.net/Icons/Blocks.png
  23. /// </summary>
  24. public static void SimpleBlocksTutorial()
  25. {
  26. var background = new Material2D("BlocksBackground");
  27. background.DrawLayer = RenderLayer.Background;
  28. var block = new Material2D("Block");
  29. var logo = new Material2D("BlocksLogo");
  30. var sound = new Sound("DefaultSound");
  31. // Define a field of 9x9 blocks
  32. const int FieldWidth = 9, FieldHeight = 9;
  33. bool[,] field = new bool[FieldWidth, FieldHeight];
  34. int currentBlockX = 4, currentBlockY = 0;
  35. Rectangle fieldRect = new Rectangle(0.2f, 0.2f, 0.6f, 0.6f);
  36. Application.Start(delegate
  37. {
  38. // Move left right depending on where the user touched the screen
  39. float inputX = 0.5f;
  40. if (Input.Touch.TouchReleased)
  41. inputX = Input.Touch.Position.X;
  42. // Note: Also supporting Mouse and Keyboard input now ;)
  43. if ((inputX < 0.45f || Input.Keyboard.CursorLeftReleased) &&
  44. currentBlockX > 0)
  45. currentBlockX--;
  46. else if ((inputX > 0.55f || Input.Keyboard.CursorRightReleased) &&
  47. currentBlockX < FieldWidth - 1)
  48. currentBlockX++;
  49. // Only handle this every 0.25 seconds (4 times per sec).
  50. if (Time.CheckEvery(0.25f))
  51. {
  52. // Check if we can move block one down
  53. if (currentBlockY < FieldHeight - 1 &&
  54. field[currentBlockX, currentBlockY + 1] == false)
  55. {
  56. currentBlockY++;
  57. }
  58. // Otherwise fix this block and create a new one at the top
  59. else
  60. {
  61. sound.Play();
  62. field[currentBlockX, currentBlockY] = true;
  63. currentBlockX = 4;
  64. currentBlockY = 0;
  65. // Quick check if the bottom line is now full
  66. int blocksInLine = 0;
  67. for (int x = 0; x < FieldWidth; x++)
  68. if (field[x, FieldHeight - 1])
  69. blocksInLine++;
  70. if (blocksInLine == FieldWidth)
  71. {
  72. // Move everything one down (we have to do it upside down)!
  73. for (int y = FieldHeight - 1; y > 0; y--)
  74. for (int x = 0; x < FieldWidth; x++)
  75. field[x, y] = field[x, y - 1];
  76. }
  77. } // else
  78. } // if
  79. // Do all drawing, first the background
  80. background.Draw(Rectangle.One);
  81. // And then draw all field blocks
  82. for (int x = 0; x < FieldWidth; x++)
  83. for (int y = 0; y < FieldHeight; y++)
  84. if (field[x, y])
  85. block.Draw(fieldRect.GetInnerRectangle(new Rectangle(
  86. x / (float)FieldWidth, y / (float)FieldHeight,
  87. 1.0f / (float)FieldWidth, 1.0f / (float)FieldHeight)));
  88. // And finally also add the current block
  89. block.Draw(fieldRect.GetInnerRectangle(new Rectangle(
  90. currentBlockX / (float)FieldWidth,
  91. currentBlockY / (float)FieldHeight,
  92. 1.0f / (float)FieldWidth, 1.0f / (float)FieldHeight)));
  93. // Draw logo in the bottom right
  94. Font.DrawTopLeftInformation(
  95. "This is not the full Blocks game, just the tutorial. Please " +
  96. "check the v0.9.0 release for the full game.");
  97. logo.Draw(new Rectangle(ScreenSpace.DrawArea.BottomRight - logo.Size,
  98. logo.Size));
  99. });
  100. }
  101. #endregion
  102. }
  103. }