1

我觉得我已经在互联网上搜寻了答案,但我仍然没有解决这个问题。我正在用 C# 构建一个刽子手游戏(我还是新手),我的表单上有一个组框,我在其中绘制不同的“身体部位”。问题是每次调用 draw 事件时都会重新绘制它。所以我最终会得到一个头,然后是一条手臂,一条腿,等等。

我试图做一些事情,比如覆盖 OnPaintBackground,虽然这解决了重绘的问题,但它搞砸了整个表单,当我这样做时它看起来很糟糕。我也尝试在面板上而不是组框上绘制,但由于某种原因,我什至无法绘制。

有人可以提供任何建议吗?顺便说一句,这不是家庭作业,只是个人项目和学习经历。

一些代码:这是我用来触发绘图功能的(它在我拥有的其他条件下触发)

this.groupBoxHang.Paint += new System.Windows.Forms.PaintEventHandler(this.groupBoxHangMan_Paint);

然后它来到这里并调用实际的函数,该函数在一个单独的类中

    private void groupBoxHangMan_Paint(object sender, PaintEventArgs e)
    {          
       draw.DrawHangMan(e.Graphics, GetRemainingGuess());      
    }

这是另一个类的一部分:

    public void DrawHangMan(Graphics dM, int guessesRemaining)
    {        
        Pen blackPen = new Pen(Brushes.Black);

        Pen deathPen = new Pen(Brushes.Red);
        deathPen.Width = 2.0F;

        if (guessesRemaining == 7)
        {
            dM.DrawEllipse(blackPen, 94, 75, 60, 60);//head
        }
        else if (guessesRemaining == 6)
        {
            dM.DrawLine(blackPen, 124, 210, 124, 135);//body
        }
        else if (guessesRemaining == 5)
        {
            dM.DrawLine(blackPen, 123, 170, 65, 145);//left arm
        }
        else if (guessesRemaining == 4)
        {
            dM.DrawLine(blackPen, 123, 170, 181, 145); //right arm
        }
        else if (guessesRemaining == 3)
        {
            dM.DrawLine(blackPen, 123, 210, 65, 255);//left leg
        }
        else if (guessesRemaining == 2)
        {
            dM.DrawLine(blackPen, 123, 210, 181, 255);//right leg
        }
        else if (guessesRemaining == 1)
        {
            dM.DrawEllipse(blackPen, 105, 93, 10, 10); //left open eye
            dM.DrawEllipse(blackPen, 133, 93, 10, 10); //right open eye
        }
        else if (guessesRemaining == 0)
        {
            dM.DrawLine(deathPen, 102, 104, 118, 92);//left eye dead
            dM.DrawLine(deathPen, 118, 104, 102, 92);

            dM.DrawLine(deathPen, 130, 104, 146, 92);//right dead eye
            dM.DrawLine(deathPen, 146, 104, 130, 92);
        }         

    }
4

1 回答 1

0

您的 If 语句有两件事。它们被设置为仅在计数等于计数时才激活,因此当计数小于阈值时您不会重绘,第二件事是使用 else 语句您只运行一个级别。在您的活动中这样做Paint是正确的。尝试这样的事情:

public void DrawHangMan(Graphics dM, int guessesRemaining)
{
    Pen blackPen = new Pen(Brushes.Black);
    Pen deathPen = new Pen(Brushes.Red);
    deathPen.Width = 2.0F;

    if (guessesRemaining < 8)
    {
        dM.DrawEllipse(blackPen, 94, 75, 60, 60);//head
    }
    if (guessesRemaining < 7)
    {
        dM.DrawLine(blackPen, 124, 210, 124, 135);//body
    }
    if (guessesRemaining < 6)
    {
        dM.DrawLine(blackPen, 123, 170, 65, 145);//left arm
    }
    if (guessesRemaining < 5)
    {
        dM.DrawLine(blackPen, 123, 170, 181, 145); //right arm
    }
    if (guessesRemaining < 4)
    {
        dM.DrawLine(blackPen, 123, 210, 65, 255);//left leg
    }
    if (guessesRemaining < 3)
    {
        dM.DrawLine(blackPen, 123, 210, 181, 255);//right leg
    }
    if (guessesRemaining < 2)
    {
        dM.DrawEllipse(blackPen, 105, 93, 10, 10); //left open eye
        dM.DrawEllipse(blackPen, 133, 93, 10, 10); //right open eye
    }
    if (guessesRemaining < 1)
    {
        dM.DrawLine(deathPen, 102, 104, 118, 92);//left eye dead
        dM.DrawLine(deathPen, 118, 104, 102, 92);
        dM.DrawLine(deathPen, 130, 104, 146, 92);//right dead eye
        dM.DrawLine(deathPen, 146, 104, 130, 92);
    }

}
于 2012-11-16T02:17:28.597 回答