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

This commit is contained in:
ShuryginDima 2024-03-10 16:04:36 +03:00
parent 8b930c99d4
commit fdeb174e90
7 changed files with 228 additions and 116 deletions

View File

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

View File

@ -1,13 +1,18 @@
namespace ProjectSeaplane;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningSeaplane
using ProjectSeaplane.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.Drawnings;
public class DrawningPlane
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntitySeaplane? EntitySeaplane { get; private set; }
public EntityPlane? EntityPlane { get; protected set; }
/// <summary>
/// Ширина окна
@ -22,42 +27,56 @@ public class DrawningSeaplane
/// <summary>
/// Левая координата прорисовки гидросамолёта
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки гидросамолёта
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки гидросамолёта
/// </summary>
private readonly int _drawningSeaplaneWidth = 130;
private readonly int _drawningPlaneWidth = 130;
/// <summary>
/// Высота прорисовки гидросамолёта
/// </summary>
private readonly int _drawningSeaplaneHeight = 50;
private readonly int _drawningPlaneHeight = 45;
/// <summary>
/// Инициализация свойств
/// Пустой конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="floats">Признак наличия поплавков</param>
/// <param name="boat">Признак наличия лодки</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool boat)
private DrawningPlane()
{
EntitySeaplane = new EntitySeaplane();
EntitySeaplane.Init(speed, weight, bodyColor, additionalColor, floats, boat);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningPlane(int speed, double weight, Color bodyColor) : this()
{
EntityPlane = new EntityPlane(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningPlaneWidth">Ширина прорисовки гидросамолёта</param>
/// <param name="drawningPlaneHeight">Высота прорисовки гидросамолёта</param>
protected DrawningPlane(int _drawningPlaneWidth, int _drawningPlaneHeight) : this()
{
_drawningPlaneWidth = drawningPlaneWidth;
_pictureHeight = drawningPlaneHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
@ -66,7 +85,7 @@ public class DrawningSeaplane
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (height < _drawningSeaplaneHeight || width < _drawningSeaplaneWidth)
{
@ -121,7 +140,7 @@ public class DrawningSeaplane
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
@ -130,31 +149,31 @@ public class DrawningSeaplane
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntitySeaplane.Step > 0)
if (_startPosX.Value - EntityPlane.Step > 0)
{
_startPosX -= (int)EntitySeaplane.Step;
_startPosX -= (int)EntityPlane.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntitySeaplane.Step > 0)
if (_startPosY.Value - EntityPlane.Step > 0)
{
_startPosY -= (int)EntitySeaplane.Step;
_startPosY -= (int)EntityPlane.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningSeaplaneWidth + EntitySeaplane.Step < _pictureWidth)
if (_startPosX.Value + _drawningSeaplaneWidth + EntityPlane.Step < _pictureWidth)
{
_startPosX += (int)EntitySeaplane.Step;
_startPosX += (int)EntityPlane.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningSeaplaneHeight + EntitySeaplane.Step < _pictureHeight)
if (_startPosY.Value + _drawningSeaplaneHeight + EntityPlane.Step < _pictureHeight)
{
_startPosY += (int)EntitySeaplane.Step;
_startPosY += (int)EntityPlane.Step;
}
return true;
default:
@ -165,19 +184,16 @@ public class DrawningSeaplane
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityPlane == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black, 2);
Brush additionalBrush = new SolidBrush(EntitySeaplane.AdditionalColor);
//Отрисовка основных частей самолёта
Brush br = new SolidBrush(EntitySeaplane.BodyColor);
Brush br = new SolidBrush(EntityPlane.BodyColor);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 20, 20, 20);
g.FillEllipse(br, _startPosX.Value, _startPosY.Value + 20, 20, 20);
Point point1 = new Point(_startPosX.Value + 10, _startPosY.Value);
@ -192,7 +208,7 @@ public class DrawningSeaplane
g.DrawLine(pen, _startPosX.Value + 10, _startPosY.Value + 20, _startPosX.Value + 40, _startPosY.Value + 20);
g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 20, _startPosX.Value + 100, _startPosY.Value + 40);
g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 30, _startPosX.Value + 130, _startPosY.Value + 30);
//Крылья
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 20, 25, 5);
g.FillEllipse(br, _startPosX.Value, _startPosY.Value + 20, 25, 5);
@ -212,26 +228,5 @@ public class DrawningSeaplane
g.FillEllipse(br, _startPosX.Value + 35, _startPosY.Value + 45, 5, 5);
g.FillEllipse(br, _startPosX.Value + 42, _startPosY.Value + 45, 5, 5);
g.FillEllipse(br, _startPosX.Value + 87, _startPosY.Value + 45, 5, 5);
//Надувнвя лодка
if (EntitySeaplane.Boat)
{
g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10);
}
//Поплавки
if (EntitySeaplane.Floats)
{
g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15);
g.DrawRectangle(pen, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15);
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10);
}
}
}
}

View File

