コード例 #1
0
        public new void Start()
        {
            _ship = new Ship(GameRef.Game.Content.Load <Texture2D>("ship"), GameRef.Game.Content.Load <Texture2D>("bullet"), this);
            AddEntity(_ship);

            _enemyship = new EnemyShip(GameRef.Game.Content.Load <Texture2D>("enemyship"), GameRef.Game.Content.Load <Texture2D>("bullet"), this);
            AddEntity(_enemyship);

            _ship.Collision.AddPartner(_enemyship);
            _enemyship.Collision.AddPartner(_ship);

            for (var i = 0; i < 10; i++)
            {
                var a = new Asteroid(GameRef.Game.Content.Load <Texture2D>("asteroid"), this);

                while (a.Collision.TestCollision(_ship))
                {
                    a = new Asteroid(GameRef.Game.Content.Load <Texture2D>("asteroid"), this);
                }
                _ship.Collision.AddPartner(a);
                a.Collision.AddPartner(_ship);

                AddEntity(a);
            }

            _font = GameRef.Game.Content.Load <SpriteFont>("font");
        }
コード例 #2
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            background      = this.Content.Load <Texture2D>("Backgrounds/space2");
            shipSpriteSheet = this.Content.Load <Texture2D>("Sprites/ship");
            shotSprite      = this.Content.Load <Texture2D>("Sprites/lasers");

            FontManager.LoadFonts(this.Content);
            SoundEffectManager.LoadSounds(this.Content);
            MusicManager.LoadMusic(this.Content);

            Player.Initialize(
                shipSpriteSheet,
                shotSprite,
                new Vector2(
                    (this.Window.ClientBounds.Width / 2) - 22.5f,
                    (this.Window.ClientBounds.Height / 2) - 20f),
                new Rectangle(0, 0, 45, 40),
                Vector2.Zero);

            enemy = new EnemyShip(shipSpriteSheet, Vector2.Zero, new Rectangle(0, 0, 45, 40));
        }
コード例 #3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            background = this.Content.Load<Texture2D>("Backgrounds/space2");
            shipSpriteSheet = this.Content.Load<Texture2D>("Sprites/ship");
            shotSprite = this.Content.Load<Texture2D>("Sprites/lasers");

            FontManager.LoadFonts(this.Content);
            SoundEffectManager.LoadSounds(this.Content);
            MusicManager.LoadMusic(this.Content);

            Player.Initialize(
                shipSpriteSheet,
                shotSprite,
                new Vector2(
                    (this.Window.ClientBounds.Width / 2) - 22.5f,
                    (this.Window.ClientBounds.Height / 2) - 20f),
                new Rectangle(0, 0, 45, 40),
                Vector2.Zero);

            enemy = new EnemyShip(shipSpriteSheet, Vector2.Zero, new Rectangle(0, 0, 45, 40));
        }
コード例 #4
0
        public void Iteration()
        {
            iteration++;

            /// Fired by the form timer.  This will Move and process all of the space objects.
            ship.Move();

            // Move along the shots.
            // Delete any old ones.

            // Bullet Loop
            for (int i = bullets.Count - 1; i >= 0; i--)
            {
                Bullets bullet = bullets[i];
                bullet.Move();

                if (bullet.Iterations > bullet.life)
                {
                    bullets.Remove(bullet);
                    BullettsFired--;
                }

                // Check if we have hit an asteroid.
                for (int a = asteroids.Count - 1; a >= 0; a--)
                {
                    Asteroid asteroid = asteroids[a];

                    if (asteroid.Area.Contains(bullet.point))
                    {
                        // Add to score.
                        PlayerScore           += asteroid.Score;
                        form2.playerScore.Text = PlayerScore.ToString();

                        // We have a hit.  Remove this asteroid and create more smaller ones if appropriate.
                        int createQty = 1;

                        if (asteroid.Size == Asteroid.Sizes.Large)
                        {
                            createQty = random.Next(1, Level);
                            for (int x = 0; x < createQty; x++)
                            {
                                Asteroid newAsteroid = new Asteroid(asteroid.point.X + 2, asteroid.point.Y, this, Asteroid.Sizes.Medium, asteroid.Colour);
                                asteroids.Add(newAsteroid);
                            }
                        }

                        if (asteroid.Size == Asteroid.Sizes.Medium)
                        {
                            createQty = random.Next(1, Level * 2);
                            for (int x = 0; x < createQty; x++)
                            {
                                Asteroid newAsteroid = new Asteroid(asteroid.point.X + 2, asteroid.point.Y, this, Asteroid.Sizes.Small, asteroid.Colour);
                                asteroids.Add(newAsteroid);
                            }
                        }

                        if (asteroid.Size == Asteroid.Sizes.Small)
                        {
                            createQty = random.Next(1, Level * 3);
                            for (int x = 0; x < createQty; x++)
                            {
                                Asteroid newAsteroid = new Asteroid(asteroid.point.X + 2, asteroid.point.Y, this, Asteroid.Sizes.Tiny, asteroid.Colour);
                                asteroids.Add(newAsteroid);
                            }
                        }

                        asteroids.Remove(asteroid);

                        bullets.Remove(bullet);
                        BullettsFired--;
                    }
                }

                // Phase 2 Check if we have hit the enemy ship
                if (enemyShip1 != null && enemyShip1.Area.Contains(bullet.point) && bullet.Enemy != true)
                {
                    // Add to score.
                    PlayerScore           += 50;
                    form2.playerScore.Text = PlayerScore.ToString();

                    // Remove the object.
                    enemyShip1 = null;

                    soundPlayerGlass.Play();

                    EnemyShipActive = false;
                }

                // Phase 2 - See if the enemy ship has hit us.
                if (bullet.Enemy == true && ship.Area.Contains(bullet.point))
                {
                    // Oh dear we have been hit.  Call the NewShip routine.
                    NewShip();
                    ShowExplosion  = 20;   // Number of times to show explosion
                    ExplosionPoint = bullet.point;

                    break;
                }


                // See is all asteroids have gone.  Need to start the next level.
                if (asteroids.Count <= 0)
                {
                    Level++;
                    NewLevel(Level);
                    return;
                }
            }   // End Bullet list loop.


            // Move the Asteroids.
            foreach (Asteroid asteroid in asteroids)
            {
                asteroid.Move();

                // See if this has hit our ship.
                if (asteroid.Area.IntersectsWith(ship.Area))
                {
                    // Oh dear we have been hit.  Call the NewShip routine.
                    NewShip();
                    ShowExplosion  = 20;   // Number of times to show explosion
                    ExplosionPoint = asteroid.point;

                    break;
                }
            }


            // Phase 2 - Randomly start the enemy ship at the right hand side.
            if (EnemyShipActive == false)
            {
                int randy = random.Next(0, 500);
                if (randy == 250)
                {
                    EnemyShipActive = true;
                    int startPosX = FormSizeX - 2;
                    int startPosY = random.Next(1, FormSizeY);
                    enemyShip1       = new EnemyShip(startPosX, startPosY, this);
                    enemyShip1.Angle = 270.0F;
                }
            }

            if (EnemyShipActive == true)
            {
                enemyShip1.Move();

                if (iteration % 10 == 0)  // Fire every ten iterations.
                {
                    enemyShip1.Shoot();
                }
            }



            // Fire the form paint event to paint the graphics.
            form1.Invalidate();
        }