Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a message that is shown when a player scores a point #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions Stromblom.Pong/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ public void Run()
Thread.Sleep(115 - _ball.Speed);
}
_roundEnded = true;
handleInputTask.Wait();

if (_ball.X <= _playerOne.X)
_playerTwo.Points++;
else
_playerOne.Points++;
Player pointScorer = _ball.X <= _playerOne.X ? _playerTwo : _playerOne;
pointScorer.Points++;
DrawPointScored(pointScorer);

handleInputTask.Wait();
Reset();
}
Clear();
Expand Down Expand Up @@ -96,10 +95,27 @@ private void DrawScoreBoard()
}
}

private void DrawPointScored(Player scorer)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
int verticalCenter = (int)(_gameAreaHeight * 0.5f);
WriteCentered($" {scorer.Name} scored! ", verticalCenter - 1);
WriteCentered(" Press any key to continue ", verticalCenter + 1);
}

private void WriteCentered(string str, int fromTop)
{
Console.CursorLeft = (int)((_gameAreaWidth * 0.5f) - (str.Length * 0.5f));
Console.CursorTop = fromTop;
Console.Write(str);
}

private void Clear()
{
Console.ResetColor();
Console.Clear();
Console.SetCursorPosition(0, 0);
}

private void Reset()
Expand All @@ -115,7 +131,6 @@ private Player InitializePlayer(bool secondPlayer = false)
var playerName = String.Empty;
while (true)
{
Console.SetCursorPosition(0, 0);
if (!secondPlayer)
Console.WriteLine("Player 1 uses w/s to move up/down");
else
Expand All @@ -128,7 +143,7 @@ private Player InitializePlayer(bool secondPlayer = false)

Clear();

Console.WriteLine("Player name must be less then 18 characters long.");
Console.WriteLine("Player name must be less then 18 characters long.\n");
}

return new Player(playerName, secondPlayer);
Expand Down