diff --git a/Views/GameWindow.xaml.cs b/Views/GameWindow.xaml.cs index 6c7f75d..dda3d64 100644 --- a/Views/GameWindow.xaml.cs +++ b/Views/GameWindow.xaml.cs @@ -8,7 +8,7 @@ namespace Tetris.Views; public partial class GameWindow : Window { - private static readonly GameViewModel GameViewModel = new GameViewModel(); + private static readonly GameViewModel GameViewModel = new(); public GameWindow() { @@ -25,25 +25,25 @@ public partial class GameWindow : Window // If key is space if (e.Key == Key.Space) { - GameViewModel.CurrentTetrominoe.RotateRight(); + GameViewModel.Game.CurrentTetrominoe?.RotateRight(); } // If key is down else if (e.Key == Key.Down) { - GameViewModel.CurrentTetrominoe.GoDown(); + GameViewModel.Game.CurrentTetrominoe?.GoDown(); } // If key is left else if (e.Key == Key.Left) { - GameViewModel.CurrentTetrominoe.GoLeft(); + GameViewModel.Game.CurrentTetrominoe?.GoLeft(); } // If key is right else if (e.Key == Key.Right) { - GameViewModel.CurrentTetrominoe.GoRight(); + GameViewModel.Game.CurrentTetrominoe?.GoRight(); } } } \ No newline at end of file diff --git a/ViewsModels/GameViewModel.cs b/ViewsModels/GameViewModel.cs index 201319b..5232abb 100644 --- a/ViewsModels/GameViewModel.cs +++ b/ViewsModels/GameViewModel.cs @@ -16,20 +16,18 @@ public class GameViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; + public static readonly Game Game = new("...", new Grid(new Color[25, 50])); + private const int RendererHertz = 5; private const int GameRendererHertz = (1 / RendererHertz) * 1000; - private const int Width = 25; - private const int Height = 50; + private const int Multiplier = 10; + private readonly int _width = (Game.Grid.MaxGrid.X + 1) * Multiplier; + private readonly int _height = (Game.Grid.MaxGrid.Y + 1) * Multiplier; private readonly WriteableBitmap _writeableBitmap; - private const int GridWidth = 25; - private const int GridHeight = 50; - private static readonly Grid Grid = new(new Color[GridWidth, GridHeight]); - private readonly Tetrominoe _currentTetrominoe = new(Grid, "J", new Point(25 / 2, 0), 0, Color.Aqua); - public GameViewModel() { - _writeableBitmap = BitmapFactory.New(Width, Height); + _writeableBitmap = BitmapFactory.New(_width, _height); var dispatcherRenderTimer = new DispatcherTimer { @@ -49,32 +47,32 @@ public class GameViewModel : INotifyPropertyChanged } public ImageSource Source => _writeableBitmap; - public Tetrominoe CurrentTetrominoe => _currentTetrominoe; private void Render(object? sender, EventArgs eventArgs) { _writeableBitmap.Lock(); _writeableBitmap.Clear(Colors.Black); - for (var x = 0; x < GridWidth; x++) + for (var x = 0; x < Game.Grid.MaxGrid.X + 1; x++) { - for (var y = 0; y < GridHeight; y++) + for (var y = 0; y < Game.Grid.MaxGrid.Y + 1; y++) { - var color = Grid.CGrid[x, y]; - _writeableBitmap.SetPixel(x, y, color.R, color.G, color.B); + var color = Game.Grid.CGrid[x, y]; + _writeableBitmap.FillRectangle(x * Multiplier, y * Multiplier, x * Multiplier + Multiplier, y * Multiplier + Multiplier, color.ToArgb()); } } - var tetrominoeWidth = _currentTetrominoe.Shape.GetLength(0); - var tetrominoeHeight = _currentTetrominoe.Shape.GetLength(1); + var tetrominoeWidth = Game.CurrentTetrominoe?.Shape.GetLength(0); + var tetrominoeHeight = Game.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.SetPixel(_currentTetrominoe.Coordinates.X + x, _currentTetrominoe.Coordinates.Y + y, color.R, color.G, color.B); + var currentPiece = Game.CurrentTetrominoe!; + if (currentPiece.Shape[x, y] == false) continue; + var color = currentPiece.Color; + _writeableBitmap.FillRectangle((currentPiece.Coordinates.X + x) * Multiplier, (currentPiece.Coordinates.Y + y) * Multiplier, (currentPiece.Coordinates.X + x) * Multiplier + Multiplier, (currentPiece.Coordinates.Y + y) * Multiplier + Multiplier, color.ToArgb()); } } @@ -83,6 +81,11 @@ public class GameViewModel : INotifyPropertyChanged private void Update(object? sender, EventArgs eventArgs) { - _currentTetrominoe.GoDown(); + Game.CurrentTetrominoe?.GoDown(); + + if (Game.HitBottom()) + { + Game.PrintTetrominoe(); + } } } \ No newline at end of file