Первая часть

This commit is contained in:
Baryshev Dmitry 2024-03-13 21:00:22 +04:00
parent 92d457b0bd
commit 6a5e5fc3bb
2 changed files with 72 additions and 27 deletions

View File

@ -29,11 +29,12 @@
private void InitializeComponent()
{
pictureBoxDumpTruck = new PictureBox();
buttonCreate = new Button();
buttonCreateDumpTruck = new Button();
buttonLeft = new Button();
buttonRight = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonCreateTruck = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit();
SuspendLayout();
//
@ -46,16 +47,16 @@
pictureBoxDumpTruck.TabIndex = 0;
pictureBoxDumpTruck.TabStop = false;
//
// buttonCreate
// buttonCreateDumpTruck
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 473);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
buttonCreateDumpTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateDumpTruck.Location = new Point(12, 473);
buttonCreateDumpTruck.Name = "buttonCreateDumpTruck";
buttonCreateDumpTruck.Size = new Size(188, 29);
buttonCreateDumpTruck.TabIndex = 1;
buttonCreateDumpTruck.Text = "Создать самосвал";
buttonCreateDumpTruck.UseVisualStyleBackColor = true;
buttonCreateDumpTruck.Click += ButtonCreateDumpTruck_Click;
//
// buttonLeft
//
@ -105,16 +106,28 @@
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonCreateTruck
//
buttonCreateTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTruck.Location = new Point(206, 473);
buttonCreateTruck.Name = "buttonCreateTruck";
buttonCreateTruck.Size = new Size(188, 29);
buttonCreateTruck.TabIndex = 6;
buttonCreateTruck.Text = "Создать грузовик";
buttonCreateTruck.UseVisualStyleBackColor = true;
buttonCreateTruck.Click += ButtonCreateTruck_Click;
//
// FormDumpTruck
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(892, 514);
Controls.Add(buttonCreateTruck);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateDumpTruck);
Controls.Add(pictureBoxDumpTruck);
Name = "FormDumpTruck";
Text = "Самосвал";
@ -125,10 +138,11 @@
#endregion
private PictureBox pictureBoxDumpTruck;
private Button buttonCreate;
private Button buttonCreateDumpTruck;
private Button buttonLeft;
private Button buttonRight;
private Button buttonUp;
private Button buttonDown;
private Button buttonCreateTruck;
}
}

View File

@ -14,8 +14,7 @@ namespace ProjectDumpTruck
public partial class FormDumpTruck : Form
{
private DrawningDumpTruck? _drawningDumpTruck;
private DrawningTruck? _drawningTruck;
public FormDumpTruck()
{
@ -24,50 +23,82 @@ namespace ProjectDumpTruck
private void Draw()
{
if (_drawningDumpTruck == null) return;
if (_drawningTruck == null) return;
Bitmap bmp = new(pictureBoxDumpTruck.Width,
pictureBoxDumpTruck.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningDumpTruck.DrawTransport(gr);
_drawningTruck.DrawTransport(gr);
pictureBoxDumpTruck.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type"></param>
private void CreateObject(string type)
{
Random random = new();
_drawningDumpTruck = new DrawningDumpTruck();
_drawningDumpTruck.Init(random.Next(100, 300), random.Next(1000, 3000),
switch (type)
{
case nameof(DrawningTruck):
_drawningTruck = new DrawningTruck(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningDumpTruck):
_drawningTruck = new DrawningDumpTruck(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)));
break;
_drawningDumpTruck.SetPictureSize(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawningDumpTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
default:
return;
}
_drawningTruck.SetPictureSize(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнокпки "Создать самосвал"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningDumpTruck));
/// <summary>
/// Обработка нажатия кнокпки "Создать грузовик"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningDumpTruck == null) return;
if (_drawningTruck == null) return;
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonLeft":
result = _drawningDumpTruck.MoveTransport(DirectionType.Left); break;
result = _drawningTruck.MoveTransport(DirectionType.Left); break;
case "buttonDown":
result = _drawningDumpTruck.MoveTransport(DirectionType.Down); break;
result = _drawningTruck.MoveTransport(DirectionType.Down); break;
case "buttonUp":
result = _drawningDumpTruck.MoveTransport(DirectionType.Up); break;
result = _drawningTruck.MoveTransport(DirectionType.Up); break;
case "buttonRight":
result = _drawningDumpTruck.MoveTransport(DirectionType.Right); break;
result = _drawningTruck.MoveTransport(DirectionType.Right); break;
}
if (result) Draw();
}
}
}