This commit is contained in:
Илья 2024-04-26 16:44:03 +04:00
parent 079f73fef7
commit df7d102484
7 changed files with 2119 additions and 2 deletions

View File

@ -0,0 +1,23 @@
namespace ProjectTrain;
/// <summary>
/// Направление перемещения
/// </summary>
public enum DirectionType
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}

View File

@ -0,0 +1,250 @@
using ProjectTrain;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Security.Cryptography.Xml;
namespace ProjectTrain;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningTrain
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTrain? EntityTrain { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
private int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int? _startPosY;
/// <summary>
/// Ширина прорисовки поезда
/// </summary>
private readonly int _drawningTrainWidth = 150;
/// <summary>
/// Высота прорисовки поезда
/// </summary>
private readonly int _drawningTrainHeight = 50;
private readonly int _drawningEnginesWidth = 3;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="horns">Признак наличия вертолетной площадки</param>
/// <param name="electro">Признак наличия шлюпок</param>
/// <param name="headlight">Признак наличия пушки</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool horns, bool electro, bool headlight)
{
EntityTrain = new EntityTrain();
EntityTrain.Init(speed, weight, bodyColor, additionalColor,
horns, electro, headlight);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
// TODO проверка, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена
if (_drawningTrainHeight > height || _drawningTrainWidth > width)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
SetPosition(_startPosX.Value, _startPosY.Value);
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
if (x < 0 || x + _drawningTrainWidth > _pictureWidth || y < 0 || y + _drawningTrainHeight > _pictureHeight)
{
_startPosX = _pictureWidth - _drawningTrainWidth;
_startPosY = _pictureHeight - _drawningTrainHeight;
}
else
{
_startPosX = x;
_startPosY = y;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityTrain == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityTrain.Step - _drawningEnginesWidth > 0)
{
_startPosX -= (int)EntityTrain.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityTrain.Step > 0)
{
_startPosY -= (int)EntityTrain.Step;
}
return true;
// вправо
case DirectionType.Right:
//TODO прописать логику сдвига в право
if (_startPosX.Value + EntityTrain.Step + _drawningTrainWidth < _pictureWidth)
{
_startPosX += (int)EntityTrain.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityTrain.Step + _drawningTrainHeight < _pictureHeight)
{
_startPosY += (int)EntityTrain.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityTrain == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black, 2);
Pen pen2 = new(Color.White, 1);
Brush wheelBrush = new SolidBrush(Color.Black);
Brush wheelBrush2 = new SolidBrush(Color.Gray);
Brush headlightBrush = new SolidBrush(Color.Yellow);
Brush glassBrush = new SolidBrush(Color.Cyan);
Brush ElectroBrush = new HatchBrush(HatchStyle.ZigZag, EntityTrain.AdditionalColor, Color.FromArgb(163, 163, 163));
Brush additionalBrush = new SolidBrush(EntityTrain.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntityTrain.BodyColor);
//границы круисера
Point[] points = { new Point(_startPosX.Value + 5, _startPosY.Value), new Point(_startPosX.Value + 140, _startPosY.Value), new Point(_startPosX.Value + 140, _startPosY.Value + 20), new Point(_startPosX.Value, _startPosY.Value + 20) };
g.FillPolygon(additionalBrush, points);
g.FillRectangle(bodyBrush, _startPosX.Value, _startPosY.Value + 20, 140, 20);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 20, 140, 20);
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value, _startPosX.Value + 140, _startPosY.Value);
g.DrawLine(pen, _startPosX.Value + 140, _startPosY.Value, _startPosX.Value + 140, _startPosY.Value + 20);
g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 20, _startPosX.Value + 5, _startPosY.Value );
g.FillRectangle(wheelBrush, _startPosX.Value + 140, _startPosY.Value + 3, 7, 34);
//дверь
g.FillRectangle(bodyBrush, _startPosX.Value + 47, _startPosY.Value + 6, 15, 29);
g.DrawRectangle(pen, _startPosX.Value + 47, _startPosY.Value + 6, 15, 29);
//окна
g.FillRectangle(glassBrush, _startPosX.Value + 12, _startPosY.Value + 3, 13, 14);
g.FillRectangle(glassBrush, _startPosX.Value + 28, _startPosY.Value + 3, 13, 14);
g.FillRectangle(glassBrush, _startPosX.Value + 124, _startPosY.Value + 3, 13, 14);
//колёса
Point[] points2 = { new Point(_startPosX.Value + 5 , _startPosY.Value + 40), new Point(_startPosX.Value + 55, _startPosY.Value + 40), new Point(_startPosX.Value + 45, _startPosY.Value + 47), new Point(_startPosX.Value - 5, _startPosY.Value + 47) };
g.FillPolygon(wheelBrush, points2);
Point[] points3 = { new Point(_startPosX.Value + 145, _startPosY.Value + 47), new Point(_startPosX.Value + 95, _startPosY.Value + 47), new Point(_startPosX.Value + 85, _startPosY.Value + 40), new Point(_startPosX.Value + 135, _startPosY.Value + 40) };
g.FillPolygon(wheelBrush, points3);
g.FillEllipse(wheelBrush2, _startPosX.Value + 37, _startPosY.Value + 41, 14, 14);
g.DrawEllipse(pen2, _startPosX.Value + 37, _startPosY.Value + 41, 15, 15);
g.FillEllipse(wheelBrush2, _startPosX.Value + 10, _startPosY.Value + 41, 14, 14);
g.DrawEllipse(pen2, _startPosX.Value + 10, _startPosY.Value + 41, 15, 15);
g.FillEllipse(wheelBrush2, _startPosX.Value + 93, _startPosY.Value + 41, 14, 14);
g.DrawEllipse(pen2, _startPosX.Value + 93, _startPosY.Value + 41, 15, 15);
g.FillEllipse(wheelBrush2, _startPosX.Value + 120, _startPosY.Value + 41, 14, 14);
g.DrawEllipse(pen2, _startPosX.Value + 120, _startPosY.Value + 41, 15, 15);
//внутренности поезда
if (EntityTrain.Horns)
{
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value , _startPosX.Value + 47, _startPosY.Value - 5);
g.DrawLine(pen, _startPosX.Value + 47, _startPosY.Value - 5, _startPosX.Value + 67, _startPosY.Value - 10);
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value - 10, _startPosX.Value + 87, _startPosY.Value - 5);
g.DrawLine(pen, _startPosX.Value + 87, _startPosY.Value - 5, _startPosX.Value + 67, _startPosY.Value);
}
if (EntityTrain.Electro)
{
g.FillRectangle(ElectroBrush, _startPosX.Value + 107, _startPosY.Value - 5 + 3, 33, 28);
g.DrawRectangle(pen, _startPosX.Value + 107, _startPosY.Value - 5 + 3, 33, 28);
}
if (EntityTrain.Headlight)
{
g.FillEllipse(headlightBrush, _startPosX.Value - 3, _startPosY.Value + 23, 6, 14);
g.DrawEllipse(pen, _startPosX.Value - 3, _startPosY.Value + 23, 6, 14);
}
}
}

