public static Canvas RenderTetramino(Tetramino tetramino) { Canvas canvas = new Canvas(); double xTranslationCoeff; switch (tetramino.Type) { case TetraminoType.I: xTranslationCoeff = 2; break; case TetraminoType.O: xTranslationCoeff = 1; break; default: xTranslationCoeff = 1.5; break; } foreach (Element element in tetramino.elements) { Rectangle rectangle = Renderer.RenderElement(element); rectangle.SetValue(Canvas.LeftProperty, (double)((element.X + xTranslationCoeff) * Settings.ElementSize)); rectangle.SetValue(Canvas.TopProperty, (double)((element.Y) * Settings.ElementSize)); canvas.Children.Add(rectangle); } return canvas; }
public Matrix Add(Tetramino block) { Matrix res = new Matrix(this.ColsCount, this.RowsCount); for (int j = 0; j < RowsCount; j++) for (int i = 0; i < ColsCount; i++) res[i, j] = this[i, j]; foreach (var element in block.elements) res[block.position.X + element.X, block.position.Y + element.Y] = element; return res; }
public Tetramino GetTetramino() { int index = rand.Next(6); Element el; //int[,] numbers = new int[4, 2] { {-1, 0}, {0, 0}, {1, 0}, { 0, -1} }; int[,] numbers; Tetramino tetramino = new Tetramino(); tetramino.Type = (TetraminoType)index; switch (tetramino.Type) { // I case TetraminoType.I: default: //block.color = Colors.Red; //numbers = new int[4, 2] { { 0, -2 }, { 0, -1 }, { 0, 0 }, { 0, 1 } }; tetramino.color = Colors.Cyan; numbers = new int[4, 2] { { -2, 0 }, { -1, 0 }, { 0, 0 }, { 1, 0 } }; break; // J left oriented, blue case TetraminoType.J: tetramino.color = Colors.Blue; //numbers = new int[4, 2] { { 0, -1 }, { 0, 0 }, { 0, 1 }, { -1, 1 } }; numbers = new int[4, 2] { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 1, 1 } }; break; // L na desno case TetraminoType.L: tetramino.color = Colors.Orange; //numbers = new int[4, 2] { { 0, -1 }, { 0, -0 }, { 0, 1 }, { 1, 1 } }; numbers = new int[4, 2] { { -1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } }; break; // O square, yellow case TetraminoType.O: tetramino.rotatable = false; tetramino.color = Colors.Yellow; numbers = new int[4, 2] { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }; break; // S mirror case TetraminoType.S: //block.color = Colors.Magenta; //numbers = new int[4, 2] { { 0, 0 }, { 1, 0 }, { -1, 1 }, { 0, 1 } }; tetramino.color = Colors.Green; numbers = new int[4, 2] { { -1, 1 }, { 0, 1 }, { 0, 0 }, { 1, 0 } }; break; // T 0 // 000 cian case TetraminoType.T: tetramino.color = Colors.Purple; //numbers = new int[4, 2] { { 0, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 } }; numbers = new int[4, 2] { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } }; break; // Z case TetraminoType.Z: tetramino.color = Colors.Red; //numbers = new int[4, 2] { { -1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 } }; numbers = new int[4, 2] { { -1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 } }; break; } for (int i = 0; i < 4; i++) { el = new Element(tetramino.color); el.Position = new Point(numbers[i, 0], numbers[i, 1]); tetramino.elements.Add(el); } return tetramino; }
private bool IsOut(Tetramino block) { bool isOut = false; int i = 0; while (!isOut && i < block.elements.Count) { isOut = (block.position.X + block.elements[i].X >= MatrixTable.ColsCount) || (block.position.X + block.elements[i].X < 0); i++; } return isOut; }
private bool IsCollision(Tetramino block) { bool isCollision = false; int i = 0; while (!isCollision && i < block.elements.Count) { isCollision = !MatrixTable[block.position.X + block.elements[i].X, block.position.Y + block.elements[i].Y].IsNull(); i++; } return isCollision; }
private bool IsBlockDocked(Tetramino block) { bool IsDocked = false; int i = -1; while (!IsDocked && i++ < block.elements.Count - 1) IsDocked = (block.position.Y + block.elements[i].Y >= MatrixTable.RowsCount - 1) || (!MatrixTable[block.position.X + block.elements[i].X, block.position.Y + block.elements[i].Y + 1].IsNull()); return IsDocked; }
private void GetNewBlock() { TetraminoNum++; TetraminoInvenory.Enqueue(tetraminoBuilder.GetTetramino()); ActiveTetramino = TetraminoInvenory.Dequeue(); }
//Action rotate block public void RotateBlock(int Direction) { Tetramino block = (Tetramino)ActiveTetramino.Clone(); ActiveTetramino.Rotate(Direction); if (IsOut(ActiveTetramino) || IsCollision(ActiveTetramino)) ActiveTetramino = (Tetramino)block.Clone(); }