コード例 #1
0
ファイル: Game.cs プロジェクト: mightyblackbeard/Asteroids
        public void Update()
        {
            if (blackHole != null)
            {
                blackHole.Move();
                if ((blackHole.Pos.X < 0) || (blackHole.Pos.X > Width) ||
                    (blackHole.Pos.Y < 0) || (blackHole.Pos.Y > Width))
                {
                    blackHole = null;
                }
            }

            if (ship != null)
            {
                ship.Move();
                // Only allow blackHole to affect ship if not Resetting...
                if (blackHole != null && state == GameState.Running)
                {
                    Vector pull = CalcGravityPull(ship.Pos, blackHole.Pos);
                    ship.Move(pull);
                }
                Wrap(ship.Pos);
            }

            foreach (Asteroid a in asteroids)
            {
                a.Move();
                if (blackHole != null)
                {       // NOTE: the -1 below is to create a "REPULSION"
                    Vector pull = -1 * CalcGravityPull(a.Pos, blackHole.Pos);
                    a.Move(pull);
                }
                Wrap(a.Pos);
            }

            foreach (Missile m in newMissles)
            {
                missiles.Add(m);
            }

            foreach (EShip e in enemiesToAdd)
            {
                enemies.Add(e);
            }

            enemiesToAdd.Clear();

            newMissles.Clear();

            foreach (Missile m in missiles)
            {
                if (m != null)
                {
                    m.Move();
                    if (blackHole != null && state == GameState.Running)
                    {
                        Vector pull = CalcGravityPull(m.Pos, blackHole.Pos);
                        m.Move(pull);
                    }
                }
            }

            foreach (EShip e in enemies)
            {
                e.Move();
                Wrap(e.Pos);
            }

            KillMissilesOutOfRange();
            KillExplosionsWhenTheyreDone();
            DetectCollisions();

            if (asteroids.Count == 0)
            {
                Wave++;

                Roids = 0;

                if (Wave % 5 == 0)
                {
                    for (int i = 0; i < Wave / 5; i++)
                    {
                        MotherShip();
                    }

                    AddAsteroid();
                }
                else
                {
                    while (Roids < 10 * (double)(Wave * .5))
                    {
                        AddAsteroid();
                    }
                }
            }

            //if((LivesLeft == 0) && (state == GameState.Resetting))
            //{
            //    string over = "Game Over\n\n" + "Waves Cleared: " + (Wave - 1).ToString() + "\nScore: " + Score.ToString();
            //    if (MessageBox.Show(over, "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK)
            //    {

            //    }
            //}
        }
