我的子弹射击算法问题

时间:2013-08-03 08:21:29

标签: c#

我正在制作游戏,我有射击子弹的功能,玩家可以获得10发子弹。这就是现在的样子:

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        bullets.Add(new Bullet(robot.RobotRec));
        Bullet -= 1;// lose a life
        lblBullet.Text = Bullet.ToString();// display number of lives
        checkBullet();
    }
}
private void pnlGame_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        bullets.Add(new Bullet(robot.RobotRec));
        Bullet -= 1;// lose a life
        lblBullet.Text = Bullet.ToString();// display number of lives
        checkBullet();
    }
}
private void checkBullet()
{
    if (Bullet == 0)
    {
        tmrShoot.Enabled = false;

    }
}

但是在子弹数量达到零之后,我仍然可以拍摄它们,尽管它们不会影响任何东西。好吧他们刚刚经历的分数,但我想禁用子弹图像。所以我在想:你可以禁用mousedown吗?那要做的是什么代码?

1 个答案:

答案 0 :(得分:0)

你正在检查Bullet == 0是否错误,它应该是这样的:

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (Bullet == 0)
    {
        tmrShoot.Enabled = false;
        return; //leaves the method and followup code doesnt run
    }

    if (e.Button == MouseButtons.Left)
    {
        bullets.Add(new Bullet(robot.RobotRec));
        Bullet -= 1;// lose a life
        lblBullet.Text = Bullet.ToString();// display number of lives
    }
}
private void pnlGame_MouseDown(object sender, MouseEventArgs e)
{
    if (Bullet == 0)
    {
        tmrShoot.Enabled = false;
        return; //leaves the method and followup code doesnt run
    }

    if (e.Button == MouseButtons.Left)
    {
        bullets.Add(new Bullet(robot.RobotRec));
        Bullet -= 1;// lose a life
        lblBullet.Text = Bullet.ToString();// display number of lives
    }
}

这将阻止创建Bullet并计算addtional -1。

你也可以在一个方法中使用MouseDown Events的整个代码来获得更少的代码,如下所示:

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    FireBullet(e);
}
private void pnlGame_MouseDown(object sender, MouseEventArgs e)
{
    FireBullet(e);
}

private void FireBullet(MouseEventArgs e)
{
        if (Bullet == 0)
        {
            tmrShoot.Enabled = false;
            return; //leaves the method and followup code doesnt run
        }

        if (e.Button == MouseButtons.Left)
        {
            bullets.Add(new Bullet(robot.RobotRec));
            Bullet -= 1;// lose a life
            lblBullet.Text = Bullet.ToString();// display number of lives
        }
}

或者在Form1和你的pnlGame上注册相同的MouseDown事件,如下所示:

//In FormLoad
Form1.MouseDown += FireBullet_MouseDown;
pnlGame.MouseDown += FireBullet_MouseDown;

private void FireBullet_MouseDown(object sender, MouseEventArgs e)
{
        if (Bullet == 0)
        {
            tmrShoot.Enabled = false;
            return; //leaves the method and followup code doesnt run
        }

        if (e.Button == MouseButtons.Left)
        {
            bullets.Add(new Bullet(robot.RobotRec));
            Bullet -= 1;// lose a life
            lblBullet.Text = Bullet.ToString();// display number of lives
        }
}