Add a new variable to a component (PictureBox)

I'm creating a game for a job in college, this game consists of several Picturebox, One is the main character who walks, jumps and shoots, the PictureBox of the shots and the PictureBox of the monsters(randomly generated by a Random), Currently, when the program detects the collision of the picturebox of the shots with that of the monster, the monster is deleted and the shot too.

What I would like to do is let these monsters have a "life" , e.g. take 3 shots for the monster die, but for this I have to create a variable "int life" and correlate it with the PictureBox of the monster that was created and I do not know how to do it

This is the part of the code where monsters are created:

Random random = new Random();
int a = random.Next(0, 300);
if (a == 150)
{
  NovoMonstro();
}

This Is The New Methodmonstrum:

PictureBox monstro = new PictureBox();
Bitmap imagem;
imagem = Properties.Resources.monstro;
monstro.Image = imagem;
monstro.Size = new Size(51, 85);
monstro.SizeMode = PictureBoxSizeMode.StretchImage;
monstro.Tag = "monstros";
monstro.Left = 1024;
monstro.Top = 555;
monstro.BackColor = Color.Transparent;
this.Controls.Add(monstro);
monstro.BringToFront();

This is the program part of the collision between the shot and the monster:

foreach (Control y in this.Controls)
{
    foreach (Control j in this.Controls)
    {
        if (y is PictureBox && (y.Tag == "bulletD" || y.Tag == "bulletE"))
        {
            if (j is PictureBox && (j.Tag == "monstros" || j.Tag == "monstrosinvertidos"))
            {  
                //detecta se teve colisão entre as picturebox
                if (y.Bounds.IntersectsWith(j.Bounds))
                {
                    this.Controls.Remove(j);
                    this.Controls.Remove(y);
                    contador++;
                }
            }
        }
    }
}

The Monster generation is within a Timer and so is the collision.

Author: João Martins, 2018-11-22

1 answers

The solution will be to create a class that extends the type PictureBox and add a property Vida:

using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public class MyPictureBox : PictureBox
    {
        public int Vida { get; set; }
    }
}

Compile the project and use the MyPictureBox component instead of the native PictureBox.
from this moment will have the property accessible in the code to be able to manipulate.

 1
Author: João Martins, 2018-11-22 15:15:54