public void StartGame() { //Console.WriteLine("Start Game"); Score = 0; Bird.Reset(); Pipes.Reset(); IsGameOver = false; IsRunning = true; IsPaused = false; loopCancellationToken = new CancellationTokenSource(); _ = GameLoop(loopCancellationToken.Token); }
private void CheckPassedPipes() { foreach (var pipes in Pipes.ToList()) { if (pipes.IsPassed) { return; } var pipeBounds = pipes.PipeTop.GetBounds(); if (pipeBounds.X + pipeBounds.Width < Bird.Left) { Score++; Pipes.AddPassedPipes(pipes); } } }
public async Task GameLoop(CancellationToken cancellationToken) { //Console.WriteLine("Start Game Loop"); var lastTicks = DateTime.UtcNow.Ticks; while (IsRunning) { var deltaMs = (DateTime.UtcNow.Ticks - lastTicks) / 10000; var delta = 2 * deltaMs / 16; if (!IsPaused) { //Console.WriteLine($"Loop {delta} {deltaMs} {stopwatch.ElapsedMilliseconds}"); Bird.Move(delta); Pipes.Move(delta); CheckCollisions(); CheckPassedPipes(); Rendered?.Invoke(); } lastTicks = DateTime.UtcNow.Ticks; await Task.Delay(16); if (cancellationToken.IsCancellationRequested) { break; } } }