ISEbd-12_Sinelnikova_A.V._S.../ProjectCruiser/FormCruiser.cs

92 lines
3.1 KiB
C#
Raw Normal View History

2024-02-29 13:53:31 +04:00
namespace ProjectCruiser
2024-02-21 16:56:08 +04:00
{
public partial class FormCruiser : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningCruiser? _drawningCruiser;
public FormCruiser()
{
InitializeComponent();
}
/// <summary>
2024-02-29 13:53:31 +04:00
/// Метод прорисовки круисера
2024-02-21 16:56:08 +04:00
/// </summary>
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Bitmap bmp = new(pictureBox1.Width,
pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBox1.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCruiser_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawningCruiser();
_drawningCruiser.Init(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawningCruiser.SetPictureSize(pictureBox1.Width,
pictureBox1.Height);
2024-02-29 13:53:31 +04:00
//начальное положение круисера
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
2024-02-21 16:56:08 +04:00
Draw();
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningCruiser.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningCruiser.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningCruiser.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}
}