游戏结束时单击按钮以重新启动游戏

时间:2019-05-17 09:24:28

标签: c# visual-studio winforms

我已经创建了一款类似太空侵略者的游戏,一旦游戏结束,它就会在最后显示得分。

我想要做的是添加一个重新启动游戏的按钮。我希望它返回到初始开始屏幕,以便下一个玩家可以输入他们的名字并开始游戏。

我曾尝试设置一个按钮,以便它重置所有计时器,然后将屏幕设置回可以正常工作的初始设置,但是在开始新游戏时,它只是立即结束,然后显示来自第一局。

{
    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))
        {
            this.lasers.Add(new Laser(this, SpaceFighter));
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // introduces 5 enemies once the game starts
        if (invaderNumber > 4 )
        {
            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))
            {
                timer2.Enabled = false;
                timer3.Enabled = false;
                timer4.Enabled = true;
                listBox1.Items.Add(lblScore.Text + "          " + lblName.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.ship.Image = SpaceInvaders.Properties.Resources.explosionGIF1;
                    ship.isDisposed = true;
                    ship.ship.Dispose();
                    score = score + 2; //adds 2 points to players score if enemy is hit
                    lblScore.Text = score.ToString(); //updates the score label
                }
            }
        }
    }

    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
        lblName.Text = txtName.Text;  
        txtName.Visible = false; // hides the textbox
        lblEnterName.Visible = false; // hides the enter name label
        SpaceFighter.Visible = true; // makes the spacefighter visible

        {
            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)
            {
                m = 00;
                s = 00;
                timer3.Enabled = false;
                timer4.Enabled = true;
                listBox1.Items.Add(lblScore.Text + "          " + lblName.Text); // adds scores to lisbox
            }
        }
    }

    // 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;
        }
        if (m >= 0)
        {
            invaderNumber--;
            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;
        lblScoreTable.Visible = false;
        lblNameTable.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
        lblNameTable.Visible = true;
        lblScoreTable.Visible = true;
    }
}
   private void BtnMenu_Click(object sender, EventArgs e)
        {
            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;
            lblName.Text = "Name";
            lblScore.Text = "Score";
            txtName.Clear();
        }

0 个答案:

没有答案
相关问题