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

This commit is contained in:
Ilya Ryabov 2024-02-17 19:33:33 +04:00
parent 701a08c583
commit d70125006c
8 changed files with 248 additions and 148 deletions

View File

@ -0,0 +1,56 @@
using ProjectStormtrooper.Entities;
namespace ProjectStormtrooper.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawingStormtrooper : DrawningBaseStormtrooper
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="rockets">Признак наличия ракет</param>
public DrawingStormtrooper(int speed, double weight, Color bodyColor, Color additionalColor, bool bombs, bool rockets) : base(140, 135)
{
EntityBaseStormtrooper = new EntityStormtrooper(speed, weight, bodyColor, additionalColor, bombs, rockets);
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public override void DrawTransport(Graphics g)
{
if (EntityBaseStormtrooper == null ||EntityBaseStormtrooper is not EntityStormtrooper entityStormtrooper|| !_startPosX.HasValue ||!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(entityStormtrooper.AdditionalColor);
base.DrawTransport(g);
//Ракеты бомбардировщика
if (entityStormtrooper.Rockets)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 20, 15, 5);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 110, 15, 5);
}
//Бомбы бомбардировщика
if (entityStormtrooper.Bombs)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 40, 10, 10);
g.FillRectangle(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 90, 10, 10);
}
}
}

View File

