PIBD-13_Fomichev_V.S._Simple_/AntiAircraftGun/FormAntiAircraftGun.cs

81 lines
3.1 KiB
C#

namespace AntiAircraftGun
{
public partial class FormAntiAircraftGun : Form
{
/// <summary>
/// Поле объект для прорисовки объекта
/// </summary>
private DrawingAntiAircraftGun? _drawningAntiAircraftGun;
/// <summary>
/// конструктор формы
/// </summary>
public FormAntiAircraftGun()
{
InitializeComponent();
}
/// <summary>
/// Метод прорисовки транспорта
/// </summary>
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;
}
/// <summary>
/// Обработка кнопик Создать
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAntiAircraftGun = new DrawingAntiAircraftGun();
_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();
}
/// <summary>
/// Перемещение объекта по форме
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
}
}