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

This commit is contained in:
Geo7312 2024-02-23 20:03:01 +04:00
parent 8861b13b44
commit 527d87741a
7 changed files with 290 additions and 174 deletions

View File

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

View File

@ -1,14 +1,18 @@
namespace ProjectTrolleybus;
using ProjectTrolleybus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningTrolleybus
namespace ProjectTrolleybus.Drawnings;
public class DrawningTrolleyB
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTrolleybus? EntityTrolleybus { get; private set; }
public EntityTrolleyB? EntityTrolleyB { get; protected set; }
/// <summary>
/// Ширина окна
@ -23,42 +27,57 @@ public class DrawningTrolleybus
/// <summary>
/// Левая координата прорисовки транспорта
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки транспорта
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки транспорта
/// </summary>
private readonly int _drawningVehicleWidth = 100;
private readonly int _drawningVehicleWidth = 90;
/// <summary>
/// Высота прорисовки транспорта
/// </summary>
private readonly int _drawningVehicleHeight = 48;
private readonly int _drawningVehicleHeight = 33;
/// <summary>
/// Инициализация свойств
/// Пустой конструктор
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <param name="additionalColor"></param>
/// <param name="batteryCompartment"></param>
/// <param name="horns"></param>
///
public void Init(EntityTrolleybus _Tr)
private DrawningTrolleyB()
{
EntityTrolleybus = _Tr;
_pictureHeight = null;
_pictureWidth = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningTrolleyB(int speed, double weight, Color bodyColor) : this()
{
EntityTrolleyB = new EntityTrolleyB(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningVehicleWidth">Ширина прорисовки транспорта</param>
/// <param name="drawningVehicleHeight">Высота прорисовки транспорта</param>
protected DrawningTrolleyB(int drawningVehicleWidth, int drawningVehicleHeight) : this()
{
_drawningVehicleWidth = drawningVehicleWidth;
_drawningVehicleHeight = drawningVehicleHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
@ -102,7 +121,6 @@ public class DrawningTrolleybus
{
return;
}
//TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
//то надо изменить координаты, чтобы он оставался в этих границах
if (x + _drawningVehicleWidth > _pictureWidth)
{
@ -127,7 +145,7 @@ public class DrawningTrolleybus
/// <returns></returns>
public bool MoveVehicle(DirectionType direction)
{
if (EntityTrolleybus == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityTrolleyB == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
@ -136,48 +154,48 @@ public class DrawningTrolleybus
{
// влево
case DirectionType.Left:
if (_startPosX.Value - EntityTrolleybus.Step > 0)
if (_startPosX.Value - EntityTrolleyB.Step > 0)
{
_startPosX -= (int)EntityTrolleybus.Step;
_startPosX -= (int)EntityTrolleyB.Step;
}
return true;
// вверх
case DirectionType.Up:
if (_startPosY.Value - EntityTrolleybus.Step > 0)
if (_startPosY.Value - EntityTrolleyB.Step > 0)
{
_startPosY -= (int)EntityTrolleybus.Step;
_startPosY -= (int)EntityTrolleyB.Step;
}
return true;
// вправо
// вправо
case DirectionType.Right:
{
if (_startPosX.Value + _drawningVehicleWidth + EntityTrolleybus.Step < _pictureWidth)
if (_startPosX.Value + _drawningVehicleWidth + EntityTrolleyB.Step < _pictureWidth)
{
_startPosX += (int)EntityTrolleybus.Step;
_startPosX += (int)EntityTrolleyB.Step;
}
}
return true;
// вниз
// вниз
case DirectionType.Down:
{
if (_startPosY.Value + _drawningVehicleHeight + EntityTrolleybus.Step < _pictureHeight)
if (_startPosY.Value + _drawningVehicleHeight + EntityTrolleyB.Step < _pictureHeight)
{
_startPosY += (int)EntityTrolleybus.Step;
_startPosY += (int)EntityTrolleyB.Step;
}
}
}
return true;
default:
return false;
}
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityTrolleybus == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityTrolleyB == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
@ -186,68 +204,44 @@ public class DrawningTrolleybus
//Колёса
pen.Width = 2;
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 36, 12, 12);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 36, 12, 12);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 21, 12, 12);
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 21, 12, 12);
pen.Width = 1;
Brush brBlackWheel = new SolidBrush(Color.Black);
g.FillEllipse(brBlackWheel, _startPosX.Value + 20, _startPosY.Value + 36, 12, 12);
g.FillEllipse(brBlackWheel, _startPosX.Value + 80, _startPosY.Value + 36, 12, 12);
g.FillEllipse(brBlackWheel, _startPosX.Value + 10, _startPosY.Value + 21, 12, 12);
g.FillEllipse(brBlackWheel, _startPosX.Value + 70, _startPosY.Value + 21, 12, 12);
Brush brYelWheel = new SolidBrush(Color.Goldenrod);
g.FillEllipse(brYelWheel, _startPosX.Value + 22, _startPosY.Value + 38, 8, 8);
g.FillEllipse(brYelWheel, _startPosX.Value + 82, _startPosY.Value + 38, 8, 8);
g.FillEllipse(brYelWheel, _startPosX.Value + 12, _startPosY.Value + 23, 8, 8);
g.FillEllipse(brYelWheel, _startPosX.Value + 72, _startPosY.Value + 23, 8, 8);
//Кузов
Brush br = new SolidBrush(EntityTrolleybus.BodyColor);
g.FillRectangle(br, _startPosX.Value + 11, _startPosY.Value + 16, 89, 24);
Brush br = new SolidBrush(EntityTrolleyB.BodyColor);
g.FillRectangle(br, _startPosX.Value + 1, _startPosY.Value + 1, 89, 24);
//Границы Автомобиля
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 15, 90, 25);
g.DrawRectangle(pen, _startPosX.Value + 36, _startPosY.Value + 25, 10, 15);
g.DrawRectangle(pen, _startPosX.Value + 88, _startPosY.Value + 19, 12, 12);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 90, 25);
g.DrawRectangle(pen, _startPosX.Value + 26, _startPosY.Value + 10, 10, 15);
g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 4, 12, 12);
pen.Width = 2;
g.DrawEllipse(pen, _startPosX.Value + 12, _startPosY.Value + 20, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 24, _startPosY.Value + 20, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 20, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 62, _startPosY.Value + 20, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 74, _startPosY.Value + 20, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 2, _startPosY.Value + 5, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 5, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 5, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 52, _startPosY.Value + 5, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 64, _startPosY.Value + 5, 8, 10);
pen.Width = 1;
//Стекла и кабина
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX.Value + 89, _startPosY.Value + 20, 11, 11);
g.FillEllipse(brBlue, _startPosX.Value + 12, _startPosY.Value + 20, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 24, _startPosY.Value + 20, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 50, _startPosY.Value + 20, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 62, _startPosY.Value + 20, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 74, _startPosY.Value + 20, 8, 10);
g.FillRectangle(brBlue, _startPosX.Value + 79, _startPosY.Value + 5, 11, 11);
g.FillEllipse(brBlue, _startPosX.Value + 2, _startPosY.Value + 5, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 14, _startPosY.Value + 5, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 40, _startPosY.Value + 5, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 52, _startPosY.Value + 5, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 64, _startPosY.Value + 5, 8, 10);
//Дверь
Brush brBlack = new SolidBrush(Color.LightGray);
g.FillRectangle(brBlack, _startPosX.Value + 37, _startPosY.Value + 26, 9, 14);
Brush additBrush = new SolidBrush(EntityTrolleybus.AdditionalColor);
// "Рога" для проводов
if (EntityTrolleybus.Horns)
{
pen.Width = 2;
g.DrawLine(pen, _startPosX.Value + 77, _startPosY.Value + 15, _startPosX.Value + 52, _startPosY.Value + 2);
pen.Width = 1;
g.DrawRectangle(pen, _startPosX.Value + 34, _startPosY.Value, 20, 5);
g.FillRectangle(additBrush, _startPosX.Value + 35, _startPosY.Value + 1, 19, 4);
}
// Отсек под батареи
if (EntityTrolleybus.BatteryCompartment)
{
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 24, 10, 12);
g.FillRectangle(additBrush, _startPosX.Value + 1, _startPosY.Value + 25, 9, 11);
}
g.FillRectangle(brBlack, _startPosX.Value + 27, _startPosY.Value + 11, 9, 14);
}
}

