From e0874c29240b0dce8ab5c11068c413f522312610 Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Wed, 4 Oct 2023 03:06:05 +0400 Subject: [PATCH 1/5] =?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=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank.sln | 12 +-- Tank/Tank/AbstractStrategy.cs | 73 +++++++++++++++++ Tank/Tank/AddBase.cs | 23 ++++++ Tank/Tank/Base.cs | 22 ++++++ Tank/Tank/DrawArmoVehicle.cs | 140 +++++++++++++++++++++++++++++++++ Tank/Tank/DrawTank.cs | 72 +++++++++++++++++ Tank/Tank/DrawingObjectTank.cs | 34 ++++++++ Tank/Tank/DrawingTank.cs | 139 -------------------------------- Tank/Tank/EntityTank.cs | 34 -------- Tank/Tank/FormTank.Designer.cs | 56 +++++++++++-- Tank/Tank/FormTank.cs | 74 ++++++++++++++--- Tank/Tank/IMoveableObject.cs | 20 +++++ Tank/Tank/MoveToBorder.cs | 47 +++++++++++ Tank/Tank/MoveToCenter.cs | 54 +++++++++++++ Tank/Tank/ObjectParameters.cs | 31 ++++++++ Tank/Tank/Status.cs | 15 ++++ Tank/Tank/Tank.csproj | 15 ---- 17 files changed, 651 insertions(+), 210 deletions(-) create mode 100644 Tank/Tank/AbstractStrategy.cs create mode 100644 Tank/Tank/AddBase.cs create mode 100644 Tank/Tank/Base.cs create mode 100644 Tank/Tank/DrawArmoVehicle.cs create mode 100644 Tank/Tank/DrawTank.cs create mode 100644 Tank/Tank/DrawingObjectTank.cs delete mode 100644 Tank/Tank/DrawingTank.cs delete mode 100644 Tank/Tank/EntityTank.cs create mode 100644 Tank/Tank/IMoveableObject.cs create mode 100644 Tank/Tank/MoveToBorder.cs create mode 100644 Tank/Tank/MoveToCenter.cs create mode 100644 Tank/Tank/ObjectParameters.cs create mode 100644 Tank/Tank/Status.cs diff --git a/Tank/Tank.sln b/Tank/Tank.sln index f5e6e36..a226ebb 100644 --- a/Tank/Tank.sln +++ b/Tank/Tank.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.6.33801.468 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{394C34D3-98AF-48A5-B765-763149963292}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{19BA0FA1-372B-490A-AA7F-757B0B8F322B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {394C34D3-98AF-48A5-B765-763149963292}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {394C34D3-98AF-48A5-B765-763149963292}.Debug|Any CPU.Build.0 = Debug|Any CPU - {394C34D3-98AF-48A5-B765-763149963292}.Release|Any CPU.ActiveCfg = Release|Any CPU - {394C34D3-98AF-48A5-B765-763149963292}.Release|Any CPU.Build.0 = Release|Any CPU + {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {29179072-02D8-4625-BA39-9548F5C3A015} + SolutionGuid = {5332F2FC-D1C2-4B42-B32E-53ECC094BBB7} EndGlobalSection EndGlobal diff --git a/Tank/Tank/AbstractStrategy.cs b/Tank/Tank/AbstractStrategy.cs new file mode 100644 index 0000000..7f74f1e --- /dev/null +++ b/Tank/Tank/AbstractStrategy.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank.MovementStrategy +{ + public abstract class AbstractStrategy + { + private IMoveableObject? _moveableObject; + private Status _state = Status.NotInit; + protected int FieldWidth { get; private set; } + protected int FieldHeight { get; private set; } + + public Status GetStatus() { return _state; } + + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldHeight = height; + FieldWidth = width; + } + + public void MakeStep() + { + if (_state != Status.InProgress) + return; + if (IsTargetDestination()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected bool MoveLeft() => MoveTo(Direction.Left); + protected bool MoveRight() => MoveTo(Direction.Right); + protected bool MoveUp() => MoveTo(Direction.Up); + protected bool MoveDown() => MoveTo(Direction.Down); + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectParameters; + + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + protected abstract void MoveToTarget(); + + protected abstract bool IsTargetDestination(); + + private bool MoveTo(Direction direction) + { + if (_state != Status.InProgress) + return false; + if (_moveableObject?.CheckCanMove(direction) ?? false) + { + _moveableObject.MoveObject(direction); + return true; + } + return false; + } + } +} diff --git a/Tank/Tank/AddBase.cs b/Tank/Tank/AddBase.cs new file mode 100644 index 0000000..d94b7d0 --- /dev/null +++ b/Tank/Tank/AddBase.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank.Entities +{ + public class AddBase : Base + { + public Color AdditionalColor { get; private set; } + public bool BodyKit { get; private set; } + public bool Caterpillar { get; private set; } + public bool Rinks { get; private set; } + public AddBase(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool rinks) : base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + BodyKit = bodyKit; + Caterpillar = caterpillar; + Rinks = rinks; + } + } +} diff --git a/Tank/Tank/Base.cs b/Tank/Tank/Base.cs new file mode 100644 index 0000000..3e2bb80 --- /dev/null +++ b/Tank/Tank/Base.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank.Entities +{ + public class Base + { + public int Speed { get; private set; } + public double Weight { get; private set; } + public Color BodyColor { get; private set; } + public double Step => (double)Speed * 100 / Weight; + public Base(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/Tank/Tank/DrawArmoVehicle.cs b/Tank/Tank/DrawArmoVehicle.cs new file mode 100644 index 0000000..ed247f8 --- /dev/null +++ b/Tank/Tank/DrawArmoVehicle.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.Pkcs; +using System.Text; +using System.Threading.Tasks; +using Tank.Entities; + +namespace Tank +{ + public class DrawArmoVehicle + { + public Base? Tank { get; protected set; } + protected int _pictureWidth; + protected int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected readonly int _Width = 160; + protected readonly int _Height = 90; + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _Width; + public int GetHeight => _Height; + + //==========ДОРАБОТКА================// + public bool CanStopObject(Direction direction) + { + return false; + } + //==================================// + + public bool CanMove(Direction direction) + { + if (Tank == null) + return false; + return direction switch + { + Direction.Left => _startPosX - Tank.Step > 0, + Direction.Up => _startPosY - Tank.Step > 0, + Direction.Right => _startPosX + _Width + Tank.Step < _pictureWidth, + Direction.Down => _startPosY + _Height + Tank.Step < _pictureHeight, + _ => false + + }; + } + // Конструктор класса + public DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height) + { + _pictureHeight = height; + _pictureWidth = width; + Tank = new Base(speed, weight, bodyColor); + } + + public DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height, int tankWidth, int tankHeight) + { + _pictureHeight = height; + _pictureWidth = width; + _Height = tankHeight; + _Width = tankWidth; + Tank = new Base(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y) + { + _startPosX = x; + _startPosY = y; + } + + public void MoveTransport(Direction direction) + { + if (!CanMove(direction) || Tank == null) + return; + switch (direction) + { + case Direction.Left: + { + if (_startPosX - Tank.Step > 0) + { + _startPosX -= (int)Tank.Step; + } + } + break; + case Direction.Up: + { + if (_startPosY - Tank.Step > 0) + { + _startPosY -= (int)Tank.Step; + } + } + break; + case Direction.Right: + { + if (_startPosX + _Width + Tank.Step < _pictureWidth) + { + _startPosX += (int)Tank.Step; + } + } + break; + case Direction.Down: + { + if (_startPosY + Tank.Step + _Height < _pictureHeight) + { + _startPosY += (int)Tank.Step; + } + } + break; + } + } + + public virtual void DrawTransport(Graphics g) // ПЕРЕПИСАТЬ В НОРМАЛЬНОЕ СОСТОЯНИЕ + { + if (Tank == null) + return; + Brush BrushRandom = new SolidBrush(Tank?.BodyColor ?? Color.Black); + + + //Корпус + Brush WhiteColor = new SolidBrush(Color.White); + g.FillRectangle(WhiteColor, _startPosX, _startPosY, 155, 30); + + Point[] pointsbody1 = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 110, _startPosY + 30), + new Point(_startPosX + 142, _startPosY + 30), new Point(_startPosX + 120, _startPosY + 45), new Point(_startPosX + 12, _startPosY + 45) }; + g.FillPolygon(BrushRandom, pointsbody1); + + // Гусеница + g.FillRectangle(BrushRandom, 28 + _startPosX, 50 + _startPosY, 10, 3); + g.FillRectangle(BrushRandom, 53 + _startPosX, 50 + _startPosY, 10, 3); + g.FillRectangle(BrushRandom, 78 + _startPosX, 50 + _startPosY, 10, 3); + g.FillRectangle(BrushRandom, 103 + _startPosX, 50 + _startPosY, 10, 3); + + // Колеса, катки + Brush ColorBlack = new SolidBrush(Color.Black); + g.FillEllipse(ColorBlack, 10 + _startPosX, 42 + _startPosY, 20, 20); + g.FillEllipse(ColorBlack, 35 + _startPosX, 42 + _startPosY, 20, 20); + g.FillEllipse(ColorBlack, 60 + _startPosX, 42 + _startPosY, 20, 20); + g.FillEllipse(ColorBlack, 85 + _startPosX, 42 + _startPosY, 20, 20); + g.FillEllipse(ColorBlack, 110 + _startPosX, 42 + _startPosY, 20, 20); + } + } +} diff --git a/Tank/Tank/DrawTank.cs b/Tank/Tank/DrawTank.cs new file mode 100644 index 0000000..2c3fb17 --- /dev/null +++ b/Tank/Tank/DrawTank.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.Entities; + +namespace Tank.DrawningObjects +{ + public class DrawTank : DrawArmoVehicle + { + + public DrawTank(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height) : base(speed, weight, bodyColor, width, height) + { + if (Tank != null) + { + Tank = new AddBase(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); + } + } + public override void DrawTransport(Graphics g) + { + if (Tank is not AddBase ArmoVehicle) + return; + base.DrawTransport(g); + if (ArmoVehicle.BodyKit) + { + Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor); + + // Корпус танка + Point[] pointsbody = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 140, _startPosY + 30), + new Point(_startPosX + 130, _startPosY + 42), new Point(_startPosX + 12, _startPosY + 42) }; + g.FillPolygon(bodyBrush, pointsbody); + + Point[] pointtower = { new Point(_startPosX + 52, _startPosY + 30), new Point(_startPosX + 52, _startPosY + 27), new Point(_startPosX + 40, _startPosY + 23), + new Point(_startPosX + 15, _startPosY + 18), new Point(_startPosX + 15,_startPosY + 15), new Point(_startPosX + 60, _startPosY + 11), new Point(_startPosX + 90, _startPosY + 11), + new Point(_startPosX + 120, _startPosY + 20), new Point(_startPosX + 100,_startPosY + 25), new Point(_startPosX + 95, _startPosY + 27), new Point(_startPosX + 90, _startPosY + 30)}; + + g.FillPolygon(bodyBrush, pointtower); + + } + + if (ArmoVehicle.Rinks) + { + Pen pen = new(Color.Black); + Brush lineBrush = new SolidBrush(Color.Gray); + // Гусеница + Brush BrushGray = new SolidBrush(Color.DarkGray); + //g.DrawEllipse(BrushGray, _startPosX + 10, _startPosY + 30, 120, 30); + // Отрисовка танковых катков + Brush BrushBlack = new SolidBrush(Color.Black); + g.FillEllipse(BrushBlack, _startPosX + 113, _startPosY + 41, 11, 11); + g.FillEllipse(BrushBlack, _startPosX + 13, _startPosY + 40, 11, 11); + } + + if (ArmoVehicle.Caterpillar) + { + Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor); + // Орудие + g.FillRectangle(bodyBrush, _startPosX + 111, _startPosY + 17, 70, 5); + + // Зенитное орудие + Point[] pointgun = { new Point(_startPosX + 44, _startPosY + 13), new Point(_startPosX + 45, _startPosY + 12), new Point(_startPosX + 41, _startPosY + 8), new Point(_startPosX + 41, _startPosY + 7), + new Point(_startPosX + 42, _startPosY + 5), new Point(_startPosX + 41, _startPosY + 4), new Point(_startPosX + 44, _startPosY + 3), new Point(_startPosX + 50, _startPosY + 3), + new Point(_startPosX + 52, _startPosY + 5), new Point(_startPosX + 53, _startPosY + 7), new Point(_startPosX + 58, _startPosY + 11)}; + g.FillPolygon(bodyBrush, pointgun); + + g.FillRectangle(bodyBrush, _startPosX + 50, _startPosY + 5, 20, 2); + } + } + } +} diff --git a/Tank/Tank/DrawingObjectTank.cs b/Tank/Tank/DrawingObjectTank.cs new file mode 100644 index 0000000..70eb9c4 --- /dev/null +++ b/Tank/Tank/DrawingObjectTank.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.DrawningObjects; + +namespace Tank.MovementStrategy +{ + public class DrawingObjectTank : IMoveableObject + { + private readonly DrawArmoVehicle? _drawTank = null; + public DrawingObjectTank(DrawArmoVehicle drawTank) + { + _drawTank = drawTank; + } + + public ObjectParameters? GetObjectParameters + { + get + { + if (_drawTank == null || _drawTank.Tank == null) + return null; + return new ObjectParameters(_drawTank.GetPosX, _drawTank.GetPosY, _drawTank.GetWidth, _drawTank.GetHeight); + } + } + public int GetStep => (int)(_drawTank?.Tank?.Step ?? 0); + public bool CheckCanMove(Direction direction) => _drawTank?.CanMove(direction) ?? false; + public void MoveObject(Direction direction) => _drawTank?.MoveTransport(direction); + + // Доработка + public bool CanStopObject(Direction direction) => _drawTank?.CanStopObject(direction) ?? false; + } +} diff --git a/Tank/Tank/DrawingTank.cs b/Tank/Tank/DrawingTank.cs deleted file mode 100644 index 8f73c5a..0000000 --- a/Tank/Tank/DrawingTank.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.NetworkInformation; -using System.Text; -using System.Threading.Tasks; - -namespace Tank -{ - public class DrawingTank - { - public EntityTank Tank { get; set; } - private int _startPosX; - private int _startPosY; - private int? _pictureWidth = null; - private int? _pictureHeight = null; - private readonly int _TankWidth = 160; - public readonly int _TankHeight = 90; - - public void Init(int speed, float weight, Color bodyColor, Color additionalColor, int Step, int width, int height, bool bodykit, bool wing, bool roadline) - { - _pictureWidth = width; - _pictureHeight = height; - Tank = new EntityTank(); - Tank.Init(speed, weight, bodyColor, additionalColor, Step, width, height, bodykit, wing, roadline); - } - - public void SetPosition(int x, int y, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (x >= 0 && x + _TankWidth <= width && y >= 0 && y + _TankHeight <= height) - { - _startPosX = x; - _startPosY = y; - } - } - - public void MoveTransport(Direction direction) - { - if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) - { - return; - } - switch (direction) - { - // вправо - case Direction.Right: - if (_startPosX + _TankWidth + Tank.Step < _pictureWidth) - { - _startPosX += Tank.Step; - } - break; - // влево - case Direction.Left: - if (_startPosX - Tank.Step > 0) - { - _startPosX -= Tank.Step; - } - break; - // вверх - case Direction.Up: - if (_startPosY - Tank.Step > 0) - { - _startPosY -= Tank.Step; - } - break; - // вниз - case Direction.Down: - if (_startPosY + _TankHeight + Tank.Step < _pictureHeight) - { - _startPosY += Tank.Step; - } - break; - } - } - - public void DrawTransport(Graphics g) - { - if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue) - { - return; - } - Pen pen = new(Color.Black); - - // Отрисовка танковых катков - Brush BrushBlack = new SolidBrush(Color.Black); - g.FillEllipse(BrushBlack, _startPosX + 113, _startPosY + 41, 11, 11); - g.FillEllipse(BrushBlack, _startPosX + 13, _startPosY + 40, 11, 11); - - // Гусеница - Brush BrushGray = new SolidBrush(Color.DarkGray); - g.DrawEllipse(pen, _startPosX + 10, _startPosY + 30, 120, 30); - - // Корпус танка - Point[] pointsbody = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 140, _startPosY + 30), - new Point(_startPosX + 130, _startPosY + 42), new Point(_startPosX + 12, _startPosY + 42) }; - g.FillPolygon(BrushGray, pointsbody); - - Point[] pointtower = { new Point(_startPosX + 52, _startPosY + 30), new Point(_startPosX + 52, _startPosY + 27), new Point(_startPosX + 40, _startPosY + 23), - new Point(_startPosX + 15, _startPosY + 18), new Point(_startPosX + 15,_startPosY + 15), new Point(_startPosX + 60, _startPosY + 11), new Point(_startPosX + 90, _startPosY + 11), - new Point(_startPosX + 120, _startPosY + 20), new Point(_startPosX + 100,_startPosY + 25), new Point(_startPosX + 95, _startPosY + 27), new Point(_startPosX + 90, _startPosY + 30)}; - - g.FillPolygon(BrushGray, pointtower); - - // Орудие - g.FillRectangle(BrushGray, _startPosX + 111, _startPosY + 17, 55, 5); - - // Зенитное орудие - Brush BrushRandom = new SolidBrush(Tank?.AdditionalColor ?? Color.Black); - Point[] pointgun = { new Point(_startPosX + 44, _startPosY + 13), new Point(_startPosX + 45, _startPosY + 12), new Point(_startPosX + 41, _startPosY + 8), new Point(_startPosX + 41, _startPosY + 7), - new Point(_startPosX + 42, _startPosY + 5), new Point(_startPosX + 41, _startPosY + 4), new Point(_startPosX + 44, _startPosY + 3), new Point(_startPosX + 50, _startPosY + 3), - new Point(_startPosX + 52, _startPosY + 5), new Point(_startPosX + 53, _startPosY + 7), new Point(_startPosX + 58, _startPosY + 11)}; - g.FillPolygon(BrushRandom, pointgun); - - g.FillRectangle(BrushRandom, _startPosX + 50, _startPosY + 5, 20, 2); - } - - public void ChangeBorders(int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureWidth <= _TankWidth || _pictureHeight <= _TankHeight) - { - _pictureWidth = null; - _pictureHeight = null; - return; - } - if (_startPosX + _TankWidth > _pictureWidth) - { - _startPosX = _pictureWidth.Value - _TankWidth; - } - if (_startPosY + _TankHeight > _pictureHeight) - { - _startPosY = _pictureHeight.Value - _TankHeight; - } - } - } -} diff --git a/Tank/Tank/EntityTank.cs b/Tank/Tank/EntityTank.cs deleted file mode 100644 index 8eae7ba..0000000 --- a/Tank/Tank/EntityTank.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.NetworkInformation; -using System.Text; -using System.Threading.Tasks; - -namespace Tank -{ - public class EntityTank - { - public int Speed { get; private set; } - public float Weight { get; private set; } - public Color BodyColor { get; private set; } - public Color AdditionalColor { get; private set; } - public int Step => Speed * 100 / (int)Weight; - - public bool BodyKit { get; private set; } - public bool Wing { get; private set; } - public bool RoadLine { get; private set; } - - public void Init(int speed, float weight, Color bodyColor, Color additionalcolor, int step, int width, int height, bool bodyKit, bool wing, bool roadline) - { - Random random = new Random(); - Speed = speed <= 0 ? random.Next(50, 100) : speed; - Weight = weight <= 0 ? random.Next(30,60) : weight; - BodyColor = bodyColor; - AdditionalColor = additionalcolor; - BodyKit = bodyKit; - Wing = wing; - RoadLine = roadline; - } - } -} diff --git a/Tank/Tank/FormTank.Designer.cs b/Tank/Tank/FormTank.Designer.cs index dda0b88..9f229b2 100644 --- a/Tank/Tank/FormTank.Designer.cs +++ b/Tank/Tank/FormTank.Designer.cs @@ -34,6 +34,9 @@ keyUp = new Button(); keyLeft = new Button(); keyRight = new Button(); + buttonArmVechicle = new Button(); + buttonStep = new Button(); + comboBoxStrategy = new ComboBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit(); SuspendLayout(); // @@ -48,12 +51,12 @@ // // ButtonCreate // - ButtonCreate.Location = new Point(14, 523); + ButtonCreate.Location = new Point(14, 500); ButtonCreate.Margin = new Padding(3, 4, 3, 4); ButtonCreate.Name = "ButtonCreate"; - ButtonCreate.Size = new Size(90, 31); + ButtonCreate.Size = new Size(145, 53); ButtonCreate.TabIndex = 1; - ButtonCreate.Text = "button"; + ButtonCreate.Text = "Создание танка"; ButtonCreate.UseVisualStyleBackColor = true; ButtonCreate.Click += ButtonCreate_Click; // @@ -62,7 +65,7 @@ keyDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyDown.BackgroundImage = Properties.Resources.KeyDown; keyDown.BackgroundImageLayout = ImageLayout.Stretch; - keyDown.Location = new Point(820, 514); + keyDown.Location = new Point(821, 515); keyDown.Margin = new Padding(3, 4, 3, 4); keyDown.Name = "keyDown"; keyDown.Size = new Size(34, 40); @@ -75,7 +78,7 @@ keyUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyUp.BackgroundImage = Properties.Resources.KeyUp; keyUp.BackgroundImageLayout = ImageLayout.Stretch; - keyUp.Location = new Point(820, 466); + keyUp.Location = new Point(821, 467); keyUp.Margin = new Padding(3, 4, 3, 4); keyUp.Name = "keyUp"; keyUp.Size = new Size(34, 40); @@ -88,7 +91,7 @@ keyLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyLeft.BackgroundImage = Properties.Resources.KeyLeft; keyLeft.BackgroundImageLayout = ImageLayout.Stretch; - keyLeft.Location = new Point(779, 514); + keyLeft.Location = new Point(779, 515); keyLeft.Margin = new Padding(3, 4, 3, 4); keyLeft.Name = "keyLeft"; keyLeft.Size = new Size(34, 40); @@ -101,7 +104,7 @@ keyRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyRight.BackgroundImage = Properties.Resources.KeyRight; keyRight.BackgroundImageLayout = ImageLayout.Stretch; - keyRight.Location = new Point(861, 514); + keyRight.Location = new Point(861, 515); keyRight.Margin = new Padding(3, 4, 3, 4); keyRight.Name = "keyRight"; keyRight.Size = new Size(34, 40); @@ -109,11 +112,46 @@ keyRight.UseVisualStyleBackColor = true; keyRight.Click += ButtonMove_Click; // + // buttonArmVechicle + // + buttonArmVechicle.Location = new Point(176, 501); + buttonArmVechicle.Margin = new Padding(3, 4, 3, 4); + buttonArmVechicle.Name = "buttonArmVechicle"; + buttonArmVechicle.Size = new Size(145, 53); + buttonArmVechicle.TabIndex = 15; + buttonArmVechicle.Text = "Создание бронеавтомобиля"; + buttonArmVechicle.UseVisualStyleBackColor = true; + buttonArmVechicle.Click += CreateButtonArmoVehicle_Click; + // + // buttonStep + // + buttonStep.Location = new Point(815, 71); + buttonStep.Margin = new Padding(3, 4, 3, 4); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(86, 31); + buttonStep.TabIndex = 17; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += ButtonStep_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); + comboBoxStrategy.Location = new Point(749, 24); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 19; + // // FormTank // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; + BackColor = SystemColors.ButtonHighlight; ClientSize = new Size(914, 568); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonStep); + Controls.Add(buttonArmVechicle); Controls.Add(keyRight); Controls.Add(keyLeft); Controls.Add(keyUp); @@ -123,6 +161,7 @@ Margin = new Padding(3, 4, 3, 4); Name = "FormTank"; Text = "FormTank"; + Load += FormTank_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); ResumeLayout(false); } @@ -135,5 +174,8 @@ private Button keyLeft; private Button keyUp; private Button keyDown; + private Button buttonArmVechicle; + private Button buttonStep; + private ComboBox comboBoxStrategy; } } \ No newline at end of file diff --git a/Tank/Tank/FormTank.cs b/Tank/Tank/FormTank.cs index d6546c5..f2984c4 100644 --- a/Tank/Tank/FormTank.cs +++ b/Tank/Tank/FormTank.cs @@ -1,8 +1,13 @@ +using Tank.DrawningObjects; +using Tank.MovementStrategy; + namespace Tank { public partial class FormTank : Form { - private DrawingTank _Tank; + private DrawArmoVehicle? _Tank; + private AbstractStrategy? _abstractStrategy; + public FormTank() { InitializeComponent(); @@ -10,29 +15,34 @@ namespace Tank private void Draw() { + if (_Tank == null) + return; + Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height); Graphics gr = Graphics.FromImage(bmp); _Tank?.DrawTransport(gr); pictureBoxTank.Image = bmp; } - private void PictureBoxTank_Resize(object sender, EventArgs e) - { - _Tank?.ChangeBorders(pictureBoxTank.Width, pictureBoxTank.Height); - Draw(); - } + private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); - _Tank = new DrawingTank(); - _Tank.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), 0, pictureBoxTank.Width, pictureBoxTank.Height, true, true, true); - _Tank.SetPosition(rnd.Next(10, 50), rnd.Next(30, 70), pictureBoxTank.Width, pictureBoxTank.Height); + _Tank = new DrawTank(rnd.Next(100, 200), rnd.Next(2000, 4000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(0, 2)), + pictureBoxTank.Width, pictureBoxTank.Height); + _Tank.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); Draw(); } private void ButtonMove_Click(object sender, EventArgs e) { // + if (_Tank == null) + return; + string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { @@ -51,5 +61,51 @@ namespace Tank } Draw(); } + + + private void CreateButtonArmoVehicle_Click(object sender, EventArgs e) + { + Random rnd = new(); + _Tank = new DrawArmoVehicle(rnd.Next(100, 200), rnd.Next(2000, 4000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + pictureBoxTank.Width, pictureBoxTank.Height); + + _Tank.SetPosition(rnd.Next(10, 50), rnd.Next(30, 70)); + Draw(); + } + + + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_Tank == null) + return; + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null + }; + if (_abstractStrategy == null) + return; + _abstractStrategy.SetData(new DrawingObjectTank(_Tank), pictureBoxTank.Width, pictureBoxTank.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + return; + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } + + private void FormTank_Load(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/Tank/Tank/IMoveableObject.cs b/Tank/Tank/IMoveableObject.cs new file mode 100644 index 0000000..17673b4 --- /dev/null +++ b/Tank/Tank/IMoveableObject.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectParameters { get; } + int GetStep { get; } + bool CheckCanMove(Direction direction); + void MoveObject(Direction direction); + + + // Доп задание + bool CanStopObject(Direction direction); + } +} diff --git a/Tank/Tank/MoveToBorder.cs b/Tank/Tank/MoveToBorder.cs new file mode 100644 index 0000000..4d98eb6 --- /dev/null +++ b/Tank/Tank/MoveToBorder.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestination() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth && objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + var diffY = objParams.DownBorder - FieldHeight; + if (diffX >= 0) + { + MoveDown(); + } + else if (diffY >= 0) + { + MoveRight(); + } + else if (Math.Abs(diffX) > Math.Abs(diffY)) + { + MoveRight(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/Tank/Tank/MoveToCenter.cs b/Tank/Tank/MoveToCenter.cs new file mode 100644 index 0000000..636db65 --- /dev/null +++ b/Tank/Tank/MoveToCenter.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestination() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/Tank/Tank/ObjectParameters.cs b/Tank/Tank/ObjectParameters.cs new file mode 100644 index 0000000..fba7a17 --- /dev/null +++ b/Tank/Tank/ObjectParameters.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank.MovementStrategy +{ + public class ObjectParameters + { + private readonly int _x; + private readonly int _y; + private readonly int _width; + private readonly int _height; + + public int LeftBorder => _x; // Левая граница + public int TopBorder => _y; // Верхняя граница + public int RightBorder => _width + _x; // Правая граница + public int DownBorder => _height + _y; // Нижняя граница + public int ObjectMiddleHorizontal => _x + _width / 2; // Середина по горизонтали + public int ObjectMiddleVertical => _y + _height / 2; // Середина по вертикали + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/Tank/Tank/Status.cs b/Tank/Tank/Status.cs new file mode 100644 index 0000000..fb08000 --- /dev/null +++ b/Tank/Tank/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank +{ + public enum Status + { + NotInit = 0, + InProgress = 1, + Finish = 2 + } +} diff --git a/Tank/Tank/Tank.csproj b/Tank/Tank/Tank.csproj index 13ee123..b57c89e 100644 --- a/Tank/Tank/Tank.csproj +++ b/Tank/Tank/Tank.csproj @@ -8,19 +8,4 @@ enable - - - True - True - Resources.resx - - - - - - ResXFileCodeGenerator - Resources.Designer.cs - - - \ No newline at end of file -- 2.25.1 From 21ce7abbe3bd24aa5ce479107f09d624e25a26b4 Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Sun, 8 Oct 2023 00:14:56 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank/DrawArmoVehicle.cs | 22 +++++++++------------- Tank/Tank/DrawTank.cs | 19 ++++++------------- Tank/Tank/DrawingObjectTank.cs | 3 --- Tank/Tank/{AddBase.cs => EntityAddBase.cs} | 8 ++++---- Tank/Tank/{Base.cs => EntityBase.cs} | 4 ++-- Tank/Tank/FormTank.Designer.cs | 3 +-- Tank/Tank/FormTank.cs | 9 ++------- Tank/Tank/IMoveableObject.cs | 4 ---- 8 files changed, 24 insertions(+), 48 deletions(-) rename Tank/Tank/{AddBase.cs => EntityAddBase.cs} (61%) rename Tank/Tank/{Base.cs => EntityBase.cs} (83%) diff --git a/Tank/Tank/DrawArmoVehicle.cs b/Tank/Tank/DrawArmoVehicle.cs index ed247f8..9996ee2 100644 --- a/Tank/Tank/DrawArmoVehicle.cs +++ b/Tank/Tank/DrawArmoVehicle.cs @@ -10,7 +10,7 @@ namespace Tank { public class DrawArmoVehicle { - public Base? Tank { get; protected set; } + public EntityBase? Tank { get; protected set; } protected int _pictureWidth; protected int _pictureHeight; protected int _startPosX; @@ -22,13 +22,6 @@ namespace Tank public int GetWidth => _Width; public int GetHeight => _Height; - //==========ДОРАБОТКА================// - public bool CanStopObject(Direction direction) - { - return false; - } - //==================================// - public bool CanMove(Direction direction) { if (Tank == null) @@ -48,16 +41,20 @@ namespace Tank { _pictureHeight = height; _pictureWidth = width; - Tank = new Base(speed, weight, bodyColor); + if (_pictureHeight < _Height || _pictureWidth < _Width) + return; + Tank = new EntityBase(speed, weight, bodyColor); } - public DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height, int tankWidth, int tankHeight) + protected DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height, int tankWidth, int tankHeight) { _pictureHeight = height; _pictureWidth = width; _Height = tankHeight; _Width = tankWidth; - Tank = new Base(speed, weight, bodyColor); + if (_pictureHeight < _Height || _pictureWidth < _Width) + return; + Tank = new EntityBase(speed, weight, bodyColor); } public void SetPosition(int x, int y) @@ -107,13 +104,12 @@ namespace Tank } } - public virtual void DrawTransport(Graphics g) // ПЕРЕПИСАТЬ В НОРМАЛЬНОЕ СОСТОЯНИЕ + public virtual void DrawTransport(Graphics g) { if (Tank == null) return; Brush BrushRandom = new SolidBrush(Tank?.BodyColor ?? Color.Black); - //Корпус Brush WhiteColor = new SolidBrush(Color.White); g.FillRectangle(WhiteColor, _startPosX, _startPosY, 155, 30); diff --git a/Tank/Tank/DrawTank.cs b/Tank/Tank/DrawTank.cs index 2c3fb17..8e94d83 100644 --- a/Tank/Tank/DrawTank.cs +++ b/Tank/Tank/DrawTank.cs @@ -10,17 +10,17 @@ namespace Tank.DrawningObjects { public class DrawTank : DrawArmoVehicle { - - public DrawTank(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height) : base(speed, weight, bodyColor, width, height) + public DrawTank(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool tower, int width, int height) : base(speed, weight, bodyColor, width, height) { if (Tank != null) { - Tank = new AddBase(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); + Tank = new EntityAddBase(speed, weight, bodyColor, additionalColor, bodyKit, caterpillar, tower); } } + public override void DrawTransport(Graphics g) { - if (Tank is not AddBase ArmoVehicle) + if (Tank is not EntityAddBase ArmoVehicle) return; base.DrawTransport(g); if (ArmoVehicle.BodyKit) @@ -28,32 +28,25 @@ namespace Tank.DrawningObjects Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor); // Корпус танка - Point[] pointsbody = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 140, _startPosY + 30), - new Point(_startPosX + 130, _startPosY + 42), new Point(_startPosX + 12, _startPosY + 42) }; - g.FillPolygon(bodyBrush, pointsbody); - Point[] pointtower = { new Point(_startPosX + 52, _startPosY + 30), new Point(_startPosX + 52, _startPosY + 27), new Point(_startPosX + 40, _startPosY + 23), new Point(_startPosX + 15, _startPosY + 18), new Point(_startPosX + 15,_startPosY + 15), new Point(_startPosX + 60, _startPosY + 11), new Point(_startPosX + 90, _startPosY + 11), new Point(_startPosX + 120, _startPosY + 20), new Point(_startPosX + 100,_startPosY + 25), new Point(_startPosX + 95, _startPosY + 27), new Point(_startPosX + 90, _startPosY + 30)}; g.FillPolygon(bodyBrush, pointtower); - } - if (ArmoVehicle.Rinks) + if (ArmoVehicle.Caterpillar) { Pen pen = new(Color.Black); Brush lineBrush = new SolidBrush(Color.Gray); // Гусеница - Brush BrushGray = new SolidBrush(Color.DarkGray); - //g.DrawEllipse(BrushGray, _startPosX + 10, _startPosY + 30, 120, 30); // Отрисовка танковых катков Brush BrushBlack = new SolidBrush(Color.Black); g.FillEllipse(BrushBlack, _startPosX + 113, _startPosY + 41, 11, 11); g.FillEllipse(BrushBlack, _startPosX + 13, _startPosY + 40, 11, 11); } - if (ArmoVehicle.Caterpillar) + if (ArmoVehicle.Tower) { Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor); // Орудие diff --git a/Tank/Tank/DrawingObjectTank.cs b/Tank/Tank/DrawingObjectTank.cs index 70eb9c4..149bcfa 100644 --- a/Tank/Tank/DrawingObjectTank.cs +++ b/Tank/Tank/DrawingObjectTank.cs @@ -27,8 +27,5 @@ namespace Tank.MovementStrategy public int GetStep => (int)(_drawTank?.Tank?.Step ?? 0); public bool CheckCanMove(Direction direction) => _drawTank?.CanMove(direction) ?? false; public void MoveObject(Direction direction) => _drawTank?.MoveTransport(direction); - - // Доработка - public bool CanStopObject(Direction direction) => _drawTank?.CanStopObject(direction) ?? false; } } diff --git a/Tank/Tank/AddBase.cs b/Tank/Tank/EntityAddBase.cs similarity index 61% rename from Tank/Tank/AddBase.cs rename to Tank/Tank/EntityAddBase.cs index d94b7d0..1527343 100644 --- a/Tank/Tank/AddBase.cs +++ b/Tank/Tank/EntityAddBase.cs @@ -6,18 +6,18 @@ using System.Threading.Tasks; namespace Tank.Entities { - public class AddBase : Base + public class EntityAddBase : EntityBase { public Color AdditionalColor { get; private set; } public bool BodyKit { get; private set; } public bool Caterpillar { get; private set; } - public bool Rinks { get; private set; } - public AddBase(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool rinks) : base(speed, weight, bodyColor) + public bool Tower { get; private set; } + public EntityAddBase(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool tower) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; BodyKit = bodyKit; Caterpillar = caterpillar; - Rinks = rinks; + Tower = tower; } } } diff --git a/Tank/Tank/Base.cs b/Tank/Tank/EntityBase.cs similarity index 83% rename from Tank/Tank/Base.cs rename to Tank/Tank/EntityBase.cs index 3e2bb80..2c23395 100644 --- a/Tank/Tank/Base.cs +++ b/Tank/Tank/EntityBase.cs @@ -6,13 +6,13 @@ using System.Threading.Tasks; namespace Tank.Entities { - public class Base + public class EntityBase { public int Speed { get; private set; } public double Weight { get; private set; } public Color BodyColor { get; private set; } public double Step => (double)Speed * 100 / Weight; - public Base(int speed, double weight, Color bodyColor) + public EntityBase(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; diff --git a/Tank/Tank/FormTank.Designer.cs b/Tank/Tank/FormTank.Designer.cs index 9f229b2..70578cb 100644 --- a/Tank/Tank/FormTank.Designer.cs +++ b/Tank/Tank/FormTank.Designer.cs @@ -58,7 +58,7 @@ ButtonCreate.TabIndex = 1; ButtonCreate.Text = "Создание танка"; ButtonCreate.UseVisualStyleBackColor = true; - ButtonCreate.Click += ButtonCreate_Click; + ButtonCreate.Click += ButtonCreateTank_Click; // // keyDown // @@ -161,7 +161,6 @@ Margin = new Padding(3, 4, 3, 4); Name = "FormTank"; Text = "FormTank"; - Load += FormTank_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); ResumeLayout(false); } diff --git a/Tank/Tank/FormTank.cs b/Tank/Tank/FormTank.cs index f2984c4..d43d757 100644 --- a/Tank/Tank/FormTank.cs +++ b/Tank/Tank/FormTank.cs @@ -25,13 +25,13 @@ namespace Tank } - private void ButtonCreate_Click(object sender, EventArgs e) + private void ButtonCreateTank_Click(object sender, EventArgs e) { Random rnd = new(); _Tank = new DrawTank(rnd.Next(100, 200), rnd.Next(2000, 4000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(0, 2)), + Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), pictureBoxTank.Width, pictureBoxTank.Height); _Tank.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); Draw(); @@ -102,10 +102,5 @@ namespace Tank _abstractStrategy = null; } } - - private void FormTank_Load(object sender, EventArgs e) - { - - } } } \ No newline at end of file diff --git a/Tank/Tank/IMoveableObject.cs b/Tank/Tank/IMoveableObject.cs index 17673b4..6c0ba64 100644 --- a/Tank/Tank/IMoveableObject.cs +++ b/Tank/Tank/IMoveableObject.cs @@ -12,9 +12,5 @@ namespace Tank.MovementStrategy int GetStep { get; } bool CheckCanMove(Direction direction); void MoveObject(Direction direction); - - - // Доп задание - bool CanStopObject(Direction direction); } } -- 2.25.1 From 37572c6e3c316a98985b9f633fc1268621d5083d Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Sun, 8 Oct 2023 00:16:27 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank/DrawArmoVehicle.cs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Tank/Tank/DrawArmoVehicle.cs b/Tank/Tank/DrawArmoVehicle.cs index 9996ee2..be5947b 100644 --- a/Tank/Tank/DrawArmoVehicle.cs +++ b/Tank/Tank/DrawArmoVehicle.cs @@ -22,20 +22,6 @@ namespace Tank public int GetWidth => _Width; public int GetHeight => _Height; - public bool CanMove(Direction direction) - { - if (Tank == null) - return false; - return direction switch - { - Direction.Left => _startPosX - Tank.Step > 0, - Direction.Up => _startPosY - Tank.Step > 0, - Direction.Right => _startPosX + _Width + Tank.Step < _pictureWidth, - Direction.Down => _startPosY + _Height + Tank.Step < _pictureHeight, - _ => false - - }; - } // Конструктор класса public DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height) { @@ -57,6 +43,21 @@ namespace Tank Tank = new EntityBase(speed, weight, bodyColor); } + public bool CanMove(Direction direction) + { + if (Tank == null) + return false; + return direction switch + { + Direction.Left => _startPosX - Tank.Step > 0, + Direction.Up => _startPosY - Tank.Step > 0, + Direction.Right => _startPosX + _Width + Tank.Step < _pictureWidth, + Direction.Down => _startPosY + _Height + Tank.Step < _pictureHeight, + _ => false + + }; + } + public void SetPosition(int x, int y) { _startPosX = x; -- 2.25.1 From 62e9cfef11b77888b43a040c01ce7cfba97182e3 Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Sun, 8 Oct 2023 00:30:07 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank/DrawArmoVehicle.cs | 13 ++----------- Tank/Tank/DrawTank.cs | 12 ++++++------ 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/Tank/Tank/DrawArmoVehicle.cs b/Tank/Tank/DrawArmoVehicle.cs index be5947b..aa389df 100644 --- a/Tank/Tank/DrawArmoVehicle.cs +++ b/Tank/Tank/DrawArmoVehicle.cs @@ -112,18 +112,9 @@ namespace Tank Brush BrushRandom = new SolidBrush(Tank?.BodyColor ?? Color.Black); //Корпус - Brush WhiteColor = new SolidBrush(Color.White); - g.FillRectangle(WhiteColor, _startPosX, _startPosY, 155, 30); - - Point[] pointsbody1 = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 110, _startPosY + 30), + Point[] pointsbody = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 110, _startPosY + 30), new Point(_startPosX + 142, _startPosY + 30), new Point(_startPosX + 120, _startPosY + 45), new Point(_startPosX + 12, _startPosY + 45) }; - g.FillPolygon(BrushRandom, pointsbody1); - - // Гусеница - g.FillRectangle(BrushRandom, 28 + _startPosX, 50 + _startPosY, 10, 3); - g.FillRectangle(BrushRandom, 53 + _startPosX, 50 + _startPosY, 10, 3); - g.FillRectangle(BrushRandom, 78 + _startPosX, 50 + _startPosY, 10, 3); - g.FillRectangle(BrushRandom, 103 + _startPosX, 50 + _startPosY, 10, 3); + g.FillPolygon(BrushRandom, pointsbody); // Колеса, катки Brush ColorBlack = new SolidBrush(Color.Black); diff --git a/Tank/Tank/DrawTank.cs b/Tank/Tank/DrawTank.cs index 8e94d83..2406af3 100644 --- a/Tank/Tank/DrawTank.cs +++ b/Tank/Tank/DrawTank.cs @@ -37,19 +37,19 @@ namespace Tank.DrawningObjects if (ArmoVehicle.Caterpillar) { - Pen pen = new(Color.Black); - Brush lineBrush = new SolidBrush(Color.Gray); // Гусеница // Отрисовка танковых катков - Brush BrushBlack = new SolidBrush(Color.Black); - g.FillEllipse(BrushBlack, _startPosX + 113, _startPosY + 41, 11, 11); - g.FillEllipse(BrushBlack, _startPosX + 13, _startPosY + 40, 11, 11); + Brush BrushRandom = new SolidBrush(Tank?.BodyColor ?? Color.Black); + g.FillRectangle(BrushRandom, 28 + _startPosX, 50 + _startPosY, 10, 3); + g.FillRectangle(BrushRandom, 53 + _startPosX, 50 + _startPosY, 10, 3); + g.FillRectangle(BrushRandom, 78 + _startPosX, 50 + _startPosY, 10, 3); + g.FillRectangle(BrushRandom, 103 + _startPosX, 50 + _startPosY, 10, 3); } if (ArmoVehicle.Tower) { - Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor); // Орудие + Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor); g.FillRectangle(bodyBrush, _startPosX + 111, _startPosY + 17, 70, 5); // Зенитное орудие -- 2.25.1 From af6582be5e2398b7083f45f45a564123345d7baf Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Sun, 8 Oct 2023 00:32:32 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank/DrawTank.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tank/Tank/DrawTank.cs b/Tank/Tank/DrawTank.cs index 2406af3..f473bf4 100644 --- a/Tank/Tank/DrawTank.cs +++ b/Tank/Tank/DrawTank.cs @@ -37,8 +37,7 @@ namespace Tank.DrawningObjects if (ArmoVehicle.Caterpillar) { - // Гусеница - // Отрисовка танковых катков + // Гусеница. Отрисовка танковых катков Brush BrushRandom = new SolidBrush(Tank?.BodyColor ?? Color.Black); g.FillRectangle(BrushRandom, 28 + _startPosX, 50 + _startPosY, 10, 3); g.FillRectangle(BrushRandom, 53 + _startPosX, 50 + _startPosY, 10, 3); -- 2.25.1