@ -1,10 +1,10 @@
namespace ProjectStormtrooper; using ProjectStormtrooper.Entities;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности namespace ProjectStormtrooper.Drawnings;
/// </summary>
public class DrawingStormtrooper public class DrawningBaseStormtrooper
{ {
public EntityStormtrooper? EntityStormtrooper { get; private set; } public EntityBaseStormtrooper? EntityBaseStormtrooper { get; protected set; }
/// <summary> /// <summary>
/// Ширина окна /// Ширина окна
/// </summary> /// </summary>
@ -18,12 +18,12 @@ public class DrawingStormtrooper
/// <summary> /// <summary>
/// Левая координата прорисовки бомбардировщика /// Левая координата прорисовки бомбардировщика
/// </summary> /// </summary>
private int? _startPosX; protected int? _startPosX;
/// <summary> /// <summary>
/// Верхняя кооридната прорисовки бомбардировщика /// Верхняя кооридната прорисовки бомбардировщика
/// </summary> /// </summary>
private int? _startPosY; protected int? _startPosY;
/// <summary> /// <summary>
/// Ширина прорисовки бомбардировщика /// Ширина прорисовки бомбардировщика
@ -36,27 +36,38 @@ public class DrawingStormtrooper
private readonly int _drawningStormtrooperHeight = 135; private readonly int _drawningStormtrooperHeight = 135;
/// <summary> /// <summary>
/// Инициализация свойств /// Пустой конструктор
/// </summary> /// </summary>
/// <param name="speed">Скорость</param> public DrawningBaseStormtrooper()
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="engines">Признак наличия двигателей</param>
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="rockets">Признак наличия ракет</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool engines, bool bombs, bool rockets)
{ {
EntityStormtrooper = new EntityStormtrooper();
EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor,
engines, bombs, rockets);
_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 DrawningBaseStormtrooper(int speed, double weight, Color bodyColor) : this()
{
EntityBaseStormtrooper = new EntityBaseStormtrooper(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningStormtrooperWidth">Ширина прорисовки бомбардировщика</param>
/// <param name="drawningStormtrooperHeight">Высота прорисовки бомбардировщика</param>
protected DrawningBaseStormtrooper(int drawningStormtrooperWidth ,int drawningStormtrooperHeight ) : this()
{
_drawningStormtrooperHeight= drawningStormtrooperHeight;
_drawningStormtrooperWidth= drawningStormtrooperWidth;
}
/// <summary> /// <summary>
/// Установка границ поля /// Установка границ поля
/// </summary> /// </summary>
@ -137,7 +148,7 @@ public class DrawingStormtrooper
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns> /// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction) public bool MoveTransport(DirectionType direction)
{ {
if (EntityStormtrooper == null || !_startPosX.HasValue || if (EntityBaseStormtrooper == null || !_startPosX.HasValue ||
!_startPosY.HasValue) !_startPosY.HasValue)
{ {
return false; return false;
@ -146,32 +157,32 @@ public class DrawingStormtrooper
{ {
//влево //влево
case DirectionType.Left: case DirectionType.Left:
if (_startPosX.Value - EntityStormtrooper.Step > 0) if (_startPosX.Value - EntityBaseStormtrooper.Step > 0)
{ {
_startPosX -= (int)EntityStormtrooper.Step; _startPosX -= (int)EntityBaseStormtrooper.Step;
} }
return true; return true;
//вверх //вверх
case DirectionType.Up: case DirectionType.Up:
if (_startPosY.Value - EntityStormtrooper.Step > 0) if (_startPosY.Value - EntityBaseStormtrooper.Step > 0)
{ {
_startPosY -= (int)EntityStormtrooper.Step; _startPosY -= (int)EntityBaseStormtrooper.Step;
} }
return true; return true;
// вправо // вправо
case DirectionType.Right: case DirectionType.Right:
//TODO прописать логику сдвига в право //TODO прописать логику сдвига в право
if (_startPosX + _drawningStormtrooperWidth + EntityStormtrooper.Step < _pictureWidth) if (_startPosX + _drawningStormtrooperWidth + EntityBaseStormtrooper.Step < _pictureWidth)
{ {
_startPosX += (int)EntityStormtrooper.Step; _startPosX += (int)EntityBaseStormtrooper.Step;
}; };
return true; return true;
//вниз //вниз
case DirectionType.Down: case DirectionType.Down:
//TODO прописать логику сдвига в вниз //TODO прописать логику сдвига в вниз
if (_startPosY + _drawningStormtrooperHeight + EntityStormtrooper.Step < _pictureHeight) if (_startPosY + _drawningStormtrooperHeight + EntityBaseStormtrooper.Step < _pictureHeight)
{ {
_startPosY += (int)EntityStormtrooper.Step; _startPosY += (int)EntityBaseStormtrooper.Step;
} }
return true; return true;
default: default:
@ -182,16 +193,16 @@ public class DrawingStormtrooper
/// Прорисовка объекта /// Прорисовка объекта
/// </summary> /// </summary>
/// <param name="g"></param> /// <param name="g"></param>
public void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
if (EntityStormtrooper == null || !_startPosX.HasValue || if (EntityBaseStormtrooper == null || !_startPosX.HasValue ||
!_startPosY.HasValue) !_startPosY.HasValue)
{ {
return; return;
} }
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush bodyColorBrush = new SolidBrush(EntityStormtrooper.BodyColor); Brush bodyColorBrush = new SolidBrush(EntityBaseStormtrooper.BodyColor);
Brush additionalBrush = new SolidBrush(EntityStormtrooper.AdditionalColor);
//Тело бомбардировщика //Тело бомбардировщика
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20); g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20);
//Задние крылья бомбардировщика //Задние крылья бомбардировщика
@ -213,21 +224,7 @@ public class DrawingStormtrooper
Nose[1].X = _startPosX.Value; Nose[1].Y = _startPosY.Value + 70; Nose[1].X = _startPosX.Value; Nose[1].Y = _startPosY.Value + 70;
Nose[2].X = _startPosX.Value + 20; Nose[2].Y = _startPosY.Value + 60; Nose[2].X = _startPosX.Value + 20; Nose[2].Y = _startPosY.Value + 60;
g.FillPolygon(bodyColorBrush, Nose); g.FillPolygon(bodyColorBrush, Nose);
//Ракеты бомбардировщика
if (EntityStormtrooper.Rockets)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 20, 15, 5);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 110, 15, 5);
} }
//Бомбы бомбардировщика
if (EntityStormtrooper.Bombs)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 40, 10, 10);
g.FillRectangle(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 90, 10, 10);
}
}
} }

