Я делаю простую игру в C#
, где вы должны стрелять, чтобы убивать врагов (оба - pictureboxs
(выстрел и враг)).
У меня есть отдельный класс только для создания и обработки выстрелов, а другой - для врага.
В моем enemy class
у меня есть 2 methods
(один предназначен для создания самого элемента управления), а другой просто для его вызова (порождает врага)
Проблема с этим кодом, который у меня есть, будет конфликтовать только с первым созданным классом (монстром), а не с 3, который я создал в своей форме с for loop.
public static PictureBox Monstro_P; //Static picturebox variable that is the enemy control
public void spawn_Monstro(Form form, int _X, int _Y) //Method that spawns the monster
{
Monstro_S(form, _X, _Y);
}
public void Monstro_S(Form form, int _X, int _Y)//Method that creates the control
{
Monstro_P = new PictureBox()
{
BackColor = Color.Chocolate,
Size = new Size(20, 35),
Left = _X,
Top = _Y,
Tag = "Monstro"
};
aa.Interval = 10;
aa.Tick += new EventHandler(aa_Timer);
aa.Start();
form.Controls.Add(Monstro_P);
Monstro_PB = new ProgressBar() //Represents the health bar of the enemy
{
Width = Monstro_P.Width,
Height = 10,
Location = Monstro_P.Location,
Maximum = vida,
Value = vida,
};
Monstro_PB.Top -= 15;
form.Controls.Add(Monstro_PB);
}
В моем классе выстрелов (это будет проверять, столкнулись ли оба из них или нет)
public PictureBox Tiro_P; //Public variable that is the shot control
public void Spawn_Tiro(Form form, int _X, int _Y)//Method that spawns te shot
{
Tiro_C(form, _X, _Y);
}
public void Tiro_C(Form form, int _X, int _Y)//Method that creates the control
{
Tiro_P = new PictureBox()
{
Left = _X,
Top = _Y,
Size = new Size(10, 8),
BackColor = Color.Crimson,
Tag = "Tiro"
};
Tiro_P.LocationChanged += new EventHandler(Tiro_Localizao); //PictureBox LocationChanged event to check when it hits
Tiro_T.Interval = 10;
Tiro_T.Start();
Tiro_T.Tick += new EventHandler(Tiro_Timer);
form.Controls.Add(Tiro_P);
}
void Tiro_Timer(object sender, EventArgs e)//Timer for the class
{
Tiro_P.Left += 5; //Moves the picturebox(shot in this case)
}
void Tiro_Localizao(object sender, EventArgs e)//LocationChanged event
{
if (Monstro.Monstro_P.Bounds.IntersectsWith(Tiro_P.Bounds))
MessageBox.Show("you got hit!");
// если статическая переменная (monster picturebox) сталкивается с элементом управления shot
}
Код загрузки Form1, где я порождаю класс монстров:
private void Form1_Load(object sender, EventArgs e)
{
for (int a = 0; a < 3; a++) //Spawns a monster 3 times in random locations
{
var x = rnd.Next(50, 550);
var y = rnd.Next(50, 550);
m = new Monstro();
m.spawn_Monstro(this, x, y);
}
}
И мой код для запуска снимка каждый раз, когда я нажимаю на форму:
private void Form1_Click(object sender, EventArgs e)//Form Click Event
{
_Tiro = new Tiro(); //Class new instance of class
_Tiro.Spawn_Tiro(this, Cursor.Position.X, Cursor.Position.Y);
}
РЕДАКТИРОВАТЬ: Я поместил точно такой же код на таймер, потому что я думал, что PictureBox LocationChanged не работает, но он также не работает.