From fdeb174e9033eee03b91858dd35234d2d0b63883 Mon Sep 17 00:00:00 2001 From: ShuryginDima Date: Sun, 10 Mar 2024 16:04:36 +0300 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 --- .../{ => Drawnings}/DirectionType.cs | 2 +- .../DrawningPlane.cs} | 113 +++++++++--------- .../Drawnings/DrawningSeaplane.cs | 55 +++++++++ .../ProjectSeaplane/Entities/EntityPlane.cs | 49 ++++++++ .../{ => Entities}/EntitySeaplane.cs | 30 +---- .../ProjectSeaplane/FormSeaplane.Designer.cs | 18 ++- .../ProjectSeaplane/FormSeaplane.cs | 77 +++++++----- 7 files changed, 228 insertions(+), 116 deletions(-) rename ProjectSeaplane/ProjectSeaplane/{ => Drawnings}/DirectionType.cs (90%) rename ProjectSeaplane/ProjectSeaplane/{DrawningSeaplane.cs => Drawnings/DrawningPlane.cs} (70%) create mode 100644 ProjectSeaplane/ProjectSeaplane/Drawnings/DrawningSeaplane.cs create mode 100644 ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs rename ProjectSeaplane/ProjectSeaplane/{ => Entities}/EntitySeaplane.cs (66%) diff --git a/ProjectSeaplane/ProjectSeaplane/DirectionType.cs b/ProjectSeaplane/ProjectSeaplane/Drawnings/DirectionType.cs similarity index 90% rename from ProjectSeaplane/ProjectSeaplane/DirectionType.cs rename to ProjectSeaplane/ProjectSeaplane/Drawnings/DirectionType.cs index ef50542..274b265 100644 --- a/ProjectSeaplane/ProjectSeaplane/DirectionType.cs +++ b/ProjectSeaplane/ProjectSeaplane/Drawnings/DirectionType.cs @@ -1,4 +1,4 @@ -namespace ProjectSeaplane; +namespace ProjectSeaplane.Drawnings; /// /// Направление перемещения /// diff --git a/ProjectSeaplane/ProjectSeaplane/DrawningSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawningPlane.cs similarity index 70% rename from ProjectSeaplane/ProjectSeaplane/DrawningSeaplane.cs rename to ProjectSeaplane/ProjectSeaplane/Drawnings/DrawningPlane.cs index fddee39..850f600 100644 --- a/ProjectSeaplane/ProjectSeaplane/DrawningSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawningPlane.cs @@ -1,13 +1,18 @@ -namespace ProjectSeaplane; -/// -/// Класс, отвечающий за прорисовку и перемещение объекта-сущности -/// -public class DrawningSeaplane +using ProjectSeaplane.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Drawnings; + +public class DrawningPlane { /// /// Класс-сущность /// - public EntitySeaplane? EntitySeaplane { get; private set; } + public EntityPlane? EntityPlane { get; protected set; } /// /// Ширина окна @@ -22,42 +27,56 @@ public class DrawningSeaplane /// /// Левая координата прорисовки гидросамолёта /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя кооридната прорисовки гидросамолёта /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки гидросамолёта /// - private readonly int _drawningSeaplaneWidth = 130; + private readonly int _drawningPlaneWidth = 130; /// /// Высота прорисовки гидросамолёта /// - private readonly int _drawningSeaplaneHeight = 50; + private readonly int _drawningPlaneHeight = 45; /// - /// Инициализация свойств + /// Пустой конструктор /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия поплавков - /// Признак наличия лодки - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool boat) + private DrawningPlane() { - EntitySeaplane = new EntitySeaplane(); - EntitySeaplane.Init(speed, weight, bodyColor, additionalColor, floats, boat); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + public DrawningPlane(int speed, double weight, Color bodyColor) : this() + { + EntityPlane = new EntityPlane(speed, weight, bodyColor); + } + + /// + /// Конструктор для наследников + /// + /// Ширина прорисовки гидросамолёта + /// Высота прорисовки гидросамолёта + protected DrawningPlane(int _drawningPlaneWidth, int _drawningPlaneHeight) : this() + { + _drawningPlaneWidth = drawningPlaneWidth; + _pictureHeight = drawningPlaneHeight; + } + /// /// Установка границ поля /// @@ -66,7 +85,7 @@ public class DrawningSeaplane /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { - + if (height < _drawningSeaplaneHeight || width < _drawningSeaplaneWidth) { @@ -121,7 +140,7 @@ public class DrawningSeaplane /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -130,31 +149,31 @@ public class DrawningSeaplane { //влево case DirectionType.Left: - if (_startPosX.Value - EntitySeaplane.Step > 0) + if (_startPosX.Value - EntityPlane.Step > 0) { - _startPosX -= (int)EntitySeaplane.Step; + _startPosX -= (int)EntityPlane.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntitySeaplane.Step > 0) + if (_startPosY.Value - EntityPlane.Step > 0) { - _startPosY -= (int)EntitySeaplane.Step; + _startPosY -= (int)EntityPlane.Step; } return true; // вправо case DirectionType.Right: - if (_startPosX.Value + _drawningSeaplaneWidth + EntitySeaplane.Step < _pictureWidth) + if (_startPosX.Value + _drawningSeaplaneWidth + EntityPlane.Step < _pictureWidth) { - _startPosX += (int)EntitySeaplane.Step; + _startPosX += (int)EntityPlane.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + _drawningSeaplaneHeight + EntitySeaplane.Step < _pictureHeight) + if (_startPosY.Value + _drawningSeaplaneHeight + EntityPlane.Step < _pictureHeight) { - _startPosY += (int)EntitySeaplane.Step; + _startPosY += (int)EntityPlane.Step; } return true; default: @@ -165,19 +184,16 @@ public class DrawningSeaplane /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black, 2); - Brush additionalBrush = new SolidBrush(EntitySeaplane.AdditionalColor); - - //Отрисовка основных частей самолёта - Brush br = new SolidBrush(EntitySeaplane.BodyColor); + Brush br = new SolidBrush(EntityPlane.BodyColor); g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 20, 20, 20); g.FillEllipse(br, _startPosX.Value, _startPosY.Value + 20, 20, 20); Point point1 = new Point(_startPosX.Value + 10, _startPosY.Value); @@ -192,7 +208,7 @@ public class DrawningSeaplane g.DrawLine(pen, _startPosX.Value + 10, _startPosY.Value + 20, _startPosX.Value + 40, _startPosY.Value + 20); g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 20, _startPosX.Value + 100, _startPosY.Value + 40); g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 30, _startPosX.Value + 130, _startPosY.Value + 30); - + //Крылья g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 20, 25, 5); g.FillEllipse(br, _startPosX.Value, _startPosY.Value + 20, 25, 5); @@ -212,26 +228,5 @@ public class DrawningSeaplane g.FillEllipse(br, _startPosX.Value + 35, _startPosY.Value + 45, 5, 5); g.FillEllipse(br, _startPosX.Value + 42, _startPosY.Value + 45, 5, 5); g.FillEllipse(br, _startPosX.Value + 87, _startPosY.Value + 45, 5, 5); - - - //Надувнвя лодка - if (EntitySeaplane.Boat) - { - g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10); - - } - - //Поплавки - if (EntitySeaplane.Floats) - { - g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15); - g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15); - g.DrawRectangle(pen, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15); - g.FillRectangle(additionalBrush, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15); - g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10); - - } } -} +} \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawningSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawningSeaplane.cs new file mode 100644 index 0000000..7a32c92 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Drawnings/DrawningSeaplane.cs @@ -0,0 +1,55 @@ +using ProjectSeaplane.Entities; +using System.Drawing; + +namespace ProjectSeaplane.Drawnings; +/// +/// Класс, отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawningSeaplane : DrawningPlane +{ + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия поплавков + /// Признак наличия лодки + public DrawningSeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool boat) : base(130, 50) + { + EntityPlane = new EntitySeaplane(speed, weight, bodyColor, additionalColor, floats, boat); + } + + public override void DrawTransport(Graphics g) + { + if (EntityPlane == null || EntityPlane is not EntitySeaplane seaplane || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black, 2); + Brush additionalBrush = new SolidBrush(seaplane.AdditionalColor); + + base.DrawTransport(g); + + //Надувнвя лодка + if (seaplane.Boat) + { + g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10); + + } + + //Поплавки + if (seaplane.Floats) + { + g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15); + g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15); + g.DrawRectangle(pen, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15); + g.FillRectangle(additionalBrush, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15); + g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10); + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs b/ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs new file mode 100644 index 0000000..3e3dac9 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Entities; + +/// +/// Класс-сущность "Самолёт" +/// + +public class EntityPlane +{ + /// + /// Скорость + /// + public int Speed { get; private set; } + + /// + /// Вес + /// + public double Weight { get; private set; } + + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + + /// + /// Шаг перемещения гидросамолёта + /// + public double Step => Speed * 100 / Weight; + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + public EntityPlane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs similarity index 66% rename from ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs rename to ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs index 625ccfc..113ec5b 100644 --- a/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs @@ -1,24 +1,9 @@ -namespace ProjectSeaplane; +namespace ProjectSeaplane.Entities; /// /// Класс-сущность "Гидросамолёт" /// -public class EntitySeaplane +public class EntitySeaplane : EntityPlane { - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } - - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// /// Дополнительный цвет (для опциональных элементов) /// @@ -35,11 +20,6 @@ public class EntitySeaplane /// public bool Boat { get; private set; } - /// - /// Шаг перемещения гидросамолёта - /// - public double Step => Speed * 100 / Weight; - /// /// Инициализация полей объекта-класса гидросамолёта /// @@ -49,12 +29,8 @@ public class EntitySeaplane /// Дополнительный цвет /// Признак наличия поплавков /// Признак наличия лодки - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool floats, bool boat) + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool boat) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Floats = floats; Boat = boat; diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs index fde1660..59c5859 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs @@ -34,6 +34,7 @@ buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); + buttonCreatePlane = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit(); SuspendLayout(); // @@ -51,9 +52,9 @@ buttonCreateSeaplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateSeaplane.Location = new Point(12, 465); buttonCreateSeaplane.Name = "buttonCreateSeaplane"; - buttonCreateSeaplane.Size = new Size(75, 23); + buttonCreateSeaplane.Size = new Size(196, 23); buttonCreateSeaplane.TabIndex = 1; - buttonCreateSeaplane.Text = "Создать"; + buttonCreateSeaplane.Text = "Создать гидросамолёт"; buttonCreateSeaplane.UseVisualStyleBackColor = true; buttonCreateSeaplane.Click += ButtonCreateSeaplane_Click; // @@ -105,11 +106,23 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // + // buttonCreatePlane + // + buttonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreatePlane.Location = new Point(214, 465); + buttonCreatePlane.Name = "buttonCreatePlane"; + buttonCreatePlane.Size = new Size(196, 23); + buttonCreatePlane.TabIndex = 6; + buttonCreatePlane.Text = "Создать самолёт"; + buttonCreatePlane.UseVisualStyleBackColor = true; + buttonCreatePlane.Click += buttonCreatePlane_Click; + // // FormSeaplane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(900, 500); + Controls.Add(buttonCreatePlane); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); @@ -130,5 +143,6 @@ private Button buttonUp; private Button buttonDown; private Button buttonRight; + private Button buttonCreatePlane; } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs index 8781929..cd79f6d 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs @@ -1,4 +1,6 @@ -namespace ProjectSeaplane; +using ProjectSeaplane.Drawnings; + +namespace ProjectSeaplane; /// /// Форма работы с объектом "Спортивный автомобиль" /// @@ -7,7 +9,7 @@ public partial class FormSeaplane : Form /// /// Поле-объект для прорисовки объекта /// - private DrawningSeaplane? _drawningSeaplane; + private DrawningPlane? _drawningPlane; /// /// Конструктор формы /// @@ -20,33 +22,58 @@ public partial class FormSeaplane : Form /// private void Draw() { - if (_drawningSeaplane == null) + if (_drawningPlane == null) { return; } Bitmap bmp = new(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningSeaplane.DrawTransport(gr); + _drawningPlane.DrawTransport(gr); pictureBoxSeaplane.Image = bmp; } + /// - /// Обработка нажатия кнопки "Создать" + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawningPlane): + _drawningPlane = new DrawningPlane(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(DrawningSeaplane): + _drawningPlane = new DrawningSeaplane(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; + } + _drawningPlane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + _drawningPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + + /// + /// Обработка нажатия кнопки "Создать гидросамолёт" /// /// /// - private void ButtonCreateSeaplane_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningSeaplane = new DrawningSeaplane(); - - _drawningSeaplane.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))); - _drawningSeaplane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); - _drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); - } + private void ButtonCreateSeaplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSeaplane)); + + /// + /// Обработка нажатия кнопки "Создать самолёт" + /// + /// + /// + private void buttonCreatePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningPlane)); + /// /// Перемещение объекта по форме (нажатие кнопок навигации) /// @@ -54,7 +81,7 @@ public partial class FormSeaplane : Form /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningSeaplane == null) + if (_drawningPlane == null) { return; } @@ -63,20 +90,16 @@ public partial class FormSeaplane : Form switch (name) { case "buttonUp": - result = - _drawningSeaplane.MoveTransport(DirectionType.Up); + result = _drawningPlane.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = - _drawningSeaplane.MoveTransport(DirectionType.Down); + result = _drawningPlane.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = - _drawningSeaplane.MoveTransport(DirectionType.Left); + result = _drawningPlane.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = - _drawningSeaplane.MoveTransport(DirectionType.Right); + result = _drawningPlane.MoveTransport(DirectionType.Right); break; } if (result)