/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
- using Delta.Engine;
- using Delta.InputSystem;
- using Delta.Multimedia;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Rendering.Basics.Materials;
- using Delta.Rendering.Enums;
- using Delta.Utilities.Datatypes;
-
- namespace Blocks
- {
- /// <summary>
- /// Tutorial for the Blocks game in just 50 lines of code (plus some comment
- /// lines). Shows up in the SampleBrowser since this file is named Tutorials.
- /// </summary>
- public class Tutorials
- {
- #region Tutorial
- /// <summary>
- /// Tutorial: Blocks game tutorial
- /// Difficulty: Medium
- /// Description: This is just the Tutorial for Blocks in 50 lines of code
- /// (70 with comments). The full game is in the Game class.
- /// Image: http://DeltaEngine.net/Icons/Blocks.png
- /// </summary>
- public static void SimpleBlocksTutorial()
- {
- var background = new Material2D("BlocksBackground");
- background.DrawLayer = RenderLayer.Background;
- var block = new Material2D("Block");
- var logo = new Material2D("BlocksLogo");
- var sound = new Sound("DefaultSound");
- // Define a field of 9x9 blocks
- const int FieldWidth = 9, FieldHeight = 9;
- bool[,] field = new bool[FieldWidth, FieldHeight];
- int currentBlockX = 4, currentBlockY = 0;
- Rectangle fieldRect = new Rectangle(0.2f, 0.2f, 0.6f, 0.6f);
- Application.Start(delegate
- {
- // Move left right depending on where the user touched the screen
- float inputX = 0.5f;
- if (Input.Touch.TouchReleased)
- inputX = Input.Touch.Position.X;
- // Note: Also supporting Mouse and Keyboard input now ;)
- if ((inputX < 0.45f || Input.Keyboard.CursorLeftReleased) &&
- currentBlockX > 0)
- currentBlockX--;
- else if ((inputX > 0.55f || Input.Keyboard.CursorRightReleased) &&
- currentBlockX < FieldWidth - 1)
- currentBlockX++;
- // Only handle this every 0.25 seconds (4 times per sec).
- if (Time.CheckEvery(0.25f))
- {
- // Check if we can move block one down
- if (currentBlockY < FieldHeight - 1 &&
- field[currentBlockX, currentBlockY + 1] == false)
- {
- currentBlockY++;
- }
- // Otherwise fix this block and create a new one at the top
- else
- {
- sound.Play();
- field[currentBlockX, currentBlockY] = true;
- currentBlockX = 4;
- currentBlockY = 0;
- // Quick check if the bottom line is now full
- int blocksInLine = 0;
- for (int x = 0; x < FieldWidth; x++)
- if (field[x, FieldHeight - 1])
- blocksInLine++;
- if (blocksInLine == FieldWidth)
- {
- // Move everything one down (we have to do it upside down)!
- for (int y = FieldHeight - 1; y > 0; y--)
- for (int x = 0; x < FieldWidth; x++)
- field[x, y] = field[x, y - 1];
- }
- } // else
- } // if
-
- // Do all drawing, first the background
- background.Draw(Rectangle.One);
- // And then draw all field blocks
- for (int x = 0; x < FieldWidth; x++)
- for (int y = 0; y < FieldHeight; y++)
- if (field[x, y])
- block.Draw(fieldRect.GetInnerRectangle(new Rectangle(
- x / (float)FieldWidth, y / (float)FieldHeight,
- 1.0f / (float)FieldWidth, 1.0f / (float)FieldHeight)));
- // And finally also add the current block
- block.Draw(fieldRect.GetInnerRectangle(new Rectangle(
- currentBlockX / (float)FieldWidth,
- currentBlockY / (float)FieldHeight,
- 1.0f / (float)FieldWidth, 1.0f / (float)FieldHeight)));
-
- // Draw logo in the bottom right
- Font.DrawTopLeftInformation(
- "This is not the full Blocks game, just the tutorial. Please " +
- "check the v0.9.0 release for the full game.");
- logo.Draw(new Rectangle(ScreenSpace.DrawArea.BottomRight - logo.Size,
- logo.Size));
- });
- }
- #endregion
- }
- }