From b10750e80175a22e6500c43c14747bbaf60094d8 Mon Sep 17 00:00:00 2001 From: selli73 <145283432+selli73@users.noreply.github.com> Date: Sat, 2 Mar 2024 14:43:02 +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=BD=D0=BE?= =?UTF-8?q?=D0=B2=D1=8B=D1=85=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Monorail/{ => Drawnings}/DirectionType.cs | 2 +- .../Monorail/Drawnings/DrawningMonorail.cs | 67 ++++++++++ .../Drawning_Monorail.cs} | 122 +++++++++--------- .../Monorail/{ => Entities}/EntityMonorail.cs | 33 ++--- Monorail/Monorail/Entities/Entity_Monorail.cs | 48 +++++++ Monorail/Monorail/FormMonorail.Designer.cs | 17 ++- Monorail/Monorail/FormMonorail.cs | 80 ++++++++---- 7 files changed, 259 insertions(+), 110 deletions(-) rename Monorail/Monorail/{ => Drawnings}/DirectionType.cs (94%) create mode 100644 Monorail/Monorail/Drawnings/DrawningMonorail.cs rename Monorail/Monorail/{DrawningMonorail.cs => Drawnings/Drawning_Monorail.cs} (78%) rename Monorail/Monorail/{ => Entities}/EntityMonorail.cs (69%) create mode 100644 Monorail/Monorail/Entities/Entity_Monorail.cs diff --git a/Monorail/Monorail/DirectionType.cs b/Monorail/Monorail/Drawnings/DirectionType.cs similarity index 94% rename from Monorail/Monorail/DirectionType.cs rename to Monorail/Monorail/Drawnings/DirectionType.cs index 3fb9deb..d6daf61 100644 --- a/Monorail/Monorail/DirectionType.cs +++ b/Monorail/Monorail/Drawnings/DirectionType.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace monorail; +namespace Monorail.Drawnings; /// /// Направление перемещения /// diff --git a/Monorail/Monorail/Drawnings/DrawningMonorail.cs b/Monorail/Monorail/Drawnings/DrawningMonorail.cs new file mode 100644 index 0000000..15ecd94 --- /dev/null +++ b/Monorail/Monorail/Drawnings/DrawningMonorail.cs @@ -0,0 +1,67 @@ +using Monorail.Entities; + +namespace Monorail.Drawnings; +/// +/// Класс отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawningMonorail : Drawning_Monorail +{ + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// магнитный рельс + /// Вторая кабинка в задней части + + public DrawningMonorail(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) : base(95, 40) + { + Entity_Monorail = new EntityMonorail(speed, weight, bodyColor, additionalColor, magneticRail, secondCabin); + } + + public override void DrawTransport(Graphics g) + { + if (Entity_Monorail == null || Entity_Monorail is not EntityMonorail monorail || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(monorail.AdditionalColor); + + //магнитная рельса + if (monorail.MagneticRail) + { + g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 40, 90, 3); + } + + //вторая часть монорельса + + if (monorail.SecondCabin) + { + Point[] points_second_cabin = { + new Point(_startPosX.Value + 85, _startPosY.Value), + new Point(_startPosX.Value + 95, _startPosY.Value + 15), + new Point(_startPosX.Value + 95, _startPosY.Value + 30), + new Point(_startPosX.Value + 85, _startPosY.Value + 30), + new Point(_startPosX.Value + 85, _startPosY.Value + 15), + new Point(_startPosX.Value + 95, _startPosY.Value + 15) + + }; + g.DrawPolygon(pen, points_second_cabin); + g.FillPolygon(additionalBrush, points_second_cabin); + + } + + + + //_startPosX += 10; + //_startPosY += 5; + base.DrawTransport(g); + //_startPosX -= 10; + //_startPosY -= 5; + } +} \ No newline at end of file diff --git a/Monorail/Monorail/DrawningMonorail.cs b/Monorail/Monorail/Drawnings/Drawning_Monorail.cs similarity index 78% rename from Monorail/Monorail/DrawningMonorail.cs rename to Monorail/Monorail/Drawnings/Drawning_Monorail.cs index c3bba62..363386f 100644 --- a/Monorail/Monorail/DrawningMonorail.cs +++ b/Monorail/Monorail/Drawnings/Drawning_Monorail.cs @@ -1,15 +1,18 @@ - +using Monorail.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; -namespace monorail; -/// -/// Класс отвечающий за прорисовку и перемещение объекта-сущности -/// -public class DrawningMonorail +namespace Monorail.Drawnings; + +public class Drawning_Monorail { /// /// Класс-сущность (объект) /// - public EntityMonorail? EntityMonorail { get; private set; } + public Entity_Monorail? Entity_Monorail { get; protected set; } /// /// Ширина окна @@ -24,42 +27,62 @@ public class DrawningMonorail /// /// Левая координата прорисовки монорельса /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя координата прорисовки монорельса /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки монорельса (размер объекта) /// - private readonly int _drawningMonorailWidth = 95; + private readonly int _drawningMonorailWidth = 90; /// /// Высота прорисовки монорельса (размер объекта) /// private readonly int _drawingMonorailHeight = 40; - /// - /// Инициализация свойства - /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// магнитный рельс - /// Вторая кабинка в задней части - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) + /// + /// Пустой конструктор + /// + private Drawning_Monorail() { - EntityMonorail = new EntityMonorail(); - EntityMonorail.Init(speed, weight, bodyColor, additionalColor, magneticRail, secondCabin); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; + } + + + /// + /// Конструтор + /// + /// Скорость + /// Вес + /// Основной цвет + + + public Drawning_Monorail(int speed, double weight, Color bodyColor) : this() + { + Entity_Monorail = new Entity_Monorail(speed, weight, bodyColor); } + /// + /// Конструтор для наследников + /// + /// Ширина прорисовки монорельса (размер объекта) + /// Высота прорисовки монорельса (размер объекта) + + + protected Drawning_Monorail(int drawningMonorailWidth, int drawingMonorailHeight) : this() + { + _drawningMonorailWidth = drawningMonorailWidth; + _drawingMonorailHeight = drawingMonorailHeight; + } + + /// /// Установка границ поля /// @@ -141,7 +164,7 @@ public class DrawningMonorail /// true - перемещение выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntityMonorail == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (Entity_Monorail == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -149,33 +172,33 @@ public class DrawningMonorail { //влево case DirectionType.Left: - if (_startPosX.Value - EntityMonorail.Step > 0) + if (_startPosX.Value - Entity_Monorail.Step > 0) { - _startPosX -= (int)EntityMonorail.Step; + _startPosX -= (int)Entity_Monorail.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntityMonorail.Step > 0) + if (_startPosY.Value - Entity_Monorail.Step > 0) { - _startPosY -= (int)EntityMonorail.Step; + _startPosY -= (int)Entity_Monorail.Step; } return true; // вправо case DirectionType.Right: // TODO прописать логику сдвига в право - if (_startPosX.Value + EntityMonorail.Step + _drawningMonorailWidth < _pictureWidth) + if (_startPosX.Value + Entity_Monorail.Step + _drawningMonorailWidth < _pictureWidth) { - _startPosX += (int)EntityMonorail.Step; + _startPosX += (int)Entity_Monorail.Step; } return true; //вниз case DirectionType.Down: //TODO прописать логику сдвига в вниз - if (_startPosY.Value + EntityMonorail.Step + _drawingMonorailHeight < _pictureHeight) + if (_startPosY.Value + Entity_Monorail.Step + _drawingMonorailHeight < _pictureHeight) { - _startPosY += (int)EntityMonorail.Step; + _startPosY += (int)Entity_Monorail.Step; } return true; default: @@ -189,43 +212,20 @@ public class DrawningMonorail /// /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityMonorail == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (Entity_Monorail == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityMonorail.AdditionalColor); - //магнитная рельса - if (EntityMonorail.MagneticRail) - { - g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 40, 90, 3); - } - - //вторая часть монорельса - - if (EntityMonorail.SecondCabin) - { - Point[] points_second_cabin = { - new Point(_startPosX.Value + 85, _startPosY.Value), - new Point(_startPosX.Value + 95, _startPosY.Value + 15), - new Point(_startPosX.Value + 95, _startPosY.Value + 30), - new Point(_startPosX.Value + 85, _startPosY.Value + 30), - new Point(_startPosX.Value + 85, _startPosY.Value + 15), - new Point(_startPosX.Value + 95, _startPosY.Value + 15) - - }; - g.DrawPolygon(pen, points_second_cabin); - g.FillPolygon(additionalBrush, points_second_cabin); - - } + //границы Монорельса - Brush br = new SolidBrush(EntityMonorail.BodyColor); + Brush br = new SolidBrush(Entity_Monorail.BodyColor); g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 15, 80, 15); Point[] points1 = { new Point(_startPosX.Value + 15, _startPosY.Value), @@ -244,7 +244,7 @@ public class DrawningMonorail new Point(_startPosX.Value + 84, _startPosY.Value + 1), new Point(_startPosX.Value + 84, _startPosY.Value + 14), new Point(_startPosX.Value + 6, _startPosY.Value + 14) - }; + }; g.FillPolygon(br, points2); @@ -266,7 +266,7 @@ public class DrawningMonorail //1-ый держатель Point[] points_cart1 = { - new Point(_startPosX.Value, _startPosY.Value + 35), + new Point(_startPosX.Value + 5, _startPosY.Value + 30), new Point(_startPosX.Value + 15, _startPosY.Value + 35), new Point(_startPosX.Value + 15, _startPosY.Value + 30), new Point(_startPosX.Value + 5, _startPosY.Value + 30) @@ -330,4 +330,4 @@ public class DrawningMonorail } -} \ No newline at end of file +} diff --git a/Monorail/Monorail/EntityMonorail.cs b/Monorail/Monorail/Entities/EntityMonorail.cs similarity index 69% rename from Monorail/Monorail/EntityMonorail.cs rename to Monorail/Monorail/Entities/EntityMonorail.cs index 1933d50..965f544 100644 --- a/Monorail/Monorail/EntityMonorail.cs +++ b/Monorail/Monorail/Entities/EntityMonorail.cs @@ -1,21 +1,9 @@ -namespace monorail; +namespace Monorail.Entities; /// /// Класс-сущность "Монорельс" /// -public class EntityMonorail +public class EntityMonorail : Entity_Monorail { - /// - /// скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// основной цвет - /// - public Color BodyColor { get; private set; } /// /// дополнительный цвет /// @@ -30,11 +18,7 @@ public class EntityMonorail public bool SecondCabin { get; private set; } /// - /// шаг - /// - public double Step => Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса монорельса + /// Конструктор /// /// Скорость /// Вес @@ -42,7 +26,8 @@ public class EntityMonorail /// Дополнительный цвет /// признак наличие рельса /// признак вторая кабинка - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) + + public EntityMonorail(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) : base(speed, weight, bodyColor) { Speed = speed; Weight = weight; @@ -50,5 +35,13 @@ public class EntityMonorail AdditionalColor = additionalColor; MagneticRail = magneticRail; SecondCabin = secondCabin; + + } + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool magneticRail, bool secondCabin) + { + AdditionalColor = additionalColor; + MagneticRail = magneticRail; + SecondCabin = secondCabin; } } \ No newline at end of file diff --git a/Monorail/Monorail/Entities/Entity_Monorail.cs b/Monorail/Monorail/Entities/Entity_Monorail.cs new file mode 100644 index 0000000..2df55f8 --- /dev/null +++ b/Monorail/Monorail/Entities/Entity_Monorail.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail.Entities; + +/// +/// Класс-сущносить "Монорельс" +/// +public class Entity_Monorail +{ + /// + /// скорость + /// + public int Speed { get; set; } + /// + /// Вес + /// + public double Weight { get; set; } + /// + /// основной цвет + /// + public Color BodyColor { get; set; } + + /// + /// шаг + /// + public double Step => Speed * 100 / Weight; + + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес + /// Основной цвет + + public Entity_Monorail(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + + } + +} diff --git a/Monorail/Monorail/FormMonorail.Designer.cs b/Monorail/Monorail/FormMonorail.Designer.cs index 70f1931..a858c62 100644 --- a/Monorail/Monorail/FormMonorail.Designer.cs +++ b/Monorail/Monorail/FormMonorail.Designer.cs @@ -34,6 +34,7 @@ buttonDown = new Button(); buttonRight = new Button(); buttonUp = new Button(); + buttonCreateMonorail = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBox1Monorail).BeginInit(); SuspendLayout(); // @@ -51,9 +52,9 @@ buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Location = new Point(10, 358); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); + buttonCreate.Size = new Size(221, 23); buttonCreate.TabIndex = 1; - buttonCreate.Text = "создать"; + buttonCreate.Text = "создать монорельс"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += buttonCreate_Click; // @@ -105,11 +106,22 @@ buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += buttonMove_Click; // + // buttonCreateMonorail + // + buttonCreateMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateMonorail.Location = new Point(247, 358); + buttonCreateMonorail.Name = "buttonCreateMonorail"; + buttonCreateMonorail.Size = new Size(221, 23); + buttonCreateMonorail.TabIndex = 6; + buttonCreateMonorail.Text = "создать monorail"; + buttonCreateMonorail.UseVisualStyleBackColor = true; + // // FormMonorail // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(755, 393); + Controls.Add(buttonCreateMonorail); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -130,5 +142,6 @@ private Button buttonDown; private Button buttonRight; private Button buttonUp; + private Button buttonCreateMonorail; } } \ No newline at end of file diff --git a/Monorail/Monorail/FormMonorail.cs b/Monorail/Monorail/FormMonorail.cs index aec1d9e..367116b 100644 --- a/Monorail/Monorail/FormMonorail.cs +++ b/Monorail/Monorail/FormMonorail.cs @@ -1,4 +1,5 @@ -using monorail; +using Monorail.Drawnings; + namespace Monorail; /// /// Форма работы с объектом "Монорельс" @@ -8,7 +9,8 @@ public partial class FormMonorail : Form /// /// Поле-объект для прорисовки объекта /// - private DrawningMonorail? _drawningMonorail; + private Drawning_Monorail? _drawning_Monorail; + /// /// конструктор формы /// @@ -21,46 +23,70 @@ public partial class FormMonorail : Form /// private void Draw() { - if (_drawningMonorail == null) + if (_drawning_Monorail == null) { return; } Bitmap bmp = new(pictureBox1Monorail.Width, pictureBox1Monorail.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningMonorail.DrawTransport(gr); + _drawning_Monorail.DrawTransport(gr); pictureBox1Monorail.Image = bmp; } + private void FormMonorail_Load(object sender, EventArgs e) { } + + + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(Drawning_Monorail): + _drawning_Monorail = new Drawning_Monorail(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(DrawningMonorail): + _drawning_Monorail = new DrawningMonorail(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; + } + + _drawning_Monorail.SetPictureSize(pictureBox1Monorail.Width, pictureBox1Monorail.Height); + _drawning_Monorail.SetPosition(random.Next(10, 100), random.Next(10, 100)); + //_strategy = null; + //comboBoxStrategy.Enabled = true; + Draw(); + } + + + /// - /// Обработка нажатия кнопки "Создать" + /// Обработка нажатия кнопки "Создать Монорельс" /// /// /// - private void buttonCreate_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningMonorail = new DrawningMonorail(); - _drawningMonorail.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))); - _drawningMonorail.SetPictureSize(pictureBox1Monorail.Width, pictureBox1Monorail.Height); - _drawningMonorail.SetPosition(random.Next(10, 100), random.Next(10, 100)); + private void buttonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMonorail)); + + + /// + /// Обработка нажатия кнопки "Создать _Монорельс" + /// + /// + /// + private void ButtonCreateMonorail_Click(object sender, EventArgs e) => CreateObject(nameof(Drawning_Monorail)); - Bitmap bmp = new(pictureBox1Monorail.Width, pictureBox1Monorail.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningMonorail.DrawTransport(gr); - pictureBox1Monorail.Image = bmp; - Draw(); - } /// /// Перемещение объекта по форме (нажатие кнопок навигации) @@ -69,7 +95,7 @@ public partial class FormMonorail : Form /// private void buttonMove_Click(object sender, EventArgs e) { - if (_drawningMonorail == null) + if (_drawning_Monorail == null) { return; } @@ -79,16 +105,16 @@ public partial class FormMonorail : Form switch (name) { case "buttonUp": - result = _drawningMonorail.MoveTransport(DirectionType.Up); + result = _drawning_Monorail.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawningMonorail.MoveTransport(DirectionType.Down); + result = _drawning_Monorail.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawningMonorail.MoveTransport(DirectionType.Left); + result = _drawning_Monorail.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawningMonorail.MoveTransport(DirectionType.Right); + result = _drawning_Monorail.MoveTransport(DirectionType.Right); break; } if (result) @@ -97,4 +123,6 @@ public partial class FormMonorail : Form } } + + }