From ab48a16f7f5ac6e8435985e8f52b2464598f3ca3 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Sun, 17 Apr 2022 19:47:22 +1000 Subject: [PATCH] Add some comments and game loop --- 13_Bounce/csharp/Game.cs | 31 +++++++++++++++++-------------- 13_Bounce/csharp/Program.cs | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/13_Bounce/csharp/Game.cs b/13_Bounce/csharp/Game.cs index 8d2baf8b..155bdf48 100644 --- a/13_Bounce/csharp/Game.cs +++ b/13_Bounce/csharp/Game.cs @@ -11,25 +11,28 @@ internal class Game _io = io; } - public void Play() + public void Play(Func playAgain) { _io.Write(Streams.Title); _io.Write(Streams.Instructions); - var timeIncrement = _io.ReadParameter("Time increment (sec)"); - var velocity = _io.ReadParameter("Velocity (fps)"); - var elasticity = _io.ReadParameter("Coefficient"); - - var bounce = new Bounce(velocity); - var bounceCount = (int)(Graph.Row.Width * timeIncrement / bounce.Duration); - var graph = new Graph(bounce.MaxHeight, timeIncrement); - - var time = 0f; - for (var i = 0; i < bounceCount; i++, bounce = bounce.Next(elasticity)) + while (playAgain.Invoke()) { - time = bounce.Plot(graph, time); - } + var timeIncrement = _io.ReadParameter("Time increment (sec)"); + var velocity = _io.ReadParameter("Velocity (fps)"); + var elasticity = _io.ReadParameter("Coefficient"); - _io.WriteLine(graph); + var bounce = new Bounce(velocity); + var bounceCount = (int)(Graph.Row.Width * timeIncrement / bounce.Duration); + var graph = new Graph(bounce.MaxHeight, timeIncrement); + + var time = 0f; + for (var i = 0; i < bounceCount; i++, bounce = bounce.Next(elasticity)) + { + time = bounce.Plot(graph, time); + } + + _io.WriteLine(graph); + } } } diff --git a/13_Bounce/csharp/Program.cs b/13_Bounce/csharp/Program.cs index 3b79a341..86af3365 100644 --- a/13_Bounce/csharp/Program.cs +++ b/13_Bounce/csharp/Program.cs @@ -3,4 +3,4 @@ global using Games.Common.Numbers; using Bounce; -new Game(new ConsoleIO()).Play(); \ No newline at end of file +new Game(new ConsoleIO()).Play(() => true); \ No newline at end of file