Archived
1
0
Fork 0

Optimize and add detection for full line

This commit is contained in:
Ziedelth 2022-06-06 11:39:10 +02:00
parent 2e901dc995
commit e441c451db
4 changed files with 29 additions and 29 deletions

View file

@ -1,12 +1,10 @@
using System; using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using Tetris.ViewsModels; using Tetris.ViewsModels;
namespace Tetris.Views; namespace Tetris.Views;
public partial class GameWindow : Window public partial class GameWindow
{ {
private static readonly GameViewModel GameViewModel = new(); private static readonly GameViewModel GameViewModel = new();
@ -22,28 +20,24 @@ public partial class GameWindow : Window
private void UIElement_OnKeyDown(object sender, KeyEventArgs e) private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
{ {
// If key is space switch (e.Key)
if (e.Key == Key.Space)
{ {
GameViewModel.Game.CurrentTetrominoe?.RotateRight(); // If key is space
} case Key.Space:
GameViewModel.Game.CurrentTetrominoe?.RotateRight();
// If key is down break;
else if (e.Key == Key.Down) // If key is down
{ case Key.Down:
GameViewModel.Game.CurrentTetrominoe?.GoDown(); GameViewModel.Game.CurrentTetrominoe?.GoDown();
} break;
// If key is left
// If key is left case Key.Left:
else if (e.Key == Key.Left) GameViewModel.Game.CurrentTetrominoe?.GoLeft();
{ break;
GameViewModel.Game.CurrentTetrominoe?.GoLeft(); // If key is right
} case Key.Right:
GameViewModel.Game.CurrentTetrominoe?.GoRight();
// If key is right break;
else if (e.Key == Key.Right)
{
GameViewModel.Game.CurrentTetrominoe?.GoRight();
} }
} }
} }

View file

@ -8,7 +8,7 @@ namespace Tetris.Views;
/// <summary> /// <summary>
/// Interaction logic for MainWindow.xaml /// Interaction logic for MainWindow.xaml
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow
{ {
public MainWindow() public MainWindow()
{ {

View file

@ -45,6 +45,7 @@ public class GameViewModel : INotifyPropertyChanged
} }
public ImageSource Source => _writeableBitmap; public ImageSource Source => _writeableBitmap;
public ImageSource RightSource => _writeableBitmap;
private void Render(object? sender, EventArgs eventArgs) private void Render(object? sender, EventArgs eventArgs)
{ {
@ -92,5 +93,11 @@ public class GameViewModel : INotifyPropertyChanged
{ {
Game.PrintTetrominoe(); Game.PrintTetrominoe();
} }
if (Game.LineFull())
{
Game.ClearLine();
// TODO: Add score
}
} }
} }

View file

@ -4,11 +4,10 @@ namespace Tetris.ViewsModels;
public class ViewModel : INotifyPropertyChanged public class ViewModel : INotifyPropertyChanged
{ {
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) protected virtual void OnPropertyChanged(string propertyName)
{ {
if (PropertyChanged != null) PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
} }