From 35221daf041de6a12bde379ace2e3e243bc50027 Mon Sep 17 00:00:00 2001 From: grishazagidulin Date: Sat, 2 Mar 2024 20:46:38 +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?=D1=8C=D1=81=D0=BA=D0=B8=D1=85=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B8=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80?= =?UTF-8?q?=D1=83=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 --- Battleship/Battleship/DrawningBattleship.cs | 126 ----------------- .../{ => Drawnings}/DirectionType.cs | 2 +- .../Drawnings/DrawningBattleship.cs | 48 +++++++ .../Battleship/Drawnings/DrawningShip.cs | 131 ++++++++++++++++++ .../Battleship/Entities/EntityBattleship.cs | 28 ++++ Battleship/Battleship/Entities/EntityShip.cs | 30 ++++ Battleship/Battleship/EntityBattleship.cs | 26 ---- .../Battleship/FormBattleship.Designer.cs | 34 +++-- Battleship/Battleship/FormBattleship.cs | 71 +++++++--- 9 files changed, 311 insertions(+), 185 deletions(-) delete mode 100644 Battleship/Battleship/DrawningBattleship.cs rename Battleship/Battleship/{ => Drawnings}/DirectionType.cs (88%) create mode 100644 Battleship/Battleship/Drawnings/DrawningBattleship.cs create mode 100644 Battleship/Battleship/Drawnings/DrawningShip.cs create mode 100644 Battleship/Battleship/Entities/EntityBattleship.cs create mode 100644 Battleship/Battleship/Entities/EntityShip.cs delete mode 100644 Battleship/Battleship/EntityBattleship.cs diff --git a/Battleship/Battleship/DrawningBattleship.cs b/Battleship/Battleship/DrawningBattleship.cs deleted file mode 100644 index e7b2217..0000000 --- a/Battleship/Battleship/DrawningBattleship.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Battleship; -// класс прорисовки и пермещения -public class DrawningBattleship -{ - #region [Variables Initialization] - public EntityBattleship? EntityBattleship { get; private set; } //класс сущность - private int? _pictureWidth; //ширина окна - private int? _pictureHeight; //высота окна - private int? _startX; // Х начальной позиции - private int? _startY; // Y начальной позиции - private readonly int _drawningShipWidth = 143; // длина корабля - private readonly int _drawningShipHeight = 75; // высота корабля - #endregion - public void Init(EntityBattleship entityBattleship) //инициализация свойств корабля и параметров окна, позиции - { - EntityBattleship = entityBattleship; - _pictureWidth = null; - _pictureHeight = null; - _startX = null; - _startY = null; - } - public bool SetPictureSize(int width, int height) // установка размеров окна - { - if (width > _drawningShipWidth && height > _drawningShipHeight) //если размеры окна позволяют вместить корабль, то присваиваем - { - if (_startX.HasValue && _startY.HasValue) - { - if (_drawningShipHeight + _startY.Value > height) - { - _startY = height - _drawningShipHeight; - } - if (_drawningShipWidth + _startX.Value > width) - { - _startX = width - _drawningShipWidth; - } - } - _pictureWidth = width; - _pictureHeight = height; - return true; - } - return false; - } - public void SetPosition(int x, int y) // установка позиции корабля - { - if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) // проверка инициализации размеров окна - return; - if (x < 0 || x + _drawningShipWidth > _pictureWidth) // если корабль не вмещается по Х - присваиваем 0 - x = 0; - if (y < 0 || y + _drawningShipHeight > _pictureHeight) // если корабль не вмещается по Y - присваиваем 0 - y = 0; - _startX = x; - _startY = y; - } - public bool MoveTransport(DirectionType direction) //метод движения корабля - { - if (EntityBattleship == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции - return false; - switch (direction) - { - case DirectionType.Left: // Влево - if (_startX.Value - EntityBattleship.Step > 0) // Проверяем, есть ли место для шага (аналогично далее) - _startX -= (int)EntityBattleship.Step; - return true; - case DirectionType.Right: // Вправо - if (_startX.Value + EntityBattleship.Step + _drawningShipWidth < _pictureWidth) - _startX += (int)EntityBattleship.Step; - return true; - case DirectionType.Up: // Вверх - if (_startY.Value - EntityBattleship.Step > 0) - _startY -= (int)EntityBattleship.Step; - return true; - case DirectionType.Down: // Вниз - if (_startY.Value + _drawningShipHeight + EntityBattleship.Step < _pictureHeight) - _startY += (int)EntityBattleship.Step; - return true; - default: return false; - } - } - public void DrawTransport(Graphics g) //метод рисования корабля - { - if (EntityBattleship == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции - return; - Pen pen = new(Color.Black, 3); - Brush addBr = new SolidBrush(EntityBattleship.AdditionalColor); - Brush br = new SolidBrush(EntityBattleship.BodyColor); - #region [Drawing] - // Гриницы Линкора - g.DrawLine(pen, _startX.Value + 3, _startY.Value + 15, _startX.Value + 93, _startY.Value + 15); - g.DrawLine(pen, _startX.Value + 93, _startY.Value + 15, _startX.Value + 143, _startY.Value + 15 + 30); - g.DrawLine(pen, _startX.Value + 143, _startY.Value + 15 + 30, _startX.Value + 93, _startY.Value + 15 + 60); - g.DrawLine(pen, _startX.Value + 93, _startY.Value + 15 + 60, _startX.Value + 3, _startY.Value + 15 + 60); - g.DrawLine(pen, _startX.Value + 3, _startY.Value + 15 + 60, _startX.Value + 3, _startY.Value + 15); - //Заливка - g.FillRectangle(br, _startX.Value + 3, _startY.Value + 15, 90, 60); - g.FillPolygon(br, new PointF[] { new PointF(_startX.Value + 93, _startY.Value + 15), new PointF(_startX.Value + 143, _startY.Value + 15 + 30), new PointF(_startX.Value + 93, _startY.Value + 15 + 60) }); - //Элементы палубы - g.DrawEllipse(pen, _startX.Value + 83, _startY.Value + 15 + 20, 20, 20); - g.DrawRectangle(pen, _startX.Value + 63, _startY.Value + 15 + 15, 15, 30); - g.DrawRectangle(pen, _startX.Value + 43, _startY.Value + 15 + 25, 20, 10); - Brush brBlack = new SolidBrush(Color.Black); - g.FillRectangle(brBlack, _startX.Value, _startY.Value + 15 + 10, 3, 15); - g.FillRectangle(brBlack, _startX.Value, _startY.Value + 15 + 35, 3, 15); - // Орудийная башня - if (EntityBattleship.Weapon) - { - g.FillPie(addBr, _startX.Value + 8, _startY.Value + 15, 30, 25, 180, 180); - g.FillRectangle(addBr, _startX.Value + 18, _startY.Value, 10, 20); - } - //Ракеты - if (EntityBattleship.Rockets) - { - g.FillRectangle(addBr, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35); - g.DrawRectangle(pen, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35); - g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 25, 10, 10); - g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 25, 10, 10); - g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 40, 10, 10); - g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 40, 10, 10); - } - #endregion - } -} \ No newline at end of file diff --git a/Battleship/Battleship/DirectionType.cs b/Battleship/Battleship/Drawnings/DirectionType.cs similarity index 88% rename from Battleship/Battleship/DirectionType.cs rename to Battleship/Battleship/Drawnings/DirectionType.cs index c1e811b..83c1b96 100644 --- a/Battleship/Battleship/DirectionType.cs +++ b/Battleship/Battleship/Drawnings/DirectionType.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Battleship; +namespace Battleship.Drawnings; public enum DirectionType { Up = 1, //Вверх diff --git a/Battleship/Battleship/Drawnings/DrawningBattleship.cs b/Battleship/Battleship/Drawnings/DrawningBattleship.cs new file mode 100644 index 0000000..53cf10d --- /dev/null +++ b/Battleship/Battleship/Drawnings/DrawningBattleship.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Battleship.Entities; +namespace Battleship.Drawnings; +// класс прорисовки и пермещения +public class DrawningBattleship : DrawningShip +{ + /// + /// Конструктор + /// + /// объект класса-сущности + public DrawningBattleship(EntityBattleship entityBattleship) + { + EntityShip = entityBattleship; + } + public override void DrawTransport(Graphics g) + { + if (EntityShip == null || EntityShip is not EntityBattleship entityBattleship || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции + return; + _startY += 15; + base.DrawTransport(g); + _startY -= 15; + #region Прорисовка доп. элеменетов + Pen pen = new(Color.Black, 3); //!!! ЗАДАТЬ ВОПРОС ПРО PEN + Brush addBr = new SolidBrush(entityBattleship.AdditionalColor); + // Орудийная башня + if (entityBattleship.Weapon) + { + g.FillPie(addBr, _startX.Value + 8, _startY.Value + 15, 30, 25, 180, 180); + g.FillRectangle(addBr, _startX.Value + 18, _startY.Value, 10, 20); + } + //Ракеты + if (entityBattleship.Rockets) + { + g.FillRectangle(addBr, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35); + g.DrawRectangle(pen, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35); + g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 25, 10, 10); + g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 25, 10, 10); + g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 40, 10, 10); + g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 40, 10, 10); + } + #endregion + } +} \ No newline at end of file diff --git a/Battleship/Battleship/Drawnings/DrawningShip.cs b/Battleship/Battleship/Drawnings/DrawningShip.cs new file mode 100644 index 0000000..9b56a0d --- /dev/null +++ b/Battleship/Battleship/Drawnings/DrawningShip.cs @@ -0,0 +1,131 @@ +using Battleship.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.Drawnings; +public class DrawningShip +{ + #region [Variables Initialization] + public EntityShip? EntityShip { get; protected set; } //класс сущность + private int? _pictureWidth; //ширина окна + private int? _pictureHeight; //высота окна + protected int? _startX; // Х начальной позиции + protected int? _startY; // Y начальной позиции + private readonly int _drawningShipWidth = 143; // длина корабля + private readonly int _drawningShipHeight = 60; // высота корабля + #endregion + /// + /// Пустой конструктор + /// + public DrawningShip() + { + _pictureWidth = null; + _pictureHeight = null; + _startX = null; + _startY = null; + } + /// + /// Конструктор прорисовки + /// + /// Объект класса-сущности + public DrawningShip(EntityShip entityShip) : this() + { + EntityShip = entityShip; + } + /// + /// Конструктор для наследников + /// + /// + /// + protected DrawningShip(int drawningShipWidth, int drawningShipHeight) : this() + { + _drawningShipHeight = drawningShipHeight; + _drawningShipWidth = drawningShipWidth; + } + public bool SetPictureSize(int width, int height) // установка размеров окна + { + if (width > _drawningShipWidth && height > _drawningShipHeight) //если размеры окна позволяют вместить корабль, то присваиваем + { + if (_startX.HasValue && _startY.HasValue) + { + if (_drawningShipHeight + _startY.Value > height) + { + _startY = height - _drawningShipHeight; + } + if (_drawningShipWidth + _startX.Value > width) + { + _startX = width - _drawningShipWidth; + } + } + _pictureWidth = width; + _pictureHeight = height; + return true; + } + return false; + } + public void SetPosition(int x, int y) // установка позиции корабля + { + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) // проверка инициализации размеров окна + return; + if (x < 0 || x + _drawningShipWidth > _pictureWidth) // если корабль не вмещается по Х - присваиваем 0 + x = 0; + if (y < 0 || y + _drawningShipHeight > _pictureHeight) // если корабль не вмещается по Y - присваиваем 0 + y = 0; + _startX = x; + _startY = y; + } + public bool MoveTransport(DirectionType direction) //метод движения корабля + { + if (EntityShip == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции + return false; + switch (direction) + { + case DirectionType.Left: // Влево + if (_startX.Value - EntityShip.Step > 0) // Проверяем, есть ли место для шага (аналогично далее) + _startX -= (int)EntityShip.Step; + return true; + case DirectionType.Right: // Вправо + if (_startX.Value + EntityShip.Step + _drawningShipWidth < _pictureWidth) + _startX += (int)EntityShip.Step; + return true; + case DirectionType.Up: // Вверх + if (_startY.Value - EntityShip.Step > 0) + _startY -= (int)EntityShip.Step; + return true; + case DirectionType.Down: // Вниз + if (_startY.Value + _drawningShipHeight + EntityShip.Step < _pictureHeight) + _startY += (int)EntityShip.Step; + return true; + default: return false; + } + } + public virtual void DrawTransport(Graphics g) //метод рисования корабля + { + if (EntityShip == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции + return; + Pen pen = new(Color.Black, 3); + Brush br = new SolidBrush(EntityShip.BodyColor); + #region [Drawing] + // Гриницы Линкора + g.DrawLine(pen, _startX.Value + 3, _startY.Value, _startX.Value + 93, _startY.Value); + g.DrawLine(pen, _startX.Value + 93, _startY.Value, _startX.Value + 143, _startY.Value + 30); + g.DrawLine(pen, _startX.Value + 143, _startY.Value + 30, _startX.Value + 93, _startY.Value + 60); + g.DrawLine(pen, _startX.Value + 93, _startY.Value + 60, _startX.Value + 3, _startY.Value + 60); + g.DrawLine(pen, _startX.Value + 3, _startY.Value + 60, _startX.Value + 3, _startY.Value); + //Заливка + g.FillRectangle(br, _startX.Value + 3, _startY.Value, 90, 60); + g.FillPolygon(br, new PointF[] { new PointF(_startX.Value + 93, _startY.Value), new PointF(_startX.Value + 143, _startY.Value + 30), new PointF(_startX.Value + 93, _startY.Value + 60) }); + //Элементы палубы + g.DrawEllipse(pen, _startX.Value + 83, _startY.Value + 20, 20, 20); + g.DrawRectangle(pen, _startX.Value + 63, _startY.Value + 15, 15, 30); + g.DrawRectangle(pen, _startX.Value + 43, _startY.Value + 25, 20, 10); + Brush brBlack = new SolidBrush(Color.Black); + g.FillRectangle(brBlack, _startX.Value, _startY.Value + 10, 3, 15); + g.FillRectangle(brBlack, _startX.Value, _startY.Value + 35, 3, 15); + #endregion + } +} + diff --git a/Battleship/Battleship/Entities/EntityBattleship.cs b/Battleship/Battleship/Entities/EntityBattleship.cs new file mode 100644 index 0000000..00f0213 --- /dev/null +++ b/Battleship/Battleship/Entities/EntityBattleship.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace Battleship.Entities; +// класс-сущность "Боевой корабль" +public class EntityBattleship : EntityShip +{ + public Color AdditionalColor; // дополнительный цвет + public bool Weapon { get; private set; } // оружие + public bool Rockets { get; set; } // рокеты + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Наличие оружейной башни + /// Наличие рокет + public EntityBattleship(int speed, double weight, Color bodycolor, Color additionalcolor, bool weapon, bool rockets) : base(speed, weight, bodycolor) // инициализация полей объекта-класса военного корабля + { + AdditionalColor = additionalcolor; + Weapon = weapon; + Rockets = rockets; + } +} \ No newline at end of file diff --git a/Battleship/Battleship/Entities/EntityShip.cs b/Battleship/Battleship/Entities/EntityShip.cs new file mode 100644 index 0000000..6d08502 --- /dev/null +++ b/Battleship/Battleship/Entities/EntityShip.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.Entities; +/// +/// Класс-сущность "Корабль" +/// +public class EntityShip +{ + public int Speed; // скорость + public double Weight; //вес + public Color BodyColor; /*основной цвет*/ + public double Step => Speed * 100 / Weight; // шаг перемещения корабля + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес + /// Цвет + public EntityShip(int speed, double weight, Color bodycolor) { + Speed = speed; + Weight = weight; + BodyColor = bodycolor; + } +} + diff --git a/Battleship/Battleship/EntityBattleship.cs b/Battleship/Battleship/EntityBattleship.cs deleted file mode 100644 index c72a1a6..0000000 --- a/Battleship/Battleship/EntityBattleship.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace Battleship; - // класс-сущность "Боевой корабль" -public class EntityBattleship -{ - public int Speed; // скорость - public double Weight; //вес - public Color BodyColor; /*основной цвет*/ - public Color AdditionalColor; // дополнительный цвет - public bool Weapon { get; private set; } // оружие - public bool Rockets { get; set; } // рокеты - public double Step => Speed * 100 / Weight; // шаг перемещения корабля - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool weapon, bool rockets) // инициализация полей объекта-класса военного корабля - { - Speed = speed; - Weight = weight; - BodyColor = bodycolor; - AdditionalColor = additionalcolor; - Weapon = weapon; - Rockets = rockets; - } -} \ No newline at end of file diff --git a/Battleship/Battleship/FormBattleship.Designer.cs b/Battleship/Battleship/FormBattleship.Designer.cs index 26234dd..0d05da1 100644 --- a/Battleship/Battleship/FormBattleship.Designer.cs +++ b/Battleship/Battleship/FormBattleship.Designer.cs @@ -32,26 +32,27 @@ buttonDown = new Button(); buttonLeft = new Button(); buttonRight = new Button(); + buttonCreateSimple = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit(); SuspendLayout(); // // pictureBoxBattleship // - pictureBoxBattleship.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + pictureBoxBattleship.Dock = DockStyle.Fill; pictureBoxBattleship.Location = new Point(0, 0); pictureBoxBattleship.Name = "pictureBoxBattleship"; - pictureBoxBattleship.Size = new Size(1259, 803); + pictureBoxBattleship.Size = new Size(1641, 945); pictureBoxBattleship.TabIndex = 0; pictureBoxBattleship.TabStop = false; // // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 745); + buttonCreate.Location = new Point(12, 887); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(150, 46); + buttonCreate.Size = new Size(316, 46); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать боевой корабль"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += buttonCreate_Click; // @@ -60,7 +61,7 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.right_arrow; buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(1152, 699); + buttonUp.Location = new Point(1534, 841); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(40, 40); buttonUp.TabIndex = 2; @@ -72,7 +73,7 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.right_arrow_2; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(1152, 745); + buttonDown.Location = new Point(1534, 887); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(40, 40); buttonDown.TabIndex = 3; @@ -84,7 +85,7 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.right_arrow_4; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(1106, 745); + buttonLeft.Location = new Point(1488, 887); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(40, 40); buttonLeft.TabIndex = 4; @@ -96,18 +97,30 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.right_arrow_3; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(1198, 745); + buttonRight.Location = new Point(1580, 887); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(40, 40); buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // + // buttonCreateSimple + // + buttonCreateSimple.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateSimple.Location = new Point(334, 887); + buttonCreateSimple.Name = "buttonCreateSimple"; + buttonCreateSimple.Size = new Size(316, 46); + buttonCreateSimple.TabIndex = 6; + buttonCreateSimple.Text = "Создать корабль"; + buttonCreateSimple.UseVisualStyleBackColor = true; + buttonCreateSimple.Click += buttonCreateSimple_Click; + // // FormBattleship // AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1259, 803); + ClientSize = new Size(1641, 945); + Controls.Add(buttonCreateSimple); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonDown); @@ -127,5 +140,6 @@ private Button buttonDown; private Button buttonLeft; private Button buttonRight; + private Button buttonCreateSimple; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormBattleship.cs b/Battleship/Battleship/FormBattleship.cs index a787a28..0e53b79 100644 --- a/Battleship/Battleship/FormBattleship.cs +++ b/Battleship/Battleship/FormBattleship.cs @@ -7,62 +7,89 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Battleship.Drawnings; +using Battleship.Entities; namespace Battleship { public partial class FormBattleship : Form { - private DrawningBattleship? _drawningBattleship; + private DrawningShip? _drawningShip; + private EntityShip? _entityShip; private EntityBattleship? _entityBattleship; - public FormBattleship() + public FormBattleship() { InitializeComponent(); - } + } private void Draw() // метод рисования корабля { - if (_drawningBattleship == null) + if (_drawningShip == null) return; Bitmap bmp = new(pictureBoxBattleship.Width, pictureBoxBattleship.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningBattleship.DrawTransport(gr); + _drawningShip.DrawTransport(gr); pictureBoxBattleship.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) // обработка кнопки "создать" + private void CreateObject(string type) { - Random random = new Random(); - _drawningBattleship = new DrawningBattleship(); - _entityBattleship = new EntityBattleship(); - _entityBattleship.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))); - _drawningBattleship.Init(_entityBattleship); - _drawningBattleship.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height); - _drawningBattleship.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); + Random random = new(); + switch (type) + { + case nameof(DrawningShip): + _entityShip = new EntityShip(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); + _drawningShip = new DrawningShip(_entityShip); + _drawningShip.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height); + _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + break; + case nameof(DrawningBattleship): + _entityBattleship = new EntityBattleship(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))); + _drawningShip = new DrawningBattleship(_entityBattleship); + _drawningShip.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height); + _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + break; + default: + return; + } + } + private void buttonCreate_Click(object sender, EventArgs e) // обработка кнопки "создать боевой корабль" + { + CreateObject(nameof(DrawningBattleship)); + } + private void buttonCreateSimple_Click(object sender, EventArgs e) // обработка кнопки "создать корабль" + { + CreateObject(nameof(DrawningShip)); + } private void ButtonMove_Click(object sender, EventArgs e) // обработка кнопок движения { - if (_drawningBattleship == null) + if (_drawningShip == null) return; string name = ((Button)sender)?.Name ?? string.Empty; bool result = false; switch (name) { case "buttonUp": - result = _drawningBattleship.MoveTransport(DirectionType.Up); + result = _drawningShip.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawningBattleship.MoveTransport(DirectionType.Down); + result = _drawningShip.MoveTransport(DirectionType.Down); break; case "buttonRight": - result = _drawningBattleship.MoveTransport(DirectionType.Right); + result = _drawningShip.MoveTransport(DirectionType.Right); break; case "buttonLeft": - result = _drawningBattleship.MoveTransport(DirectionType.Left); + result = _drawningShip.MoveTransport(DirectionType.Left); break; } if (result) Draw(); } + + } }