92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
namespace ProjectCruiser
|
|
{
|
|
public partial class FormCruiser : Form
|
|
{
|
|
/// <summary>
|
|
/// Поле-объект для прорисовки объекта
|
|
/// </summary>
|
|
private DrawningCruiser? _drawningCruiser;
|
|
public FormCruiser()
|
|
{
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Метод прорисовки круисера
|
|
/// </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);
|
|
|
|
//начальное положение круисера
|
|
_drawningCruiser.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 (_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();
|
|
}
|
|
}
|
|
}
|
|
}
|