C# 从列表数组中清除pictureboxes

C# 从列表数组中清除pictureboxes,c#,visual-studio,winforms,C#,Visual Studio,Winforms,我正在尝试清除游戏结束后从列表中创建的picturebox,我已清除列表,但picturebox仍显示我已尝试在游戏结束时启用的计时器中使用以下代码 foreach (Invader ship in invaders) { ship.isDisposed = true; ship.ship.Dispose(); } 这没什么用。那么有人知道我怎么做吗 public partial c

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

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:表单
{
私有列表入侵者=新列表();
私有列表=新列表();
int-investerNumber=0;
智力得分=0;
公共表格1()
{
初始化组件();
}
私有void Form1\u KeyDown(对象发送方,KeyEventArgs e)
{
//单击时向上移动太空战斗机
如果(e.KeyCode.Equals(Keys.W))
{
如果(SpaceFighter.Top>0)
{
SpaceFighter.Top=SpaceFighter.Top-30;
}
}
//单击时向左移动太空战斗机
如果(例如,键代码等于(键A))
{
如果(SpaceFighter.Left>0)
{
SpaceFighter.Left=SpaceFighter.Left-10;
}
}
//单击时向右移动太空战斗机
如果(例如键代码等于(键D))
{
if(SpaceFighter.Right<此宽度)
{
太空战斗机。左=太空战斗机。左+10;
}
}
//单击时向下移动太空战斗机
如果(例如键代码等于(键S))
{
if(SpaceFighter.Bottom<此高度-10)
{
太空战斗机.Top=太空战斗机.Top+10;
}
}
//单击时发射激光
if(例如KeyCode.Equals(Keys.Space))
{
System.Media.SoundPlayer LaserSound=新的System.Media.SoundPlayer(Properties.Resources.LaserSound);
LaserSound.Play();
这个。激光。添加(新激光(这个,太空战斗机));
}
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
//游戏开始后,引入10个敌人
如果(入侵编号>9)
{
timer1.Enabled=false;
timer2.Enabled=true;
}
其他的
{
入侵者。添加(新入侵者(此));
InvesterNumber++;
}
}
私有无效计时器2_刻度(对象发送方,事件参数e)
{
//检测敌舰是否与太空战斗机互动,如果发生这种情况,则结束游戏
入侵者.RemoveAll(ship=>ship.isDisposed);
foreach(入侵者中的入侵者船)
{
船。移动入侵者(本);
if(太空战斗机.边界.与(飞船.飞船.边界)相交)
{
//为爆炸的船播放声音
System.Media.SoundPlayer SpaceshipSound=新的System.Media.SoundPlayer(Properties.Resources.SpaceshipSound);
太空船之声;
timer2.Enabled=false;
timer3.Enabled=false;
timer4.Enabled=true;
入侵者;
listBox1.Items.Add(lblScore.Text);//将分数添加到listbox
MessageBox.Show(“你输了!”);
返回;
}
}
//探测敌舰是否被激光击中
laser.RemoveAll(laser=>laser.isDisposed);
foreach(激光器中的激光器)
{
激光。移动激光(本);
foreach(入侵者中的入侵者船)
{
if(激光.激光.边界.与(船舶.船舶.边界)相交)
{
laser.isDisposed=真;
laser.laser.Dispose();
ship.isDisposed=true;
ship.ship.Dispose();//使船在射击后消失
System.Media.SoundPlayer ShipSound=新的System.Media.SoundPlayer(Properties.Resources.EnemySound);//正在摧毁的敌舰的声音
ShipSound.Play();
score=score+2;//如果敌人被击中,玩家的分数将增加2分
lblScore.Text=score.ToString();//更新分数标签
入侵者编号=入侵者编号-1;
}
}
}
foreach(入侵者中的入侵者船)
{
如果(ship.ship.Top>485)
{
ship.isDisposed=true;
ship.ship.Dispose();
入侵者编号=入侵者编号-1;
}
}
}
私有void btnStart_单击(对象发送方,事件参数e)
{
timer1.Enabled=true;//激活计时器1
timer3.Enabled=true;//激活计时器3
btnStart.Visible=false;//隐藏开始按钮
lblScore.Text=“0”//在游戏开始时将分数标签更新为0
lblName.Text=txtName.Text;//将名称标签更新为用户nput
txtName.Visible=false;//隐藏文本框
lblEnterName.Visible=false;//隐藏输入名称标签
SpaceFighter.Visible=true;//使SpaceFighter可见
}
//倒计时钟的代码
int m=2;
int s=60;
私有无效计时器3_刻度(对象发送方,事件参数e)
{
如果(s>0)
{
s=s-1;
lblTimer.Text=“0”+m.ToString()+”:“+s.ToString();
}
如果(s==0)
{
s=59;
        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);
        }