从列表数组中清除图片框

时间:2019-05-17 20:16:02

标签: c# visual-studio winforms

我试图清除游戏结束后从列表中创建的图片框,我清除了列表,但图片框仍显示,我尝试使用以下代码在游戏结束时启用的计时器中

foreach (Invader ship in invaders)
            {
                ship.isDisposed = true;
                ship.ship.Dispose();
            }

这没什么用,所以有人对我该怎么做有任何想法吗?

public partial class Form1 : Form
    {


        private List<Invader> invaders = new List<Invader>(); 
        private List<Laser> lasers = new List<Laser>();

        int invaderNumber = 0;
        int score = 0;




    public Form1()
        {
            InitializeComponent();
        }



    private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // moves the spacefighter up when clicked
            if (e.KeyCode.Equals(Keys.W)) 
            {
                if (SpaceFighter.Top > 0)
                {
                    SpaceFighter.Top = SpaceFighter.Top - 30;

                }
            }
            // moves the spacefighter left when clicked
            if (e.KeyCode.Equals(Keys.A))
            {
                if (SpaceFighter.Left > 0)
                {
                    SpaceFighter.Left = SpaceFighter.Left - 10;

                }
            }
            // moves the spacefighter right when clicked
            if (e.KeyCode.Equals(Keys.D))
            {
                if (SpaceFighter.Right < this.Width)
                {
                    SpaceFighter.Left = SpaceFighter.Left + 10;

                }
            }
            // moves the spacefighter down when clicked
            if (e.KeyCode.Equals(Keys.S))
            {
                if (SpaceFighter.Bottom < this.Height - 10)
                {
                    SpaceFighter.Top = SpaceFighter.Top + 10;

                }
            }
            // fires lasers when clicked
            if (e.KeyCode.Equals(Keys.Space))
            {
                System.Media.SoundPlayer LaserSound = new System.Media.SoundPlayer(Properties.Resources.LaserSound);
                LaserSound.Play();
                this.lasers.Add(new Laser(this, SpaceFighter));
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // introduces 10 enemies once the game starts
            if (invaderNumber > 9 )
                {
                    timer1.Enabled = false;
                    timer2.Enabled = true;
                }
                else
                {
                    invaders.Add(new Invader(this));
                    invaderNumber++;
                }






        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            // detects if the enemy ship interacts with the spacefighter and ends the game if this happens
            invaders.RemoveAll(ship => ship.isDisposed);
            foreach(Invader ship in invaders)
            {
                ship.MoveInvader(this);
                if (SpaceFighter.Bounds.IntersectsWith(ship.ship.Bounds))
                {
                    // plays sound for exploding ship
                    System.Media.SoundPlayer SpaceshipSound = new System.Media.SoundPlayer(Properties.Resources.SpaceshipSound);
                    SpaceshipSound.Play();
                    timer2.Enabled = false;
                    timer3.Enabled = false;
                    timer4.Enabled = true;
                    invaders.Clear();
                    listBox1.Items.Add(lblScore.Text); // adds score to listbox
                    MessageBox.Show("You Lose!");
                    return;

                }
            }
            // detects if an enemy ship his hit by a laser
            lasers.RemoveAll(laser => laser.isDisposed);
            foreach (Laser laser in lasers)
            {
                laser.MoveLaser(this);
                foreach (Invader ship in invaders)
                {
                    if (laser.laser.Bounds.IntersectsWith(ship.ship.Bounds))
                    {
                        laser.isDisposed = true;
                        laser.laser.Dispose();
                        ship.isDisposed = true;
                        ship.ship.Dispose(); // makes the ship dissappear once its shot
                        System.Media.SoundPlayer ShipSound = new System.Media.SoundPlayer(Properties.Resources.EnemySound); // sound for the enemy ship being destroyed
                        ShipSound.Play();
                        score = score + 2; //adds 2 points to players score if enemy is hit
                        lblScore.Text = score.ToString(); //updates the score label
                        invaderNumber = invaderNumber - 1;

                    }


                }


            }
            foreach (Invader ship in invaders)
            {
                if (ship.ship.Top > 485)
                {
                    ship.isDisposed = true;
                    ship.ship.Dispose();
                    invaderNumber = invaderNumber - 1;


                }
            }



        }




        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true; // activates timer 1
            timer3.Enabled = true; // activates timer 3
            btnStart.Visible = false; // hidesthe start button
            lblScore.Text = "0"; // updates score label to 0 for start of game
            lblName.Text = txtName.Text; // updates the name label to user nput
            txtName.Visible = false; // hides the textbox
            lblEnterName.Visible = false; // hides the enter name label
            SpaceFighter.Visible = true; // makes the spacefighter visible



        }


        // code for the countdown clock
        int m = 2;
        int s = 60;
        private void timer3_Tick(object sender, EventArgs e)
        {
            if(s > 0)
            {

                s = s - 1;
                lblTimer.Text = "0" + m.ToString() + ":" + s.ToString();
            }
            if(s == 0)
            {
                s = 59;
                m = m - 1;
                lblTimer.Text = "0" + m.ToString() + ":" + s.ToString();
            }
            if(s < 10)
            {
                s = s - 1;
                lblTimer.Text = "0" + m.ToString() + ":" + "0" + s.ToString();
            }
            if (m < 0)
            {
                listBox1.Items.Add(lblScore.Text + "    " + lblName.Text); // adds score to list box
                timer4.Enabled = true;
                invaders.Clear();
            }
            if (m >= 0)
            {

                timer1.Enabled = true;


            }




        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SpaceFighter.Visible = false; // hides the space fighter until the player starts the game
            listBox1.Visible = false; // keepsscore table hidden
            lblScoreTable.Visible = false; // score table lables kept hidden
            lblNameTable.Visible = false;
            btnMenu.Visible = false;
        }

        private void Timer4_Tick(object sender, EventArgs e)
        {
            lblTimer.Text = "00:00"; // sets game timer to 00:00
            timer3.Enabled = false; // disbales timer 3
            listBox1.Visible = true; // makes score card visible
            listBox1.Sorted = true;
            lblNameTable.Visible = true; // displays score table labels
            lblScoreTable.Visible = true;
            btnMenu.Visible = true;
            foreach (Invader ship in invaders)
            {
                ship.isDisposed = true;
                ship.ship.Dispose();
            }







        }

        private void BtnMenu_Click(object sender, EventArgs e)
        {
            // resets game to its original state in order to play another game
            m = 2;
            s = 60;
            lblTimer.Text = "03:00";
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
            timer4.Enabled = false;
            listBox1.Visible = false;
            lblNameTable.Visible = false;
            lblScoreTable.Visible = false;
            lblEnterName.Visible = true;
            txtName.Visible = true;
            SpaceFighter.Visible = false;
            btnMenu.Visible = false;
            btnStart.Visible = true;
            score = 0;
            lblScore.Text = "Score";
            lblName.Text = "Name";
            txtName.Clear();
            invaderNumber = 0;
            SpaceFighter.Top = 380;
            SpaceFighter.Left = 400;

        }


    } 

1 个答案:

答案 0 :(得分:1)

这可以做到:

        foreach (var control in this.Controls)
        {
            var pb = control as PictureBox;
            if (pb != null)
            {
                if (pb.Name != "SpaceFighter")
                {
                    pb.Dispose();
                    this.Controls.Remove(pb);
                }
            }
        }

用于列表框排序:

using System.Collections;

ArrayList Sorting = new ArrayList();
        foreach (var o in listBox1.Items)
        {
            Sorting.Add(o);
        }

        Sorting.Sort();
        Sorting.Reverse();
        listBox1.Items.Clear();

        foreach (var o in Sorting)
        {
            listBox1.Items.Add(o);
        }