コード例 #1
0
        public override void Draw()
        {
            Buffer.Graphics.Clear(Color.Black);

            var r            = new Random();
            var corePosition = r.Next(100, 480);

            Buffer.Graphics.DrawImage(Resources.background, new Rectangle(0, 0, 800, 500));
            Buffer.Graphics.DrawImage(new Bitmap(Resources.planet, new Size(200, 200)), 100, 100);

            foreach (var obj in _stars)
            {
                obj.Draw();
            }


            foreach (var asteroid in _asteroids)
            {
                asteroid.Draw();
            }

            foreach (var boolet in _bullets)
            {
                boolet.Draw();
            }

            _comet.Draw();
            journal = Journal.ObjectCreation;
            journal();


            if (_energyCore == null && _ship.Energy <= 50)
            {
                _energyCore = new EnergyCore(new Point(22, corePosition), new Point(0, 4), new Size(44, 44));
                journal     = Journal.ObjectCreation;
                journal();
            }
            if (_energyCore != null)
            {
                _energyCore.Draw();
            }



            if (_ship != null)
            {
                _ship.Draw();
                Buffer.Graphics.DrawString($"Energy: {_ship.Energy}", SystemFonts.DefaultFont, Brushes.White, 0, 0);
                Buffer.Graphics.DrawString($"Score {Score}", SystemFonts.DefaultFont, Brushes.White, 700, 0);
            }

            Buffer.Render();
        }
コード例 #2
0
        public void Update()
        {
            // Пройдемся по всем астероидам (с конца в начало)
            for (int i = _asteroids.Count - 1; i >= 0; i--)
            {
                // Проверим столкновение с кораблем
                if (_asteroids[i].Collision(_ship))
                {
                    System.Media.SystemSounds.Asterisk.Play();
                    _ship.EnergyLow(random.Next(1, 10));
                    Score += random.Next(_asteroids[i].Rect.Width, _asteroids[i].Rect.Height);
                    _asteroids.RemoveAt(i);
                    if (_ship.Energy <= 0)
                    {
                        _ship.Die();
                    }
                }
                else // Проверим столкновение с пулей
                {
                    // Пройдемся по всем снарядам (с конца в начало)
                    for (int j = _bullets.Count - 1; j >= 0; j--)
                    {
                        if (_asteroids[i].Collision(_bullets[j]))
                        {
                            System.Media.SystemSounds.Hand.Play();
                            Score += random.Next(_asteroids[i].Rect.Width, _asteroids[i].Rect.Height);
                            _asteroids.RemoveAt(i);
                            _bullets.RemoveAt(j);
                            break;
                        }
                    }
                }
            }

            if (_asteroids.Count == 0)
            {
                AsteroidsCount++;
                for (int i = 0; i < AsteroidsCount; i++)
                {
                    var size  = random.Next(10, 40);
                    var pos   = random.Next(50, 450);
                    var speed = random.Next(3, 10);
                    _asteroids.Add(new Asteroid(new Point(600, pos), new Point(-speed, -speed), new Size(size, size)));
                }
            }

            if (_energyCore != null && _ship != null && _ship.Collision(_energyCore))
            {
                _energyCore = null;
                journal     = Journal.CoreTaken;
                journal();
                _ship.EnergyLow(-(random.Next(20, 31)));
            }

            _comet.Update();

            for (int k = _bullets.Count - 1; k >= 0; k--)
            {
                if (_comet.Collision(_bullets[k]))
                {
                    System.Media.SystemSounds.Hand.Play();
                    _bullets.RemoveAt(k);
                    break;
                }
            }

            if (_ship != null && _ship.Collision(_comet))
            {
                _ship.Die();
            }



            if (_energyCore != null)
            {
                _energyCore.Update();
            }

            foreach (var obj in _stars)
            {
                obj.Update();
            }

            foreach (var asteroid in _asteroids)
            {
                asteroid.Update();
            }

            foreach (var boolet in _bullets)
            {
                boolet.Update();
            }
        }