View File

@ -0,0 +1,64 @@
using ProjectTrolleybus.Entities;
using System.Drawing;
namespace ProjectTrolleybus.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningTrolleybus : DrawningTrolleyB
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <param name="additionalColor"></param>
/// <param name="batteryCompartment"></param>
/// <param name="horns"></param>
///
public DrawningTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, bool batteryCompartment, bool horns) : base(100, 48)
{
EntityTrolleyB = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, batteryCompartment, horns);
}
public override void DrawTransport(Graphics g)
{
if (EntityTrolleyB == null || EntityTrolleyB is not EntityTrolleybus trolleybus || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additBrush = new SolidBrush(trolleybus.AdditionalColor);
// "Рога" для проводов
if (trolleybus.Horns)
{
pen.Width = 2;
g.DrawLine(pen, _startPosX.Value + 77, _startPosY.Value + 15, _startPosX.Value + 52, _startPosY.Value + 2);
pen.Width = 1;
g.DrawRectangle(pen, _startPosX.Value + 34, _startPosY.Value, 20, 5);
g.FillRectangle(additBrush, _startPosX.Value + 35, _startPosY.Value + 1, 19, 4);
}
// Отсек под батареи
if (trolleybus .BatteryCompartment)
{
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 24, 10, 12);
g.FillRectangle(additBrush, _startPosX.Value + 1, _startPosY.Value + 25, 9, 11);
}
_startPosX += 10;
_startPosY += 15;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 15;
}
}

