From 45691d205ff21731c496514d66b31bd6d1be3ffb Mon Sep 17 00:00:00 2001 From: Almaz <79022113685@mail.ru> Date: Tue, 9 Apr 2024 11:53:13 +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 --- .../{ => Drawings}/DirectionType.cs | 10 +-- .../DrawningCruiser.cs} | 83 ++++++++++--------- .../Drawings/DrawningMilitaryCruiser.cs | 68 +++++++++++++++ .../ProjectCruiser/Entities/EntityCruiser.cs | 46 ++++++++++ .../EntityMilitaryCruiser.cs} | 30 ++----- .../ProjectCruiser/FormCruiser.Designer.cs | 38 ++++++--- ProjectCruiser/ProjectCruiser/FormCruiser.cs | 53 +++++++++--- 7 files changed, 236 insertions(+), 92 deletions(-) rename ProjectCruiser/ProjectCruiser/{ => Drawings}/DirectionType.cs (85%) rename ProjectCruiser/ProjectCruiser/{DrawingCruiser.cs => Drawings/DrawningCruiser.cs} (58%) create mode 100644 ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs create mode 100644 ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs rename ProjectCruiser/ProjectCruiser/{EntityCruiser.cs => Entities/EntityMilitaryCruiser.cs} (66%) diff --git a/ProjectCruiser/ProjectCruiser/DirectionType.cs b/ProjectCruiser/ProjectCruiser/Drawings/DirectionType.cs similarity index 85% rename from ProjectCruiser/ProjectCruiser/DirectionType.cs rename to ProjectCruiser/ProjectCruiser/Drawings/DirectionType.cs index 4cdd1e9..3ea0799 100644 --- a/ProjectCruiser/ProjectCruiser/DirectionType.cs +++ b/ProjectCruiser/ProjectCruiser/Drawings/DirectionType.cs @@ -4,17 +4,17 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectCruiser; +namespace ProjectCruiser.Drawings; /// /// Направление перемещения /// - public enum DirectionType - { +public enum DirectionType +{ /// /// Вверх /// - Up = 1, + Up = 1, /// /// Вниз /// @@ -27,5 +27,5 @@ namespace ProjectCruiser; /// Вправо /// Right = 4 - } +} diff --git a/ProjectCruiser/ProjectCruiser/DrawingCruiser.cs b/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs similarity index 58% rename from ProjectCruiser/ProjectCruiser/DrawingCruiser.cs rename to ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs index a1fb426..b953713 100644 --- a/ProjectCruiser/ProjectCruiser/DrawingCruiser.cs +++ b/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs @@ -3,15 +3,16 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectCruiser.Entities; -namespace ProjectCruiser; +namespace ProjectCruiser.Drawings; -public class DrawingCruiser +public class DrawningCruiser { /// /// Класс-сущность /// - public EntityCruiser? EntityCruiser { get; private set; } + public EntityCruiser? EntityCruiser { get; protected set; } /// /// Ширина окна /// @@ -23,41 +24,53 @@ public class DrawingCruiser /// /// Левая координата прорисовки крейсера /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя координата прорисовки крейсера /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки крейсера /// - private readonly int _drawningCruiserWidth = 110; + private readonly int _drawningCruiserWidth = 90; /// /// Высота прорисовки крейсера /// - private readonly int _drawingCruiserHeight = 60; + private readonly int _drawingCruiserHeight = 50; /// - /// Инициализация полей объекта класса крейсера + /// Пустой конструктор /// - /// Скорость - /// Вес крейсера - /// Скорость - /// Дополнительный цвет - /// Признак наличия обвеса - /// Признак наличия брони - /// Признак наличия оружия - public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon) - { - EntityCruiser = new EntityCruiser(); - EntityCruiser.Init(speed, weigth, bodyColor, additionalColor, bodyKit, armor, weapon); + private DrawningCruiser() { _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Констуктор + /// + /// Скорость + /// Вес крейсера + /// Скорость + public DrawningCruiser(int speed, double weigth, Color bodyColor): this() + { + EntityCruiser = new EntityCruiser(speed, weigth, bodyColor); + } + + /// + /// Констуктор для наследников + /// + /// Ширина прорисовки крейсера + /// Высота прорисовки крейсера + protected DrawningCruiser(int drawningCruiserWidth, int drawingCruiserHeight) : this() + { + _drawningCruiserWidth = drawningCruiserWidth; + _drawingCruiserHeight = drawingCruiserHeight; + } + public bool SetPictireSize(int width, int height) { _pictureWidth = width; @@ -115,7 +128,7 @@ public class DrawingCruiser } } - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue) { @@ -124,31 +137,21 @@ public class DrawingCruiser Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor); //Границы крейсера - g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20); - g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20); - g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20); - g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 35, 20, 20); - g.DrawRectangle(pen, _startPosX.Value + 9, _startPosY.Value + 15, 10, 30); - g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30); - g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70, 52); + g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value, 20, 20); + g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 30, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 30, 20, 20); + g.DrawRectangle(pen, _startPosX.Value + 1, _startPosY.Value + 10, 10, 30); + g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 10, 10, 30); + g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 1, 70, 52); //кузов крейсера Brush br = new SolidBrush(EntityCruiser.BodyColor); - g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10, 30); - g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30); - g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70, 50); - - // оружие крейсера - if (EntityCruiser.Weapon) - { - g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 23, 25, 15); - g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 23, 35, 15); - g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 23, 20, 15); - - } + g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 10, 10, 30); + g.FillRectangle(br, _startPosX.Value + 80, _startPosY.Value + 10, 10, 30); + g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value, 70, 50); } } diff --git a/ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs b/ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs new file mode 100644 index 0000000..b86968c --- /dev/null +++ b/ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectCruiser.Entities; + +namespace ProjectCruiser.Drawings; + +public class DrawningMilitaryCruiser: DrawningCruiser +{ + /// + /// Конструктор + /// + /// Скорость + /// Вес крейсера + /// Скорость + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия брони + /// Признак наличия оружия + public DrawningMilitaryCruiser(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon): base(110, 60) + { + EntityCruiser = new EntityMilitaryCruiser(speed, weigth, bodyColor, additionalColor, bodyKit, armor, weapon); + } + + public override void DrawTransport(Graphics g) + { + + if (EntityCruiser == null || EntityCruiser is not EntityMilitaryCruiser militaryCruiser || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(militaryCruiser.AdditionalColor); + + //Границы крейсера + g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 35, 20, 20); + g.DrawRectangle(pen, _startPosX.Value + 9, _startPosY.Value + 15, 10, 30); + g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30); + g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70, 52); + + //кузов крейсера + Brush br = new SolidBrush(militaryCruiser.BodyColor); + g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10, 30); + g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30); + g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70, 50); + + _startPosX += 10; + _startPosY += 5; + base.DrawTransport(g); + _startPosX -= 10; + _startPosY -= 5; + + // оружие крейсера + if (militaryCruiser.Weapon) + { + g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 23, 25, 15); + g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 23, 35, 15); + g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 23, 20, 15); + + } + } +} diff --git a/ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs b/ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs new file mode 100644 index 0000000..0f9bcb5 --- /dev/null +++ b/ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser.Entities; + +/// +/// Класс-сущность "Крейсер" Вариант 18 +/// +public class EntityCruiser +{ + /// + /// Скорость + /// + public int Speed { get; protected set; } + + /// + /// Вес + /// + public double Weigth { get; protected set; } + + /// + /// Основной цвет + /// + public Color BodyColor { get; protected set; } + + /// + /// Шаг перемещения крейсера + /// + public double Step => Speed * 100 / Weigth; + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес крейсера + /// Скорость + public EntityCruiser(int speed, double weigth, Color bodyColor) + { + Speed = speed; + Weigth = weigth; + BodyColor = bodyColor; + } +} diff --git a/ProjectCruiser/ProjectCruiser/EntityCruiser.cs b/ProjectCruiser/ProjectCruiser/Entities/EntityMilitaryCruiser.cs similarity index 66% rename from ProjectCruiser/ProjectCruiser/EntityCruiser.cs rename to ProjectCruiser/ProjectCruiser/Entities/EntityMilitaryCruiser.cs index 8fdcd5d..eb780c0 100644 --- a/ProjectCruiser/ProjectCruiser/EntityCruiser.cs +++ b/ProjectCruiser/ProjectCruiser/Entities/EntityMilitaryCruiser.cs @@ -1,25 +1,10 @@ -namespace ProjectCruiser +namespace ProjectCruiser.Entities { /// - /// Класс-сущность "Крейсер" Вариант 18 + /// Класс-сущность "Военный Крейсер" Вариант 18 /// - public class EntityCruiser - { - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weigth { get; private set; } - - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - + public class EntityMilitaryCruiser: EntityCruiser + { /// /// Дополнительный цвет (для опциональных элементов) /// @@ -40,11 +25,6 @@ /// public bool Weapon { get; private set; } - /// - /// Шаг перемещения крейсера - /// - public double Step => Speed * 100 / Weigth; - /// /// Инициализация полей объекта класса крейсера /// @@ -55,7 +35,7 @@ /// Признак наличия обвеса /// Признак наличия брони /// Признак наличия оружия - public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon) + public EntityMilitaryCruiser(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon): base(speed, weigth, bodyColor) { Speed = speed; Weigth = weigth; diff --git a/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs b/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs index 69e341a..ec630ad 100644 --- a/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs +++ b/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs @@ -29,11 +29,12 @@ private void InitializeComponent() { pictureBoxCruiser = new PictureBox(); - buttonCreateCruiser = new Button(); + buttonCreateMilitaryCruiser = new Button(); buttonDown = new Button(); buttonUp = new Button(); buttonRight = new Button(); buttonLeft = new Button(); + buttonCreateCruiser = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).BeginInit(); SuspendLayout(); // @@ -46,16 +47,16 @@ pictureBoxCruiser.TabIndex = 0; pictureBoxCruiser.TabStop = false; // - // buttonCreateCruiser + // buttonCreateMilitaryCruiser // - buttonCreateCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateCruiser.Location = new Point(12, 409); - buttonCreateCruiser.Name = "buttonCreateCruiser"; - buttonCreateCruiser.Size = new Size(94, 29); - buttonCreateCruiser.TabIndex = 1; - buttonCreateCruiser.Text = "Создать"; - buttonCreateCruiser.UseVisualStyleBackColor = true; - buttonCreateCruiser.Click += ButtonCreateCruiser_Click; + buttonCreateMilitaryCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateMilitaryCruiser.Location = new Point(12, 409); + buttonCreateMilitaryCruiser.Name = "buttonCreateMilitaryCruiser"; + buttonCreateMilitaryCruiser.Size = new Size(216, 29); + buttonCreateMilitaryCruiser.TabIndex = 1; + buttonCreateMilitaryCruiser.Text = "Создать военный крейсер"; + buttonCreateMilitaryCruiser.UseVisualStyleBackColor = true; + buttonCreateMilitaryCruiser.Click += ButtonCreateMilitaryCruiser_Click; // // buttonDown // @@ -105,16 +106,28 @@ buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; // + // buttonCreateCruiser + // + buttonCreateCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateCruiser.Location = new Point(234, 409); + buttonCreateCruiser.Name = "buttonCreateCruiser"; + buttonCreateCruiser.Size = new Size(179, 29); + buttonCreateCruiser.TabIndex = 6; + buttonCreateCruiser.Text = "Создать крейсер"; + buttonCreateCruiser.UseVisualStyleBackColor = true; + buttonCreateCruiser.Click += buttonCreateCruiser_Click; + // // FormCruiser // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonCreateCruiser); Controls.Add(buttonLeft); Controls.Add(buttonRight); Controls.Add(buttonUp); Controls.Add(buttonDown); - Controls.Add(buttonCreateCruiser); + Controls.Add(buttonCreateMilitaryCruiser); Controls.Add(pictureBoxCruiser); Name = "FormCruiser"; Text = "Крейсер"; @@ -125,10 +138,11 @@ #endregion private PictureBox pictureBoxCruiser; - private Button buttonCreateCruiser; + private Button buttonCreateMilitaryCruiser; private Button buttonDown; private Button buttonUp; private Button buttonRight; private Button buttonLeft; + private Button buttonCreateCruiser; } } \ No newline at end of file diff --git a/ProjectCruiser/ProjectCruiser/FormCruiser.cs b/ProjectCruiser/ProjectCruiser/FormCruiser.cs index b12c168..af4d3d6 100644 --- a/ProjectCruiser/ProjectCruiser/FormCruiser.cs +++ b/ProjectCruiser/ProjectCruiser/FormCruiser.cs @@ -1,9 +1,11 @@ -namespace ProjectCruiser +using ProjectCruiser.Drawings; + +namespace ProjectCruiser { public partial class FormCruiser : Form { - private DrawingCruiser? _drawingCruiser; + private DrawningCruiser? _drawingCruiser; public FormCruiser() { InitializeComponent(); @@ -22,12 +24,20 @@ pictureBoxCruiser.Image = bmp; } - private void ButtonCreateCruiser_Click(object sender, EventArgs e) + private void CreateObject(string type) { Random random = new(); - _drawingCruiser = new DrawingCruiser(); - _drawingCruiser.Init( - random.Next(100, 300), + switch (type) + { + case nameof(DrawningCruiser): + + _drawingCruiser = new DrawningCruiser(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(DrawningMilitaryCruiser): + + _drawingCruiser = new DrawningMilitaryCruiser(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)), @@ -35,24 +45,45 @@ Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + + default: + return; + } + _drawingCruiser.SetPictireSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height); _drawingCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } + /// + /// Обработка нажатия кнопки "Создать военный крейсер" + /// + /// + /// + private void ButtonCreateMilitaryCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMilitaryCruiser)); + + /// + /// Обработка нажатия кнопки "Создать крейсер" + /// + /// + /// + private void buttonCreateCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCruiser)); + private void ButtonMove_Click(object sender, EventArgs e) { - if(_drawingCruiser == null) + if (_drawingCruiser == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; bool result = false; - switch(name) { + switch (name) + { case "buttonUp": - result = _drawingCruiser.MoveTransport(DirectionType.Up); + result = _drawingCruiser.MoveTransport(DirectionType.Up); break; case "buttonDown": result = _drawingCruiser.MoveTransport(DirectionType.Down); @@ -65,9 +96,11 @@ break; } - if (result) { + if (result) + { Draw(); } } + } }