diff --git a/Models/Grid.cs b/Models/Grid.cs index d4adda9..976b479 100644 --- a/Models/Grid.cs +++ b/Models/Grid.cs @@ -8,6 +8,8 @@ namespace Tetris.Models; public class Grid { private Color[,] _grid; + public Color[,] CGrid => (Color[,]) _grid.Clone(); + public Point MinGrid => new Point(0,0); public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1); diff --git a/ViewsModels/GameViewModel.cs b/ViewsModels/GameViewModel.cs index 3069105..3fdbbf7 100644 --- a/ViewsModels/GameViewModel.cs +++ b/ViewsModels/GameViewModel.cs @@ -1,8 +1,13 @@ -using System; + + +using System; using System.ComponentModel; +using System.Drawing; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Threading; +using Tetris.Models; +using Color = System.Drawing.Color; namespace Tetris.ViewsModels; @@ -15,10 +20,14 @@ public class GameViewModel : INotifyPropertyChanged private readonly int _width = 480, _height = 640; private readonly WriteableBitmap _writeableBitmap; + private const int GridWidth = 10; + private const int GridHeight = 20; + private static readonly Grid Grid = new(new Color[GridWidth, GridHeight]); + private readonly Tetrominoe _currentTetrominoe = new(Grid, "J", new Point(10, 10), 0, Color.Aqua); + public GameViewModel() { _writeableBitmap = BitmapFactory.New(_width, _height); - // image.Source = _writeableBitmap; var dispatcherRenderTimer = new DispatcherTimer { @@ -46,11 +55,33 @@ public class GameViewModel : INotifyPropertyChanged _writeableBitmap.Lock(); _writeableBitmap.Clear(Colors.Black); + for (var x = 0; x < GridWidth; x++) + { + for (var y = 0; y < GridHeight; y++) + { + var color = Grid.CGrid[x, y]; + _writeableBitmap.SetPixel(x, y, color.R, color.G, color.B); + } + } + + var tetrominoeWidth = _currentTetrominoe.Shape.GetLength(0); + var tetrominoeHeight = _currentTetrominoe.Shape.GetLength(1); + + for (int x = 0; x < tetrominoeWidth; x++) + { + for (int y = 0; y < tetrominoeHeight; y++) + { + if (!_currentTetrominoe.Shape[x, y]) continue; + var color = _currentTetrominoe.Color; + _writeableBitmap.FillRectangle(_currentTetrominoe.Coordinates.X * 10, _currentTetrominoe.Coordinates.Y * 10, _currentTetrominoe.Coordinates.X * 10 + 10, _currentTetrominoe.Coordinates.Y * 10 + 10, color.ToArgb()); + } + } + _writeableBitmap.Unlock(); } private void Update(object? sender, EventArgs eventArgs) { - + _currentTetrominoe.GoDown(); } } \ No newline at end of file