示例#1
0
        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.Clear(Color.Black);

            if (!bStarted)
            {
                String drawString = "Asteroids";
                // Create font and brush.
                Font       drawFont  = new Font("Verdana", 48);
                SolidBrush drawBrush = new SolidBrush(Color.Red);

                PointF drawPoint = new PointF(this.ClientRectangle.Width / 4, this.ClientRectangle.Height - iTextHolder);
                // Draw string to screen.
                e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);

                drawString = "AURORA";
                // Create font and brush.
                drawFont  = new Font("TIMES NEW ROMAN", 14);
                drawBrush = new SolidBrush(Color.White);

                drawPoint = new PointF(this.ClientRectangle.Width / 4, this.ClientRectangle.Height - (iTextHolder - 90));
                // Draw string to screen.
                e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);

                if (iTextHolder > this.ClientRectangle.Height + 25)
                {
                    iTextHolder = 25;
                }
                else
                {
                    iTextHolder = iTextHolder + 5;
                }

                return;
            }

            Bullet[] BulletArray = TheShip.BulletArray;
            // Move Ship
            if (TheShip.Active)
            {
                TheShip.Move();
                // check ship is not out of bounds
                TheShip.InRectangle(this.ClientRectangle);
            }
            // Move Bullets
            for (int i = 0; i < BulletArray.Length; i++)
            {
                // check if bullet is still in scope
                if (BulletArray[i] != null)
                {
                    if (BulletArray[i].Active)
                    {
                        BulletArray[i].Move();
                        // trim or reposition bullets
                        if (BulletArray[i].InRectangle(this.ClientRectangle))
                        {
                            BulletArray[i].Active = false;
                        }

                        // test ship & bullet collision
                        if (TheShip.Active)
                        {
                            if (BulletArray[i].IntersectsWith(TheShip.GetBounds(), TheShip.ShipImage))
                            {
                                BulletArray[i].Active = false;
                                TheShip.Active        = false;
                            }
                        }
                    }
                }
            }
            // Move Asteroids
            foreach (Asteroid A in AsteroidList)
            {
                A.Move();
                A.InRectangle(this.ClientRectangle);
            }

            // check for collisions
            for (int j = 0; j < AsteroidList.Count; j++)
            {
                Asteroid A = (Asteroid)AsteroidList[j];
                // if exploding nothing to do
                if (A.Exploding == false)
                {
                    // with bullets
                    for (int i = 0; i < BulletArray.Length; i++)
                    {
                        if (BulletArray[i] != null)
                        {
                            if (BulletArray[i].Active)
                            {
                                if (BulletArray[i].IntersectsWith(A.GetBounds(), A.AsteroidImage))
                                {
                                    A.Exploding = true;
                                    spawnMoreAsteroids(A);
                                    BulletArray[i].Active = false;
                                }
                            }
                        }
                    }

                    // with ship
                    if (TheShip.Active)
                    {
                        if (TheShip.IntersectsWith(A.GetBounds(), A.AsteroidImage))
                        {
                            A.Exploding = true;
                            spawnMoreAsteroids(A);
                            TheShip.Active = false;
                        }
                    }
                }
            }

            // trim any inactive asteroids
            for (int i = 0; i < AsteroidList.Count; i++)
            {
                Asteroid A = (Asteroid)AsteroidList[i];
                if (A.Active)
                {
                    A.Draw(g);
                }
                else
                {
                    AsteroidList.RemoveAt(i);
                }
            }

            for (int i = 0; i < BulletArray.Length; i++)
            {
                if (BulletArray[i] != null)
                {
                    if (BulletArray[i].Active)
                    {
                        BulletArray[i].Draw(g);
                    }
                }
            }

            if (TheShip.Active)
            {
                TheShip.DrawShip(g);
            }
        }