@ -0,0 +1,55 @@
using ProjectSeaplane.Entities;
using System.Drawing;
namespace ProjectSeaplane.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningSeaplane : DrawningPlane
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="floats">Признак наличия поплавков</param>
/// <param name="boat">Признак наличия лодки</param>
public DrawningSeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool boat) : base(130, 50)
{
EntityPlane = new EntitySeaplane(speed, weight, bodyColor, additionalColor, floats, boat);
}
public override void DrawTransport(Graphics g)
{
if (EntityPlane == null || EntityPlane is not EntitySeaplane seaplane || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black, 2);
Brush additionalBrush = new SolidBrush(seaplane.AdditionalColor);
base.DrawTransport(g);
//Надувнвя лодка
if (seaplane.Boat)
{
g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 15, 30, 10);
}
//Поплавки
if (seaplane.Floats)
{
g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 30, 4, 15);
g.DrawRectangle(pen, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 66, _startPosY.Value + 30, 4, 15);
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10);
g.FillEllipse(additionalBrush, _startPosX.Value + 20, _startPosY.Value + 40, 70, 10);
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.Entities;
/// <summary>
/// Класс-сущность "Самолёт"
/// </summary>
public class EntityPlane
{
/// <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 EntityPlane(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -1,24 +1,9 @@
namespace ProjectSeaplane;
namespace ProjectSeaplane.Entities;
/// <summary>
/// Класс-сущность "Гидросамолёт"
/// </summary>
public class EntitySeaplane
public class EntitySeaplane : EntityPlane
{
/// <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>
@ -35,11 +20,6 @@ public class EntitySeaplane
/// </summary>
public bool Boat { get; private set; }
/// <summary>
/// Шаг перемещения гидросамолёта
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса гидросамолёта
/// </summary>
@ -49,12 +29,8 @@ public class EntitySeaplane
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="floats">Признак наличия поплавков</param>
/// <param name="boat">Признак наличия лодки</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool floats, bool boat)
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool floats, bool boat)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Floats = floats;
Boat = boat;

View File

@ -34,6 +34,7 @@
buttonUp = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonCreatePlane = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit();
SuspendLayout();
//
@ -51,9 +52,9 @@
buttonCreateSeaplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateSeaplane.Location = new Point(12, 465);
buttonCreateSeaplane.Name = "buttonCreateSeaplane";
buttonCreateSeaplane.Size = new Size(75, 23);
buttonCreateSeaplane.Size = new Size(196, 23);
buttonCreateSeaplane.TabIndex = 1;
buttonCreateSeaplane.Text = "Создать";
buttonCreateSeaplane.Text = "Создать гидросамолёт";
buttonCreateSeaplane.UseVisualStyleBackColor = true;
buttonCreateSeaplane.Click += ButtonCreateSeaplane_Click;
//
@ -105,11 +106,23 @@
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonCreatePlane
//
buttonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreatePlane.Location = new Point(214, 465);
buttonCreatePlane.Name = "buttonCreatePlane";
buttonCreatePlane.Size = new Size(196, 23);
buttonCreatePlane.TabIndex = 6;
buttonCreatePlane.Text = "Создать самолёт";
buttonCreatePlane.UseVisualStyleBackColor = true;
buttonCreatePlane.Click += buttonCreatePlane_Click;
//
// FormSeaplane
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(900, 500);
Controls.Add(buttonCreatePlane);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
@ -130,5 +143,6 @@
private Button buttonUp;
private Button buttonDown;
private Button buttonRight;
private Button buttonCreatePlane;
}
}

View File

@ -1,4 +1,6 @@
namespace ProjectSeaplane;
using ProjectSeaplane.Drawnings;
namespace ProjectSeaplane;
/// <summary>
/// Форма работы с объектом "Спортивный автомобиль"
/// </summary>
@ -7,7 +9,7 @@ public partial class FormSeaplane : Form
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningSeaplane? _drawningSeaplane;
private DrawningPlane? _drawningPlane;
/// <summary>
/// Конструктор формы
/// </summary>
@ -20,33 +22,58 @@ public partial class FormSeaplane : Form
/// </summary>
private void Draw()
{
if (_drawningSeaplane == null)
if (_drawningPlane == null)
{
return;
}
Bitmap bmp = new(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningSeaplane.DrawTransport(gr);
_drawningPlane.DrawTransport(gr);
pictureBoxSeaplane.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningPlane):
_drawningPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningSeaplane):
_drawningPlane = new DrawningSeaplane(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;
}
_drawningPlane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
_drawningPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать гидросамолёт"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateSeaplane_Click(object sender, EventArgs e)
{
Random random = new();
_drawningSeaplane = new DrawningSeaplane();
_drawningSeaplane.Init(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawningSeaplane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height);
_drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonCreateSeaplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSeaplane));
/// <summary>
/// Обработка нажатия кнопки "Создать самолёт"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreatePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningPlane));
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
@ -54,7 +81,7 @@ public partial class FormSeaplane : Form
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningSeaplane == null)
if (_drawningPlane == null)
{
return;
}
@ -63,20 +90,16 @@ public partial class FormSeaplane : Form
switch (name)
{
case "buttonUp":
result =
_drawningSeaplane.MoveTransport(DirectionType.Up);
result = _drawningPlane.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningSeaplane.MoveTransport(DirectionType.Down);
result = _drawningPlane.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningSeaplane.MoveTransport(DirectionType.Left);
result = _drawningPlane.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningSeaplane.MoveTransport(DirectionType.Right);
result = _drawningPlane.MoveTransport(DirectionType.Right);
break;
}
if (result)