diff --git a/Views/GameWindow.xaml b/Views/GameWindow.xaml index 6c1731d..4014365 100644 --- a/Views/GameWindow.xaml +++ b/Views/GameWindow.xaml @@ -4,8 +4,8 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" - Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}"> - + Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}" KeyDown="UIElement_OnKeyDown"> + \ No newline at end of file diff --git a/Views/GameWindow.xaml.cs b/Views/GameWindow.xaml.cs index 616691a..6c7f75d 100644 --- a/Views/GameWindow.xaml.cs +++ b/Views/GameWindow.xaml.cs @@ -1,18 +1,49 @@ -using System.Runtime.InteropServices; +using System; +using System.Runtime.InteropServices; using System.Windows; +using System.Windows.Input; using Tetris.ViewsModels; namespace Tetris.Views; public partial class GameWindow : Window { + private static readonly GameViewModel GameViewModel = new GameViewModel(); + public GameWindow() { AttachConsole(-1); InitializeComponent(); - DataContext = new GameViewModel(); + DataContext = GameViewModel; } [DllImport("kernel32.dll")] private static extern bool AttachConsole(int dwProcessId); + + private void UIElement_OnKeyDown(object sender, KeyEventArgs e) + { + // If key is space + if (e.Key == Key.Space) + { + GameViewModel.CurrentTetrominoe.RotateRight(); + } + + // If key is down + else if (e.Key == Key.Down) + { + GameViewModel.CurrentTetrominoe.GoDown(); + } + + // If key is left + else if (e.Key == Key.Left) + { + GameViewModel.CurrentTetrominoe.GoLeft(); + } + + // If key is right + else if (e.Key == Key.Right) + { + GameViewModel.CurrentTetrominoe.GoRight(); + } + } } \ No newline at end of file diff --git a/ViewsModels/GameViewModel.cs b/ViewsModels/GameViewModel.cs index 3fdbbf7..201319b 100644 --- a/ViewsModels/GameViewModel.cs +++ b/ViewsModels/GameViewModel.cs @@ -3,6 +3,7 @@ using System; using System.ComponentModel; using System.Drawing; +using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Threading; @@ -17,17 +18,18 @@ public class GameViewModel : INotifyPropertyChanged private const int RendererHertz = 5; private const int GameRendererHertz = (1 / RendererHertz) * 1000; - private readonly int _width = 480, _height = 640; + private const int Width = 25; + private const int Height = 50; private readonly WriteableBitmap _writeableBitmap; - private const int GridWidth = 10; - private const int GridHeight = 20; + 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(10, 10), 0, Color.Aqua); + 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 { @@ -39,16 +41,15 @@ public class GameViewModel : INotifyPropertyChanged var dispatcherUpdateTimer = new DispatcherTimer { - Interval = new TimeSpan(0, 0, 0, 0, 25) + Interval = new TimeSpan(0, 0, 0, 0, 100) }; dispatcherUpdateTimer.Tick += Update; dispatcherUpdateTimer.Start(); } - - public int Width => _width; - public int Height => _height; + public ImageSource Source => _writeableBitmap; + public Tetrominoe CurrentTetrominoe => _currentTetrominoe; private void Render(object? sender, EventArgs eventArgs) { @@ -73,7 +74,7 @@ public class GameViewModel : INotifyPropertyChanged { 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.SetPixel(_currentTetrominoe.Coordinates.X + x, _currentTetrominoe.Coordinates.Y + y, color.R, color.G, color.B); } }