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

This commit is contained in:
Anastasia Yazykova 2024-04-02 21:50:52 +04:00
parent af92c80989
commit d6b157b93a
8 changed files with 331 additions and 142 deletions

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34024.191
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrolleybusProject", "TrolleybusProject\TrolleybusProject.csproj", "{212BCE90-1598-4573-A91D-8498ED312A2B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrolleybusProject", "TrolleybusProject\TrolleybusProject.csproj", "{212BCE90-1598-4573-A91D-8498ED312A2B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TrolleybusProject;
namespace TrolleybusProject.Drawnings;
public enum DirectionType
{

View File

@ -3,15 +3,16 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TrolleybusProject.Entities;
namespace TrolleybusProject;
namespace TrolleybusProject.Drawnings;
public class DrawningTrolleybus
public class DrawningBus
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTrolleybus? EntityTrolleybus { get; private set; }
public EntityBus? EntityBus { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
@ -23,41 +24,70 @@ public class DrawningTrolleybus
/// <summary>
/// Левая координата прорисовки троллейбуса
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки троллейбуса
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки троллейбуса
/// </summary>
public readonly int _drawningTrolleybusWidth = 150;
public readonly int _drawningBusWidth = 150;
/// <summary>
/// Высота прорисовки троллейбуса
/// </summary>
public readonly int _drawningTrolleybusHeight = 86;
public readonly int _drawningBusHeight = 86;
/// <summary>
/// Инициализация свойств
/// Конструктор пустой
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="roga">Признак наличия рогов</param>
/// <param name="doors">Признак наличия дверей</param>
/// <param name="otsek">Признак наличия отсека</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool doors, bool roga, bool otsek)
private DrawningBus()
{
EntityTrolleybus = new EntityTrolleybus();
EntityTrolleybus.Init(speed, weight, bodyColor, additionalColor,
doors, roga, otsek);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningBus(int speed, double weight, Color bodyColor):this()
{
EntityBus = new EntityBus(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningBusWidth">Ширина прорисовки автобуса</param>
/// <param name="drawningBusHeight">Высота прорисовки автобуса</param>
protected DrawningBus(int drawningBusWidth, int drawningBusHeight) : this()
{
_drawningBusWidth= drawningBusWidth;
_drawningBusHeight = drawningBusHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
@ -66,7 +96,7 @@ public class DrawningTrolleybus
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (width> _drawningTrolleybusWidth || height > _drawningTrolleybusHeight)
if (width > _drawningBusWidth || height > _drawningBusHeight)
{
_pictureWidth = width;
_pictureHeight = height;
@ -75,17 +105,17 @@ public class DrawningTrolleybus
if (_startPosY.HasValue && _startPosX.HasValue)
{
if (_startPosX + _drawningTrolleybusWidth > width)
if (_startPosX + _drawningBusWidth > width)
{
_startPosX = width - _drawningTrolleybusWidth;
_startPosX = width - _drawningBusWidth;
}
if (_startPosY + _drawningTrolleybusHeight > height)
if (_startPosY + _drawningBusHeight > height)
{
_startPosY = height - _drawningTrolleybusHeight;
_startPosY = height - _drawningBusHeight;
}
@ -109,7 +139,7 @@ public class DrawningTrolleybus
return false;
@ -137,22 +167,22 @@ public class DrawningTrolleybus
if (_startPosY < 0)
{ _startPosY = 0; }
if (x + _drawningTrolleybusWidth > _pictureWidth)
if (x + _drawningBusWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningTrolleybusWidth;
_startPosX = _pictureWidth - _drawningBusWidth;
}
if (y + _drawningTrolleybusHeight > _pictureHeight)
if (y + _drawningBusHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningTrolleybusHeight;
_startPosY = _pictureHeight - _drawningBusHeight;
}
}
/// <summary>
/// Изменение направления перемещения
@ -161,7 +191,7 @@ public class DrawningTrolleybus
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityTrolleybus == null || !_startPosX.HasValue ||
if (EntityBus == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
@ -170,31 +200,31 @@ public class DrawningTrolleybus
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityTrolleybus.Step > 0)
if (_startPosX.Value - EntityBus.Step > 0)
{
_startPosX -= (int)EntityTrolleybus.Step;
_startPosX -= (int)EntityBus.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityTrolleybus.Step > 0)
if (_startPosY.Value - EntityBus.Step > 0)
{
_startPosY -= (int)EntityTrolleybus.Step;
_startPosY -= (int)EntityBus.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityTrolleybus.Step < _pictureWidth - _drawningTrolleybusWidth)
if (_startPosX.Value + EntityBus.Step < _pictureWidth - _drawningBusWidth)
{
_startPosX += (int)EntityTrolleybus.Step;
_startPosX += (int)EntityBus.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityTrolleybus.Step < _pictureHeight - _drawningTrolleybusHeight)
if (_startPosY.Value + EntityBus.Step < _pictureHeight - _drawningBusHeight)
{
_startPosY += (int)EntityTrolleybus.Step;
_startPosY += (int)EntityBus.Step;
}
return true;
default:
@ -205,18 +235,18 @@ public class DrawningTrolleybus
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityTrolleybus == null || !_startPosX.HasValue ||
if (EntityBus == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new
SolidBrush(EntityTrolleybus.AdditionalColor);
Brush brushBodyColor = new SolidBrush(EntityTrolleybus.BodyColor);
Pen addpen = new(EntityTrolleybus.AdditionalColor);
Brush brushBodyColor = new SolidBrush(EntityBus.BodyColor);
//троллейбус границы
g.DrawEllipse(pen, _startPosX.Value + 27, _startPosY.Value +
64, 20, 20);
@ -301,45 +331,5 @@ public class DrawningTrolleybus
//рога
if (EntityTrolleybus.Roga)
{
g.DrawLine(addpen, _startPosX.Value + 42, _startPosY.Value + 7,
_startPosX.Value + 124, _startPosY.Value + 29);
g.DrawLine(addpen, _startPosX.Value + 62, _startPosY.Value + 2,
_startPosX.Value + 124, _startPosY.Value + 29);
}
//отсек для батареек
if (EntityTrolleybus.Otsek)
{
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value +
48, 5, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value +
48, 5, 20);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value +
48, 5, 20);
}
//двойная дверь
if (EntityTrolleybus.Doors)
{
g.DrawLine(addpen, _startPosX.Value + 66, _startPosY.Value + 40,
_startPosX.Value + 66, _startPosY.Value + 70);
}
}
}

View File

@ -0,0 +1,93 @@
using TrolleybusProject.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace TrolleybusProject.Drawnings;
public class DrawningTrolleybus:DrawningBus
{
/// <summary>
/// Клнструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="roga">Признак наличия рогов</param>
/// <param name="doors">Признак наличия дверей</param>
/// <param name="otsek">Признак наличия отсека</param>
public DrawningTrolleybus(int speed, double weight, Color bodyColor, Color
additionalColor, bool doors, bool roga, bool otsek):base(150,86)
{
EntityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor,
doors, roga, otsek);
}
public override void DrawTransport(Graphics g)
{
if (EntityBus == null || EntityBus is not EntityTrolleybus trolleybus|| !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(trolleybus.AdditionalColor);
Pen addpen = new(trolleybus.AdditionalColor);
if (trolleybus.Otsek)
{
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value +
48, 5, 20);
g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value +
48, 5, 20);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value +
48, 5, 20);
}
//двойная дверь
_startPosX += 22;
_startPosY += 15;
base.DrawTransport(g);
_startPosX -= 22;
_startPosY -= 15;
if (trolleybus.Doors)
{
g.DrawLine(addpen, _startPosX.Value + 66, _startPosY.Value + 40,
_startPosX.Value + 66, _startPosY.Value + 70);
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace TrolleybusProject.Entities;
/// <summary>
/// Класс-сущность Автобус
/// </summary>
public class EntityBus
{
/// <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;
///Конструктор сущности
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес троллейбуса</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityBus(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -4,26 +4,13 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TrolleybusProject;
public class EntityTrolleybus
namespace TrolleybusProject.Entities;
public class EntityTrolleybus: EntityBus
{
/// <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>
/// Признак (опция) наличие рогов
@ -38,10 +25,15 @@ public class EntityTrolleybus
/// Признак (опция) наличия двойной двери
/// </summary>
public bool Doors { get; private set; }
/// <summary>
/// Шаг перемещения троллейбуса
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса троллейбуса
/// </summary>
@ -52,12 +44,9 @@ public class EntityTrolleybus
/// <param name="roga">Признак наличия рогов</param>
/// <param name="otsek">Признак наличия отсека</param>
/// <param name="doors">Признак наличия двойной двери</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool roga, bool otsek, bool doors)
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color
additionalColor, bool roga, bool otsek, bool doors):base(speed,weight,bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Roga = roga;
Otsek = otsek;

View File

@ -34,6 +34,7 @@
buttonRight = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonCreateBus = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxTrolleybus).BeginInit();
SuspendLayout();
//
@ -51,9 +52,9 @@
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(21, 515);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(112, 34);
buttonCreate.Size = new Size(188, 34);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.Text = "Создать Троллейбус";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
//
@ -105,11 +106,23 @@
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click;
//
// buttonCreateBus
//
buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateBus.Location = new Point(249, 515);
buttonCreateBus.Name = "buttonCreateBus";
buttonCreateBus.Size = new Size(188, 34);
buttonCreateBus.TabIndex = 6;
buttonCreateBus.Text = "Создать Автобус";
buttonCreateBus.UseVisualStyleBackColor = true;
buttonCreateBus.Click += buttonCreateBus_Click;
//
// FormTrolleybus
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1273, 561);
Controls.Add(buttonCreateBus);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
@ -130,5 +143,6 @@
private Button buttonRight;
private Button buttonUp;
private Button buttonDown;
private Button buttonCreateBus;
}
}

View File

@ -7,40 +7,91 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TrolleybusProject.Drawnings;
namespace TrolleybusProject
{
public partial class FormTrolleybus : Form
{
private DrawningTrolleybus? _drawningTrolleybus;
private DrawningBus? _drawningBus;
public FormTrolleybus()
{
InitializeComponent();
}
/// <summary>
/// Метод прорисовки машины
/// </summary>
private void Draw()
{
if (_drawningBus == null)
{
return;
}
Bitmap bmp = new(pictureBoxTrolleybus.Width,
pictureBoxTrolleybus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningBus.DrawTransport(gr);
pictureBoxTrolleybus.Image = bmp;
}
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningBus):
_drawningBus = new DrawningBus(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):
_drawningBus = 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)), Convert.ToBoolean(random.Next(0, 2)));
break;
default:
return;
}
_drawningBus.SetPictureSize(pictureBoxTrolleybus.Width,
pictureBoxTrolleybus.Height);
_drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningTrolleybus = new DrawningTrolleybus();
_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)), Convert.ToBoolean(random.Next(0, 2)));
_drawningTrolleybus.SetPictureSize(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
_drawningTrolleybus.SetPosition(random.Next(10, 100), random.Next(10, 100));
Bitmap bmp = new(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrolleybus.DrawTransport(gr);
pictureBoxTrolleybus.Image = bmp;
CreateObject(nameof(DrawningTrolleybus));
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningTrolleybus == null)
if (_drawningBus == null)
{
return;
}
@ -49,34 +100,32 @@ namespace TrolleybusProject
switch (name)
{
case "buttonUp":
result = _drawningTrolleybus.MoveTransport(DirectionType.Up);
result = _drawningBus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningTrolleybus.MoveTransport(DirectionType.Down);
result = _drawningBus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningTrolleybus.MoveTransport(DirectionType.Left);
result = _drawningBus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTrolleybus.MoveTransport(DirectionType.Right);
result = _drawningBus.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Bitmap bmp = new(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrolleybus.DrawTransport(gr);
pictureBoxTrolleybus.Image = bmp;
Draw();
}
}
private void buttonCreateBus_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawningBus));
}
}
}