コード例 #2
0
ファイル: Game.cs プロジェクト: mightyblackbeard/Asteroids
        protected void DetectCollisions()
        {
            List <int> asteroidsToDelete = new List <int>();
            List <int> missilesToDelete  = new List <int>();
            List <int> enemiesToDelete   = new List <int>();


            foreach (Asteroid a in asteroids)
            {
                foreach (EShip Enemy in enemies)
                {
                    int    sumOfWidths = Enemy.Image.Width / 2 + a.Image.Width / 2;
                    Vector pointing    = new Vector(Enemy.Pos - a.Pos);
                    float  dist        = pointing.Magnitude;

                    if (dist - 10 < sumOfWidths)
                    {
                        pointing  = new Vector(a.Pos - Enemy.Pos);
                        Enemy.Vel = new Vector(pointing.Unitized * -5f);
                    }

                    if (dist < sumOfWidths)
                    {
                        if (!enemiesToDelete.Contains(enemies.IndexOf(Enemy)))
                        {
                            enemiesToDelete.Insert(0, enemies.IndexOf(Enemy));
                        }
                    }
                }

                foreach (Missile m in missiles)
                {
                    float  sumOfWidths = m.Image.Width / 2 + a.Image.Width / 2;
                    Vector pointing    = m.Pos - a.Pos;
                    float  dist        = pointing.Magnitude;
                    if ((dist < sumOfWidths) && (m.friendlyMissle == true))
                    {
                        // Collision detected! Store index for later removal

                        int idxAsteroid = asteroids.IndexOf(a);
                        int idxMissile  = missiles.IndexOf(m);
                        if (!asteroidsToDelete.Contains(idxAsteroid))
                        {
                            asteroidsToDelete.Insert(0, idxAsteroid);
                        }
                        if (!missilesToDelete.Contains(idxMissile))
                        {
                            missilesToDelete.Insert(0, idxMissile);
                        }
                    }

                    if (ship != null)
                    {
                        sumOfWidths = m.Image.Width / 2 + ship.Image.Width / 2;
                        pointing    = m.Pos - ship.Pos;
                        dist        = pointing.Magnitude;

                        if (dist < sumOfWidths && !m.friendlyMissle)
                        {       // Ship was shot by enemy laser
                            ship.Crashed = true;
                            Explosion ex = new Explosion(new Vector(ship.Pos),
                                                         Properties.Resources.explosionShip, 0,
                                                         8, 4, 256, 256);
                            ex.Finished += ex_Finished;
                            explosions.Add(ex);
                            ship = null;
                            livesLeft--;
                            if (!missilesToDelete.Contains(missiles.IndexOf(m)))
                            {
                                missilesToDelete.Insert(0, missiles.IndexOf(m));
                            }
                            // Create a "white hole" for repulsion...
                            blackHole = new Asteroid(new Vector(width / 2, height / 2),
                                                     new Vector(0, 0), 0, 0, Properties.Resources.blackhole);

                            shipSpawnTmr.Elapsed += shipSpawnTmr_Elapsed;
                            state = GameState.Resetting;
                        }
                    }

                    if (m.friendlyMissle)
                    {//killing the alien ships
                        foreach (EShip enemy in enemies)
                        {
                            sumOfWidths = m.Image.Width / 2 + enemy.Image.Width / 2;
                            pointing    = m.Pos - enemy.Pos;
                            dist        = pointing.Magnitude;

                            if (dist < sumOfWidths)
                            {
                                int idxofenemy = enemies.IndexOf(enemy);
                                int idxMissile = missiles.IndexOf(m);
                                if (!enemiesToDelete.Contains(idxofenemy))
                                {
                                    enemiesToDelete.Insert(0, idxofenemy);
                                }
                                if (!missilesToDelete.Contains(idxMissile))
                                {
                                    missilesToDelete.Insert(0, idxMissile);
                                }
                            }
                        }
                    }
                }

                if (ship != null)
                {
                    float  sumOfWidths2 = ship.Image.Width / 2 + a.Image.Width / 2;
                    Vector pointing2    = ship.Pos - a.Pos;
                    float  dist2        = pointing2.Magnitude;
                    if (dist2 < sumOfWidths2)
                    {       // Ship has collided with an asteroid
                        ship.Crashed = true;
                        Explosion ex = new Explosion(new Vector(ship.Pos),
                                                     Properties.Resources.explosionShip, 0,
                                                     8, 4, 256, 256);
                        ex.Finished += ex_Finished;
                        explosions.Add(ex);
                        ship = null;
                        livesLeft--;
                        if (!asteroidsToDelete.Contains(asteroids.IndexOf(a)))
                        {
                            asteroidsToDelete.Insert(0, asteroids.IndexOf(a));
                        }
                        // Create a "white hole" for repulsion...
                        blackHole = new Asteroid(new Vector(width / 2, height / 2),
                                                 new Vector(0, 0), 0, 0, Properties.Resources.blackhole);

                        shipSpawnTmr.Elapsed += shipSpawnTmr_Elapsed;
                        state = GameState.Resetting;
                    }
                }

                foreach (int idx in enemiesToDelete)
                {
                    Image     img = Properties.Resources.explosion3;
                    Explosion ex  = new Explosion(new Vector(enemies[idx].Pos),
                                                  img, rand.Next(0, 360),
                                                  5, 5, 80, 80);
                    ex.Finished += ex_Finished;
                    explosions.Add(ex);

                    Score += 1000;
                    ak    += 5;
                    enemies[idx].shoot.Stop();
                    enemies.RemoveAt(idx);
                }
                foreach (int idx in missilesToDelete)
                {   // Remove missiles which had been detected to collide
                    if (missiles[idx].friendlyMissle)
                    {
                        shipMissles--;
                    }
                    missiles.RemoveAt(idx);
                }



                missilesToDelete.Clear();
                enemiesToDelete.Clear();
            }

            foreach (int idx in asteroidsToDelete)
            {   // Remove asteroids which had been detected to collide
                Image    img;
                int      numAcross, numDown, spriteWidth, spriteHeight;
                Asteroid a = asteroids[idx];
                if (a.Type == 0)
                {
                    img       = Properties.Resources.Explosion;
                    numAcross = 9; numDown = 9; spriteWidth = 100; spriteHeight = 100;
                }
                else
                {
                    img       = Properties.Resources.explosion2;
                    numAcross = 3; numDown = 4; spriteWidth = 256; spriteHeight = 128;
                }
                Explosion ex = new Explosion(new Vector(asteroids[idx].Pos),
                                             img, rand.Next(0, 360),
                                             numAcross, numDown, spriteWidth, spriteHeight);
                ex.Finished += ex_Finished;
                explosions.Add(ex);
                // Update the score...
                Score += 50 * (4 - asteroids[idx].Size);

                //Split the asteroids up

                if (asteroids[idx].Size == 3)
                {
                    if (asteroids[idx].Type == 0)
                    {
                        Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.AsteroidMed);
                        A1.Size = asteroids[idx].Size - 1;
                        Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.AsteroidMed);
                        A2.Size = asteroids[idx].Size - 1;

                        asteroids.Add(A1);
                        asteroids.Add(A2);
                    }
                    else
                    {
                        Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Med);
                        A1.Size = asteroids[idx].Size - 1;
                        Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Med);
                        A2.Size = asteroids[idx].Size - 1;

                        asteroids.Add(A1);
                        asteroids.Add(A2);
                    }
                }
                else if (asteroids[idx].Size == 2)
                {
                    if (asteroids[idx].Type == 0)
                    {
                        Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.AsteroidSmall);
                        A1.Size = asteroids[idx].Size - 1;
                        Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.AsteroidSmall);
                        A2.Size = asteroids[idx].Size - 1;

                        asteroids.Add(A1);
                        asteroids.Add(A2);
                    }
                    else
                    {
                        Asteroid A1 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle + 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Small);
                        A1.Size = asteroids[idx].Size - 1;
                        Asteroid A2 = new Asteroid(new Vector(asteroids[idx].Pos), new Vector(rand.Next(-10, 10), rand.Next(-10, 10)),
                                                   asteroids[idx].Angle - 45, rand.Next(-10, 10), Properties.Resources.Asteroid2Small);
                        A2.Size = asteroids[idx].Size - 1;

                        asteroids.Add(A1);
                        asteroids.Add(A2);
                    }
                }

                asteroids.RemoveAt(idx);
                ak++;
            }
            if (ak >= 20 && ak < 40)
            {
                numMissiles = 20;
            }
            else if (ak >= 40)
            {
                numMissiles = 30;
            }
        }
