簡體   English   中英

從列表數組中清除圖片框

[英]clearing pictureboxes from a list array

我試圖清除游戲結束后從列表中創建的圖片框,我清除了列表,但圖片框仍顯示,我嘗試在游戲結束時啟用的計時器中使用以下代碼

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;

        }


    } 

這樣做:

        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);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM