diff --git a/laba 0/laba 0/DirectionType.cs b/laba 0/laba 0/Drawnings/DirectionType.cs similarity index 92% rename from laba 0/laba 0/DirectionType.cs rename to laba 0/laba 0/Drawnings/DirectionType.cs index ae2e927..f2ff634 100644 --- a/laba 0/laba 0/DirectionType.cs +++ b/laba 0/laba 0/Drawnings/DirectionType.cs @@ -1,4 +1,4 @@ -namespace laba_0; +namespace laba_0.Drawning; /// /// направление перемещения diff --git a/laba 0/laba 0/DrawningBoat.cs b/laba 0/laba 0/Drawnings/DrawningB.cs similarity index 54% rename from laba 0/laba 0/DrawningBoat.cs rename to laba 0/laba 0/Drawnings/DrawningB.cs index 174e7c8..568863b 100644 --- a/laba 0/laba 0/DrawningBoat.cs +++ b/laba 0/laba 0/Drawnings/DrawningB.cs @@ -1,14 +1,18 @@ -namespace laba_0; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using laba_0.Entities; -/// -/// класс, отвечающий за прорисовку и перемещение объекта-сущности -/// -public class DrawningBoat +namespace laba_0.Drawning; + +public class DrawningB { /// /// класс-сущность /// - public EntityBoat? EntityBoat { get; private set; } + public EntityB? EntityB{ get; protected set; } /// /// ширина окна @@ -23,45 +27,56 @@ public class DrawningBoat /// /// левая координата прорисовки катера /// - private int? _startPosX; + protected int? _startPosX; /// /// верхняя координата прорисовки катера /// - private int? _startPosY; + protected int? _startPosY; /// /// ширина прорисовки катера /// - private readonly int _drawningBoatWidth = 115; + private readonly int _drawningBoatWidth = 90; /// /// высота прорисовки катера /// - private readonly int _drawningBoatHeight = 40; + private readonly int _drawningBoatHeight = 50; /// - /// Инициализация свойств + /// Пустой конструктор /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия кабины - /// Признак наличия двигателя - - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool kabina, bool dvigatel) + private DrawningB() { - EntityBoat = new EntityBoat(); - EntityBoat.Init(speed, weight, bodyColor, additionalColor, - kabina, dvigatel); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + public DrawningB(int speed, double weight, Color bodyColor) : this() + { + EntityB = new EntityB(speed, weight, bodyColor); + } + + /// + /// Конструктор для наследников + /// + /// ширина прорисовки катера + /// высота прорисовки катера + protected DrawningB(int drawningBoatWidth, int drawningBoatHeight) : this() + { + _drawningBoatWidth = drawningBoatWidth; + _drawningBoatHeight = drawningBoatHeight; + } + /// /// Установка границ поля /// @@ -74,9 +89,9 @@ public class DrawningBoat { _pictureWidth = width; _pictureHeight = height; - if(_startPosX.HasValue && _startPosY.HasValue) - { - if(_startPosX + _drawningBoatWidth > _pictureWidth) + if (_startPosX.HasValue && _startPosY.HasValue) + { + if (_startPosX + _drawningBoatWidth > _pictureWidth) { _startPosX = _pictureWidth - _drawningBoatWidth; } @@ -103,20 +118,20 @@ public class DrawningBoat return; } - if(x < 0) //если новая позиция левее левой границы формы + if (x < 0) //если новая позиция левее левой границы формы { - x = 0;//устанавливаем позицию по оси х равную 0 + x = 0; //устанавливаем позицию по оси х равную 0 } - else if(x + _drawningBoatWidth > _pictureWidth) //если новая позиция выходит за правую границу формы + else if (x + _drawningBoatWidth > _pictureWidth) //если новая позиция выходит за правую границу формы { x = _pictureWidth.Value - _drawningBoatWidth;//корректируем по оси х } - if(y < 0)//если новая позиция выше верхней границы формы + if (y < 0)//если новая позиция выше верхней границы формы { y = 0; } - else if(y + _drawningBoatHeight > _pictureHeight)//если новая позиция выходит за границу формы + else if (y + _drawningBoatHeight > _pictureHeight) //если новая позиция выходит за границу формы { y = _pictureHeight.Value - _drawningBoatHeight;//корректируем по оси у } @@ -132,7 +147,7 @@ public class DrawningBoat /// true - перемещение выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntityBoat == null || !_startPosX.HasValue || + if (EntityB == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; @@ -141,33 +156,33 @@ public class DrawningBoat { //влево case DirectionType.Left: - if (_startPosX.Value - EntityBoat.Step > 0) + if (_startPosX.Value - EntityB.Step > 0) { - _startPosX -= (int)EntityBoat.Step; + _startPosX -= (int)EntityB.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntityBoat.Step > 0) + if (_startPosY.Value - EntityB.Step > 0) { - _startPosY -= (int)EntityBoat.Step; + _startPosY -= (int)EntityB.Step; } return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityBoat.Step + _drawningBoatWidth < _pictureWidth) + if (_startPosX.Value + EntityB.Step + _drawningBoatWidth < _pictureWidth) { - _startPosX += (int)EntityBoat.Step; + _startPosX += (int)EntityB.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityBoat.Step + _drawningBoatHeight < _pictureHeight) + if (_startPosY.Value + EntityB.Step + _drawningBoatHeight < _pictureHeight) { - _startPosY += (int)EntityBoat.Step; + _startPosY += (int)EntityB.Step; } return true; @@ -180,52 +195,24 @@ public class DrawningBoat /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityBoat == null || !_startPosX.HasValue || - !_startPosY.HasValue) + if (EntityB == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityBoat.AdditionalColor); - Brush bodyBrush = new SolidBrush(EntityBoat.BodyColor); //корпус - g.FillRectangle(bodyBrush, _startPosX.Value + 5, _startPosY.Value , 75, 40); g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value, 75, 40); - //лестница - g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value + 10, 20, 20); g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 10, 20, 20); - - //двигатель - if (EntityBoat.dvigatelBody) - { - g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 15, 5, 10); - g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 15, 5, 10); - } - //корма Point point1 = new Point(_startPosX.Value + 80, _startPosY.Value); - Point point2 = new Point(_startPosX.Value + 80, _startPosY.Value+40); + Point point2 = new Point(_startPosX.Value + 80, _startPosY.Value + 40); Point point3 = new Point(_startPosX.Value + 120, _startPosY.Value + 20); Point[] curvePointsKorma = { point1, point2, point3 }; g.DrawPolygon(pen, curvePointsKorma); - g.FillPolygon(bodyBrush, curvePointsKorma); - - //кабина - if (EntityBoat.kabinaBody) - { - Point point4 = new Point(_startPosX.Value + 50, _startPosY.Value + 10); - Point point5 = new Point(_startPosX.Value + 55, _startPosY.Value + 15); - Point point6 = new Point(_startPosX.Value + 55, _startPosY.Value + 25); - Point point7 = new Point(_startPosX.Value + 50, _startPosY.Value + 30); - Point point8 = new Point(_startPosX.Value + 50, _startPosY.Value + 10); - Point[] curvePointsKabina = { point4, point5, point6, point7, point8 }; - g.FillPolygon(additionalBrush, curvePointsKabina); - g.DrawPolygon(pen, curvePointsKabina); - } } } diff --git a/laba 0/laba 0/Drawnings/DrawningBoat.cs b/laba 0/laba 0/Drawnings/DrawningBoat.cs new file mode 100644 index 0000000..ee69378 --- /dev/null +++ b/laba 0/laba 0/Drawnings/DrawningBoat.cs @@ -0,0 +1,60 @@ +using laba_0.Entities; + +namespace laba_0.Drawning; + +/// +/// класс, отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawningBoat : DrawningB +{ + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия кабины + /// Признак наличия двигателя + + public DrawningBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel) : base(110, 60) + { + EntityB = new EntityBoat(speed, weight, bodyColor, additionalColor, kabina, dvigatel); + } + + /// + /// Прорисовка объекта + /// + /// + public override void DrawTransport(Graphics g) + { + Pen pen = new(Color.Black); + + if (EntityB == null || EntityB is not EntityBoat Boat || !_startPosX.HasValue || !_startPosY.HasValue) + return; + base.DrawTransport(g); + + Brush additionalBrush = new SolidBrush(Boat.AdditionalColor); + + //кабина + if (Boat.kabinaBody) + { + Point point4 = new Point(_startPosX.Value + 55, _startPosY.Value + 10); + Point point5 = new Point(_startPosX.Value + 60, _startPosY.Value + 15); + Point point6 = new Point(_startPosX.Value + 60, _startPosY.Value + 25); + Point point7 = new Point(_startPosX.Value + 55, _startPosY.Value + 30); + Point point8 = new Point(_startPosX.Value + 55, _startPosY.Value + 10); + Point[] curvePointsKabina = { point4, point5, point6, point7, point8 }; + g.FillPolygon(additionalBrush, curvePointsKabina); + g.DrawPolygon(pen, curvePointsKabina); + } + //двигатель + if (Boat.dvigatelBody) + { + g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 15, 5, 10); + g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 15, 5, 10); + } + + base.DrawTransport(g); + } +} diff --git a/laba 0/laba 0/Entities/EntityB.cs b/laba 0/laba 0/Entities/EntityB.cs new file mode 100644 index 0000000..441580e --- /dev/null +++ b/laba 0/laba 0/Entities/EntityB.cs @@ -0,0 +1,41 @@ +namespace laba_0.Entities; + +/// +/// Класс-сущность "Катер" +/// +public class EntityB +{ + /// + /// скорость + /// + 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 EntityB(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/laba 0/laba 0/EntityBoat.cs b/laba 0/laba 0/Entities/EntityBoat.cs similarity index 60% rename from laba 0/laba 0/EntityBoat.cs rename to laba 0/laba 0/Entities/EntityBoat.cs index 11d2779..12d10ba 100644 --- a/laba 0/laba 0/EntityBoat.cs +++ b/laba 0/laba 0/Entities/EntityBoat.cs @@ -1,22 +1,10 @@ -namespace laba_0; +namespace laba_0.Entities; -public class EntityBoat +/// +/// Класс-сущность "Катер" +/// +public class EntityBoat : EntityB { - /// - /// скорость - /// - public int Speed { get; private set; } - - /// - /// вес - /// - public double Weight { get; private set; } - - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// /// Дополнительный цвет (для опциональных элементов) /// @@ -33,12 +21,7 @@ public class EntityBoat public bool dvigatelBody { get; private set; } /// - /// Шаг перемещения катера - /// - public double Step => Speed * 100 / Weight; - - /// - /// + /// Инициализация полей объекта-класса катера /// /// Скорость /// Вес @@ -47,11 +30,8 @@ public class EntityBoat /// Признак наличия кабины /// Признак наличия двигателя - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel) + public EntityBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; kabinaBody = kabina; dvigatelBody = dvigatel; diff --git a/laba 0/laba 0/FormBoat.Designer.cs b/laba 0/laba 0/FormBoat.Designer.cs index 26102a7..5554186 100644 --- a/laba 0/laba 0/FormBoat.Designer.cs +++ b/laba 0/laba 0/FormBoat.Designer.cs @@ -30,11 +30,12 @@ partial class FormBoat { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormBoat)); pictureBoxBoat = new PictureBox(); - buttonCreate = new Button(); + buttonCreateBoat = new Button(); buttonRight = new Button(); buttonDown = new Button(); buttonUp = new Button(); buttonLeft = new Button(); + ButtonCreateB = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxBoat).BeginInit(); SuspendLayout(); // @@ -50,16 +51,16 @@ partial class FormBoat pictureBoxBoat.TabStop = false; pictureBoxBoat.Click += ButtonMove_Click; // - // buttonCreate + // buttonCreateBoat // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 327); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать "; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += ButtonCreateBoat_Click; + buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBoat.Location = new Point(12, 327); + buttonCreateBoat.Name = "buttonCreateBoat"; + buttonCreateBoat.Size = new Size(153, 23); + buttonCreateBoat.TabIndex = 1; + buttonCreateBoat.Text = "Создать катер"; + buttonCreateBoat.UseVisualStyleBackColor = true; + buttonCreateBoat.Click += ButtonCreateBoat_Click; // // buttonRight // @@ -109,16 +110,28 @@ partial class FormBoat buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; // + // ButtonCreateB + // + ButtonCreateB.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreateB.Location = new Point(171, 327); + ButtonCreateB.Name = "ButtonCreateB"; + ButtonCreateB.Size = new Size(147, 23); + ButtonCreateB.TabIndex = 6; + ButtonCreateB.Text = "Создать простой катер"; + ButtonCreateB.UseVisualStyleBackColor = true; + ButtonCreateB.Click += ButtonCreateB_Click; + // // FormBoat // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(640, 359); + Controls.Add(ButtonCreateB); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonUp); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateBoat); Controls.Add(pictureBoxBoat); Name = "FormBoat"; Text = "FormBoat"; @@ -130,9 +143,10 @@ partial class FormBoat #endregion private PictureBox pictureBoxBoat; - private Button buttonCreate; + private Button buttonCreateBoat; private Button buttonRight; private Button buttonDown; private Button buttonUp; private Button buttonLeft; + private Button ButtonCreateB; } \ No newline at end of file diff --git a/laba 0/laba 0/FormBoat.cs b/laba 0/laba 0/FormBoat.cs index 07add3d..a224d88 100644 --- a/laba 0/laba 0/FormBoat.cs +++ b/laba 0/laba 0/FormBoat.cs @@ -7,12 +7,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using laba_0.Drawning; namespace laba_0; public partial class FormBoat : Form { - private DrawningBoat? _drawningBoat; + private DrawningB? _drawningB; public FormBoat() { @@ -21,34 +22,73 @@ public partial class FormBoat : Form private void Draw() { - if (_drawningBoat == null) + if (_drawningB == null) { return; } Bitmap bmp = new(pictureBoxBoat.Width, pictureBoxBoat.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningBoat.DrawTransport(gr); + _drawningB.DrawTransport(gr); pictureBoxBoat.Image = bmp; } - private void ButtonCreateBoat_Click(object sender, EventArgs e) + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) { Random random = new(); - _drawningBoat = new DrawningBoat(); - _drawningBoat.Init(random.Next(650, 700), random.Next(15760, 16130), - 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))); - _drawningBoat.SetPictureSize(pictureBoxBoat.Width, pictureBoxBoat.Height); - _drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); - + switch (type) + { + case nameof(DrawningB): + _drawningB = new DrawningB(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(DrawningBoat): + _drawningB = new DrawningBoat(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; + } + _drawningB.SetPictureSize(pictureBoxBoat.Width, pictureBoxBoat.Height); + _drawningB.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } + /// + /// Обработка создания кнопки "создать катер" + /// + /// + /// + private void ButtonCreateBoat_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningBoat)); + } + + /// + /// Обработка создания кнопки "создать простой катер" + /// + /// + /// + private void ButtonCreateB_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningB)); + } + + + /// + /// Перемещение объекта по форме (нажатие кнопок навигации) + /// + /// + /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningBoat == null) + if (_drawningB == null) { return; } @@ -57,16 +97,16 @@ public partial class FormBoat : Form switch (name) { case "buttonUp": - result = _drawningBoat.MoveTransport(DirectionType.Up); + result = _drawningB.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawningBoat.MoveTransport(DirectionType.Down); + result = _drawningB.MoveTransport(DirectionType.Down); break; case "buttonLeft": - result = _drawningBoat.MoveTransport(DirectionType.Left); + result = _drawningB.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawningBoat.MoveTransport(DirectionType.Right); + result = _drawningB.MoveTransport(DirectionType.Right); break; } if (result) diff --git a/laba 0/laba 0/FormBoat.resx b/laba 0/laba 0/FormBoat.resx index 7183fb7..da0e379 100644 --- a/laba 0/laba 0/FormBoat.resx +++ b/laba 0/laba 0/FormBoat.resx @@ -121,7 +121,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAYAAAA+s9J6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO - vwAADr8BOAVTJAAACHlJREFUeF7tnTFuGzEQRdWnSiUDAVKksIG0aXKAHCBdLujKJ/B5XKUxkBsoM7CN + vAAADrwBlbxySQAACHlJREFUeF7tnTFuGzEQRdWnSiUDAVKksIG0aXKAHCBdLujKJ/B5XKUxkBsoM7CN yJuxrF2RHJL/PeAhsS2RQ0AfWopccXcAgFQIIUAyhBAgGUIIkAwhBEiGEAIkQwgBkiGEAMkQQoBkCCFA MoQQIBlCCJAMIQRIhhACJEMIAZIhhADJEEJx7u/vDw8PD88/QQaEUJzb29vDbrc7/Pnz5/k30BpCKM5L CD98+HB4fHx8/i20hBCK8xJC9/r6+vD79+/nv0ArCKE4xyF0v379ShAbQwjFWYbQ3e/3zBEbQgjFiUL4 @@ -163,7 +163,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAYAAAA+s9J6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO - vwAADr8BOAVTJAAACBBJREFUeF7tnTtOI0sARcmJiIyENAEBSKQkLIAFkLFBIlbAeohIkNiB37vMWBqY + vAAADrwBlbxySQAACBBJREFUeF7tnTtOI0sARcmJiIyENAEBSKQkLIAFkLFBIlbAeohIkNiB37vMWBqY BmO7PrfqniMdjeZv7D7uqq4yfbSGYXl9fV3f3t6+/wjjQoSD8vLysr68vFwfHR29/6ifw5gQ4YDozLcJ cKN+zhlxTIhwMN7e3tYnJycfAtyoX9fvw1gQ4UBoyHl2drYY4Eb9PkPTsSDCQVBYV1dXi+F9Vn+OEMeB CAdAc73z8/PF4L5Sf5454hgQoTkK6fj4eDG0bervEaI/RGiMhpS7ngE/q7/P0NQbIjRllzngNpkjekOE