diff --git a/Tetris/Migrations/20220607174015_initialSetup.Designer.cs b/Tetris/Migrations/20220607174015_initialSetup.Designer.cs
new file mode 100644
index 0000000..adf209f
--- /dev/null
+++ b/Tetris/Migrations/20220607174015_initialSetup.Designer.cs
@@ -0,0 +1,42 @@
+//
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Tetris.Models;
+
+#nullable disable
+
+namespace Tetris.Migrations
+{
+ [DbContext(typeof(ScoreContext))]
+ [Migration("20220607174015_initialSetup")]
+ partial class initialSetup
+ {
+ ///
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder.HasAnnotation("ProductVersion", "7.0.0-preview.4.22229.2");
+
+ modelBuilder.Entity("Tetris.Models.Score", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("INTEGER");
+
+ b.Property("Points")
+ .HasColumnType("INTEGER");
+
+ b.Property("UserName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("Scores");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/Tetris/Migrations/20220607174015_initialSetup.cs b/Tetris/Migrations/20220607174015_initialSetup.cs
new file mode 100644
index 0000000..d7acdd2
--- /dev/null
+++ b/Tetris/Migrations/20220607174015_initialSetup.cs
@@ -0,0 +1,35 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace Tetris.Migrations
+{
+ ///
+ public partial class initialSetup : Migration
+ {
+ ///
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "Scores",
+ columns: table => new
+ {
+ Id = table.Column(type: "INTEGER", nullable: false)
+ .Annotation("Sqlite:Autoincrement", true),
+ Points = table.Column(type: "INTEGER", nullable: false),
+ UserName = table.Column(type: "TEXT", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Scores", x => x.Id);
+ });
+ }
+
+ ///
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "Scores");
+ }
+ }
+}
diff --git a/Tetris/Migrations/ScoreContextModelSnapshot.cs b/Tetris/Migrations/ScoreContextModelSnapshot.cs
new file mode 100644
index 0000000..08c8499
--- /dev/null
+++ b/Tetris/Migrations/ScoreContextModelSnapshot.cs
@@ -0,0 +1,39 @@
+//
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Tetris.Models;
+
+#nullable disable
+
+namespace Tetris.Migrations
+{
+ [DbContext(typeof(ScoreContext))]
+ partial class ScoreContextModelSnapshot : ModelSnapshot
+ {
+ protected override void BuildModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder.HasAnnotation("ProductVersion", "7.0.0-preview.4.22229.2");
+
+ modelBuilder.Entity("Tetris.Models.Score", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("INTEGER");
+
+ b.Property("Points")
+ .HasColumnType("INTEGER");
+
+ b.Property("UserName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("Scores");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
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 @@
-
+
+
+
+
\ No newline at end of file
diff --git a/Tetris/Views/MainWindow.xaml.cs b/Tetris/Views/MainWindow.xaml.cs
index ea38c31..3accbc6 100644
--- a/Tetris/Views/MainWindow.xaml.cs
+++ b/Tetris/Views/MainWindow.xaml.cs
@@ -1,6 +1,8 @@
using System;
using System.Runtime.InteropServices;
using System.Windows;
+using Microsoft.EntityFrameworkCore;
+using Tetris.Models;
using Tetris.ViewsModels;
namespace Tetris.Views;
@@ -10,8 +12,9 @@ namespace Tetris.Views;
///
public partial class MainWindow
{
- public MainWindow()
- {
+ public MainWindow() {
+ using var db = new ScoreContext();
+ db.Database.Migrate();
AttachConsole(-1);
DataContext = new ViewModel();
InitializeComponent();
@@ -23,7 +26,7 @@ public partial class MainWindow
private void StartButton_OnClick(object sender, RoutedEventArgs e)
{
Console.WriteLine("Start game...");
- new GameWindow().Show();
+ new GameWindow().Start(((ViewModel) DataContext).UserName);
Close();
}
}
\ No newline at end of file
diff --git a/Tetris/ViewsModels/GameViewModel.cs b/Tetris/ViewsModels/GameViewModel.cs
index 0d64eaf..9403ff4 100644
--- a/Tetris/ViewsModels/GameViewModel.cs
+++ b/Tetris/ViewsModels/GameViewModel.cs
@@ -1,8 +1,7 @@
-
-
-using System;
+using System;
+using System.Collections.Generic;
using System.ComponentModel;
-using System.Windows.Controls;
+using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
@@ -30,12 +29,11 @@ public class GameViewModel : INotifyPropertyChanged
private readonly int _height = (Game.Grid.MaxGrid.Y + 1) * Multiplier;
private readonly WriteableBitmap _writeableBitmap;
private readonly int _colorLine = 0xFFFFFF;
- private bool _isPaused;
private uint _countDown;
private string _scoreText = "";
+ private string _userNameText = "";
- public GameViewModel()
- {
+ public GameViewModel() {
_writeableBitmap = BitmapFactory.New(_width, _height);
var dispatcherRenderTimer = new DispatcherTimer
@@ -67,11 +65,29 @@ public class GameViewModel : INotifyPropertyChanged
}
}
+ public List ScoreBoard {
+ get {
+ using var db = new ScoreContext();
+ return db.Scores.ToList();
+ }
+ }
+
+ public string UserNameText
+ {
+ get => _userNameText;
+ set
+ {
+ _userNameText = value;
+ OnPropertyChanged("UserNameText");
+ }
+ }
+
private void Render(object? sender, EventArgs eventArgs)
{
_writeableBitmap.Lock();
_writeableBitmap.Clear(Colors.Black);
ScoreText = "Score: " + Game.Score;
+ UserNameText = "Username: " + Game.UserName;
var colorGrid = Game.Grid.CGrid;
for (var x = 0; x < Game.Grid.MaxGrid.X + 1; x++)
@@ -113,7 +129,7 @@ public class GameViewModel : INotifyPropertyChanged
private void Update(object? sender, EventArgs eventArgs)
{
- if (_isPaused)
+ if (!Game.Playing)
{
return;
}
@@ -135,7 +151,9 @@ public class GameViewModel : INotifyPropertyChanged
if (Game.HitTop())
{
- _isPaused = true;
+ Game.Playing = false;
+ Game.PrintTetrominoe();
+ Game.SaveGame();
}
}
}
\ No newline at end of file
diff --git a/Tetris/ViewsModels/ViewModel.cs b/Tetris/ViewsModels/ViewModel.cs
index 3fe7583..a63bfb8 100644
--- a/Tetris/ViewsModels/ViewModel.cs
+++ b/Tetris/ViewsModels/ViewModel.cs
@@ -6,6 +6,17 @@ public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
+ private string _userName = "Player";
+
+ public string UserName {
+ get { return _userName; }
+ set
+ {
+ _userName = value;
+ OnPropertyChanged("UserName");
+ }
+ }
+
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));