Fix and add score text
This commit is contained in:
parent
e441c451db
commit
8fc2ad081f
6 changed files with 92 additions and 34 deletions
|
@ -133,14 +133,16 @@ public class GridTest {
|
||||||
[Test]
|
[Test]
|
||||||
public void LineFull() {
|
public void LineFull() {
|
||||||
Console.Out.WriteLine(g.ToString());
|
Console.Out.WriteLine(g.ToString());
|
||||||
Assert.False(g.LineFull());
|
Assert.AreEqual(-1, g.LineFull());
|
||||||
|
|
||||||
|
int line = random.Next(g.MinGrid.Y, g.MaxGrid.Y);
|
||||||
|
|
||||||
for (int x = 0; x < ig.GetLength(0); x++)
|
for (int x = 0; x < ig.GetLength(0); x++)
|
||||||
ig[x, ig.GetLength(1) - 1] = Color.Aqua;
|
ig[x, line] = Color.Aqua;
|
||||||
|
|
||||||
Console.Out.WriteLine("==========");
|
Console.Out.WriteLine("==========");
|
||||||
Console.Out.WriteLine(g.ToString());
|
Console.Out.WriteLine(g.ToString());
|
||||||
Assert.True(g.LineFull());
|
Assert.AreEqual(line, g.LineFull());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -150,16 +152,17 @@ public class GridTest {
|
||||||
ig[x, y] = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
|
ig[x, y] = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int line = random.Next(g.MinGrid.Y, g.MaxGrid.Y);
|
||||||
for (int x = 0; x < ig.GetLength(0); x++)
|
for (int x = 0; x < ig.GetLength(0); x++)
|
||||||
ig[x, ig.GetLength(1) - 1] = Color.Aqua;
|
ig[x, line] = Color.Aqua;
|
||||||
|
|
||||||
Color[,] olg_grid = (Color[,])ig.Clone();
|
Color[,] olg_grid = (Color[,])ig.Clone();
|
||||||
Console.Out.WriteLine(g.ToString());
|
Console.Out.WriteLine(g.ToString());
|
||||||
g.ClearLine();
|
g.ClearLine(line);
|
||||||
Console.Out.WriteLine("==========");
|
Console.Out.WriteLine("==========");
|
||||||
Console.Out.WriteLine(g.ToString());
|
Console.Out.WriteLine(g.ToString());
|
||||||
for (uint x = 0; x < ig.GetLength(0); x++)
|
for (uint x = 0; x < ig.GetLength(0); x++)
|
||||||
for (uint y = 1; y < ig.GetLength(1); y++) {
|
for (uint y = 1; y <= line; y++) {
|
||||||
Assert.AreEqual(olg_grid[x, y-1], ig[x,y]);
|
Assert.AreEqual(olg_grid[x, y-1], ig[x,y]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,12 +61,16 @@ public class Game : INotifyPropertyChanged {
|
||||||
return _grid.MinGrid.Y == _currentTetrominoe.Coordinates.Y;
|
return _grid.MinGrid.Y == _currentTetrominoe.Coordinates.Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool LineFull() {
|
public void ClearLine()
|
||||||
return _grid.LineFull();
|
{
|
||||||
}
|
int line = _grid.LineFull();
|
||||||
|
|
||||||
public void ClearLine() {
|
while (line != -1)
|
||||||
_grid.ClearLine();
|
{
|
||||||
|
_grid.ClearLine(line);
|
||||||
|
Score += Grid.CGrid.GetLength(0);
|
||||||
|
line = _grid.LineFull();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PrintTetrominoe() {
|
public void PrintTetrominoe() {
|
||||||
|
|
|
@ -38,18 +38,32 @@ public class Grid {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool LineFull() {
|
public int LineFull()
|
||||||
return Enumerable.Range(0, _grid.GetLength(0)).Select(x => _grid[x, MaxGrid.Y]).All(x => x != Color.Empty);
|
{
|
||||||
|
for (int y = MaxGrid.Y; y > 0; y--)
|
||||||
|
{
|
||||||
|
bool full = true;
|
||||||
|
for (int x = 0; x <= MaxGrid.X; x++)
|
||||||
|
if (_grid[x, y] == Color.Empty)
|
||||||
|
{
|
||||||
|
full = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (full)
|
||||||
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClearLine() {
|
return -1;
|
||||||
for (int x = 0; x <= MaxGrid.X; x++)
|
}
|
||||||
for (int y = MaxGrid.Y; y > 0; y--) {
|
|
||||||
|
public void ClearLine(int line) {
|
||||||
|
for (int x = MinGrid.X; x <= MaxGrid.X; x++)
|
||||||
|
for (int y = line; y > MinGrid.Y; y--) {
|
||||||
_grid[x, y] = _grid[x, y - 1];
|
_grid[x, y] = _grid[x, y - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int x = 0; x <= MaxGrid.X; x++)
|
for (int x = 0; x <= MaxGrid.X; x++)
|
||||||
_grid[x,0] = Color.Empty;
|
_grid[x,MinGrid.Y] = Color.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PrintTetrominoe(Tetrominoe t) {
|
public void PrintTetrominoe(Tetrominoe t) {
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}" KeyDown="UIElement_OnKeyDown">
|
Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}" KeyDown="UIElement_OnKeyDown">
|
||||||
<Grid Background="Black">
|
<Grid Background="Black">
|
||||||
|
<TextBlock Text="{Binding ScoreText}" Foreground="White" FontSize="32" />
|
||||||
<Image Source="{Binding Source}" />
|
<Image Source="{Binding Source}" />
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
|
@ -6,13 +6,11 @@ namespace Tetris.Views;
|
||||||
|
|
||||||
public partial class GameWindow
|
public partial class GameWindow
|
||||||
{
|
{
|
||||||
private static readonly GameViewModel GameViewModel = new();
|
|
||||||
|
|
||||||
public GameWindow()
|
public GameWindow()
|
||||||
{
|
{
|
||||||
AttachConsole(-1);
|
AttachConsole(-1);
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
DataContext = GameViewModel;
|
DataContext = new GameViewModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
[DllImport("kernel32.dll")]
|
[DllImport("kernel32.dll")]
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Windows.Controls;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
using System.Windows.Threading;
|
using System.Windows.Threading;
|
||||||
using Tetris.Models;
|
using Tetris.Models;
|
||||||
using Color = System.Drawing.Color;
|
using Color = System.Drawing.Color;
|
||||||
|
using Grid = Tetris.Models.Grid;
|
||||||
|
|
||||||
namespace Tetris.ViewsModels;
|
namespace Tetris.ViewsModels;
|
||||||
|
|
||||||
|
@ -14,7 +16,12 @@ public class GameViewModel : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
public event PropertyChangedEventHandler? PropertyChanged;
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
|
|
||||||
public static readonly Game Game = new("...", new Grid(new Color[20, 40]));
|
protected virtual void OnPropertyChanged(string propertyName)
|
||||||
|
{
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly Game Game = new("...", new Grid(new Color[15, 30]));
|
||||||
|
|
||||||
private const int RendererHertz = 5;
|
private const int RendererHertz = 5;
|
||||||
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
|
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
|
||||||
|
@ -22,6 +29,10 @@ public class GameViewModel : INotifyPropertyChanged
|
||||||
private readonly int _width = (Game.Grid.MaxGrid.X + 1) * Multiplier;
|
private readonly int _width = (Game.Grid.MaxGrid.X + 1) * Multiplier;
|
||||||
private readonly int _height = (Game.Grid.MaxGrid.Y + 1) * Multiplier;
|
private readonly int _height = (Game.Grid.MaxGrid.Y + 1) * Multiplier;
|
||||||
private readonly WriteableBitmap _writeableBitmap;
|
private readonly WriteableBitmap _writeableBitmap;
|
||||||
|
private readonly int _colorLine = 0xFFFFFF;
|
||||||
|
private bool _isPaused;
|
||||||
|
private uint _countDown;
|
||||||
|
private string _scoreText = "";
|
||||||
|
|
||||||
public GameViewModel()
|
public GameViewModel()
|
||||||
{
|
{
|
||||||
|
@ -37,7 +48,7 @@ public class GameViewModel : INotifyPropertyChanged
|
||||||
|
|
||||||
var dispatcherUpdateTimer = new DispatcherTimer
|
var dispatcherUpdateTimer = new DispatcherTimer
|
||||||
{
|
{
|
||||||
Interval = new TimeSpan(0, 0, 0, 0, 100)
|
Interval = new TimeSpan(0, 0, 0, 0, 200)
|
||||||
};
|
};
|
||||||
|
|
||||||
dispatcherUpdateTimer.Tick += Update;
|
dispatcherUpdateTimer.Tick += Update;
|
||||||
|
@ -45,23 +56,38 @@ public class GameViewModel : INotifyPropertyChanged
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageSource Source => _writeableBitmap;
|
public ImageSource Source => _writeableBitmap;
|
||||||
public ImageSource RightSource => _writeableBitmap;
|
|
||||||
|
public string ScoreText
|
||||||
|
{
|
||||||
|
get => _scoreText;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_scoreText = value;
|
||||||
|
OnPropertyChanged("ScoreText");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
ScoreText = "Score: " + Game.Score;
|
||||||
|
|
||||||
var colorGrid = Game.Grid.CGrid;
|
var colorGrid = Game.Grid.CGrid;
|
||||||
for (var x = 0; x < Game.Grid.MaxGrid.X + 1; x++)
|
for (var x = 0; x < Game.Grid.MaxGrid.X + 1; x++)
|
||||||
{
|
{
|
||||||
|
var startX = x * Multiplier;
|
||||||
|
var endX = startX + Multiplier;
|
||||||
|
|
||||||
for (var y = 0; y < Game.Grid.MaxGrid.Y + 1; y++)
|
for (var y = 0; y < Game.Grid.MaxGrid.Y + 1; y++)
|
||||||
{
|
{
|
||||||
if (colorGrid[x, y] == Color.Empty) continue;
|
|
||||||
|
|
||||||
var startX = x * Multiplier;
|
|
||||||
var startY = y * Multiplier;
|
var startY = y * Multiplier;
|
||||||
_writeableBitmap.FillRectangle(startX, startY, startX + Multiplier, startY + Multiplier, colorGrid[x, y].ToArgb());
|
var endY = startY + Multiplier;
|
||||||
|
|
||||||
|
_writeableBitmap.DrawLine(startX, startY, endX, startY, _colorLine);
|
||||||
|
_writeableBitmap.DrawLine(startX, startY, startX, endY, _colorLine);
|
||||||
|
if (colorGrid[x, y] == Color.Empty) continue;
|
||||||
|
_writeableBitmap.FillRectangle(startX, startY, endX, endY, colorGrid[x, y].ToArgb());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,17 +113,29 @@ public class GameViewModel : INotifyPropertyChanged
|
||||||
|
|
||||||
private void Update(object? sender, EventArgs eventArgs)
|
private void Update(object? sender, EventArgs eventArgs)
|
||||||
{
|
{
|
||||||
|
if (_isPaused)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Game.CurrentTetrominoe?.GoDown();
|
Game.CurrentTetrominoe?.GoDown();
|
||||||
|
|
||||||
if (Game.HitBottom())
|
if (Game.HitBottom())
|
||||||
|
{
|
||||||
|
if (_countDown >= 3)
|
||||||
{
|
{
|
||||||
Game.PrintTetrominoe();
|
Game.PrintTetrominoe();
|
||||||
|
_countDown = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
_countDown++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Game.LineFull())
|
|
||||||
{
|
|
||||||
Game.ClearLine();
|
Game.ClearLine();
|
||||||
// TODO: Add score
|
|
||||||
|
if (Game.HitTop())
|
||||||
|
{
|
||||||
|
_isPaused = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue