How to go about making a variable in a class output to a label after clicking on a picturebox?

StackOverflow https://stackoverflow.com/questions/22850160

  •  27-06-2023
  •  | 
  •  

Question

I'm fairly new to OOP and am not sure how I would go about implementing something in my program. My program is pretty much similar to whack a mole and has an array of picture boxes with an image in and an image of a monster moves randomly between the picture boxes with a time interval applied or will move to a new random picture box whenever the user clicks on the monster in time. I have created an monster and a player sub class to try and add some OOP concepts to the program but am not sure how to implement what I want. Basically I have a label for score on my main form and a score variable in my animal class with a value. I want to be able to add the value of score from the label on my form when the user clicks on the picture box with the mole in and take away the value of score from the label when they don't click on it in time.

Here is my code:

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
    {
    }
}

Nothing has been added in the player class yet.

What do I need to add or change to be able to get the initial score to change by the value of the score in the monster class when clicking on the moving image?

Was it helpful?

Solution

To unify the updating/incrementing and visualization of the score you should extract that to a method:

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

in the Form1_Load you call this like:

incrementScore(0);

for the click on the monster you have different possibilities: if all the monsters have the same points you can make it a static variable in the Monster class.

protected static int Score = 10;

which allows you to use it in the boxes_MouseClick event handler:

incrementScore(Monster.Score);

in case all monsters have another value you have to hold the score variable as an instance variable, identify somehow the instance of the monster class you clicked on and increment with this value

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top