first part lab2

This commit is contained in:
yu.shlyapin 2025-02-03 17:02:01 +04:00
parent d35129e2c1
commit aa2c0de477
9 changed files with 152 additions and 69 deletions

View File

@ -23,4 +23,8 @@
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="MovementStategy\" />
</ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,8 @@
namespace AircraftCarrier; namespace AircraftCarrier.Drawings;
public enum DirectionType public enum DirectionType
{ {
Unknown = -1,
Up = 1, Up = 1,
Down = 2, Down = 2,
Left = 3, Left = 3,

View File

@ -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);
}
}
}

View File

@ -1,26 +1,36 @@
namespace AircraftCarrier; using AircraftCarrier.Drawings;
public class DrawingAircraftCarrier namespace AircraftCarrier.Drawing;
public class DrawingSimpleAircraftCarrier
{ {
public EntityAircraftCarrier? EntityAircraftCarrier { get; private set; } public EntitySimpleAircraftCarrier? entityAircraftCarrier { get; protected set; }
private int? _startPosX; protected int? _startPosX;
private int? _startPosY; protected int? _startPosY;
private int? _pictureWidth; private int? _pictureWidth;
private int? _pictureHeight; private int? _pictureHeight;
private readonly int _drawingCarWidth = 250; private readonly int _drawingCarWidth = 250;
private readonly int _drawingCarHeight = 100; private readonly int _drawingCarHeight = 100;
public void Init(int speed, double weight, Color primaryColor, Color secondaryColor, public int? GetPosX => _startPosX;
bool hasDeck, bool hasСabin) 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; _startPosX = null;
_startPosY = null; _startPosY = null;
_pictureWidth = null; _pictureWidth = null;
_pictureHeight = 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) public void SetPictureSize(int width, int height)
{ {
if (_drawingCarHeight > height || _drawingCarWidth > width) return; if (_drawingCarHeight > height || _drawingCarWidth > width) return;
@ -42,7 +52,7 @@ public class DrawingAircraftCarrier
public bool MoveAircraftCarrier(DirectionType direction) public bool MoveAircraftCarrier(DirectionType direction)
{ {
if (EntityAircraftCarrier == null || !_startPosX.HasValue || if (entityAircraftCarrier == null || !_startPosX.HasValue ||
!_startPosY.HasValue) !_startPosY.HasValue)
{ {
return false; return false;
@ -51,27 +61,27 @@ public class DrawingAircraftCarrier
switch (direction) switch (direction)
{ {
case DirectionType.Up: case DirectionType.Up:
if (_startPosY - EntityAircraftCarrier.Step > 0) if (_startPosY - entityAircraftCarrier.Step > 0)
{ {
_startPosY -= (int)EntityAircraftCarrier.Step; _startPosY -= (int)entityAircraftCarrier.Step;
} }
return true; return true;
case DirectionType.Down: 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; return true;
case DirectionType.Left: case DirectionType.Left:
if (_startPosX - EntityAircraftCarrier.Step > 0) if (_startPosX - entityAircraftCarrier.Step > 0)
{ {
_startPosX -= (int)EntityAircraftCarrier.Step; _startPosX -= (int)entityAircraftCarrier.Step;
} }
return true; return true;
case DirectionType.Right: 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; return true;
default: default:
@ -79,17 +89,15 @@ 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; return;
} }
Pen pen = new(Color.Black); 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, _startPosY.Value),
@ -98,18 +106,9 @@ public class DrawingAircraftCarrier
new Point(_startPosX.Value + 200, _startPosY.Value + 100), new Point(_startPosX.Value + 200, _startPosY.Value + 100),
new Point(_startPosX.Value, _startPosY.Value + 100), new Point(_startPosX.Value, _startPosY.Value + 100),
}; };
g.DrawPolygon(pen, point); g.DrawPolygon(pen, point);
g.DrawEllipse(pen, _startPosX.Value + 175, _startPosY.Value + 35, 30, 30); 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);
}
} }
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -34,6 +34,7 @@
buttonDown = new Button(); buttonDown = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonCreateSimple = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -110,11 +111,23 @@
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click; 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 // FormAircraftCarrier
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 438); ClientSize = new Size(800, 438);
Controls.Add(buttonCreateSimple);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonDown); Controls.Add(buttonDown);
@ -137,5 +150,6 @@
private Button buttonDown; private Button buttonDown;
private Button buttonRight; private Button buttonRight;
private Button buttonUp; private Button buttonUp;
private Button buttonCreateSimple;
} }
} }

View File

@ -1,10 +1,12 @@
using System.Security.Cryptography; using System.Security.Cryptography;
using AircraftCarrier.Drawing;
using AircraftCarrier.Drawings;
namespace AircraftCarrier; namespace AircraftCarrier;
public partial class FormAircraftCarrier : Form public partial class FormAircraftCarrier : Form
{ {
private DrawingAircraftCarrier? _drawingAircraftCarrier; private DrawingSimpleAircraftCarrier? _drawingAircraftCarrier;
public FormAircraftCarrier() public FormAircraftCarrier()
{ {
@ -20,20 +22,36 @@ public partial class FormAircraftCarrier : Form
pictureBoxAircraftCarrier.Image = bmp; pictureBoxAircraftCarrier.Image = bmp;
} }
private void ButtonCreate_Click(object sender, EventArgs e) private void CreatedrawingAircraftCarrier(string name)
{ {
Random random = new(); Random random = new();
_drawingAircraftCarrier = new(); switch (name)
_drawingAircraftCarrier.Init(random.Next(100, 200), random.Next(1000, 2000), {
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)),
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)), 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.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
_drawingAircraftCarrier.SetPosition(10, 10); _drawingAircraftCarrier.SetPosition(random.Next(10, 50), random.Next(10, 50));
Draw(); 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) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawingAircraftCarrier == null) return; if (_drawingAircraftCarrier == null) return;