View File

@ -0,0 +1,40 @@
namespace ProjectStormtrooper.Entities;
/// <summary>
/// Класс-сущность "Базовый Бомбардировщик"
/// </summary>
public class EntityBaseStormtrooper
{
/// <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 EntityBaseStormtrooper(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,41 @@
namespace ProjectStormtrooper.Entities;
/// <summary>
/// Класс-сущность "Бомбардировщик"
/// </summary>
public class EntityStormtrooper : EntityBaseStormtrooper
{
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; protected set; }
/// <summary>
/// Признак (опция) наличия ракет
/// </summary>
public bool Rockets { get; private set; }
/// <summary>
/// Признак (опция) наличия бомб
/// </summary>
public bool Bombs { get; private set; }
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес бомбардировщика</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="rockets">Признак наличия ракет</param>
public EntityStormtrooper(int speed, double weight, Color bodyColor, Color additionalColor, bool bombs, bool rockets) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Bombs = bombs;
Rockets = rockets;
}
}

View File

@ -1,71 +0,0 @@
namespace ProjectStormtrooper;
/// <summary>
/// Класс-сущность "Бомбардировщик"
/// </summary>
public class EntityStormtrooper
{
/// <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 Engines { get; private set; }
/// <summary>
/// Признак (опция) наличия ракет
/// </summary>
public bool Rockets { get; private set; }
/// <summary>
/// Признак (опция) наличия бомб
/// </summary>
public bool Bombs { 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>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="engines">Признак наличия двигателей</param>
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="rockets">Признак наличия ракет</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool engines, bool bombs, bool rockets)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Engines = engines;
Bombs = bombs;
Rockets = rockets;
}
}

View File

@ -34,19 +34,20 @@
buttonDown = new Button(); buttonDown = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
pictureBoxStormtrooper = new PictureBox(); pictureBoxStormtrooper = new PictureBox();
buttonCreateBaseStormtrooper = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// buttonCreateStormtrooper // buttonCreateStormtrooper
// //
buttonCreateStormtrooper.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateStormtrooper.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateStormtrooper.Location = new Point(35, 693); buttonCreateStormtrooper.Location = new Point(21, 693);
buttonCreateStormtrooper.Name = "buttonCreateStormtrooper"; buttonCreateStormtrooper.Name = "buttonCreateStormtrooper";
buttonCreateStormtrooper.Size = new Size(126, 32); buttonCreateStormtrooper.Size = new Size(260, 32);
buttonCreateStormtrooper.TabIndex = 0; buttonCreateStormtrooper.TabIndex = 0;
buttonCreateStormtrooper.Text = "Создать"; buttonCreateStormtrooper.Text = "Создать бомбардировщик";
buttonCreateStormtrooper.UseVisualStyleBackColor = true; buttonCreateStormtrooper.UseVisualStyleBackColor = true;
buttonCreateStormtrooper.Click += buttonCreate_Click; buttonCreateStormtrooper.Click += buttonCreateStormtrooper_Click;
// //
// buttonUp // buttonUp
// //
@ -106,11 +107,23 @@
pictureBoxStormtrooper.TabStop = false; pictureBoxStormtrooper.TabStop = false;
pictureBoxStormtrooper.Click += ButtonMove_Click; pictureBoxStormtrooper.Click += ButtonMove_Click;
// //
// buttonCreateBaseStormtrooper
//
buttonCreateBaseStormtrooper.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateBaseStormtrooper.Location = new Point(287, 693);
buttonCreateBaseStormtrooper.Name = "buttonCreateBaseStormtrooper";
buttonCreateBaseStormtrooper.Size = new Size(260, 32);
buttonCreateBaseStormtrooper.TabIndex = 6;
buttonCreateBaseStormtrooper.Text = "Создать базовый бомбардировщик";
buttonCreateBaseStormtrooper.UseVisualStyleBackColor = true;
buttonCreateBaseStormtrooper.Click += buttonCreateBaseStormtrooper_Click;
//
// FormStormtrooper // FormStormtrooper
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1260, 737); ClientSize = new Size(1260, 737);
Controls.Add(buttonCreateBaseStormtrooper);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonRight); Controls.Add(buttonRight);
@ -131,5 +144,6 @@
private Button buttonDown; private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private PictureBox pictureBoxStormtrooper; private PictureBox pictureBoxStormtrooper;
private Button buttonCreateBaseStormtrooper;
} }
} }

