Skip to content

Commit

Permalink
feat: level support
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanJosipovic committed Jan 18, 2024
1 parent 5d82a30 commit f8e6ebb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions AvaloniaTetris.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvaloniaTetris.Desktop", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvaloniaTetris.Browser", "src\AvaloniaTetris.Browser\AvaloniaTetris.Browser.csproj", "{C024C876-CDFA-4FB7-9D24-132D66286E7E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1936A950-9AB4-4157-8A60-C2731831E079}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# AvaloniaTetris
A Tetris game written with Avalonia

[Live Demo](https://AvaloniaTetris.netlify.app)
43 changes: 35 additions & 8 deletions src/AvaloniaTetris/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ public Game()
private GridPoint[,] _positions = new GridPoint[10,20];

[ObservableProperty]
private int _level = 1;
private int _level;

[ObservableProperty]
private int _score = 0;
private int _score;

[ObservableProperty]
private int _lines = 0;
private int _lines;

[ObservableProperty]
private bool _isActive = true;

[ObservableProperty]
private int _speed;

private readonly object _lock = new();

private DispatcherTimer? timer;
Expand Down Expand Up @@ -125,8 +128,11 @@ private void RemoveFullRow()

if (removeRow)
{
Score += 100;
Lines++;
Level = Lines / 10;
Score += 40 * (Level + 1);

SetGameSpeed();

for (int yy = y + 1; yy <= 19; yy++)
{
Expand All @@ -149,6 +155,29 @@ private void RemoveFullRow()
}
}

private void SetGameSpeed()
{
timer.Interval = Level switch
{
0 => TimeSpan.FromMilliseconds(800),
1 => TimeSpan.FromMilliseconds(716),
2 => TimeSpan.FromMilliseconds(633.33),
3 => TimeSpan.FromMilliseconds(550),
4 => TimeSpan.FromMilliseconds(466.66),
5 => TimeSpan.FromMilliseconds(383.33),
6 => TimeSpan.FromMilliseconds(300),
7 => TimeSpan.FromMilliseconds(216.66),
8 => TimeSpan.FromMilliseconds(133.33),
9 => TimeSpan.FromMilliseconds(100),
10 or 11 or 12 => TimeSpan.FromMilliseconds(85),
13 or 14 or 15 => TimeSpan.FromMilliseconds(65),
16 or 17 or 18 => TimeSpan.FromMilliseconds(50),
19 or 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 => TimeSpan.FromMilliseconds(33.3),
_ => TimeSpan.FromMilliseconds(16.67),
};
Speed = (int)timer.Interval.TotalMilliseconds;
}

// Public

public void Start()
Expand All @@ -158,10 +187,8 @@ public void Start()
AddNewPiece();
SetActivePiece();

timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1)
};
timer = new DispatcherTimer();
SetGameSpeed();
timer.Tick += Timer_Tick;
timer.Start();
}
Expand Down
4 changes: 4 additions & 0 deletions src/AvaloniaTetris/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<Label Content="Lines: "></Label>
<Label Content="{Binding Game.Lines}"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Speed: "></Label>
<Label Content="{Binding Game.Speed}"></Label>
</StackPanel>
<CheckBox IsChecked="{Binding Game.IsActive}" Content="Is Active" IsEnabled="False" />
<Button Command="{Binding Game.RestartCommand}">Restart</Button>
<Button Command="{Binding Game.PauseCommand}">Pause</Button>
Expand Down

0 comments on commit f8e6ebb

Please sign in to comment.