View File

@ -0,0 +1,41 @@
namespace ProjectTrolleybus.Entities;
/// <summary>
/// Класс-Сущность "Троллейбус"
/// </summary>
public class EntityTrolleyB
{
/// <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 EntityTrolleyB(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -1,24 +1,9 @@
namespace ProjectTrolleybus;
namespace ProjectTrolleybus.Entities;
/// <summary>
/// Класс-Сущность "Троллейбус"
/// </summary>
public class EntityTrolleybus
public class EntityTrolleybus : EntityTrolleyB
{
/// <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>
@ -27,15 +12,13 @@ public class EntityTrolleybus
/// <summary>
/// Признак (опция) наличия отсека под батареи
/// </summary>
public bool BatteryCompartment { get; private set; }
public bool BatteryCompartment { get; private set; }
/// <summary>
/// Признак (опция) наличия "Рогов" для подключения проводов
/// </summary>
public bool Horns { get; private set; }
public double Step => Speed * 100 / Weight;
/// <summary>
///
/// </summary>
@ -45,11 +28,8 @@ public class EntityTrolleybus
/// <param name="additionalColor"></param>
/// <param name="batteryCompartment"></param>
/// <param name="horns"></param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool batteryCompartment, bool horns)
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, bool batteryCompartment, bool horns) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BatteryCompartment = batteryCompartment;
Horns = horns;

View File

@ -34,6 +34,7 @@
buttonDown = new Button();
buttonRight = new Button();
buttonUp = new Button();
buttonCreateTrolleyB = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxTrolleybus).BeginInit();
SuspendLayout();
//
@ -51,9 +52,9 @@
buttonCreateTrolleybus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTrolleybus.Location = new Point(22, 406);
buttonCreateTrolleybus.Name = "buttonCreateTrolleybus";
buttonCreateTrolleybus.Size = new Size(75, 23);
buttonCreateTrolleybus.Size = new Size(204, 23);
buttonCreateTrolleybus.TabIndex = 1;
buttonCreateTrolleybus.Text = "Создать";
buttonCreateTrolleybus.Text = "Создать улучшенный троллейбус";
buttonCreateTrolleybus.UseVisualStyleBackColor = true;
buttonCreateTrolleybus.Click += ButtonCreateTrolleybus_Click;
//
@ -105,11 +106,23 @@
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonCreateTrolleyB
//
buttonCreateTrolleyB.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTrolleyB.Location = new Point(246, 406);
buttonCreateTrolleyB.Name = "buttonCreateTrolleyB";
buttonCreateTrolleyB.Size = new Size(130, 23);
buttonCreateTrolleyB.TabIndex = 6;
buttonCreateTrolleyB.Text = "Создать троллейбус";
buttonCreateTrolleyB.UseVisualStyleBackColor = true;
buttonCreateTrolleyB.Click += ButtonCreateTrolleyB_Click;
//
// FormTrolleybus
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonCreateTrolleyB);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
@ -130,5 +143,6 @@
private Button buttonDown;
private Button buttonRight;
private Button buttonUp;
private Button buttonCreateTrolleyB;
}
}