View File

@ -1,4 +1,6 @@
namespace ProjectStormtrooper using ProjectStormtrooper.Drawnings;
namespace ProjectStormtrooper
{ {
/// <summary> /// <summary>
/// Форма работы с объектом "Бомбардировщик" /// Форма работы с объектом "Бомбардировщик"
@ -8,7 +10,7 @@
/// <summary> /// <summary>
/// Поле-объект для прорисовки объекта /// Поле-объект для прорисовки объекта
/// </summary> /// </summary>
private DrawingStormtrooper? _drawningStormtrooper; private DrawningBaseStormtrooper? _drawningBaseStormtrooper;
/// <summary> /// <summary>
/// Конструктор формы /// Конструктор формы
@ -24,38 +26,57 @@
/// </summary> /// </summary>
private void Draw() private void Draw()
{ {
if (_drawningStormtrooper == null) if (_drawningBaseStormtrooper == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxStormtrooper.Width, Bitmap bmp = new(pictureBoxStormtrooper.Width,
pictureBoxStormtrooper.Height); pictureBoxStormtrooper.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningStormtrooper.DrawTransport(gr); _drawningBaseStormtrooper.DrawTransport(gr);
pictureBoxStormtrooper.Image = bmp; pictureBoxStormtrooper.Image = bmp;
} }
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningBaseStormtrooper):
_drawningBaseStormtrooper = new DrawningBaseStormtrooper(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(DrawingStormtrooper):
_drawningBaseStormtrooper = new DrawingStormtrooper(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;
}
_drawningBaseStormtrooper.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
_drawningBaseStormtrooper.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary> /// <summary>
/// Обработка нажатия кнопки "Создать" /// Обработка нажатия кнопки "Создать"
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreateStormtrooper_Click(object sender, EventArgs e)
{ {
Random random = new(); CreateObject(nameof(DrawingStormtrooper));
_drawningStormtrooper = new DrawingStormtrooper(); }
_drawningStormtrooper.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)), Convert.ToBoolean(random.Next(0, 2)));
_drawningStormtrooper.SetPictureSize(pictureBoxStormtrooper.Width,pictureBoxStormtrooper.Height);
_drawningStormtrooper.SetPosition(random.Next(10, 100), random.Next(10,100));
Draw();
private void buttonCreateBaseStormtrooper_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningBaseStormtrooper));
} }
/// <summary> /// <summary>
@ -65,7 +86,7 @@
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawningStormtrooper == null) if (_drawningBaseStormtrooper == null)
{ {
return; return;
} }
@ -75,19 +96,19 @@
{ {
case "buttonUp": case "buttonUp":
result = result =
_drawningStormtrooper.MoveTransport(DirectionType.Up); _drawningBaseStormtrooper.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
result = result =
_drawningStormtrooper.MoveTransport(DirectionType.Down); _drawningBaseStormtrooper.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
result = result =
_drawningStormtrooper.MoveTransport(DirectionType.Left); _drawningBaseStormtrooper.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
result = result =
_drawningStormtrooper.MoveTransport(DirectionType.Right); _drawningBaseStormtrooper.MoveTransport(DirectionType.Right);
break; break;
} }
if (result) if (result)
@ -95,5 +116,7 @@
Draw(); Draw();
} }
} }
} }
} }