diff --git a/Tetris.Core/Brick.cs b/Tetris.Core/Brick.cs index e137ab7..cda9d75 100644 --- a/Tetris.Core/Brick.cs +++ b/Tetris.Core/Brick.cs @@ -8,10 +8,7 @@ namespace Tetris.Core { public class Brick - { - public int Width { get; set; } - public int Height { get; set; } - + { public int X { get; set; } public int Y { get; set; } @@ -24,20 +21,22 @@ public void SetPosition(int x, int y) X = x; Y = y; } + public void SetRelativePosition(int x, int y) { X += x; Y += y; } - public void Rotate() { } public void Draw(Graphics graphics, int cellWidth, int cellHeight) { using (var brush = new SolidBrush(Color.Red)) { graphics.FillRectangle(brush, - new Rectangle(cellWidth * X, cellHeight * Y, cellWidth * Width, cellHeight * Height)); + new Rectangle(cellWidth * X, cellHeight * Y, cellWidth, cellHeight)); } } + + public bool Intersect(int x, int y) => x == X && y == Y; } } diff --git a/Tetris.Core/Figures/BaseFigure.cs b/Tetris.Core/Figures/BaseFigure.cs new file mode 100644 index 0000000..e46b545 --- /dev/null +++ b/Tetris.Core/Figures/BaseFigure.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Text; + +namespace Tetris.Core.Figures +{ + public abstract class BaseFigure + { + public int Width => Bricks.Max(b => b.X) - Bricks.Min(b => b.X); + public int Height => Bricks.Max(b => b.Y) - Bricks.Min(b => b.Y); + public bool IsActive { get; set; } + + protected Brick RotationBrick; + + protected List Bricks; + + public BaseFigure() + { + Bricks = new List(); + IsActive = true; + } + + public abstract void Rotate(); + + public void Draw(Graphics graphics, int cellWidth, int cellHeight) => + Bricks.ForEach(b => b.Draw(graphics, cellWidth, cellHeight)); + + public void SetRelativePosition(int x, int y) => + Bricks.ForEach(b => b.SetRelativePosition(x, y)); + + public virtual bool Intersect(int x, int y) => !Bricks.TrueForAll(b => !b.Intersect(x, y)); + public virtual bool Intersect(Brick brick) => !Bricks.TrueForAll(b => !b.Intersect(brick.X, brick.Y)); + + public virtual bool Intersect(BaseFigure figure) => !figure.Bricks.TrueForAll(b => !Intersect(b)); + + } +} \ No newline at end of file diff --git a/Tetris.Core/Figures/Border.cs b/Tetris.Core/Figures/Border.cs new file mode 100644 index 0000000..772c830 --- /dev/null +++ b/Tetris.Core/Figures/Border.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tetris.Core.Figures +{ + public class Border : BaseFigure + { + public Border(int x, int y, int width, int height) + { + IsActive = false; + for (int i = x; i < x + width; i++) + for (int o = y; o < y + height; o++) + Bricks.Add(new Brick { X = i, Y = o }); + } + + public override void Rotate() + { + } + public new void Draw(Graphics graphics, int cellWidth, int cellHeight) + { + + } + } +} diff --git a/Tetris.Core/Figures/Stab.cs b/Tetris.Core/Figures/Stab.cs new file mode 100644 index 0000000..ff91f45 --- /dev/null +++ b/Tetris.Core/Figures/Stab.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tetris.Core.Figures +{ + public class Stab : BaseFigure + { + public Stab() + { + for (int i = 0; i < 4; i++) + { + var b = new Brick { X = 0, Y = i }; + Bricks.Add(b); + if (i == 1) + RotationBrick = b; + } + } + + public override void Rotate() + { + if(Bricks.GroupBy(x=>x.Y).ToList().Count > 1) + { + for (int i = -1; i < 3; i++) + { + Bricks[i+1].Y = RotationBrick.Y; + Bricks[i+1].X = RotationBrick.X + i; + } + } + else + { + for (int i = -1; i < 3; i++) + { + Bricks[i + 1].Y = RotationBrick.Y + i; + Bricks[i + 1].X = RotationBrick.X; + } + } + } + } +} diff --git a/Tetris.Core/Game.cs b/Tetris.Core/Game.cs index 95b9f29..77c88c0 100644 --- a/Tetris.Core/Game.cs +++ b/Tetris.Core/Game.cs @@ -7,6 +7,7 @@ using System.Text; using System.Timers; using System.Windows.Forms; +using Tetris.Core.Figures; namespace Tetris.Core { @@ -17,11 +18,19 @@ public class Game public int CellWidth { get; set; } public int CellHeight { get; set; } + public BaseFigure CurrentFigure { get; set; } + public List AllFigures { get; set; } + /// + /// Used for Input. Check if Key is pressed + /// Dictionary> KeyDictionary; System.Timers.Timer timer; - Brick brick; + Stab stab; + Border bottom; + Border left; + Border right; ulong frames = 0; @@ -38,12 +47,13 @@ public Game(int width, int height) [Keys.Up] = new KeyValuePair(false, 0) }; - brick = new Brick() - { - Width = 2, - Height = 2 - }; - + stab = new Stab(); + CurrentFigure = stab; + left = new Border(-1, 0, 1, Height); + right = new Border(Width, 0, 1, Height); + bottom = new Border(0, Height, Width, 1); + AllFigures = new List { stab, bottom, left, right }; + timer = new System.Timers.Timer() { Interval = 33 @@ -57,18 +67,68 @@ public Game(int width, int height) private void Timer_Elapsed(object sender, ElapsedEventArgs e) { frames++; - if(frames % 10 == 0) - brick.SetRelativePosition(0, brick.Y + 3 > Height ? 0 : 1); - KeyDictionary.Where(x => x.Value.Key).Select(x => x.Value.Value).ToList().ForEach(x => brick.SetRelativePosition(x, 0)); + + //KeyDictionary.Where( + // x => x.Key == Keys.Right || x.Key == Keys.Left).Where( + // x => x.Value.Key).Select( + // x => x.Value.Value).ToList().ForEach( + // x => CurrentFigure.SetRelativePosition( + // x > 0 ? + // CurrentFigure.Intersect(right) ? 0 : 1 :// stab.X + stab.Width + 1 > Width ? 0 : x : + // CurrentFigure.Intersect(left) ? 0 : -1, 0)); + + if (KeyDictionary[Keys.Down].Key) + if (!CurrentFigure.Intersect(bottom)) + CurrentFigure.SetRelativePosition(0, 1); + + if (frames % 4 == 0) + { + if (KeyDictionary[Keys.Up].Key) + { + KeyDictionary[Keys.Up] = new KeyValuePair(false, 0); + CurrentFigure.Rotate(); + } + } + + if (frames % 10 == 0) + CurrentFigure.SetRelativePosition(0, 1); + + bool intersect = false; + foreach (var item in AllFigures.Where(x => x != CurrentFigure)) + { + if (intersect) + continue; + intersect = CurrentFigure.Intersect(item); + } + if(!intersect) + KeyDictionary.Where( + x => x.Key == Keys.Right || x.Key == Keys.Left).Where( + x => x.Value.Key).Select( + x => x.Value.Value).ToList().ForEach( + x => CurrentFigure.SetRelativePosition( + x > 0 ? intersect ? -1 : 1 : intersect ? 1 : -1, 0)); + + while (CurrentFigure.Intersect(bottom)) + { + CurrentFigure.SetRelativePosition(0, -1); + CurrentFigure.IsActive = false; + CurrentFigure = new Stab(); + AllFigures.Add(CurrentFigure); + } } public void OnDraw(Graphics graphics) { - brick.Draw(graphics, CellWidth, CellHeight); + AllFigures.ForEach(f => { + if (!(f is Border)) + f.Draw(graphics, CellWidth, CellHeight); + }); } public void MoveBrick(KeyEventArgs keyEventArgs, bool keyUp) { + if (keyEventArgs.KeyCode == Keys.Up && !keyUp) + return; if (KeyDictionary.TryGetValue(keyEventArgs.KeyCode, out var val)) KeyDictionary[keyEventArgs.KeyCode] = new KeyValuePair(keyUp, val.Value); } diff --git a/Tetris.Core/Tetris.Core.csproj b/Tetris.Core/Tetris.Core.csproj index 60f9b17..acd8b59 100644 --- a/Tetris.Core/Tetris.Core.csproj +++ b/Tetris.Core/Tetris.Core.csproj @@ -44,8 +44,12 @@ + + + + \ No newline at end of file