namespace AntiAircraftGun { public partial class FormAntiAircraftGun : Form { /// /// Поле объект для прорисовки объекта /// private DrawningAntiAircraftGun? _drawningAntiAircraftGun; /// /// конструктор формы /// public FormAntiAircraftGun() { InitializeComponent(); } /// /// Метод прорисовки транспорта /// private void Draw() { if (_drawningAntiAircraftGun == null) { return; } Bitmap bmp = new(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); Graphics gr = Graphics.FromImage(bmp); _drawningAntiAircraftGun.DrawTransport(gr); pictureBoxAntiAircraftGun.Image = bmp; } /// /// Обработка кнопик Создать /// /// /// private void ButtonCreate_Click(object sender, EventArgs e) { Random random = new(); _drawningAntiAircraftGun = new DrawningAntiAircraftGun(); _drawningAntiAircraftGun.Init(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); _drawningAntiAircraftGun.SetPictureSize(pictureBoxAntiAircraftGun.Width, pictureBoxAntiAircraftGun.Height); _drawningAntiAircraftGun.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } /// /// Перемещение объекта по форме /// /// /// private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningAntiAircraftGun == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; bool result = false; switch (name) { case "buttonUp": result = _drawningAntiAircraftGun.MoveTransport(DirectionType.Up); break; case "buttonDown": result = _drawningAntiAircraftGun.MoveTransport(DirectionType.Down); break; case "buttonRight": result = _drawningAntiAircraftGun.MoveTransport(DirectionType.Right); break; case "buttonLeft": result = _drawningAntiAircraftGun.MoveTransport(DirectionType.Left); break; } if (result) { Draw(); } } } }