Добавление родителей и ввод конструкторов

This commit is contained in:
DjonniStorm 2024-03-02 23:59:06 +04:00
parent 18cad36a8d
commit da11fee833
7 changed files with 228 additions and 133 deletions

View File

@ -1,4 +1,4 @@
namespace ProjectCleaningCar;
namespace ProjectCleaningCar.Drawning;
/// <summary>
/// Направления перемещения
/// </summary>

View File

@ -1,13 +1,13 @@
namespace ProjectCleaningCar;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта сущности
/// </summary>
public class DrawningCleaningCar
using ProjectCleaningCar.Entities;
namespace ProjectCleaningCar.Drawning;
public class DrawningCar
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityCleaningCar? EntityCleaningCar { get; private set; }
public EntityCar? EntityCar { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
@ -19,11 +19,11 @@ public class DrawningCleaningCar
/// <summary>
/// Левая координата прорисовки машины
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки машины
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки машины
/// </summary>
@ -31,29 +31,38 @@ public class DrawningCleaningCar
/// <summary>
/// Высота прорисовки машины
/// </summary>
private readonly int _drawningCarHeight = 90;
private readonly int _drawningCarHeight = 80;
/// <summary>
/// Инициализация свойств
/// Пустой конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="tank">Бак с водой</param>
/// <param name="sweepingBrush">Подметательная щётка</param>
/// <param name="flashlight">Проблескового маячок</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool tank, bool sweepingBrush, bool flashlight)
private DrawningCar()
{
EntityCleaningCar = new EntityCleaningCar();
EntityCleaningCar.Init(speed, weight, bodyColor, additionalColor,
tank, sweepingBrush, flashlight);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningCar(int speed, double weight, Color bodyColor) : this()
{
EntityCar = new EntityCar(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningCarWidth">Ширина прорисовки машины</param>
/// <param name="drawningCarHeight">Высота прорисовки машины</param>
protected DrawningCar(int drawningCarWidth, int drawningCarHeight) : this()
{
_drawningCarWidth = drawningCarWidth;
_drawningCarHeight = drawningCarHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
@ -134,7 +143,7 @@ tank, sweepingBrush, flashlight);
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityCleaningCar == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityCar == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
@ -142,30 +151,30 @@ tank, sweepingBrush, flashlight);
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityCleaningCar.Step > 0)
if (_startPosX.Value - EntityCar.Step > 0)
{
_startPosX -= (int)EntityCleaningCar.Step;
_startPosX -= (int)EntityCar.Step;
}
return true;
//вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningCarWidth + EntityCleaningCar.Step < _pictureWidth)
if (_startPosX.Value + _drawningCarWidth + EntityCar.Step < _pictureWidth)
{
_startPosX += (int)EntityCleaningCar.Step;
_startPosX += (int)EntityCar.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityCleaningCar.Step > 0)
if (_startPosY.Value - EntityCar.Step > 0)
{
_startPosY -= (int)EntityCleaningCar.Step;
_startPosY -= (int)EntityCar.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningCarHeight + EntityCleaningCar.Step < _pictureHeight)
if (_startPosY.Value + _drawningCarHeight + EntityCar.Step < _pictureHeight)
{
_startPosY += (int)EntityCleaningCar.Step;
_startPosY += (int)EntityCar.Step;
}
return true;
default:
@ -177,64 +186,26 @@ tank, sweepingBrush, flashlight);
/// Отрисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityCleaningCar == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityCar == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityCleaningCar.BodyColor);
Brush additionalBrush = new SolidBrush(EntityCleaningCar.AdditionalColor);
//бак
if (EntityCleaningCar.Tank)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value, _drawningCarWidth - 32, 38);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, _drawningCarWidth - 32, 38);
g.FillRectangle(bodyBrush, _startPosX.Value, _startPosY.Value + 10, _drawningCarWidth - 32, 10);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 10, _drawningCarWidth - 32, 10);
}
//подметательная щётка
if (EntityCleaningCar.SweepingBrush)
{
Pen additionalPen = new(EntityCleaningCar.AdditionalColor);
g.DrawEllipse(additionalPen, _startPosX.Value + _drawningCarWidth - 5, _startPosY.Value + 50, 18, 18);
g.FillEllipse(additionalBrush, _startPosX.Value + _drawningCarWidth - 5, _startPosY.Value + 50, 18, 18);
g.FillEllipse(bodyBrush, _startPosX.Value + _drawningCarWidth - 1, _startPosY.Value + 54, 10, 10);
g.DrawLine(pen, _startPosX.Value + _drawningCarWidth - 10, _startPosY.Value + 50, _startPosX.Value + _drawningCarWidth + 5, _startPosY.Value + 60);
}
//проблесковый маячок
if (EntityCleaningCar.Flashlight)
{
g.FillRectangle(additionalBrush, _startPosX.Value + _drawningCarWidth - 25, _startPosY.Value + 5, 5, 7);
}
Brush bodyBrush = new SolidBrush(EntityCar.BodyColor);
//основание
g.FillRectangle(bodyBrush, _startPosX.Value, _startPosY.Value + 40, _drawningCarWidth - 10, 10);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 40, _drawningCarWidth - 10, 10);
g.FillRectangle(bodyBrush, _startPosX.Value, _startPosY.Value + 35, 120, 10);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 35, 120, 10);
//
g.FillRectangle(bodyBrush, _startPosX.Value + _drawningCarWidth - 30, _startPosY.Value + 10, 20, 30);
g.DrawRectangle(pen, _startPosX.Value + _drawningCarWidth - 30, _startPosY.Value + 10, 20, 30);
g.FillRectangle(bodyBrush, _startPosX.Value + 100, _startPosY.Value + 5, 20, 30);
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + 5, 20, 30);
//колёса
g.FillEllipse(bodyBrush, _startPosX.Value + 10, _startPosY.Value + 50, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 50, 20, 20);
g.FillEllipse(bodyBrush, _startPosX.Value + 35, _startPosY.Value + 50, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 50, 20, 20);
g.FillEllipse(bodyBrush, _startPosX.Value + 97, _startPosY.Value + 50, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 97, _startPosY.Value + 50, 20, 20);
//диски
g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 55, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 55, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 55, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 55, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 102, _startPosY.Value + 55, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 102, _startPosY.Value + 55, 10, 10);
//окно
g.DrawRectangle(pen, _startPosX.Value + _drawningCarWidth - 30, _startPosY.Value + 10, 20, 30);
g.FillRectangle(additionalBrush, _startPosX.Value + _drawningCarWidth - 22, _startPosY.Value + 16, 10, 11);
g.FillEllipse(bodyBrush, _startPosX.Value + 10, _startPosY.Value + 45, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 45, 20, 20);
g.FillEllipse(bodyBrush, _startPosX.Value + 35, _startPosY.Value + 45, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 45, 20, 20);
g.FillEllipse(bodyBrush, _startPosX.Value + 97, _startPosY.Value + 45, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 97, _startPosY.Value + 45, 20, 20);
}
}

