Archived
1
0
Fork 0
This commit is contained in:
Ziedelth 2022-05-04 11:08:04 +02:00
parent 010a5e7349
commit 38e3b197ba
2 changed files with 27 additions and 24 deletions

View file

@ -8,7 +8,7 @@ namespace Tetris.Views;
public partial class GameWindow : Window public partial class GameWindow : Window
{ {
private static readonly GameViewModel GameViewModel = new GameViewModel(); private static readonly GameViewModel GameViewModel = new();
public GameWindow() public GameWindow()
{ {
@ -25,25 +25,25 @@ public partial class GameWindow : Window
// If key is space // If key is space
if (e.Key == Key.Space) if (e.Key == Key.Space)
{ {
GameViewModel.CurrentTetrominoe.RotateRight(); GameViewModel.Game.CurrentTetrominoe?.RotateRight();
} }
// If key is down // If key is down
else if (e.Key == Key.Down) else if (e.Key == Key.Down)
{ {
GameViewModel.CurrentTetrominoe.GoDown(); GameViewModel.Game.CurrentTetrominoe?.GoDown();
} }
// If key is left // If key is left
else if (e.Key == Key.Left) else if (e.Key == Key.Left)
{ {
GameViewModel.CurrentTetrominoe.GoLeft(); GameViewModel.Game.CurrentTetrominoe?.GoLeft();
} }
// If key is right // If key is right
else if (e.Key == Key.Right) else if (e.Key == Key.Right)
{ {
GameViewModel.CurrentTetrominoe.GoRight(); GameViewModel.Game.CurrentTetrominoe?.GoRight();
} }
} }
} }

View file

@ -16,20 +16,18 @@ public class GameViewModel : INotifyPropertyChanged
{ {
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
public static readonly Game Game = new("...", new Grid(new Color[25, 50]));
private const int RendererHertz = 5; private const int RendererHertz = 5;
private const int GameRendererHertz = (1 / RendererHertz) * 1000; private const int GameRendererHertz = (1 / RendererHertz) * 1000;
private const int Width = 25; private const int Multiplier = 10;
private const int Height = 50; 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 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() public GameViewModel()
{ {
_writeableBitmap = BitmapFactory.New(Width, Height); _writeableBitmap = BitmapFactory.New(_width, _height);
var dispatcherRenderTimer = new DispatcherTimer var dispatcherRenderTimer = new DispatcherTimer
{ {
@ -49,32 +47,32 @@ public class GameViewModel : INotifyPropertyChanged
} }
public ImageSource Source => _writeableBitmap; public ImageSource Source => _writeableBitmap;
public Tetrominoe CurrentTetrominoe => _currentTetrominoe;
private void Render(object? sender, EventArgs eventArgs) private void Render(object? sender, EventArgs eventArgs)
{ {
_writeableBitmap.Lock(); _writeableBitmap.Lock();
_writeableBitmap.Clear(Colors.Black); _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]; var color = Game.Grid.CGrid[x, y];
_writeableBitmap.SetPixel(x, y, color.R, color.G, color.B); _writeableBitmap.FillRectangle(x * Multiplier, y * Multiplier, x * Multiplier + Multiplier, y * Multiplier + Multiplier, color.ToArgb());
} }
} }
var tetrominoeWidth = _currentTetrominoe.Shape.GetLength(0); var tetrominoeWidth = Game.CurrentTetrominoe?.Shape.GetLength(0);
var tetrominoeHeight = _currentTetrominoe.Shape.GetLength(1); var tetrominoeHeight = Game.CurrentTetrominoe?.Shape.GetLength(1);
for (int x = 0; x < tetrominoeWidth; x++) for (int x = 0; x < tetrominoeWidth; x++)
{ {
for (int y = 0; y < tetrominoeHeight; y++) for (int y = 0; y < tetrominoeHeight; y++)
{ {
if (!_currentTetrominoe.Shape[x, y]) continue; var currentPiece = Game.CurrentTetrominoe!;
var color = _currentTetrominoe.Color; if (currentPiece.Shape[x, y] == false) continue;
_writeableBitmap.SetPixel(_currentTetrominoe.Coordinates.X + x, _currentTetrominoe.Coordinates.Y + y, color.R, color.G, color.B); 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) private void Update(object? sender, EventArgs eventArgs)
{ {
_currentTetrominoe.GoDown(); Game.CurrentTetrominoe?.GoDown();
if (Game.HitBottom())
{
Game.PrintTetrominoe();
}
} }
} }