From 493ffef69cc3fd02689d2a25f1e4ed5502097250 Mon Sep 17 00:00:00 2001 From: Glliza Date: Fri, 22 Mar 2024 22:49:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=80=D0=BE=D0=B4=D0=B8=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=B9=20=D0=B8=20=D0=B2=D0=B2=D0=BE=D0=B4=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => Drawning}/DirectionType.cs | 3 +- .../ProjectAirbus/Drawning/DrawningAirbus.cs | 69 +++++++++ .../DrawningBus.cs} | 134 +++++++++++------- .../{ => Entities}/EntityAirbus.cs | 25 +--- .../ProjectAirbus/Entities/EntityBus.cs | 46 ++++++ .../ProjectAirbus/FormAirbus.Designer.cs | 20 ++- ProjectAirbus/ProjectAirbus/FormAirbus.cs | 67 ++++++--- 7 files changed, 263 insertions(+), 101 deletions(-) rename ProjectAirbus/ProjectAirbus/{ => Drawning}/DirectionType.cs (91%) create mode 100644 ProjectAirbus/ProjectAirbus/Drawning/DrawningAirbus.cs rename ProjectAirbus/ProjectAirbus/{DrawningAirbus.cs => Drawning/DrawningBus.cs} (66%) rename ProjectAirbus/ProjectAirbus/{ => Entities}/EntityAirbus.cs (71%) create mode 100644 ProjectAirbus/ProjectAirbus/Entities/EntityBus.cs diff --git a/ProjectAirbus/ProjectAirbus/DirectionType.cs b/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs similarity index 91% rename from ProjectAirbus/ProjectAirbus/DirectionType.cs rename to ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs index 6a5dcb5..8d00506 100644 --- a/ProjectAirbus/ProjectAirbus/DirectionType.cs +++ b/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs @@ -1,5 +1,4 @@ - -namespace ProjectAirbus; +namespace ProjectAirbus.Drawning; /// /// Направление перемещения diff --git a/ProjectAirbus/ProjectAirbus/Drawning/DrawningAirbus.cs b/ProjectAirbus/ProjectAirbus/Drawning/DrawningAirbus.cs new file mode 100644 index 0000000..530c042 --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Drawning/DrawningAirbus.cs @@ -0,0 +1,69 @@ +using ProjectAirbus.Entities; + +namespace ProjectAirbus.Drawning; + +/// +/// Класс, отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawningAirbus : DrawningBus +{ + + /// + /// Ширина прорисовки аэробуса + /// + private readonly int _drawningAirbusWidth = 133; + + /// + /// Высота прорисовки аэробуса + /// + private readonly int _drawningAirbusHeight = 60; + + /// + /// Конструтор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия дополнительного отсека для пассажиров + /// Признак наличия дополнительных двигателей + public DrawningAirbus(int speed, double weight, Color bodycolor, Color additionalcolor, bool compartment, bool engine) : base(133, 60) + { + EntityBus = new EntityAirbus(speed, weight, bodycolor, additionalcolor, compartment, engine); + } + + public override void DrawTransport(Graphics g) + { + if (EntityBus == null || EntityBus is not EntityAirbus airbus || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(airbus.AdditionalColor); + + base.DrawTransport(g); + + if (airbus.Engine) + { + g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 33, 13, 5); + g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 33, 15, 5); + } + + Brush brBlue = new SolidBrush(Color.LightBlue); + if (airbus.Compartment) + { + //g.FillEllipse(brBlue, _startPosX.Value + 37, _startPosY.Value + 13, 49, 15); + + g.FillPolygon(brBlue, new Point[] + { + new Point(_startPosX.Value + 35, _startPosY.Value + 20), new Point(_startPosX.Value + 60, _startPosY.Value + 12), + new Point(_startPosX.Value + 80, _startPosY.Value + 12), new Point(_startPosX.Value + 95, _startPosY.Value + 20)}); + + g.DrawLine(pen, _startPosX.Value + 35, _startPosY.Value + 20, _startPosX.Value + 60, _startPosY.Value + 12); + g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 12, _startPosX.Value + 80, _startPosY.Value + 12); + g.DrawLine(pen, _startPosX.Value + 80, _startPosY.Value + 12, _startPosX.Value + 95, _startPosY.Value + 20); + } + } +} + diff --git a/ProjectAirbus/ProjectAirbus/DrawningAirbus.cs b/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs similarity index 66% rename from ProjectAirbus/ProjectAirbus/DrawningAirbus.cs rename to ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs index 5d5ff2c..ce13cd1 100644 --- a/ProjectAirbus/ProjectAirbus/DrawningAirbus.cs +++ b/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs @@ -1,15 +1,18 @@ - -namespace ProjectAirbus; +using ProjectAirbus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; -/// -/// Класс, отвечающий за прорисовку и перемещение объекта-сущности -/// -public class DrawningAirbus +namespace ProjectAirbus.Drawning; + +public class DrawningBus { /// /// Класс-сущность /// - public EntityAirbus? EntityAirbus { get; private set; } + public EntityBus? EntityBus { get; protected set; } /// /// Ширина окна @@ -24,42 +27,82 @@ public class DrawningAirbus /// /// Левая координата прорисовки аэробуса /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя кооридната прорисовки аэробуса /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки аэробуса /// - private readonly int _drawningAirbusWidth = 133; + private readonly int _drawningBusWidth = 133; /// /// Высота прорисовки аэробуса /// - private readonly int _drawningAirbusHeight = 60; + private readonly int _drawningBusHeight = 60; /// - /// Инициализация свойств + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия дополнительного отсека для пассажиров - /// Признак наличия дополнительных двигателей - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool compartment, bool engine) + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningBusWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningBusHeight; + + /// + /// Пустой конструктор + /// + private DrawningBus() { - EntityAirbus = new EntityAirbus(); - EntityAirbus.Init(speed, weight, bodycolor, additionalcolor, compartment, engine); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + + public DrawningBus(int speed, double weight, Color bodycolor) : this() + { + EntityBus = new EntityBus(speed, weight, bodycolor); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + + /// + /// Конструктор для наследников + /// + /// Ширина прорисовки аэробуса + /// Высота прорисовки аэробуса + + protected DrawningBus(int drawningAirbusWidth, int drawningAirbusHeight) : this() + { + _drawningBusWidth = drawningAirbusWidth; + _drawningBusHeight = drawningAirbusHeight; + } + /// /// Установка границ поля /// @@ -71,7 +114,7 @@ public class DrawningAirbus // TODO проверка, что объект "влезает" в размеры поля // если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена - if (width >= _drawningAirbusWidth || height >= _drawningAirbusHeight) + if (width >= _drawningBusWidth || height >= _drawningBusHeight) { _pictureWidth = width; _pictureHeight = height; @@ -102,17 +145,17 @@ public class DrawningAirbus { x = 0; } - else if (x > _pictureWidth - _drawningAirbusWidth) + else if (x > _pictureWidth - _drawningBusWidth) { - x = _pictureWidth.Value - _drawningAirbusWidth; + x = _pictureWidth.Value - _drawningBusWidth; } if (y < 0) { y = 0; } - else if (y > _pictureHeight - _drawningAirbusHeight) + else if (y > _pictureHeight - _drawningBusHeight) { - y = _pictureHeight.Value - _drawningAirbusHeight; + y = _pictureHeight.Value - _drawningBusHeight; } _startPosX = x; @@ -125,7 +168,7 @@ public class DrawningAirbus /// public bool MoveTransport(DirectionType direction) { - if (EntityAirbus == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -133,33 +176,33 @@ public class DrawningAirbus { // Влево case DirectionType.Left: - if (_startPosX.Value - EntityAirbus.Step > 0) + if (_startPosX.Value - EntityBus.Step > 0) { - _startPosX -= (int)EntityAirbus.Step; + _startPosX -= (int)EntityBus.Step; } return true; //Вверх case DirectionType.Up: - if (_startPosY.Value - EntityAirbus.Step > 0) + if (_startPosY.Value - EntityBus.Step > 0) { - _startPosY -= (int)EntityAirbus.Step; + _startPosY -= (int)EntityBus.Step; } return true; //Вправо case DirectionType.Right: { - if (_startPosX.Value + _drawningAirbusWidth + EntityAirbus.Step < _pictureWidth) + if (_startPosX.Value + _drawningBusWidth + EntityBus.Step < _pictureWidth) { - _startPosX += (int)EntityAirbus.Step; + _startPosX += (int)EntityBus.Step; } } return true; //Вниз case DirectionType.Down: { - if (_startPosY.Value + _drawningAirbusHeight + EntityAirbus.Step < _pictureHeight) + if (_startPosY.Value + _drawningBusHeight + EntityBus.Step < _pictureHeight) { - _startPosY += (int)EntityAirbus.Step; + _startPosY += (int)EntityBus.Step; } } return true; @@ -171,23 +214,15 @@ public class DrawningAirbus /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityAirbus == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush bodyColorBrush = new SolidBrush(EntityAirbus.BodyColor); - Brush additionalBrush = new SolidBrush(EntityAirbus.AdditionalColor); - - //Дополнительный отсек - Brush brBlue = new SolidBrush(Color.LightBlue); - if (EntityAirbus.Compartment) - { - g.FillEllipse(brBlue, _startPosX.Value + 37, _startPosY.Value + 13, 49, 15); - } + Brush bodyColorBrush = new SolidBrush(EntityBus.BodyColor); //корпус g.FillRectangle(bodyColorBrush, _startPosX.Value, _startPosY.Value + 20, 100, 20); @@ -224,12 +259,5 @@ public class DrawningAirbus g.DrawLine(pen, _startPosX.Value + 90, _startPosY.Value + 40, _startPosX.Value + 90, _startPosY.Value + 45); g.FillEllipse(darkBrush, _startPosX.Value + 87, _startPosY.Value + 45, 6, 6); - // Двигатели - if (EntityAirbus.Engine) - { - g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 33, 13, 5); - g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 33, 15, 5); - } } } - diff --git a/ProjectAirbus/ProjectAirbus/EntityAirbus.cs b/ProjectAirbus/ProjectAirbus/Entities/EntityAirbus.cs similarity index 71% rename from ProjectAirbus/ProjectAirbus/EntityAirbus.cs rename to ProjectAirbus/ProjectAirbus/Entities/EntityAirbus.cs index a507c30..97d6ad5 100644 --- a/ProjectAirbus/ProjectAirbus/EntityAirbus.cs +++ b/ProjectAirbus/ProjectAirbus/Entities/EntityAirbus.cs @@ -1,26 +1,10 @@ - -namespace ProjectAirbus; +namespace ProjectAirbus.Entities; /// /// Класс-сущность "Аэробус" /// -public class EntityAirbus +public class EntityAirbus : EntityBus { - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } - - /// - /// Основной цывет - /// - public Color BodyColor { get; private set; } - /// /// Дополнительный цвет /// @@ -50,11 +34,8 @@ public class EntityAirbus /// Дополнительный цвет /// Признак наличия дополнительного отсека для пассажиров /// Признак наличия дополнительных двигателей - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool compartment, bool engine) + public EntityAirbus(int speed, double weight, Color bodyColor, Color additionalColor, bool compartment, bool engine) : base (speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Compartment = compartment; Engine = engine; diff --git a/ProjectAirbus/ProjectAirbus/Entities/EntityBus.cs b/ProjectAirbus/ProjectAirbus/Entities/EntityBus.cs new file mode 100644 index 0000000..4fa528e --- /dev/null +++ b/ProjectAirbus/ProjectAirbus/Entities/EntityBus.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirbus.Entities; + +/// +/// Класс-сущность "Аэробус2" +/// +public class EntityBus +{ + /// + /// Скорость + /// + public int Speed { get; private set; } + + /// + /// Вес + /// + public double Weight { get; private set; } + + /// + /// Основной цывет + /// + public Color BodyColor { get; private set; } + + /// + /// Шаг перемещения аэробуса + /// + public double Step => Speed * 100 / Weight; + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес + /// Основной цветm> + public EntityBus(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs b/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs index ab25a76..5a9faec 100644 --- a/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs +++ b/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs @@ -34,6 +34,7 @@ buttonRight = new Button(); buttonUp = new Button(); pictureBoxAirbus = new PictureBox(); + buttonCreateBus = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirbus).BeginInit(); SuspendLayout(); // @@ -42,9 +43,9 @@ buttonCreateAirbus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateAirbus.Location = new Point(12, 498); buttonCreateAirbus.Name = "buttonCreateAirbus"; - buttonCreateAirbus.Size = new Size(75, 23); + buttonCreateAirbus.Size = new Size(193, 23); buttonCreateAirbus.TabIndex = 1; - buttonCreateAirbus.Text = "Создать"; + buttonCreateAirbus.Text = "Создать аэробус"; buttonCreateAirbus.UseVisualStyleBackColor = true; buttonCreateAirbus.Click += ButtonCreateAirbus_Click; // @@ -54,7 +55,7 @@ ButtonLeft.BackgroundImage = Properties.Resources.free_icon_left_arrow_10696518; ButtonLeft.BackgroundImageLayout = ImageLayout.Stretch; ButtonLeft.Location = new Point(927, 491); - ButtonLeft.Name = "buttonLeft"; + ButtonLeft.Name = "ButtonLeft"; ButtonLeft.Size = new Size(35, 35); ButtonLeft.TabIndex = 2; ButtonLeft.UseVisualStyleBackColor = true; @@ -105,10 +106,22 @@ pictureBoxAirbus.TabIndex = 6; pictureBoxAirbus.TabStop = false; // + // buttonCreateBus + // + buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBus.Location = new Point(229, 498); + buttonCreateBus.Name = "buttonCreateBus"; + buttonCreateBus.Size = new Size(193, 23); + buttonCreateBus.TabIndex = 7; + buttonCreateBus.Text = "Создать автобус"; + buttonCreateBus.UseVisualStyleBackColor = true; + buttonCreateBus.Click += ButtonCreateBus_Click; + // // FormAirbus // AutoScaleMode = AutoScaleMode.None; ClientSize = new Size(1059, 533); + Controls.Add(buttonCreateBus); Controls.Add(buttonCreateAirbus); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -128,5 +141,6 @@ private Button buttonRight; private Button buttonUp; private PictureBox pictureBoxAirbus; + private Button buttonCreateBus; } } \ No newline at end of file diff --git a/ProjectAirbus/ProjectAirbus/FormAirbus.cs b/ProjectAirbus/ProjectAirbus/FormAirbus.cs index b047d21..235e87e 100644 --- a/ProjectAirbus/ProjectAirbus/FormAirbus.cs +++ b/ProjectAirbus/ProjectAirbus/FormAirbus.cs @@ -1,4 +1,6 @@  +using ProjectAirbus.Drawning; + namespace ProjectAirbus { /// @@ -9,7 +11,7 @@ namespace ProjectAirbus /// /// Поле-объект для прорисовки объекта /// - private DrawningAirbus? _drawningAirbus; + private DrawningBus? _drawningBus; /// /// Конструктор формы @@ -24,7 +26,7 @@ namespace ProjectAirbus /// private void Draw() { - if (_drawningAirbus == null) + if (_drawningBus == null) { return; } @@ -32,28 +34,50 @@ namespace ProjectAirbus Bitmap bmp = new(pictureBoxAirbus.Width, pictureBoxAirbus.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningAirbus.DrawTransport(gr); + _drawningBus.DrawTransport(gr); pictureBoxAirbus.Image = bmp; } /// - /// Обработка нажатия кнопки "Создать" + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawningBus): + _drawningBus = new DrawningBus(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(DrawningAirbus): + _drawningBus = new DrawningAirbus(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; + default: + return; + } + _drawningBus.SetPictureSize(pictureBoxAirbus.Width, pictureBoxAirbus.Height); + _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + /// + /// Обработка нажатия кнопки "Создать Аэробус" /// /// /// - private void ButtonCreateAirbus_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningAirbus = new DrawningAirbus(); - _drawningAirbus.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))); - _drawningAirbus.SetPictureSize(pictureBoxAirbus.Width, pictureBoxAirbus.Height); - _drawningAirbus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + private void ButtonCreateAirbus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirbus)); - Draw(); - } + /// + /// Обработка нажатия кнопки "Создать автобус" + /// + /// + /// + private void ButtonCreateBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus)); /// /// Перемещение объекта по форме (нажатие кнопок навигации) @@ -62,7 +86,7 @@ namespace ProjectAirbus /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningAirbus == null) + if (_drawningBus == null) { return; } @@ -72,19 +96,19 @@ namespace ProjectAirbus { case "buttonUp": result = - _drawningAirbus.MoveTransport(DirectionType.Up); + _drawningBus.MoveTransport(DirectionType.Up); break; case "buttonDown": result = - _drawningAirbus.MoveTransport(DirectionType.Down); + _drawningBus.MoveTransport(DirectionType.Down); break; case "buttonLeft": result = - _drawningAirbus.MoveTransport(DirectionType.Left); + _drawningBus.MoveTransport(DirectionType.Left); break; case "buttonRight": result = - _drawningAirbus.MoveTransport(DirectionType.Right); + _drawningBus.MoveTransport(DirectionType.Right); break; } if (result) @@ -92,5 +116,6 @@ namespace ProjectAirbus Draw(); } } + } }