按下按钮时如何画球?

时间:2014-12-09 21:40:37

标签: c# winforms

在我的游戏中我有以下代码

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        paper = e.Graphics;
        paddle.drawPaddle(paper);
        ball.drawBall(paper);
        bricks.drawBricks(paper);
    }

我想要的是当我点击一个按钮时,球被绘制,但只有当我点击按钮时,所以我想将抽奖球放在代码的这一部分

private void button1_Click(object sender, EventArgs e)
    {

    }

我该怎么做才是因为我知道你只在“形式画”下画球

1 个答案:

答案 0 :(得分:2)

添加一个特殊标志,用于存储球的可见性:

private bool _isVisible = false;

private void Form1_Paint(object sender, PaintEventArgs e)
{
    paper = e.Graphics;
    paddle.drawPaddle(paper);
    if (_isVisible)
    {
         ball.drawBall(paper);
    }
    bricks.drawBricks(paper);
}

private void button1_Click(object sender, EventArgs e)
{
    _isVisible = true;
    // Now you need to invalidate the form to get it repainted
    Refresh();
}