Ejemplo n.º 1
0
        public void DrawScreen()
        {
            //Draw instructions
            _textDraw.DrawText(
                instructions
                , TextDraw.Justify.CENTER
                , instructionOffset
                , instructionSize, instructionSize
                );

            // Flip back and forth between "Game Over" and "Asteroids"
            if ((_letterSize > 1000) || (_letterSize < 40))
            {
                _increment = -_increment;
                if (_letterSize < 40)
                {
                    if (_title == "GAME OVER")
                    {
                        _title = "ASTEROIDS";
                    }
                    else
                    {
                        _title = "GAME OVER";
                    }
                }
            }
            _letterSize += _increment;
            _textDraw.DrawText(_title, TextDraw.Justify.CENTER,
                               ScreenCanvas.CANVAS_HEIGHT / 2 - _letterSize, _letterSize, _letterSize * 2);

            // Draw copyright notice
            _textDraw.DrawText(copyright1, TextDraw.Justify.CENTER,
                               titleOffset1, titleSize, titleSize);

            _textDraw.DrawText(copyright2, TextDraw.Justify.CENTER,
                               titleOffset2, titleSize, titleSize);

            // Draw the asteroid belt
            _cache.Repopulate();

            foreach (var asteroid in _cache.Asteroids)
            {
                asteroid.ScreenObject.Move();
                _canvas.LoadPolygon(asteroid.PolygonPoints, ColorHexStrings.WhiteHex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Main game-play routine to determine object state and screen refresh.
        /// </summary>
        public void DrawScreen()
        {
            if (_paused)
            {
                // Pause flashes on and off
                if (_pauseTimer > PAUSE_INTERVAL / 2)
                {
                    _textDraw.DrawText(
                        "PAUSE"
                        , TextDraw.Justify.CENTER
                        , ScreenCanvas.CANVAS_HEIGHT / 3
                        , 200, 400
                        );
                }

                if (--_pauseTimer < 0)
                {
                    _pauseTimer = PAUSE_INTERVAL;
                }
            }
            else // Do all game processing if game is not paused
            {
                var origScore = _score.CurrentScore;

                // If no ship displaying, after explosions are done
                // get a new one - or end the game
                var noExplosions = _cache.ExplosionCount() == 0;

                if (!_cache.Ship.IsAlive && noExplosions)
                {
                    if (!_score.HasReserveShips())
                    {
                        // Game over
                        _inProcess = false;
                    }
                    else if (_collisionManager.IsCenterSafe())
                    {
                        _score.DecrementReserveShips();
                        _cache.UpdatedShip(new Ship());
                    }
                }

                // Create a new asteroid belt if no explosions and no asteroids
                if (noExplosions && _cache.Belt.Count() == 0)
                {
                    _cache.UpdateBelt(new AsteroidBelt(++_currentLevel));
                }

                // Move all objects starting with the ship
                _cache.Ship.Move();

                //Move the saucer and its missile
                if (_cache.Saucer != null)
                {
                    if (_cache.Saucer.Move())
                    {
                        //Aim for the ship oe nothing
                        var target = _cache.Ship.IsAlive
                            ? _cache.Ship.GetCurrLoc()
                            : default(Point?);

                        _cache.Saucer.Target(target);
                    }
                    else
                    {
                        //Saucer has completed its passes
                        _cache.UpdateSaucer(null);
                        _neededSaucerPoints = SAUCER_SCORE;
                    }
                }

                //Move the bullets, asteroids, and explosions
                _collisionManager.MoveBullets();
                _collisionManager.MoveAsteroids();
                _collisionManager.MoveExplosions();

                // Check bullets for collisions
                foreach (var bullet in _cache.GetBulletsInFlight())
                {
                    var points = new List <Point> {
                        bullet.Location
                    };

                    if (_collisionManager.AsteroidBeltCollision(points) ||
                        _collisionManager.SaucerCollision(points) ||
                        _collisionManager.MissileCollision(bullet.ScreenObject.GetPoints()))
                    {
                        _cache.AddExplosion(bullet.Location);
                        bullet.ScreenObject.Disable();
                    }
                }

                // Check ship for collisions
                if (_cache.Ship.IsAlive)
                {
                    var shipPoints = _cache.ShipPoints;

                    if (_collisionManager.SaucerCollision(shipPoints) ||
                        _collisionManager.MissileCollision(shipPoints) ||
                        _collisionManager.AsteroidBeltCollision(shipPoints)
                        )
                    {
                        foreach (var explosion in _cache.Ship.Explode())
                        {
                            _cache.AddExplosion(explosion);
                        }
                    }
                }

                //See if a bullet or the ship hit the saucer
                if (_cache.Saucer != null && !_cache.Saucer.IsAlive)
                {
                    _cache.UpdateSaucer(null);
                    _neededSaucerPoints = SAUCER_SCORE;
                }

                //See if the score is enough to show a suacer
                else if (_cache.Saucer == null)
                {
                    _neededSaucerPoints -= _score.CurrentScore - origScore;

                    if (_neededSaucerPoints <= 0)
                    {
                        var pt = new Point(
                            Random.Next(2) == 0 ? 0 : ScreenCanvas.CANVAS_WIDTH
                            , (Random.Next(10, 90) * ScreenCanvas.CANVAS_HEIGHT) / 100
                            );

                        _cache.UpdateSaucer(new Saucer(pt));
                    }
                }
            }

            //Commit the changes
            _cache.Repopulate();

            // Draw all objects
            _drawingManager.DrawObjects();
            _score.Draw();
        }