View File

@ -0,0 +1,62 @@
namespace ProjectTrain;
/// <summary>
/// Класс-сущность "автобус"
/// </summary>
public class EntityTrain
{
//свойства
/// <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>
/// Признак (опция) наличие электрических батарей
/// </summary>
public bool Electro { get; private set; }
/// <summary>
/// Признак (опция) наличие фары
/// </summary>
public bool Headlight { 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="horns">второй этаж</param>
/// <param name="electro">лестница</param>
/// <param name="headlight">наличие фар</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool horns, bool electro, bool headlight)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Horns = horns;
Electro = electro;
Headlight = headlight;
}
}

View File

@ -0,0 +1,146 @@

namespace ProjectTrain
{
partial class FormTrain
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormTrain));
pictureBox1 = new PictureBox();
button1 = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonLeft = new Button();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
SuspendLayout();
//
// pictureBox1
//
pictureBox1.Location = new Point(0, 0);
pictureBox1.Margin = new Padding(3, 2, 3, 2);
pictureBox1.Name = "pictureBox1";
pictureBox1.Size = new Size(700, 338);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.TabIndex = 0;
pictureBox1.TabStop = false;
pictureBox1.Click += pictureBox1_Click;
//
// button1
//
button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
button1.Location = new Point(10, 307);
button1.Margin = new Padding(3, 2, 3, 2);
button1.Name = "button1";
button1.Size = new Size(82, 22);
button1.TabIndex = 1;
button1.Text = "создать";
button1.UseVisualStyleBackColor = true;
button1.Click += ButtonCreateTrain_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = (Image)resources.GetObject("buttonUp.BackgroundImage");
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(632, 280);
buttonUp.Margin = new Padding(3, 2, 3, 2);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(26, 22);
buttonUp.TabIndex = 2;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonDown
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = (Image)resources.GetObject("buttonDown.BackgroundImage");
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(632, 307);
buttonDown.Margin = new Padding(3, 2, 3, 2);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(26, 22);
buttonDown.TabIndex = 3;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = (Image)resources.GetObject("buttonRight.BackgroundImage");
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(663, 307);
buttonRight.Margin = new Padding(3, 2, 3, 2);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(26, 22);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = (Image)resources.GetObject("buttonLeft.BackgroundImage");
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(600, 307);
buttonLeft.Margin = new Padding(3, 2, 3, 2);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(26, 22);
buttonLeft.TabIndex = 5;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// FormTrain
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(700, 338);
Controls.Add(buttonLeft);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(button1);
Controls.Add(pictureBox1);
Margin = new Padding(3, 2, 3, 2);
Name = "FormTrain";
Text = "FormTrain";
Click += ButtonMove_Click;
((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private PictureBox pictureBox1;
private Button button1;
private Button buttonUp;
private Button buttonDown;
private Button buttonRight;
private Button buttonLeft;
}
}

View File

@ -0,0 +1,96 @@
namespace ProjectTrain
{
public partial class FormTrain : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningTrain? _drawningTrain;
public FormTrain()
{
InitializeComponent();
}
/// <summary>
/// Метод прорисовки поезда
/// </summary>
private void Draw()
{
if (_drawningTrain == null)
{
return;
}
Bitmap bmp = new(pictureBox1.Width,
pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrain.DrawTransport(gr);
pictureBox1.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateTrain_Click(object sender, EventArgs e)
{
Random random = new();
_drawningTrain = new DrawningTrain();
_drawningTrain.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)));
_drawningTrain.SetPictureSize(pictureBox1.Width,
pictureBox1.Height);
//начальное положение поезда
_drawningTrain.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTrain == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningTrain.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningTrain.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningTrain.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningTrain.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
namespace ProjertTrain
namespace ProjectTrain
{
internal static class Program
{
@ -11,7 +11,7 @@ namespace ProjertTrain
// 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 FormTrain());
}
}
}