PageRenderTime 178ms CodeModel.GetById 86ms app.highlight 44ms RepoModel.GetById 40ms app.codeStats 1ms

/Samples/Blocks/Tutorials.cs

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