78 lines
3.0 KiB
C#
78 lines
3.0 KiB
C#
namespace LocomotivesAdvanced
|
||
{
|
||
public partial class FormLocomotive : Form
|
||
{
|
||
/// <summary>
|
||
/// Объект от класса отрисовки локомотива
|
||
/// </summary>
|
||
private DrawningLocomotive _locomotive;
|
||
|
||
public FormLocomotive()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
/// <summary>
|
||
/// Метод отрисовки локомотива
|
||
/// </summary>
|
||
private void Draw()
|
||
{
|
||
Bitmap bmp = new(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
_locomotive?.DrawTransport(gr);
|
||
pictureBoxLocomotive.Image = bmp;
|
||
}
|
||
/// <summary>
|
||
/// Метод обработки нажатия на кнопку "Создать"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||
{
|
||
Random rnd = new();
|
||
_locomotive = new DrawningLocomotive();
|
||
_locomotive.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
|
||
_locomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
|
||
toolStripStatusLabelSpeed.Text = $"Скорость: {_locomotive.Locomotive.Speed}";
|
||
toolStripStatusLabelWeight.Text = $"Вес: {_locomotive.Locomotive.Weight}";
|
||
toolStripStatusLabelBodyColor.Text = $"Цвет: {_locomotive.Locomotive.BodyColor.Name}";
|
||
Draw();
|
||
}
|
||
/// <summary>
|
||
/// Метод обработки нажатия на кнопки перемещения
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonMove_Click(object sender, EventArgs e)
|
||
{
|
||
//получаем имя кнопки
|
||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||
switch (name)
|
||
{
|
||
case "buttonUp":
|
||
_locomotive?.MoveLocomotive(Direction.Up);
|
||
break;
|
||
case "buttonDown":
|
||
_locomotive?.MoveLocomotive(Direction.Down);
|
||
break;
|
||
case "buttonLeft":
|
||
_locomotive?.MoveLocomotive(Direction.Left);
|
||
break;
|
||
case "buttonRight":
|
||
_locomotive?.MoveLocomotive(Direction.Right);
|
||
break;
|
||
}
|
||
Draw();
|
||
}
|
||
/// <summary>
|
||
/// Изменение размеров формы
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void PictureBoxLocomotive_Resize(object sender, EventArgs e)
|
||
{
|
||
_locomotive?.ChangeBorders(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
|
||
Draw();
|
||
}
|
||
}
|
||
}
|