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

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;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawingStormtrooper
using ProjectStormtrooper.Entities;
namespace ProjectStormtrooper.Drawnings;
public class DrawningBaseStormtrooper
{
public EntityStormtrooper? EntityStormtrooper { get; private set; }
public EntityBaseStormtrooper? EntityBaseStormtrooper { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
@ -18,12 +18,12 @@ public class DrawingStormtrooper
/// <summary>
/// Левая координата прорисовки бомбардировщика
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки бомбардировщика
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки бомбардировщика
@ -36,27 +36,38 @@ public class DrawingStormtrooper
private readonly int _drawningStormtrooperHeight = 135;
/// <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)
public DrawningBaseStormtrooper()
{
EntityStormtrooper = new EntityStormtrooper();
EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor,
engines, bombs, rockets);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = 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>
@ -137,7 +148,7 @@ public class DrawingStormtrooper
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityStormtrooper == null || !_startPosX.HasValue ||
if (EntityBaseStormtrooper == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
@ -146,32 +157,32 @@ public class DrawingStormtrooper
{
//влево
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;
//вверх
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;
// вправо
case DirectionType.Right:
//TODO прописать логику сдвига в право
if (_startPosX + _drawningStormtrooperWidth + EntityStormtrooper.Step < _pictureWidth)
if (_startPosX + _drawningStormtrooperWidth + EntityBaseStormtrooper.Step < _pictureWidth)
{
_startPosX += (int)EntityStormtrooper.Step;
_startPosX += (int)EntityBaseStormtrooper.Step;
};
return true;
//вниз
case DirectionType.Down:
//TODO прописать логику сдвига в вниз
if (_startPosY + _drawningStormtrooperHeight + EntityStormtrooper.Step < _pictureHeight)
if (_startPosY + _drawningStormtrooperHeight + EntityBaseStormtrooper.Step < _pictureHeight)
{
_startPosY += (int)EntityStormtrooper.Step;
_startPosY += (int)EntityBaseStormtrooper.Step;
}
return true;
default:
@ -182,16 +193,16 @@ public class DrawingStormtrooper
/// Прорисовка объекта
/// </summary>
/// <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)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyColorBrush = new SolidBrush(EntityStormtrooper.BodyColor);
Brush additionalBrush = new SolidBrush(EntityStormtrooper.AdditionalColor);
Brush bodyColorBrush = new SolidBrush(EntityBaseStormtrooper.BodyColor);
//Тело бомбардировщика
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[2].X = _startPosX.Value + 20; Nose[2].Y = _startPosY.Value + 60;
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();
buttonLeft = new Button();
pictureBoxStormtrooper = new PictureBox();
buttonCreateBaseStormtrooper = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit();
SuspendLayout();
//
// buttonCreateStormtrooper
//
buttonCreateStormtrooper.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateStormtrooper.Location = new Point(35, 693);
buttonCreateStormtrooper.Location = new Point(21, 693);
buttonCreateStormtrooper.Name = "buttonCreateStormtrooper";
buttonCreateStormtrooper.Size = new Size(126, 32);
buttonCreateStormtrooper.Size = new Size(260, 32);
buttonCreateStormtrooper.TabIndex = 0;
buttonCreateStormtrooper.Text = "Создать";
buttonCreateStormtrooper.Text = "Создать бомбардировщик";
buttonCreateStormtrooper.UseVisualStyleBackColor = true;
buttonCreateStormtrooper.Click += buttonCreate_Click;
buttonCreateStormtrooper.Click += buttonCreateStormtrooper_Click;
//
// buttonUp
//
@ -106,11 +107,23 @@
pictureBoxStormtrooper.TabStop = false;
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
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1260, 737);
Controls.Add(buttonCreateBaseStormtrooper);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
@ -131,5 +144,6 @@
private Button buttonDown;
private Button buttonLeft;
private PictureBox pictureBoxStormtrooper;
private Button buttonCreateBaseStormtrooper;
}
}

View File

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