View File

@ -0,0 +1,74 @@
using ProjectCleaningCar.Entities;
namespace ProjectCleaningCar.Drawning;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта сущности
/// </summary>
public class DrawningCleaningCar : DrawningCar
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="tank">Бак с водой</param>
/// <param name="sweepingBrush">Подметательная щётка</param>
/// <param name="flashlight">Проблескового маячок</param>
public DrawningCleaningCar(int speed, double weight, Color bodyColor, Color
additionalColor, bool tank, bool sweepingBrush, bool flashlight) : base(130, 90)
{
EntityCar = new EntityCleaningCar(speed, weight, bodyColor, additionalColor,
tank, sweepingBrush, flashlight);
}
/// <summary>
/// Отрисовка объекта
/// </summary>
/// <param name="g"></param>
public override void DrawTransport(Graphics g)
{
if (EntityCar == null || EntityCar is not EntityCleaningCar cleaningCar || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
base.DrawTransport(g);
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityCar.BodyColor);
Brush additionalBrush = new SolidBrush(cleaningCar.AdditionalColor);
//бак
if (cleaningCar.Tank)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value, 98, 38);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 98, 38);
g.FillRectangle(bodyBrush, _startPosX.Value, _startPosY.Value + 10, 98, 10);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 10, 98, 10);
}
//подметательная щётка
if (cleaningCar.SweepingBrush)
{
Pen additionalPen = new(cleaningCar.AdditionalColor);
g.DrawEllipse(additionalPen, _startPosX.Value + 125, _startPosY.Value + 50, 18, 18);
g.FillEllipse(additionalBrush, _startPosX.Value + 125, _startPosY.Value + 50, 18, 18);
g.FillEllipse(bodyBrush, _startPosX.Value + 129, _startPosY.Value + 54, 10, 10);
g.DrawLine(pen, _startPosX.Value + 120, _startPosY.Value + 50, _startPosX.Value + 135, _startPosY.Value + 60); ////////!!!!!
}
//проблесковый маячок
if (cleaningCar.Flashlight)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 105, _startPosY.Value, 4, 7);
}
//диски
g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 50, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 50, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 50, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 50, 10, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 102, _startPosY.Value + 50, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 102, _startPosY.Value + 50, 10, 10);
//окно
g.DrawRectangle(pen, _startPosX.Value + 108, _startPosY.Value + 11, 10, 11);
g.FillRectangle(additionalBrush, _startPosX.Value + 108, _startPosY.Value + 11, 10, 11);
}
}

