This commit is contained in:
Игорь Гордеев 2023-10-06 12:54:51 +04:00
parent 025bd88523
commit d1f9a5eead
3 changed files with 89 additions and 60 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;
@ -10,35 +11,38 @@ namespace ElectricLocomotive
{
public class DrawningElectricLocomotive
{
private EntityElectricLocomotive enitity_electric_locomotive = new EntityElectricLocomotive();
public EntityElectricLocomotive? _ElectricLocomotive { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private const int _LengthLocomotive = 120;
private readonly int _LocomotiveWidth = 100;
private const int _HeightLocomorive=90;
private readonly int _LocomoriveHeight =50;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor)
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns, int width, int height)
{
if (speed < 0 || bodyColor == additionalColor)
_pictureWidth = width;
_pictureHeight = height;
if ((_pictureHeight < _LocomoriveHeight) || (_pictureWidth < _LocomotiveWidth))
{
return false;
}
else
{
enitity_electric_locomotive.Init(speed, weight, bodyColor, additionalColor);
_ElectricLocomotive = new EntityElectricLocomotive();
_ElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns);
return true;
}
}
public void SetPosition(int x, int y)
{
if (x < 0 || y < 0 || x + _LengthLocomotive > ElectricLocomotive.ActiveForm.Size.Width || y + _HeightLocomorive > ElectricLocomotive.ActiveForm.Size.Height)
if (x < 0 || y < 0 || x + _LocomotiveWidth > _pictureWidth || y + _LocomoriveHeight > _pictureHeight)
{
return ;
}
@ -52,7 +56,7 @@ namespace ElectricLocomotive
public void MoveTransport(DirectionType direction)
{
if (enitity_electric_locomotive == null)
if (_ElectricLocomotive == null)
{
return;
}
@ -61,33 +65,33 @@ namespace ElectricLocomotive
{
//влево
case DirectionType.Left:
if (_startPosX - enitity_electric_locomotive.Step > 0)
if (_startPosX - _ElectricLocomotive.Step > 0)
{
_startPosX -= (int)enitity_electric_locomotive.Step;
_startPosX -= (int)_ElectricLocomotive.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - enitity_electric_locomotive.Step > 0)
if (_startPosY - _ElectricLocomotive.Step > 0)
{
_startPosY -= (int)enitity_electric_locomotive.Step;
_startPosY -= (int)_ElectricLocomotive.Step;
}
break;
// вправо
case DirectionType.Right: // TODO: Продумать логику
if (_startPosX+_LengthLocomotive+(int)enitity_electric_locomotive.Step < ElectricLocomotive.ActiveForm.Size.Width)
if (_startPosX+ _LocomotiveWidth + (int)_ElectricLocomotive.Step < _pictureWidth)
{
_startPosX += (int)enitity_electric_locomotive.Step;
_startPosX += (int)_ElectricLocomotive.Step;
}
break;
//вниз
case DirectionType.Down: // TODO: Продумать логику
if (_startPosY + _HeightLocomorive + (int)enitity_electric_locomotive.Step < ElectricLocomotive.ActiveForm.Size.Height)
if (_startPosY + _LocomoriveHeight + (int)_ElectricLocomotive.Step < _pictureHeight)
{
_startPosY += (int)enitity_electric_locomotive.Step;
_startPosY += (int)_ElectricLocomotive.Step;
}
break;
}
@ -98,13 +102,13 @@ namespace ElectricLocomotive
public void DrawTransport(Graphics g)
{
if (enitity_electric_locomotive == null)
if (_ElectricLocomotive == null)
{
return;
}
Pen pen = new(enitity_electric_locomotive.AdditionalColor);
Brush brush = new SolidBrush(enitity_electric_locomotive.BodyColor);
Pen pen = new(_ElectricLocomotive.AdditionalColor);
Brush brush = new SolidBrush(_ElectricLocomotive.BodyColor);
///ВЛ60к-1595
@ -165,6 +169,8 @@ namespace ElectricLocomotive
g.FillRectangle(brush, _startPosX + 50, _startPosY + 15, 10, 25);
///рога
if (_ElectricLocomotive.Horns == true)
{
g.FillRectangle(brush, _startPosX + 25, _startPosY + 5, 15, 5);
Point[] horns =
@ -191,7 +197,7 @@ namespace ElectricLocomotive
new Point(_startPosX+80,_startPosY+10)
};
g.DrawPolygon(pen, horns2);
}
///Батарея
g.FillRectangle(brush, _startPosX + 45, _startPosY + 5, 15, 5);

View File

@ -28,7 +28,9 @@ namespace ElectricLocomotive
_drawningElectricLocomotive = new DrawningElectricLocomotive();
_drawningElectricLocomotive.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)));
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
_drawningElectricLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();

View File

@ -8,28 +8,49 @@ namespace ElectricLocomotive
{
public class EntityElectricLocomotive
{
/// <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 Horns { get; private set; }
/// <summary>
/// Признак (опция) roga
/// </summary>
public double Step => (double)Speed * 100 / Weight;
public double Step => (double)Speed * 100 / Weight; ///свойство с лямбда выражением
/// <summary>
/// Инициализация полей объекта-класса электролокоматива
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="Horns">Признак наличия рогов</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor)
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Horns = horns;
}
}
}
}
}