From 0fbebbe0dc107bcfe22104fb1d857cccdd0c11ba Mon Sep 17 00:00:00 2001 From: Sem730 Date: Sun, 25 Sep 2022 14:20:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectLocomotive/Direction.cs | 6 +- .../ProjectLocomotive/DrawningLocomotive.cs | 182 ++++++++++++++++++ .../ProjectLocomotive/EntityLocomotive.cs | 42 ++++ .../ProjectLocomotive/Form1.resx | 120 ------------ ...Designer.cs => FormLocomotive.Designer.cs} | 13 +- .../{Form1.cs => FormLocomotive.cs} | 4 +- .../ProjectLocomotive/FormLocomotive.resx | 60 ++++++ .../ProjectLocomotive/Program.cs | 2 +- 8 files changed, 302 insertions(+), 127 deletions(-) create mode 100644 ProjectLocomotive/ProjectLocomotive/DrawningLocomotive.cs create mode 100644 ProjectLocomotive/ProjectLocomotive/EntityLocomotive.cs delete mode 100644 ProjectLocomotive/ProjectLocomotive/Form1.resx rename ProjectLocomotive/ProjectLocomotive/{Form1.Designer.cs => FormLocomotive.Designer.cs} (76%) rename ProjectLocomotive/ProjectLocomotive/{Form1.cs => FormLocomotive.cs} (54%) create mode 100644 ProjectLocomotive/ProjectLocomotive/FormLocomotive.resx diff --git a/ProjectLocomotive/ProjectLocomotive/Direction.cs b/ProjectLocomotive/ProjectLocomotive/Direction.cs index 251edeb..f284e18 100644 --- a/ProjectLocomotive/ProjectLocomotive/Direction.cs +++ b/ProjectLocomotive/ProjectLocomotive/Direction.cs @@ -6,7 +6,11 @@ using System.Threading.Tasks; namespace ProjectLocomotive { - internal class Direction + internal enum Direction { + Up = 1, + Down = 2, + Left = 3, + Right = 4 } } diff --git a/ProjectLocomotive/ProjectLocomotive/DrawningLocomotive.cs b/ProjectLocomotive/ProjectLocomotive/DrawningLocomotive.cs new file mode 100644 index 0000000..0aa3f7a --- /dev/null +++ b/ProjectLocomotive/ProjectLocomotive/DrawningLocomotive.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLocomotive +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + internal class DrawningLocomotive + { + /// + /// Класс-сущность + /// + public EntityLocomotive Locomotivе { private set; get; } + /// + /// Левая координата отрисовки локомотива + /// + private float _startPosX; + /// + /// Верхняя кооридната отрисовки локомотива + /// + private float _startPosY; + /// + /// Ширина окна отрисовки + /// + private int? _pictureWidth = null; + /// + /// Высота окна отрисовки + /// + private int? _pictureHeight = null; + /// + /// Ширина отрисовки локомотива + /// + private readonly int _LocWidth = 80; + /// + /// Высота отрисовки локомотива + /// + private readonly int _LocHeight = 50; + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес локомотива + /// Цвет кузова + public void Init(int speed, float weight, Color bodyColor) + { + Locomotivе = new EntityLocomotive(); + Locomotivе.Init(speed, weight, bodyColor); + } + /// + /// Установка позиции автомобиля + /// + /// Координата X + /// Координата Y + /// Ширина картинки + /// Высота картинки + public void SetPosition(int x, int y, int width, int height) + { + // TODO проверки + if (x < 0 || y < 0 || width < x + _LocWidth || height < y + _LocHeight) + { + _pictureHeight = null; + _pictureWidth = null; + return; + } + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(Direction direction) + { + if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) + { + return; + } + switch (direction) + { + // вправо + case Direction.Right: + if (_startPosX + _LocWidth + Locomotivе.Step < _pictureWidth) + { + _startPosX += Locomotivе.Step; + } + break; + //влево + case Direction.Left: + // TODO: Продумать логику + if (_startPosX - Locomotivе.Step > 0) + { + _startPosX -= Locomotivе.Step; + } + break; + //вверх + case Direction.Up: + //TODO: Продумать логику + if (_startPosY - Locomotivе.Step > 0) + { + _startPosY -= Locomotivе.Step; + } + break; + //вниз + case Direction.Down: + if (_startPosY + _LocHeight + Locomotivе.Step < _pictureHeight) + { + _startPosY += Locomotivе.Step; + } + break; + } + } + /// + /// Отрисовка автомобиля + /// + /// + public void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 + || !_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + Pen pen = new(Color.Black); + // кузов электролокомотива (верхняя часть) + //g.DrawRectangle(pen, _startPosX - 1, _startPosY - 1, 80, 30); + g.DrawLine(pen, _startPosX + 7, _startPosY, _startPosX + 80, _startPosY); + g.DrawLine(pen, _startPosX + 80, _startPosY, _startPosX + 80, _startPosY + 15); + g.DrawLine(pen, _startPosX + 80, _startPosY + 15, _startPosX + 2, _startPosY + 15); + g.DrawLine(pen, _startPosX + 2, _startPosY + 15, _startPosX + 7, _startPosY); + // кузов электролокомотива (нижняя часть) + g.DrawLine(pen, _startPosX + 2, _startPosY + 15, _startPosX + 2, _startPosY + 30); + g.DrawLine(pen, _startPosX + 2, _startPosY + 30, _startPosX + 80, _startPosY + 30); + g.DrawLine(pen, _startPosX + 80, _startPosY + 30, _startPosX + 80, _startPosY + 15); + // колёса электролокомотива + Brush br = new SolidBrush(Locomotivе?.BodyColor ?? Color.Black); + g.FillEllipse(br, _startPosX + 3, _startPosY + 30, 15, 15); + g.FillEllipse(br, _startPosX + 22, _startPosY + 30, 15, 15); + g.FillEllipse(br, _startPosX + 42, _startPosY + 30, 15, 15); + g.FillEllipse(br, _startPosX + 62, _startPosY + 30, 15, 15); + // окна электролокомотива + Brush brBlue = new SolidBrush(Color.Blue); + g.FillRectangle(brBlue, _startPosX + 10, _startPosY + 3, 5, 10); + g.FillRectangle(brBlue, _startPosX + 50, _startPosY + 3, 5, 10); + g.FillRectangle(brBlue, _startPosX + 70, _startPosY + 3, 5, 10); + // дверь электролокомотива + g.DrawLine(pen, _startPosX + 20, _startPosY + 6, _startPosX + 30, _startPosY + 6); + g.DrawLine(pen, _startPosX + 30, _startPosY + 8, _startPosX + 30, _startPosY + 25); + g.DrawLine(pen, _startPosX + 30, _startPosY + 25, _startPosX + 20, _startPosY + 25); + g.DrawLine(pen, _startPosX + 20, _startPosY + 25, _startPosX + 20, _startPosY + 6); + } + /// + /// Смена границ формы отрисовки + /// + /// Ширина картинки + /// Высота картинки + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _LocWidth || _pictureHeight <= _LocHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _LocWidth > _pictureWidth) + { + _startPosX = _pictureWidth.Value - _LocWidth; + } + if (_startPosY + _LocHeight > _pictureHeight) + { + _startPosY = _pictureHeight.Value - _LocHeight; + } + } + } +} diff --git a/ProjectLocomotive/ProjectLocomotive/EntityLocomotive.cs b/ProjectLocomotive/ProjectLocomotive/EntityLocomotive.cs new file mode 100644 index 0000000..7ff411d --- /dev/null +++ b/ProjectLocomotive/ProjectLocomotive/EntityLocomotive.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLocomotive +{ + internal class EntityLocomotive + { + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public float Weight { get; private set; } + /// + /// Цвет кузова + /// + public Color BodyColor { get; private set; } + /// + /// Шаг передвижения локомотива + /// + public float Step => Speed * 100 / Weight; + /// + /// Инициализация полей объекта-класса локомотива + /// + /// + /// + /// + /// + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new(); + Speed = speed <= 0 ? rnd.Next(50, 150) : speed; + Weight = weight <= 0 ? rnd.Next(40, 70) : weight; + BodyColor = bodyColor; + } + } +} diff --git a/ProjectLocomotive/ProjectLocomotive/Form1.resx b/ProjectLocomotive/ProjectLocomotive/Form1.resx deleted file mode 100644 index 1af7de1..0000000 --- a/ProjectLocomotive/ProjectLocomotive/Form1.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/ProjectLocomotive/ProjectLocomotive/Form1.Designer.cs b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.Designer.cs similarity index 76% rename from ProjectLocomotive/ProjectLocomotive/Form1.Designer.cs rename to ProjectLocomotive/ProjectLocomotive/FormLocomotive.Designer.cs index 4cb5164..124dc6c 100644 --- a/ProjectLocomotive/ProjectLocomotive/Form1.Designer.cs +++ b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.Designer.cs @@ -1,6 +1,6 @@ namespace ProjectLocomotive { - partial class Form1 + partial class FormLocomotive { /// /// Required designer variable. @@ -28,10 +28,17 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.SuspendLayout(); + // + // FormLocomotive + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + this.Name = "FormLocomotive"; + this.Text = "Локомотив"; + this.ResumeLayout(false); + } #endregion diff --git a/ProjectLocomotive/ProjectLocomotive/Form1.cs b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.cs similarity index 54% rename from ProjectLocomotive/ProjectLocomotive/Form1.cs rename to ProjectLocomotive/ProjectLocomotive/FormLocomotive.cs index 1527fc7..e76f9ea 100644 --- a/ProjectLocomotive/ProjectLocomotive/Form1.cs +++ b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.cs @@ -1,8 +1,8 @@ namespace ProjectLocomotive { - public partial class Form1 : Form + public partial class FormLocomotive : Form { - public Form1() + public FormLocomotive() { InitializeComponent(); } diff --git a/ProjectLocomotive/ProjectLocomotive/FormLocomotive.resx b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProjectLocomotive/ProjectLocomotive/Program.cs b/ProjectLocomotive/ProjectLocomotive/Program.cs index 06ff648..8f40489 100644 --- a/ProjectLocomotive/ProjectLocomotive/Program.cs +++ b/ProjectLocomotive/ProjectLocomotive/Program.cs @@ -11,7 +11,7 @@ namespace ProjectLocomotive // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + Application.Run(new FormLocomotive()); } } } \ No newline at end of file