【问题标题】:How to go about making a variable in a class output to a label after clicking on a picturebox?单击图片框后,如何将类中的变量输出到标签?
【发布时间】:2025-12-22 10:05:07
【问题描述】:

我对 OOP 还很陌生,我不确定如何在我的程序中实现一些东西。我的程序与打鼹鼠非常相似,并且有一组带有图像的图片框,并且怪物的图像在应用了时间间隔的图片框之间随机移动,或者每当用户移动到新的随机图片框及时点击怪物。我创建了一个怪物和一个玩家子类来尝试向程序添加一些 OOP 概念,但不确定如何实现我想要的。基本上,我的主表单上有一个分数标签,而我的动物类中有一个分数变量,其中包含一个值。当用户单击带有痣的图片框时,我希望能够从我的表单上的标签中添加分数值,并在他们没有及时单击标签时从标签中删除分数值。

这是我的代码:

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        PictureBox[] boxes;
        int initialscore = 0;
        int time = 0;
        int randPos;
        public Form1()
        {
            InitializeComponent();
        }
        private void boxes_MouseClick(object sender, MouseEventArgs e)
        {
            PictureBox pb2 = new PictureBox() { Image = Image.FromFile("sword2.png") };
            this.Cursor = new Cursor(((Bitmap)pb2.Image).GetHicon());
            for (int x = 0; x < 27; x++)
            {

                if (sender.Equals(boxes[x]))
                {
                    Image grass = Image.FromFile("swamp.png");
                    PictureBox temp = (PictureBox)sender;
                    temp.Image = grass;
                }

                if (sender.Equals(boxes[x]))
                {
                PictureBox pb = (PictureBox)sender;
                if (pb.Tag == "skeleton.png")
                initialscore++;
                }


            }
            label1.Text = " Score: " +initialscore.ToString();  
        }
        public void timer1_Tick(object sender, EventArgs e)
        {
            boxes[randPos].Image = Image.FromFile("swamp.png"); 
            boxes[randPos].Tag = "swamp.png"; 
            Random r = new Random();
            randPos=r.Next(0, 27);
            boxes[randPos].Image = Image.FromFile("skeleton.png"); 
            boxes[randPos].Tag = "skeleton.png";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            boxes = new PictureBox[27];
            int top = 100; 
            int left = 100; 
            for (int x = 0; x < 27; x++)
            {

                boxes[x] = new PictureBox();
                boxes[x].Image = Image.FromFile("swamp.png");
                boxes[x].Height = 100;
                boxes[x].Width = 100;

                if (x % 9 == 0)
                {
                    top += 120;
                    left = 120;
                }

                else
                    left += 120;
                boxes[x].Top = top;
                boxes[x].Left = (50 + left);
                Controls.Add(boxes[x]);
                this.boxes[x].MouseClick += new
                System.Windows.Forms.MouseEventHandler(this.boxes_MouseClick);
                label1.Text = " Score: " + initialscore.ToString();
                label2.Text = " Time: " + time.ToString();
        }
    }

}

namespace WindowsFormsApplication1
{
    class Monster
    {
        protected int score;
        public Monster()
        {
            score = 10;
        }
    }
} 

namespace WindowsFormsApplication1
{
    class Player:Monster
    {
    }
}

尚未在播放器类中添加任何内容。

点击动态图像时,我需要添加或更改什么才能使初始分数根据怪物类中的分数值进行更改?

【问题讨论】:

    标签: c# arrays oop picturebox


    【解决方案1】:

    要统一分数的更新/递增和可视化,您应该将其提取到一个方法中:

    public void incrementScore(int increment)
    {
      initialscore += increment;
      label1.Text = " Score: " + initialscore.ToString();
    }
    

    在 Form1_Load 你这样称呼它:

    incrementScore(0);
    

    点击怪物有不同的可能性: 如果所有的怪物都有相同的点,你可以在 Monster 类中将其设为静态变量。

    protected static int Score = 10;
    

    允许您在 box_MouseClick 事件处理程序中使用它:

    incrementScore(Monster.Score);
    

    如果所有怪物都有另一个值,您必须将 score 变量作为实例变量保存,以某种方式识别您单击的怪物类的实例并使用该值递增

    【讨论】:

    • 感谢您的帮助,它按我想要的方式工作。如果玩家没有及时点击图片,您对我如何从初始分数中减去玩家继承的分数有什么建议吗?