示例#1
0
        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);
        }
示例#2
0
        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);
                }
            }
        }
示例#3
0
        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;
                }
            }
        }