View File

@ -0,0 +1,36 @@
namespace ProjectCleaningCar.Entities;
/// <summary>
/// Класс-сущность "Машина"
/// </summary>
public class EntityCar
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения подметательно-уборочной машины
/// </summary>
public double Step => Speed * 200 / Weight;
/// <summary>
/// Инициализация полей объекта-класса подметально-уборочной машины
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityCar(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -1,21 +1,9 @@
namespace ProjectCleaningCar;
namespace ProjectCleaningCar.Entities;
/// <summary>
/// Класс-сущность "Подметательно-уборочная машина"
/// </summary>
public class EntityCleaningCar
public class EntityCleaningCar : EntityCar
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
@ -33,10 +21,6 @@ public class EntityCleaningCar
/// </summary>
public bool Flashlight { get; private set; }
/// <summary>
/// Шаг перемещения подметательно-уборочной машины
/// </summary>
public double Step => Speed * 200 / Weight;
/// <summary>
/// Инициализация полей объекта-класса подметально-уборочной машины
/// </summary>
/// <param name="speed">Скорость</param>
@ -46,12 +30,8 @@ public class EntityCleaningCar
/// <param name="tank">Признак наличия бака под воду</param>
/// <param name="sweepingBrush">Признак наличия подметательной щётки</param>
/// <param name="flashlight">Признак наличия проблескового маячка</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
bool tank, bool sweepingBrush, bool flashlight)
public EntityCleaningCar(int speed, double weight, Color bodyColor, Color additionalColor, bool tank, bool sweepingBrush, bool flashlight) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Tank = tank;
SweepingBrush = sweepingBrush;

View File

