From 33f58a23109e4f2e4ad3d1cceea75aa1b7881560 Mon Sep 17 00:00:00 2001 From: Pyro <75787064+pyro-p@users.noreply.github.com> Date: Sun, 18 Feb 2024 15:44:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectMotorBoat/DrawingMotorBoat.cs | 148 ++++++++++++++++-- .../FormMotorBoat.Designer.cs | 28 ++-- .../ProjectMotorBoat/FormMotorBoat.cs | 109 +++++++++---- .../ProjectMotorBoat/ProjectMotorBoat.csproj | 2 +- 4 files changed, 228 insertions(+), 59 deletions(-) diff --git a/ProjectMotorBoat/ProjectMotorBoat/DrawingMotorBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/DrawingMotorBoat.cs index 6345e59..b379b19 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/DrawingMotorBoat.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/DrawingMotorBoat.cs @@ -1,4 +1,7 @@ -namespace ProjectMotorBoat; +using System; +using System.DirectoryServices.ActiveDirectory; + +namespace ProjectMotorBoat; /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности @@ -34,12 +37,12 @@ public class DrawingMotorBoat /// /// Ширина прорисовки катера /// - private readonly int _drawningBoatWidth = 110; + private readonly int _drawningBoatWidth = 100; /// /// Высота прорисовки катера /// - private readonly int _drawningBoatHeight = 60; + private readonly int _drawningBoatHeight = 50; /// /// Инициализация свойств @@ -70,9 +73,25 @@ public class DrawingMotorBoat public bool SetPictureSize(int width, int height) { // TODO проверка, что объект "влезает" в размеры поля - // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена - _pictureWidth = width; - _pictureHeight = height; + // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена ✔ + if (_drawningBoatWidth < width) + { + _pictureWidth = width; + if (_startPosX + _drawningBoatWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningBoatWidth; + } + } + + if (_drawningBoatHeight < height) + { + _pictureHeight = height; + if (_startPosY + _drawningBoatHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningBoatHeight; + } + } + return true; } @@ -88,7 +107,17 @@ public class DrawingMotorBoat return; } // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы - // то надо изменить координаты, чтобы он оставался в этих границах + // то надо изменить координаты, чтобы он оставался в этих границах ✔ + if (x + _drawningBoatWidth > _pictureWidth) + { + x = (int)_pictureWidth - _drawningBoatWidth; + } + + if (y + _drawningBoatHeight > _pictureHeight) + { + y = (int)_pictureHeight - _drawningBoatHeight; + } + _startPosX = x; _startPosY = y; } @@ -123,21 +152,21 @@ public class DrawingMotorBoat return true; // вправо case DirectionType.Right: - //TODO прописать логику сдвига в право + //TODO прописать логику сдвига в право ✔ - //if (_startPosX.Value + EntityMotorBoat.Step < _pictureWidth) - //{ - // _startPosX += (int)EntityMotorBoat.Step; - //} + if (_startPosX.Value + EntityMotorBoat.Step < _pictureWidth - _drawningBoatWidth) + { + _startPosX += (int)EntityMotorBoat.Step; + } return true; //вниз case DirectionType.Down: - //TODO прописать логику сдвига в низ + //TODO прописать логику сдвига в низ ✔ - //if (_startPosY.Value + EntityMotorBoat.Step < _pictureHeight) - //{ - // _startPosY += (int)EntityMotorBoat.Step; - //} + if (_startPosY.Value + EntityMotorBoat.Step < _pictureHeight - _drawningBoatHeight) + { + _startPosY += (int)EntityMotorBoat.Step; + } return true; default: return false; @@ -154,5 +183,90 @@ public class DrawingMotorBoat { return; } + + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(EntityMotorBoat.AdditionalColor); + Brush br = new SolidBrush(EntityMotorBoat.BodyColor); + + // Границы катера + Point[] points = + { + new Point(_startPosX.Value, _startPosY.Value + 10), + new Point(_startPosX.Value + 70, _startPosY.Value + 10), + new Point(_startPosX.Value + 100, _startPosY.Value + 25), + new Point(_startPosX.Value + 70, _startPosY.Value + 40), + new Point(_startPosX.Value, _startPosY.Value + 40), + new Point(_startPosX.Value, _startPosY.Value + 10), + }; + g.DrawLines(pen, points); + + points = new Point[] + { + new Point(_startPosX.Value + 1, _startPosY.Value + 11), + new Point(_startPosX.Value + 72, _startPosY.Value + 11), + new Point(_startPosX.Value + 99, _startPosY.Value + 25), + new Point(_startPosX.Value + 70, _startPosY.Value + 40), + new Point(_startPosX.Value + 1, _startPosY.Value + 40), + new Point(_startPosX.Value + 1, _startPosY.Value + 11), + }; + g.FillPolygon(br, points); + + // Палуба + g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 20, 65, 10); + g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 15, 55, 20); + + g.FillRectangle(br, _startPosX.Value + 6, _startPosY.Value + 20, 64, 11); + g.FillRectangle(br, _startPosX.Value + 11, _startPosY.Value + 16, 56, 19); + + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 15, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 15, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 25, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 60, _startPosY.Value + 25, 10, 10); + + g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 15, 11, 11); + g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 15, 11, 11); + g.FillEllipse(br, _startPosX.Value + 5, _startPosY.Value + 24, 11, 11); + g.FillEllipse(br, _startPosX.Value + 59, _startPosY.Value + 24, 11, 11); + + // Мотор в корме + if (EntityMotorBoat.InboardEngine) + { + g.DrawRectangle(pen, _startPosX.Value + 3, _startPosY.Value + 13, 20, 24); + g.FillRectangle(additionalBrush, _startPosX.Value + 4, _startPosY.Value + 14, 19, 23); + + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 15, 6, 6); + g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 15, 6, 6); + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 28, 6, 6); + g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 28, 6, 6); + g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 16, 4, 4); + g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 16, 4, 4); + g.FillEllipse(additionalBrush, _startPosX.Value + 6, _startPosY.Value + 29, 4, 4); + g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 29, 4, 4); + } + + // Вёсла + if (EntityMotorBoat.Paddle) + { + g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 3, 2, 17); + g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 30, 2, 17); + g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 3, 1, 17); + g.FillRectangle(additionalBrush, _startPosX.Value + 33, _startPosY.Value + 31, 1, 16); + + g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value, 6, 8); + g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8); + g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value, 6, 8); + g.FillEllipse(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 42, 6, 8); + } + + // Защитное стекло + if (EntityMotorBoat.ProtectiveGlass) + { + Brush brBlue = new SolidBrush(Color.LightBlue); + g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 20, 25, 11); + g.FillRectangle(brBlue, _startPosX.Value + 45, _startPosY.Value + 16, 17, 19); + + g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 15, 11, 11); + g.FillEllipse(brBlue, _startPosX.Value + 59, _startPosY.Value + 24, 11, 11); + } } } \ No newline at end of file diff --git a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs index 55dd971..f95b315 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.Designer.cs @@ -29,7 +29,7 @@ private void InitializeComponent() { pictureBoxMotorBoat = new PictureBox(); - buttonCreate = new Button(); + buttonCreateMotorBoat = new Button(); buttonLeft = new Button(); buttonUp = new Button(); buttonRight = new Button(); @@ -47,16 +47,16 @@ pictureBoxMotorBoat.TabIndex = 0; pictureBoxMotorBoat.TabStop = false; // - // buttonCreate + // buttonCreateMotorBoat // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 448); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += ButtonCreate_Click; + buttonCreateMotorBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateMotorBoat.Location = new Point(12, 448); + buttonCreateMotorBoat.Name = "buttonCreateMotorBoat"; + buttonCreateMotorBoat.Size = new Size(75, 23); + buttonCreateMotorBoat.TabIndex = 1; + buttonCreateMotorBoat.Text = "Создать"; + buttonCreateMotorBoat.UseVisualStyleBackColor = true; + buttonCreateMotorBoat.Click += ButtonCreateMotorBoat_Click; // // buttonLeft // @@ -68,6 +68,7 @@ buttonLeft.Size = new Size(35, 35); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; + buttonLeft.Click += ButtonMoveMotorBoat_click; // // buttonUp // @@ -79,6 +80,7 @@ buttonUp.Size = new Size(35, 35); buttonUp.TabIndex = 3; buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += ButtonMoveMotorBoat_click; // // buttonRight // @@ -90,6 +92,7 @@ buttonRight.Size = new Size(35, 35); buttonRight.TabIndex = 4; buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += ButtonMoveMotorBoat_click; // // buttonDown // @@ -101,6 +104,7 @@ buttonDown.Size = new Size(35, 35); buttonDown.TabIndex = 5; buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += ButtonMoveMotorBoat_click; // // FormMotorBoat // @@ -111,7 +115,7 @@ Controls.Add(buttonRight); Controls.Add(buttonUp); Controls.Add(buttonLeft); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateMotorBoat); Controls.Add(pictureBoxMotorBoat); Name = "FormMotorBoat"; Text = "Катер"; @@ -123,7 +127,7 @@ #endregion private PictureBox pictureBoxMotorBoat; - private Button buttonCreate; + private Button buttonCreateMotorBoat; private Button buttonLeft; private Button buttonUp; private Button buttonRight; diff --git a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs index b5d45a2..b12a68c 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/FormMotorBoat.cs @@ -1,41 +1,92 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +namespace ProjectMotorBoat; -namespace ProjectMotorBoat +/// +/// Форма работы с объектом "Катер" +/// +public partial class FormMotorBoat : Form { - public partial class FormMotorBoat : Form - { - private DrawingMotorBoat? _drawingMotorBoat; + /// + /// Поле-объект для прорисовки объекта + /// + private DrawingMotorBoat? _drawingMotorBoat; - public FormMotorBoat() + /// + /// Конструктор формы + /// + public FormMotorBoat() + { + InitializeComponent(); + } + + /// + /// Метод прорисовки катера + /// + private void Draw() + { + if (_drawingMotorBoat == null) { - InitializeComponent(); + return; } - private void ButtonCreate_Click(object sender, EventArgs e) + Bitmap bmp = new(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawingMotorBoat.DrawTransport(gr); + pictureBoxMotorBoat.Image = bmp; + } + + /// + /// Обработка нажатия кнопки "Создать" + /// + /// + /// + private void ButtonCreateMotorBoat_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingMotorBoat = new DrawingMotorBoat(); + _drawingMotorBoat.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)), + Convert.ToBoolean(random.Next(0, 2))); + _drawingMotorBoat.SetPictureSize(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); + _drawingMotorBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + + Draw(); + } + + /// + /// Перемещение объекта по форме (нажатие кнопок навигации) + /// + /// + /// + private void ButtonMoveMotorBoat_click(object sender, EventArgs e) + { + if (_drawingMotorBoat == null) { - Random random = new(); - _drawingMotorBoat = new DrawingMotorBoat(); - _drawingMotorBoat.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)), - Convert.ToBoolean(random.Next(0, 2))); - _drawingMotorBoat.SetPictureSize(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); - _drawingMotorBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + return; + } - Bitmap bmp = new(pictureBoxMotorBoat.Width, pictureBoxMotorBoat.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawingMotorBoat.DrawTransport(gr); - pictureBoxMotorBoat.Image = bmp; + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonUp": + result = _drawingMotorBoat.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = _drawingMotorBoat.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + result = _drawingMotorBoat.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + result = _drawingMotorBoat.MoveTransport(DirectionType.Right); + break; + } + if (result) + { + Draw(); } } } diff --git a/ProjectMotorBoat/ProjectMotorBoat/ProjectMotorBoat.csproj b/ProjectMotorBoat/ProjectMotorBoat/ProjectMotorBoat.csproj index 244387d..13ee123 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/ProjectMotorBoat.csproj +++ b/ProjectMotorBoat/ProjectMotorBoat/ProjectMotorBoat.csproj @@ -2,7 +2,7 @@ WinExe - net7.0-windows + net6.0-windows enable true enable