Добавление родителей и ввод конструкторов

This commit is contained in:
IlyasValiulov 2024-02-17 22:27:24 +04:00
parent dc7b7be31e
commit ff1d888ee6
8 changed files with 235 additions and 149 deletions

View File

@ -1,4 +1,4 @@
namespace ProjectWarmlyShip; namespace ProjectWarmlyShip.Drawnings;
/// <summary> /// <summary>
/// Направления при перемещении /// Направления при перемещении
/// </summary> /// </summary>

View File

@ -1,11 +1,13 @@
namespace ProjectWarmlyShip; using ProjectWarmlyShip.Entities;
public class DrawningWarmlyShip namespace ProjectWarmlyShip.Drawnings;
public class DrawningShip
{ {
/// <summary> /// <summary>
/// Класс-сущность /// Класс-сущность
/// </summary> /// </summary>
public EntityWarmlyShip? EntityWarmlyShip { get; private set; } public EntityShip? EntityShip { get; protected set; }
/// <summary> /// <summary>
/// Ширина окна /// Ширина окна
@ -20,12 +22,12 @@ public class DrawningWarmlyShip
/// <summary> /// <summary>
/// Левая координата прорисовки /// Левая координата прорисовки
/// </summary> /// </summary>
private int? _startPosX; protected int? _startPosX;
/// <summary> /// <summary>
/// Верхняя кооридната прорисовки /// Верхняя кооридната прорисовки
/// </summary> /// </summary>
private int? _startPosY; protected int? _startPosY;
/// <summary> /// <summary>
/// Ширина прорисовки /// Ширина прорисовки
@ -35,26 +37,37 @@ public class DrawningWarmlyShip
/// <summary> /// <summary>
/// Высота прорисовки /// Высота прорисовки
/// </summary> /// </summary>
private int _drawningShipHeight = 140; private int _drawningShipHeight = 80;
/// <summary> /// <summary>
/// Инициализация свойств /// Пустой конструктор
/// </summary> /// </summary>
/// <param name="speed"></param> private DrawningShip()
/// <param name="weight"></param>
/// <param name="bodycolor"></param>
/// <param name="additionalcolor"></param>
/// <param name="shpipipes"></param>
/// <param name="fueltank"></param>
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool shpipipes, bool fueltank)
{ {
EntityWarmlyShip = new EntityWarmlyShip();
EntityWarmlyShip.Init(speed, weight, bodycolor, additionalcolor, shpipipes, fueltank);
_pictureWidth = null; _pictureWidth = null;
_pictureHeight = null; _pictureHeight = null;
_startPosX = null; _startPosX = null;
_startPosY = null; _startPosY = null;
} }
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodycolor"></param>
public DrawningShip(int speed, double weight, Color bodycolor) : this()
{
EntityShip = new EntityShip(speed, weight, bodycolor);
}
/// <summary>
/// Конструктор для наследования
/// </summary>
/// <param name="_drawningShipWidth"></param>
/// <param name="_drawnShipHeight"></param>
protected DrawningShip(int _drawningShipWidth, int _drawnShipHeight) : this()
{
this._drawningShipWidth= _drawningShipWidth;
this._drawningShipHeight = _drawnShipHeight;
}
public bool SetPictureSize(int width, int height) public bool SetPictureSize(int width, int height)
{ {
// TODO проверка, что объект "влезает" в размеры поля // TODO проверка, что объект "влезает" в размеры поля
@ -105,7 +118,7 @@ public class DrawningWarmlyShip
} }
public bool MoveTransport(DirectionType direction) public bool MoveTransport(DirectionType direction)
{ {
if (EntityWarmlyShip == null || !_startPosX.HasValue || !_startPosY.HasValue) if (EntityShip == null || !_startPosX.HasValue || !_startPosY.HasValue)
{ {
return false; return false;
} }
@ -113,32 +126,32 @@ public class DrawningWarmlyShip
{ {
//влево //влево
case DirectionType.Left: case DirectionType.Left:
if (_startPosX.Value - EntityWarmlyShip.Step > 0) if (_startPosX.Value - EntityShip.Step > 0)
{ {
_startPosX -= (int)EntityWarmlyShip.Step; _startPosX -= (int)EntityShip.Step;
} }
return true; return true;
//вверх //вверх
case DirectionType.Up: case DirectionType.Up:
if (_startPosY.Value - EntityWarmlyShip.Step > 0) if (_startPosY.Value - EntityShip.Step > 0)
{ {
_startPosY -= (int)EntityWarmlyShip.Step; _startPosY -= (int)EntityShip.Step;
} }
return true; return true;
// вправо // вправо
case DirectionType.Right: case DirectionType.Right:
if (_startPosX.Value + _drawningShipWidth + EntityWarmlyShip.Step < _pictureWidth) if (_startPosX.Value + _drawningShipWidth + EntityShip.Step < _pictureWidth)
{ {
_startPosX += (int)EntityWarmlyShip.Step; _startPosX += (int)EntityShip.Step;
} }
return true; return true;
//вниз //вниз
case DirectionType.Down: case DirectionType.Down:
if (_startPosY.Value + _drawningShipHeight + EntityWarmlyShip.Step < _pictureHeight) if (_startPosY.Value + _drawningShipHeight + EntityShip.Step < _pictureHeight)
{ {
_startPosY += (int)EntityWarmlyShip.Step; _startPosY += (int)EntityShip.Step;
} }
return true; return true;
default: default:
return false; return false;
} }
@ -147,58 +160,24 @@ public class DrawningWarmlyShip
/// Отрисовка объекта /// Отрисовка объекта
/// </summary> /// </summary>
/// <param name="g"></param> /// <param name="g"></param>
public void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
if (EntityWarmlyShip == null || !_startPosX.HasValue || !_startPosY.HasValue) if (EntityShip == null || !_startPosX.HasValue || !_startPosY.HasValue)
{ {
return; return;
} }
Pen pen = new(Color.Black , 5); Pen pen = new(Color.Black, 5);
//надстройка
//трубы Brush brush = new SolidBrush(EntityShip.BodyColor);
Brush brush = new SolidBrush(EntityWarmlyShip.AdditionalColor); g.FillRectangle(brush, _startPosX.Value + 30, _startPosY.Value, 100, 30);
if (EntityWarmlyShip.ShipPipes) //корпус
g.FillPolygon(brush, new Point[]
{ {
g.FillRectangle(brush, _startPosX.Value + 70, _startPosY.Value, 12, 60); new Point(_startPosX.Value,_startPosY.Value + 30), new Point(_startPosX.Value + 150, _startPosY.Value + 30),
g.FillRectangle(brush, _startPosX.Value + 90, _startPosY.Value, 12, 60); new Point(_startPosX.Value + 150, _startPosY.Value + 30), new Point(_startPosX.Value + 120,_startPosY.Value + 80),
//надстройка new Point(_startPosX.Value + 120,_startPosY.Value + 80), new Point(_startPosX.Value + 30,_startPosY.Value + 80),
brush = new SolidBrush(EntityWarmlyShip.BodyColor); new Point(_startPosX.Value + 30,_startPosY.Value + 80), new Point(_startPosX.Value,_startPosY.Value + 30),
g.FillRectangle(brush, _startPosX.Value + 30, _startPosY.Value + 60, 100, 30); });
//корпус
g.FillPolygon(brush, new Point[]
{
new Point(_startPosX.Value,_startPosY.Value + 90), new Point(_startPosX.Value + 150, _startPosY.Value + 90),
new Point(_startPosX.Value + 150, _startPosY.Value + 90), new Point(_startPosX.Value + 120,_startPosY.Value + 140),
new Point(_startPosX.Value + 120,_startPosY.Value + 140), new Point(_startPosX.Value + 30,_startPosY.Value + 140),
new Point(_startPosX.Value + 30,_startPosY.Value + 140), new Point(_startPosX.Value,_startPosY.Value + 90),
});
_drawningShipHeight = 140;
}
else
{
//надстройка
brush = new SolidBrush(EntityWarmlyShip.BodyColor);
g.FillRectangle(brush, _startPosX.Value + 30, _startPosY.Value, 100, 30);
//корпус
g.FillPolygon(brush, new Point[]
{
new Point(_startPosX.Value,_startPosY.Value + 30), new Point(_startPosX.Value + 150, _startPosY.Value + 30),
new Point(_startPosX.Value + 150, _startPosY.Value + 30), new Point(_startPosX.Value + 120,_startPosY.Value + 80),
new Point(_startPosX.Value + 120,_startPosY.Value + 80), new Point(_startPosX.Value + 30,_startPosY.Value + 80),
new Point(_startPosX.Value + 30,_startPosY.Value + 80), new Point(_startPosX.Value,_startPosY.Value + 30),
});
_drawningShipHeight = 80;
}
//топливный бак
if (EntityWarmlyShip.FuelTank)
{
brush = new SolidBrush(EntityWarmlyShip.AdditionalColor);
if (EntityWarmlyShip.ShipPipes)
g.FillRectangle(brush, _startPosX.Value + 40, _startPosY.Value + 120, 70, 10);
else
g.FillRectangle(brush, _startPosX.Value + 40, _startPosY.Value + 60, 70, 10);
}
} }
} }