@ -34,6 +34,7 @@
buttonLeft = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonCreateCar = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCleaningCar).BeginInit();
SuspendLayout();
//
@ -49,11 +50,12 @@
// buttonCreateCleaningCar
//
buttonCreateCleaningCar.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateCleaningCar.Location = new Point(12, 419);
buttonCreateCleaningCar.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point);
buttonCreateCleaningCar.Location = new Point(12, 421);
buttonCreateCleaningCar.Name = "buttonCreateCleaningCar";
buttonCreateCleaningCar.Size = new Size(123, 34);
buttonCreateCleaningCar.Size = new Size(192, 32);
buttonCreateCleaningCar.TabIndex = 1;
buttonCreateCleaningCar.Text = "Создать";
buttonCreateCleaningCar.Text = "Создать уборочную машину";
buttonCreateCleaningCar.UseVisualStyleBackColor = true;
buttonCreateCleaningCar.Click += ButtonCreateCleaningCar_Click;
//
@ -105,11 +107,22 @@
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonCreateCar
//
buttonCreateCar.Location = new Point(210, 421);
buttonCreateCar.Name = "buttonCreateCar";
buttonCreateCar.Size = new Size(192, 32);
buttonCreateCar.TabIndex = 6;
buttonCreateCar.Text = "Создать машину";
buttonCreateCar.UseVisualStyleBackColor = true;
buttonCreateCar.Click += ButtonCreateCar_Click;
//
// FormCleaningCar
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(824, 465);
Controls.Add(buttonCreateCar);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
@ -130,5 +143,6 @@
private Button buttonLeft;
private Button buttonUp;
private Button buttonDown;
private Button buttonCreateCar;
}
}

View File

@ -1,4 +1,6 @@
namespace ProjectCleaningCar;
using ProjectCleaningCar.Drawning;
namespace ProjectCleaningCar;
/// <summary>
/// Форма работы с классом Подметально-уборочная машина
/// </summary>
@ -7,7 +9,7 @@ public partial class FormCleaningCar : Form
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningCleaningCar? _drawningCleaningCar;
private DrawningCar? _drawningCar;
/// <summary>
/// Конструктор формы
/// </summary>
@ -20,35 +22,54 @@ public partial class FormCleaningCar : Form
/// </summary>
private void Draw()
{
if (_drawningCleaningCar == null)
if (_drawningCar == null)
{
return;
}
Bitmap bmp = new(pictureBoxCleaningCar.Width,
pictureBoxCleaningCar.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCleaningCar.DrawTransport(gr);
_drawningCar.DrawTransport(gr);
pictureBoxCleaningCar.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// Обработка нажатия кнопки "Создать подметательно-уборочную машину"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCleaningCar_Click(object sender, EventArgs e)
private void ButtonCreateCleaningCar_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCleaningCar));
/// <summary>
/// Обработка нажатия кнопки "Создать машину"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCar_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCar));
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
public void CreateObject(string type)
{
Random random = new();
_drawningCleaningCar = new DrawningCleaningCar();
_drawningCleaningCar.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)));
_drawningCleaningCar.SetPictureSize(pictureBoxCleaningCar.Width, pictureBoxCleaningCar.Height);
_drawningCleaningCar.SetPosition(random.Next(0, 200), random.Next(0, 200));
Draw();
Random random = new Random();
switch (type)
{
case nameof(DrawningCar):
_drawningCar = new DrawningCar(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)));
break;
case nameof(DrawningCleaningCar):
_drawningCar = new DrawningCleaningCar(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)));
break;
default:
return;
}
_drawningCar.SetPictureSize(pictureBoxCleaningCar.Width, pictureBoxCleaningCar.Height);
_drawningCar.SetPosition(random.Next(0, 200), random.Next(0, 200));
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
@ -57,7 +78,7 @@ public partial class FormCleaningCar : Form
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCleaningCar == null)
if (_drawningCar == null)
{
return;
}
@ -66,16 +87,16 @@ public partial class FormCleaningCar : Form
switch (name)
{
case "buttonUp":
result = _drawningCleaningCar.MoveTransport(DirectionType.Up);
result = _drawningCar.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningCleaningCar.MoveTransport(DirectionType.Down);
result = _drawningCar.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningCleaningCar.MoveTransport(DirectionType.Left);
result = _drawningCar.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningCleaningCar.MoveTransport(DirectionType.Right);
result = _drawningCar.MoveTransport(DirectionType.Right);
break;
}
if (result)
@ -83,5 +104,4 @@ public partial class FormCleaningCar : Form
Draw();
}
}
}