更改图形的可见性

时间:2019-03-08 01:11:28

标签: c# visibility

我正在制作一个子手游戏,我需要使图形的每个身体部分都变得不可见,然后在玩家猜错字母时又回来。我尝试使用图片框,但它们会切掉框内其他图像的一部分,因此我改用图形。这是使角色取决于角色选择者的代码...

 Graphics test = e.Graphics;
        Graphics t2 = e.Graphics;
        //test.DrawImage(Cowboy.head, 180f, 180f, 250f, 250f);
        //test.DrawImage(Cowgirl.head, 35f, 90f, 650f, 550f);
        //test.DrawImage(Criminal.head, 35f, 80f, 650f, 550f);
        //test.DrawImage(Cyborg.head, 180f, 180f, 200f, 350f);
        //test.DrawImage(Princess.head, 180f, 180f, 200f, 350f);
        if (Variables.p1_type=="Cowboy")
        {

            test.DrawImage(Cowboy.head, 180f, 180f, 250f, 250f);
            test.DrawImage(Cowboy.body, 180f, 180f, 250f, 250f);
            test.DrawImage(Cowboy.leftArm, 180f, 180f, 250f, 250f);
            test.DrawImage(Cowboy.rightArm, 180f, 180f, 250f, 250f);
            test.DrawImage(Cowboy.leftLeg, 180f, 180f, 250f, 250f);
            test.DrawImage(Cowboy.rightLeg, 180f, 180f, 250f, 250f);


        }

        if (Variables.p1_type == "Cowgirl")
        {
            test.DrawImage(Cowgirl.head, 35f, 90f, 650f, 550f);
            test.DrawImage(Cowgirl.body, 35f, 90f, 650f, 550f);
            test.DrawImage(Cowgirl.leftArm, 35f, 90f, 650f, 550f);
            test.DrawImage(Cowgirl.rightArm, 35f, 90f, 650f, 550f);
            test.DrawImage(Cowgirl.leftLeg, 35f, 90f, 650f, 550f);
            test.DrawImage(Cowgirl.rightLeg, 35f, 90f, 650f, 550f);
        }

        if (Variables.p1_type == "Criminal")
        {
            test.DrawImage(Criminal.head, 35f, 80f, 650f, 550f);
            test.DrawImage(Criminal.body, 35f, 80f, 650f, 550f);
            test.DrawImage(Criminal.leftArm, 35f, 80f, 650f, 550f);
            test.DrawImage(Criminal.rightArm, 35f, 80f, 650f, 550f);
            test.DrawImage(Criminal.leftLeg, 35f, 80f, 650f, 550f);
            test.DrawImage(Criminal.rightLeg, 35f, 80f, 650f, 550f);
        }

        if (Variables.p1_type == "Cyborg")
        {
            test.DrawImage(Cyborg.head, 180f, 180f, 200f, 350f);
            test.DrawImage(Cyborg.body, 180f, 180f, 200f, 350f);
            test.DrawImage(Cyborg.leftArm, 180f, 180f, 200f, 350f);
            test.DrawImage(Cyborg.rightArm, 180f, 180f, 200f, 350f);
            test.DrawImage(Cyborg.leftLeg, 180f, 180f, 200f, 350f);
            test.DrawImage(Cyborg.rightLeg, 180f, 180f, 200f, 350f);
        }

        if (Variables.p1_type == "Princess")
        {
            test.DrawImage(Princess.head, 180f, 180f, 200f, 350f);
            test.DrawImage(Princess.body, 180f, 180f, 200f, 350f);
            test.DrawImage(Princess.leftArm, 180f, 180f, 200f, 350f);
            test.DrawImage(Princess.rightArm, 180f, 180f, 200f, 350f);
            test.DrawImage(Princess.leftLeg, 180f, 180f, 200f, 350f);
            test.DrawImage(Princess.rightLeg, 180f, 180f, 200f, 350f);
        }

我需要这些身体部位中的每一个都从不可见的开始,然后在选择错误的角色时变得可见。我该怎么办?如果不可能,还有其他方法可以做到吗?

如果玩家猜错了

   if (Variables.word4player1.ToUpper().Contains(e.KeyCode.ToString())!=true)
                {


                    p1Turn = false;
                    label2.Font = new Font(label1.Font, FontStyle.Underline | FontStyle.Bold);//change underlined player
                    label1.Font = new Font(label1.Font, FontStyle.Regular);
                    p1_wrong.Text = p1_wrong.Text + " " + e.KeyCode.ToString();

                }

设置变量p1_type

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Variables.p1_type= listBox_P1.SelectedItem.ToString();
        label_player1Type.Text = Variables.p1_type;
    }

1 个答案:

答案 0 :(得分:1)

您可以“欺骗”表单以仅绘制图像,而不是整个PictureBox

当然,还有更多“优雅”的方法可以做到这一点,例如覆盖PictureBox OnPaint事件或进行自己的控制。

但是我自己不是它的主人,所以我不会回答你。

首先要做的就是将所有PictureBoxs'可见性设置为false。

然后将“ false”添加到每个PictureBox的Tag。我们将使用它作为“虚拟可见性”来告诉表单是否绘制图像。

在表单的Paint事件中,添加:

(假设pbs是包含所有PictureBoxs的数组。)

Graphics g = e.Graphics;

for(int i=0;i<pbs.Count();i++)
{
    if(pbs[i].Tag.ToString()=="true") 
        g.DrawImage(pbs[i].Image, pbs[i].Location);
}

在检查玩家输入的单词是否错误的方法中,执行:

(假设cur是当前PictureBox的索引。)

pbs[cur].Tag = true;
this.Refresh();

还有VOILA!您有一个PictureBox不会挡住后面的图片!