Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

Commit

Permalink
Hightscores system
Browse files Browse the repository at this point in the history
  • Loading branch information
Team-on committed Aug 12, 2018
1 parent 67ab1de commit 364b0fe
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions source/GameOverWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<TextBlock Text="You lose" Style="{StaticResource menuText}" DockPanel.Dock="Top"/>
<TextBlock Text="Score:" x:Name="ScoreText" Style="{StaticResource menuText}" DockPanel.Dock="Top"/>
<StackPanel Margin="0 35 0 0">
<TextBox Text="Player" x:Name="nick" HorizontalContentAlignment="Center" FontSize="32" Width="220" FontFamily="{StaticResource PixelFont}" />
<Button Content="Play again" Click="Button_Play" Style="{StaticResource menuBtn}"/>
<Button Content="Menu" Click="Button_Menu" Style="{StaticResource menuBtn}"/>
</StackPanel>
Expand Down
6 changes: 6 additions & 0 deletions source/GameOverWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ private void Button_Play(object sender, EventArgs e) {
Sound.Click();
WindowManager.ReopenWindow(this, MenuWindow.menuWindow);
MenuWindow.menuWindow.Button_Play(null, null);
SaveScore();
}

private void Button_Menu(object sender, EventArgs e) {
Sound.Click();
WindowManager.ReopenWindow(this, MenuWindow.menuWindow);
SaveScore();
}

void SaveScore() {
System.IO.File.AppendAllText(@".\score", nick.Text + '|' + MenuWindow.gameWindow.score.ToString() + '\n');
}

private void Window_Activated(object sender, EventArgs e) {
Expand Down
4 changes: 2 additions & 2 deletions source/HightscoresWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

<TextBlock Text="Hightscores:" Style="{StaticResource menuText}" DockPanel.Dock="Top"/>
<Button Content="Back" Click="Button_Back" DockPanel.Dock="Bottom" FontFamily="{StaticResource PixelFont}" FontSize="20" HorizontalAlignment="Right" Width="100"/>
<StackPanel Margin="10" DockPanel.Dock="Left" x:Name="nickPanel">
<StackPanel Margin="10" DockPanel.Dock="Left" x:Name="nickPanel" HorizontalAlignment="Right">
<TextBlock Style="{StaticResource hightscoresTextNick}" Text="Team-on"/>
<TextBlock Style="{StaticResource hightscoresTextNick}" Text="Player"/>
<TextBlock Style="{StaticResource hightscoresTextNick}" Text="One more player"/>
<TextBlock Style="{StaticResource hightscoresTextNick}" Text="Very long nickname player"/>
</StackPanel>
<StackPanel Margin="10" DockPanel.Dock="Left" x:Name="scorePanel">
<StackPanel Margin="10" HorizontalAlignment="Left" DockPanel.Dock="Left" x:Name="scorePanel">
<TextBlock Style="{StaticResource hightscoresTextScore}" Text="1234567890"/>
<TextBlock Style="{StaticResource hightscoresTextScore}" Text="234662"/>
<TextBlock Style="{StaticResource hightscoresTextScore}" Text="125"/>
Expand Down
14 changes: 14 additions & 0 deletions source/HightscoresWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ private void Button_Back(object sender, RoutedEventArgs e) {
private void Window_Activated(object sender, EventArgs e) {
nickPanel.Children.Clear();
scorePanel.Children.Clear();

string[] lines = System.IO.File.ReadAllLines(@".\score");
List<Tuple<string, ulong>> scores = new List<Tuple<string, ulong>>(lines.Length);
foreach (var l in lines) {
var tmp = l.Split('|');
scores.Add(new Tuple<string, ulong>(tmp[0], ulong.Parse(tmp[1])));
}

scores.Sort((a, b) => (int)(b.Item2 - a.Item2));

foreach (var s in scores) {
nickPanel.Children.Add(new TextBlock() { Text = s.Item1, Style = (Style)FindResource("hightscoresTextNick") });
scorePanel.Children.Add(new TextBlock() { Text = s.Item2.ToString(), Style = (Style)FindResource("hightscoresTextNick") });
}
}
}
}

0 comments on commit 364b0fe

Please sign in to comment.