View File

@ -1,74 +1,97 @@
namespace ProjectTrolleybus
using ProjectTrolleybus.Drawnings;
using ProjectTrolleybus.Entities;
namespace ProjectTrolleybus;
public partial class FormTrolleybus : Form
{
public partial class FormTrolleybus : Form
private DrawningTrolleyB? _drawningTrolleyB;
public FormTrolleybus()
{
private DrawningTrolleybus? _drawningTrolleybus;
InitializeComponent();
}
EntityTrolleybus? _Trolleybus;
private void Draw()
{
Bitmap bmp = new(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrolleyB.DrawTransport(gr);
pictureBoxTrolleybus.Image = bmp;
}
public FormTrolleybus()
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
InitializeComponent();
}
private void Draw()
{
Bitmap bmp = new(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrolleybus.DrawTransport(gr);
pictureBoxTrolleybus.Image = bmp;
}
private void ButtonCreateTrolleybus_Click(object sender, EventArgs e)
{
Random random = new();
_Trolleybus = new EntityTrolleybus();
_Trolleybus.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)));
_drawningTrolleybus = new DrawningTrolleybus();
_drawningTrolleybus.Init(_Trolleybus);
//_drawningTrolleybus.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)));
_drawningTrolleybus.SetPictureSize(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
_drawningTrolleybus.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTrolleybus == null)
{
case nameof(DrawningTrolleyB):
_drawningTrolleyB = new DrawningTrolleyB(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(DrawningTrolleybus):
_drawningTrolleyB = new DrawningTrolleybus(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;
}
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningTrolleybus.MoveVehicle(DirectionType.Up);
break;
case "buttonDown":
result = _drawningTrolleybus.MoveVehicle(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningTrolleybus.MoveVehicle(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTrolleybus.MoveVehicle(DirectionType.Right);
break;
}
_drawningTrolleyB.SetPictureSize(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
_drawningTrolleyB.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
Draw();
}
if (result)
{
Draw();
}
/// <summary>
/// Обработка нажатия создать улучшеный троллейбус
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateTrolleybus_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningTrolleybus));
}
/// <summary>
/// Обработка нажатия создать троллейбус
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateTrolleyB_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningTrolleyB));
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTrolleyB == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningTrolleyB.MoveVehicle(DirectionType.Up);
break;
case "buttonDown":
result = _drawningTrolleyB.MoveVehicle(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningTrolleyB.MoveVehicle(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTrolleyB.MoveVehicle(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}