Archived
1
0
Fork 0

Merge branch 'full-line' into 'master'

Full line

See merge request tetris-dotnet/tetris!6
This commit is contained in:
Ethanell 2022-06-07 14:46:43 +00:00
commit 17f1d0f108
8 changed files with 117 additions and 59 deletions

View file

@ -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]);
} }

View file

@ -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() {

View file

@ -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;
}
return -1;
} }
public void ClearLine() { public void ClearLine(int line) {
for (int x = 0; x <= MaxGrid.X; x++) for (int x = MinGrid.X; x <= MaxGrid.X; x++)
for (int y = MaxGrid.Y; y > 0; y--) { 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) {

View file

@ -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>

View file

@ -1,20 +1,16 @@
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();
public GameWindow() public GameWindow()
{ {
AttachConsole(-1); AttachConsole(-1);
InitializeComponent(); InitializeComponent();
DataContext = GameViewModel; DataContext = new GameViewModel();
} }
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
@ -22,28 +18,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

@ -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;
@ -46,21 +57,37 @@ public class GameViewModel : INotifyPropertyChanged
public ImageSource Source => _writeableBitmap; public ImageSource Source => _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());
} }
} }
@ -86,11 +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())
{ {
Game.PrintTetrominoe(); if (_countDown >= 3)
{
Game.PrintTetrominoe();
_countDown = 0;
}
else
_countDown++;
}
Game.ClearLine();
if (Game.HitTop())
{
_isPaused = true;
} }
} }
} }

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));
} }
} }