diff --git a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj index af03d74..e0f9d61 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj +++ b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj @@ -23,4 +23,8 @@ + + + + \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/DirectionType.cs b/AircraftCarrier/AircraftCarrier/Drawings/DirectionType.cs similarity index 60% rename from AircraftCarrier/AircraftCarrier/DirectionType.cs rename to AircraftCarrier/AircraftCarrier/Drawings/DirectionType.cs index 7492629..5ddd3e0 100644 --- a/AircraftCarrier/AircraftCarrier/DirectionType.cs +++ b/AircraftCarrier/AircraftCarrier/Drawings/DirectionType.cs @@ -1,7 +1,8 @@ -namespace AircraftCarrier; +namespace AircraftCarrier.Drawings; public enum DirectionType { + Unknown = -1, Up = 1, Down = 2, Left = 3, diff --git a/AircraftCarrier/AircraftCarrier/Drawings/DrawingAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/Drawings/DrawingAircraftCarrier.cs new file mode 100644 index 0000000..7285fd2 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/Drawings/DrawingAircraftCarrier.cs @@ -0,0 +1,37 @@ +using AircraftCarrier.Entities; + +namespace AircraftCarrier.Drawing; + +public class DrawingAircraftCarrier : DrawingSimpleAircraftCarrier +{ + public DrawingAircraftCarrier(int speed, double weight, Color primaryColor, Color secondaryColor, + bool hasDeck, bool hasСabin) : base() + { + entityAircraftCarrier = new EntityAircraftCarrier(speed, weight, primaryColor, secondaryColor, hasDeck, hasСabin); + } + + public override void DrawAircraftCarrier(Graphics g) + { + if (entityAircraftCarrier == null || + !(entityAircraftCarrier is EntityAircraftCarrier entitySimpleAircraftCarrier) || + !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + base.DrawAircraftCarrier(g); + + Brush brushPC = new SolidBrush(entitySimpleAircraftCarrier.PrimaryColor); + Brush brushSC = new SolidBrush(entitySimpleAircraftCarrier.SecondaryColor); + + if (entitySimpleAircraftCarrier.HasDeck) + { + g.FillRectangle(brushPC, _startPosX.Value + 50, _startPosY.Value + 40, 55, 20); + } + + if (entitySimpleAircraftCarrier.HasСabin) + { + g.FillRectangle(brushSC, _startPosX.Value + 105, _startPosY.Value + 25, 30, 50); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/DrawingAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/Drawings/DrawingSimpleAircraftCarrier.cs similarity index 53% rename from AircraftCarrier/AircraftCarrier/DrawingAircraftCarrier.cs rename to AircraftCarrier/AircraftCarrier/Drawings/DrawingSimpleAircraftCarrier.cs index b9e473c..36f7e3c 100644 --- a/AircraftCarrier/AircraftCarrier/DrawingAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/Drawings/DrawingSimpleAircraftCarrier.cs @@ -1,26 +1,36 @@ -namespace AircraftCarrier; +using AircraftCarrier.Drawings; -public class DrawingAircraftCarrier +namespace AircraftCarrier.Drawing; + +public class DrawingSimpleAircraftCarrier { - public EntityAircraftCarrier? EntityAircraftCarrier { get; private set; } - private int? _startPosX; - private int? _startPosY; + public EntitySimpleAircraftCarrier? entityAircraftCarrier { get; protected set; } + protected int? _startPosX; + protected int? _startPosY; private int? _pictureWidth; private int? _pictureHeight; private readonly int _drawingCarWidth = 250; private readonly int _drawingCarHeight = 100; - - public void Init(int speed, double weight, Color primaryColor, Color secondaryColor, - bool hasDeck, bool hasСabin) + + public int? GetPosX => _startPosX; + public int? GetPosY => _startPosY; + public int GetWidth => _drawingCarWidth; + public int GetHeight => _drawingCarHeight; + + + protected DrawingSimpleAircraftCarrier() { - EntityAircraftCarrier = new EntityAircraftCarrier(); - EntityAircraftCarrier.Init(speed, weight, primaryColor, secondaryColor, hasDeck, hasСabin); _startPosX = null; _startPosY = null; _pictureWidth = null; _pictureHeight = null; } + public DrawingSimpleAircraftCarrier(int speed, double weight, Color primaryColor) : this() + { + entityAircraftCarrier = new EntitySimpleAircraftCarrier(speed, weight, primaryColor); + } + public void SetPictureSize(int width, int height) { if (_drawingCarHeight > height || _drawingCarWidth > width) return; @@ -33,8 +43,8 @@ public class DrawingAircraftCarrier if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) return; if (x < 0 || y < 0 || - x+_drawingCarWidth > _pictureWidth || - y+_drawingCarHeight > _pictureHeight) return; + x + _drawingCarWidth > _pictureWidth || + y + _drawingCarHeight > _pictureHeight) return; _startPosX = x; _startPosY = y; @@ -42,7 +52,7 @@ public class DrawingAircraftCarrier public bool MoveAircraftCarrier(DirectionType direction) { - if (EntityAircraftCarrier == null || !_startPosX.HasValue || + if (entityAircraftCarrier == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; @@ -51,27 +61,27 @@ public class DrawingAircraftCarrier switch (direction) { case DirectionType.Up: - if (_startPosY - EntityAircraftCarrier.Step > 0) + if (_startPosY - entityAircraftCarrier.Step > 0) { - _startPosY -= (int)EntityAircraftCarrier.Step; + _startPosY -= (int)entityAircraftCarrier.Step; } return true; case DirectionType.Down: - if (_startPosY + EntityAircraftCarrier.Step + _drawingCarHeight < _pictureHeight) + if (_startPosY + entityAircraftCarrier.Step + _drawingCarHeight < _pictureHeight) { - _startPosY += (int)EntityAircraftCarrier.Step; + _startPosY += (int)entityAircraftCarrier.Step; } return true; case DirectionType.Left: - if (_startPosX - EntityAircraftCarrier.Step > 0) + if (_startPosX - entityAircraftCarrier.Step > 0) { - _startPosX -= (int)EntityAircraftCarrier.Step; + _startPosX -= (int)entityAircraftCarrier.Step; } return true; case DirectionType.Right: - if (_startPosX + EntityAircraftCarrier.Step + _drawingCarWidth < _pictureWidth) + if (_startPosX + entityAircraftCarrier.Step + _drawingCarWidth < _pictureWidth) { - _startPosX += (int)EntityAircraftCarrier.Step; + _startPosX += (int)entityAircraftCarrier.Step; } return true; default: @@ -79,37 +89,26 @@ public class DrawingAircraftCarrier } } - public void DrawAircraftCarrier(Graphics g) + public virtual void DrawAircraftCarrier(Graphics g) { - if (EntityAircraftCarrier == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (entityAircraftCarrier == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } - + Pen pen = new(Color.Black); - Brush brushPC = new SolidBrush(EntityAircraftCarrier.PrimaryColor); - Brush brushSC = new SolidBrush(EntityAircraftCarrier.SecondaryColor); - Point[] point = { + Point[] point = { new Point(_startPosX.Value, _startPosY.Value), new Point(_startPosX.Value + 200, _startPosY.Value), new Point(_startPosX.Value + 250, _startPosY.Value + 50), new Point(_startPosX.Value + 200, _startPosY.Value + 100), new Point(_startPosX.Value, _startPosY.Value + 100), }; + g.DrawPolygon(pen, point); g.DrawEllipse(pen, _startPosX.Value + 175, _startPosY.Value + 35, 30, 30); - - if (EntityAircraftCarrier.HasDeck) - { - g.FillRectangle(brushPC, _startPosX.Value + 50, _startPosY.Value + 40, 55, 20); - } - - if (EntityAircraftCarrier.HasСabin) - { - g.FillRectangle(brushSC, _startPosX.Value + 105, _startPosY.Value + 25, 30, 50); - } } } diff --git a/AircraftCarrier/AircraftCarrier/Entities/EntityAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/Entities/EntityAircraftCarrier.cs new file mode 100644 index 0000000..7ce67c5 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/Entities/EntityAircraftCarrier.cs @@ -0,0 +1,17 @@ +namespace AircraftCarrier.Entities; + +public class EntityAircraftCarrier : EntitySimpleAircraftCarrier +{ + public Color SecondaryColor { get; private set; } + public bool HasDeck { get; private set; } + public bool HasСabin { get; private set; } + + public EntityAircraftCarrier(int speed, double weight, Color primaryColor, Color secondaryColor, + bool hasDeck, bool hasСabin) + :base(speed, weight, primaryColor) + { + SecondaryColor = secondaryColor; + HasDeck = hasDeck; + HasСabin = hasСabin; + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/Entities/EntitySimpleAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/Entities/EntitySimpleAircraftCarrier.cs new file mode 100644 index 0000000..bde9c54 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/Entities/EntitySimpleAircraftCarrier.cs @@ -0,0 +1,16 @@ +namespace AircraftCarrier; + +public class EntitySimpleAircraftCarrier +{ + public int Speed { get; private set; } + public double Weight { get; private set; } + public double Step => Speed * 100 / Weight; + public Color PrimaryColor { get; private set; } + + public EntitySimpleAircraftCarrier(int speed, double weight, Color primaryColor) + { + Speed = speed; + Weight = weight; + PrimaryColor = primaryColor; + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs deleted file mode 100644 index e0ca730..0000000 --- a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace AircraftCarrier; - -public class EntityAircraftCarrier -{ - public int Speed { get; private set; } - public double Weight { get; private set; } - public Color PrimaryColor { get; private set; } - public Color SecondaryColor { get; private set; } - public bool HasDeck { get; private set; } - public bool HasСabin { get; private set; } - public double Step => Speed * 100 / Weight; - - public void Init(int speed, double weight, Color primaryColor, Color secondaryColor, - bool hasDeck, bool hasСabin) - { - Speed = speed; - Weight = weight; - PrimaryColor = primaryColor; - SecondaryColor = secondaryColor; - HasDeck = hasDeck; - HasСabin = hasСabin; - } -} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs index 0e80181..b0cf125 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs @@ -34,6 +34,7 @@ buttonDown = new Button(); buttonRight = new Button(); buttonUp = new Button(); + buttonCreateSimple = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit(); SuspendLayout(); // @@ -110,11 +111,23 @@ buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; // + // buttonCreateSimple + // + buttonCreateSimple.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateSimple.Location = new Point(120, 389); + buttonCreateSimple.Name = "buttonCreateSimple"; + buttonCreateSimple.Size = new Size(128, 29); + buttonCreateSimple.TabIndex = 6; + buttonCreateSimple.Text = "Создать контур"; + buttonCreateSimple.UseVisualStyleBackColor = true; + buttonCreateSimple.Click += buttonCreateSimple_Click; + // // FormAircraftCarrier // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 438); + Controls.Add(buttonCreateSimple); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -137,5 +150,6 @@ private Button buttonDown; private Button buttonRight; private Button buttonUp; + private Button buttonCreateSimple; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs index c751768..d3a8f73 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs @@ -1,10 +1,12 @@ using System.Security.Cryptography; +using AircraftCarrier.Drawing; +using AircraftCarrier.Drawings; namespace AircraftCarrier; public partial class FormAircraftCarrier : Form { - private DrawingAircraftCarrier? _drawingAircraftCarrier; + private DrawingSimpleAircraftCarrier? _drawingAircraftCarrier; public FormAircraftCarrier() { @@ -20,20 +22,36 @@ public partial class FormAircraftCarrier : Form pictureBoxAircraftCarrier.Image = bmp; } - private void ButtonCreate_Click(object sender, EventArgs e) + private void CreatedrawingAircraftCarrier(string name) { Random random = new(); - _drawingAircraftCarrier = new(); - _drawingAircraftCarrier.Init(random.Next(100, 200), random.Next(1000, 2000), - 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))); + switch (name) + { + case nameof(DrawingAircraftCarrier): + _drawingAircraftCarrier = new DrawingAircraftCarrier(random.Next(100, 200), random.Next(1000, 2000), + 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; + case nameof(DrawingSimpleAircraftCarrier): + _drawingAircraftCarrier = new DrawingSimpleAircraftCarrier(random.Next(100, 200), random.Next(1000, 2000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); + break; + default: + _drawingAircraftCarrier = null; + break; + } + if (_drawingAircraftCarrier == null) return; + _drawingAircraftCarrier.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); - _drawingAircraftCarrier.SetPosition(10, 10); + _drawingAircraftCarrier.SetPosition(random.Next(10, 50), random.Next(10, 50)); Draw(); } + private void ButtonCreate_Click(object sender, EventArgs e) => CreatedrawingAircraftCarrier(nameof(DrawingAircraftCarrier)); + + private void buttonCreateSimple_Click(object sender, EventArgs e) => CreatedrawingAircraftCarrier(nameof(DrawingSimpleAircraftCarrier)); + private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingAircraftCarrier == null) return;