View File

@ -0,0 +1,49 @@
using ProjectWarmlyShip.Entities;
namespace ProjectWarmlyShip.Drawnings;
public class DrawningWarmlyShip : DrawningShip
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityWarmlyShip? EntityWarmlyShip { get; private set; }
/// <summary>
/// Конструктор
/// </summary>
/// <param name="additionalcolor"></param>
/// <param name="shpipipes"></param>
/// <param name="fueltank"></param>
public DrawningWarmlyShip(int speed, double weight, Color bodycolor, Color additionalcolor, bool shpipipes, bool fueltank) : base(150,140)
{
EntityShip = new EntityWarmlyShip(speed, weight, bodycolor, additionalcolor, shpipipes, fueltank); //дописать конструктор
}
public override void DrawTransport(Graphics g)
{
//оператор is совместимость объекта с заданным типом
if (EntityShip == null || EntityShip is not EntityWarmlyShip warmlyship || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Brush brush = new SolidBrush(warmlyship.AdditionalColor);
if (warmlyship.ShipPipes)
{
g.FillRectangle(brush, _startPosX.Value + 70, _startPosY.Value, 12, 60);
g.FillRectangle(brush, _startPosX.Value + 90, _startPosY.Value, 12, 60);
_startPosY += 60;
}
base.DrawTransport(g);
if (warmlyship.FuelTank)
{
//if (EntityWarmlyShip.ShipPipes)
//{
// g.FillRectangle(brush, _startPosX.Value + 40, _startPosY.Value + 120, 70, 10);
//}
//else
// g.FillRectangle(brush, _startPosX.Value + 40, _startPosY.Value + 60, 70, 10);
g.FillRectangle(brush, _startPosX.Value + 40, _startPosY.Value + 60, 70, 10);
}
if (warmlyship.ShipPipes) _startPosY -= 60;
}
}