コード例 #3
0
ファイル: Game.cs プロジェクト: mightyblackbeard/Asteroids
        public void AddAsteroid()
        {
            float velX = 0, velY = 0;

            while (velX == 0 || velY == 0)
            {
                velX = rand.Next(-3, 4);
                velY = rand.Next(-3, 4);
            }
            int angV = 0;

            while (angV == 0)
            {
                angV = rand.Next(-8, 9);
            }

            int type = rand.Next(0, 2);
            // decide which asteroid size...
            int   size = rand.Next(1, 4);
            Image img;

            if (size == 1)
            {
                if (type == 0)
                {
                    img = Properties.Resources.AsteroidSmall;
                }
                else
                {
                    img = Properties.Resources.Asteroid2Small;
                }
            }
            else if (size == 2)
            {
                if (type == 0)
                {
                    img = Properties.Resources.AsteroidMed;
                }
                else
                {
                    img = Properties.Resources.Asteroid2Med;
                }
            }
            else
            {
                if (type == 0)
                {
                    img = Properties.Resources.AsteroidLg;
                }
                else
                {
                    img = Properties.Resources.Asteroid2Lg;
                }
            }

            Vector location = new Vector(rand.Next(0, width),
                                         rand.Next(0, height));



            Asteroid a = new Asteroid(location, new Vector(velX, velY),
                                      0, angV, img);

            a.Size = size;
            a.Type = type;

            Roids += size;

            if (ship != null)
            {
                float  sumOfWidths = ship.Image.Width / 2 + a.Image.Width / 2;
                Vector pointing    = ship.Pos - a.Pos;
                float  dist        = pointing.Magnitude;

                while (dist < sumOfWidths + 150)
                {
                    location.X = rand.Next(0, width);
                    location.Y = rand.Next(0, height);
                    a.Pos      = location;

                    pointing = ship.Pos - a.Pos;
                    dist     = pointing.Magnitude;
                }
            }

            asteroids.Add(a);
        }