diff --git a/Tetris/Models/Game.cs b/Tetris/Models/Game.cs index 4411e88..73d6c19 100644 --- a/Tetris/Models/Game.cs +++ b/Tetris/Models/Game.cs @@ -28,6 +28,16 @@ public class Game : INotifyPropertyChanged { } } + private bool _playing = true; + + public bool Playing { + get => _playing; + set { + _playing = value; + OnPropertyChanged("Playing"); + } + } + private Grid _grid; public Grid Grid => _grid; @@ -78,8 +88,14 @@ public class Game : INotifyPropertyChanged { return; _grid.PrintTetrominoe(_currentTetrominoe); - _currentTetrominoe = _nextTetrominoe; - _nextTetrominoe = GetNewTetrominoe(); + + if (Playing) { + _currentTetrominoe = _nextTetrominoe; + _nextTetrominoe = GetNewTetrominoe(); + } else { + _currentTetrominoe = null; + _nextTetrominoe = null; + } } public Tetrominoe GetNewTetrominoe() { @@ -87,6 +103,14 @@ public class Game : INotifyPropertyChanged { return new Tetrominoe(_grid, tetrominoes[_random.Next(tetrominoes.Count)]); } + public void SaveGame() { + Console.WriteLine("Game over"); + using var db = new ScoreContext(); + var score = new Score{Points = Score, UserName = UserName}; + db.Scores.Add(score); + db.SaveChanges(); + } + public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) diff --git a/Tetris/Models/Score.cs b/Tetris/Models/Score.cs new file mode 100644 index 0000000..dda2478 --- /dev/null +++ b/Tetris/Models/Score.cs @@ -0,0 +1,11 @@ +namespace Tetris.Models; + +public class Score { + public int Id { get; set; } + public int Points { get; set; } + public string UserName { get; set; } + + public override string ToString() { + return UserName + ": " + Points; + } +} diff --git a/Tetris/Models/ScoreContext.cs b/Tetris/Models/ScoreContext.cs new file mode 100644 index 0000000..114500b --- /dev/null +++ b/Tetris/Models/ScoreContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; + +namespace Tetris.Models; + +public class ScoreContext : DbContext { + public DbSet Scores { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite(@"Data Source=.\Resources\db.sl3"); + } +} diff --git a/Tetris/Tetris.csproj b/Tetris/Tetris.csproj index f0ad596..d0ce089 100644 --- a/Tetris/Tetris.csproj +++ b/Tetris/Tetris.csproj @@ -20,11 +20,21 @@ Always + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/Tetris/Views/GameWindow.xaml b/Tetris/Views/GameWindow.xaml index 80015c7..51346c5 100644 --- a/Tetris/Views/GameWindow.xaml +++ b/Tetris/Views/GameWindow.xaml @@ -6,7 +6,19 @@ mc:Ignorable="d" Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}" KeyDown="UIElement_OnKeyDown"> - + + + + + + + + + + \ No newline at end of file diff --git a/Tetris/Views/GameWindow.xaml.cs b/Tetris/Views/GameWindow.xaml.cs index 561455f..85108ef 100644 --- a/Tetris/Views/GameWindow.xaml.cs +++ b/Tetris/Views/GameWindow.xaml.cs @@ -13,11 +13,19 @@ public partial class GameWindow DataContext = new GameViewModel(); } + public void Start(string userName) { + GameViewModel.Game.UserName = userName; + Show(); + } + [DllImport("kernel32.dll")] private static extern bool AttachConsole(int dwProcessId); private void UIElement_OnKeyDown(object sender, KeyEventArgs e) { + if (!GameViewModel.Game.Playing) + return; + switch (e.Key) { // If key is space diff --git a/Tetris/Views/MainWindow.xaml b/Tetris/Views/MainWindow.xaml index 8993ed9..b91dcf7 100644 --- a/Tetris/Views/MainWindow.xaml +++ b/Tetris/Views/MainWindow.xaml @@ -10,7 +10,12 @@ -