View File

@ -0,0 +1,34 @@
namespace ProjectWarmlyShip.Entities;
public class EntityShip
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения судна
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodycolor"></param>
public EntityShip(int speed, double weight, Color bodycolor)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
}
}

View File

@ -0,0 +1,29 @@
namespace ProjectWarmlyShip.Entities;
public class EntityWarmlyShip : EntityShip
{
/// <summary>
/// Дополнительтный цвет
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак наличия труб
/// </summary>
public bool ShipPipes { get; private set; }
/// <summary>
/// Признак наличия топливного бака
/// </summary>
public bool FuelTank { get; private set; }
/// <summary>
/// Инициализация полей класса EntityWarmlyShip
/// </summary>
/// <param name="additionalcolor"></param>
/// <param name="shpipipes"></param>
/// <param name="fueltank"></param>
public EntityWarmlyShip(int speed, double weight, Color bodycolor, Color additionalcolor, bool shpipipes, bool fueltank) : base(speed, weight, bodycolor)
{
AdditionalColor = additionalcolor;
ShipPipes = shpipipes;
FuelTank = fueltank;
}
}

View File

@ -1,51 +0,0 @@
namespace ProjectWarmlyShip;
public class EntityWarmlyShip
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительтный цвет
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак наличия труб
/// </summary>
public bool ShipPipes { get; private set; }
/// <summary>
/// Признак наличия топливного бака
/// </summary>
public bool FuelTank { get; private set; }
/// <summary>
/// Шаг перемещения судна
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей класса WarmlyShip
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodycolor"></param>
/// <param name="additionalcolor"></param>
/// <param name="shpipipes"></param>
/// <param name="fueltank"></param>
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool shpipipes, bool fueltank)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
ShipPipes = shpipipes;
FuelTank = fueltank;
}
}

View File

