Add a new variable to a component (PictureBox)

1

I am creating a game for a college job, this game consists of several Picturebox , one is the main character who walks, jumps and shoots, PictureBox of shots and PictureBox of monsters (randomly generated by a Random ), currently, when the program detects the picturebox's collision of the shots with that of the monster, the monster is deleted and the shot as well.

What I would like to do is to let these monsters have a "life", for example, take 3 shots for the monster to die, but for this I have to create a variable "int life" and correlate it with% 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 Monster Method:

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 part of the crash program 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 generation of monsters is within a PictureBox and the collision as well.

    
asked by anonymous 22.11.2018 / 15:00

1 answer

1

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

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 you will have the property accessible in the code to be able to manipulate.

    
22.11.2018 / 16:15