示例#1
0
        private void GameLoop()
        {
            var t = new Stopwatch();

            t.Start();

            while (GameState != EGameState.Exit)
            {
                // game active
                if (GameState == EGameState.Running && t.ElapsedMilliseconds >= 100)
                {
                    t.Restart();
                    // check input
                    CheckInput();

                    // move all moveables
                    foreach (var t1 in LevelManager.Moveables)
                    {
                        t1.UpdatePosition();
                    }

                    // check collisions among all collidables
                    var collisions = CollisionManager.CheckCollision(LevelManager.Collidables);
                    if (collisions != null)
                    {
                        ApplyCollisions(collisions);
                    }

                    // check to see if there are still players playing
                    var playersLeft = false;
                    var playerWon   = false;
                    foreach (var go in LevelManager.GameObjects)
                    {
                        if (!(go is IPlayer))
                        {
                            continue;
                        }
                        playersLeft = true;
                        if (go.Body.Count >= LevelManager.Height * LevelManager.Width)
                        {
                            playerWon = true;
                        }
                    }

                    // end game if no players or player won
                    if (!playersLeft || playerWon)
                    {
                        GameState = EGameState.Over;
                        OnGameStateChangedListener(GameState);
                    }

                    // render all renderables
                    if (GameState == EGameState.Running)
                    {
                        Console.Clear();
                        foreach (IRenderable t1 in LevelManager.Renderables)
                        {
                            t1.Render();
                        }
                    }
                    // game initializing
                }
                else
                {
                    switch (GameState)
                    {
                    case EGameState.Init:
                        CheckInput();
                        break;

                    case EGameState.Over:
                        if (LevelManager.GameOver != null)
                        {
                            LevelManager.GameOver.Render(ScoreManager.GetScore());
                        }
                        else
                        {
                            Environment.Exit(0);
                        }
                        while (GameState == EGameState.Over)
                        {
                            CheckInput();
                        }
                        break;

                    case EGameState.Paused:
                        CheckInput();
                        break;
                    }
                }
            }
            StartupManager.Reset();
        }
示例#2
0
        private void GameLoop()
        {
            var t = new Stopwatch();

            t.Start();

            while (GameState != EGameState.Over)
            {
                if (GameState != EGameState.Paused)
                {
                    if (t.ElapsedMilliseconds < 100)
                    {
                        continue;
                    }
                    t.Restart();

                    // check input
                    CheckInput();

                    // move all moveables
                    foreach (IMoveable t1 in LevelManager.Moveables)
                    {
                        t1.UpdatePosition();
                    }

                    // check collisions among all collidables
                    var collisions = CollisionManager.CheckCollision(LevelManager.Collidables);
                    if (collisions != null)
                    {
                        ApplyCollisions(collisions);
                    }

                    // check to see if there are still players playing
                    var playersLeft = false;
                    foreach (var go in LevelManager.GameObjects)
                    {
                        if (!(go is IPlayer))
                        {
                            continue;
                        }
                        playersLeft = true;
                    }

                    if (!playersLeft)
                    {
                        GameState = EGameState.Over;
                        OnGameStateChangedListener(GameState);
                    }

                    // render all renderables
                    if (GameState == EGameState.Running)
                    {
                        Console.Clear();
                        foreach (IRenderable t1 in LevelManager.Renderables)
                        {
                            t1.Render();
                        }
                    }
                }
                else
                {
                    // CheckInput if paused
                    CheckInput();
                }
            }
        }