@ -34,6 +34,7 @@
buttonRight = new Button(); buttonRight = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonСreateShip = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -55,9 +56,9 @@
buttonCreate.ForeColor = SystemColors.ControlText; buttonCreate.ForeColor = SystemColors.ControlText;
buttonCreate.Location = new Point(12, 331); buttonCreate.Location = new Point(12, 331);
buttonCreate.Name = "buttonCreate"; buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23); buttonCreate.Size = new Size(118, 23);
buttonCreate.TabIndex = 1; buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать теплоход";
buttonCreate.UseVisualStyleBackColor = false; buttonCreate.UseVisualStyleBackColor = false;
buttonCreate.Click += ButtonCreate_Click; buttonCreate.Click += ButtonCreate_Click;
// //
@ -109,11 +110,26 @@
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click; buttonDown.Click += ButtonMove_Click;
// //
// buttonСreateShip
//
buttonСreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonСreateShip.BackColor = SystemColors.ButtonHighlight;
buttonСreateShip.Cursor = Cursors.IBeam;
buttonСreateShip.ForeColor = SystemColors.ControlText;
buttonСreateShip.Location = new Point(146, 331);
buttonСreateShip.Name = "buttonСreateShip";
buttonСreateShip.Size = new Size(118, 23);
buttonСreateShip.TabIndex = 6;
buttonСreateShip.Text = "Создать судно";
buttonСreateShip.UseVisualStyleBackColor = false;
buttonСreateShip.Click += buttonСreateShip_Click;
//
// FormWarmlyShip // FormWarmlyShip
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(830, 366); ClientSize = new Size(830, 366);
Controls.Add(buttonСreateShip);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonRight); Controls.Add(buttonRight);
@ -135,5 +151,6 @@
private Button buttonRight; private Button buttonRight;
private Button buttonLeft; private Button buttonLeft;
private Button buttonDown; private Button buttonDown;
private Button buttonСreateShip;
} }
} }

View File

@ -1,31 +1,61 @@
namespace ProjectWarmlyShip using ProjectWarmlyShip.Drawnings;
namespace ProjectWarmlyShip
{ {
public partial class FormWarmlyShip : Form public partial class FormWarmlyShip : Form
{ {
private DrawningWarmlyShip? _drawningWarmlyShip; private DrawningShip? _drawningShip;
public FormWarmlyShip() public FormWarmlyShip()
{ {
InitializeComponent(); InitializeComponent();
} }
private void ButtonCreate_Click(object sender, EventArgs e) private void CreateObject(string type)
{ {
Random random = new(); Random random = new Random();
_drawningWarmlyShip = new DrawningWarmlyShip(); switch(type)
_drawningWarmlyShip.Init(random.Next(100, 300), random.Next(1000, 3000), {
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), case nameof(DrawningShip):
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), _drawningShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
_drawningWarmlyShip.SetPictureSize(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); break;
_drawningWarmlyShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); case nameof(DrawningWarmlyShip):
_drawningShip = new DrawningWarmlyShip(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)));
break;
default:
return;
}
_drawningShip.SetPictureSize(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningWarmlyShip.DrawTransport(gr); _drawningShip.DrawTransport(gr);
pictureBoxWarmlyShip.Image = bmp; pictureBoxWarmlyShip.Image = bmp;
}
/// <summary>
/// Кнопка создания теплохода
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningWarmlyShip));
}
/// <summary>
/// Кнопка создания судна
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonСreateShip_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningShip));
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawningWarmlyShip == null) if (_drawningShip == null)
{ {
return; return;
} }
@ -34,27 +64,26 @@
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
result = _drawningWarmlyShip.MoveTransport(DirectionType.Up); result = _drawningShip.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
result = _drawningWarmlyShip.MoveTransport(DirectionType.Down); result = _drawningShip.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
result = _drawningWarmlyShip.MoveTransport(DirectionType.Left); result = _drawningShip.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
result = _drawningWarmlyShip.MoveTransport(DirectionType.Right); result = _drawningShip.MoveTransport(DirectionType.Right);
break; break;
} }
if (result) if (result)
{ {
Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningWarmlyShip.DrawTransport(gr); _drawningShip.DrawTransport(gr);
pictureBoxWarmlyShip.Image = bmp; pictureBoxWarmlyShip.Image = bmp;
} }
} }
} }
} }