From 5a2601f0fa531c272b4b82a12cd928a11d128118 Mon Sep 17 00:00:00 2001 From: cyxarukk Date: Sun, 10 Mar 2024 12:07:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectCar/ProjectCar/DrawningGasMachine.cs | 117 +++++++++++++++--- ProjectCar/ProjectCar/EntityMachine.cs | 16 +-- .../ProjectCar/FormGasMachine.Designer.cs | 23 +++- ProjectCar/ProjectCar/FormGasMachine.cs | 83 ++++++++++++- ProjectCar/ProjectCar/Program.cs | 2 +- .../ProjectCar/ProjectCar - Backup.csproj | 26 ++++ ProjectCar/ProjectCar/ProjectCar.csproj | 5 + 7 files changed, 235 insertions(+), 37 deletions(-) create mode 100644 ProjectCar/ProjectCar/ProjectCar - Backup.csproj diff --git a/ProjectCar/ProjectCar/DrawningGasMachine.cs b/ProjectCar/ProjectCar/DrawningGasMachine.cs index 6bdd53f..11ec7a3 100644 --- a/ProjectCar/ProjectCar/DrawningGasMachine.cs +++ b/ProjectCar/ProjectCar/DrawningGasMachine.cs @@ -1,4 +1,6 @@ -namespace ProjectCar; +using System.Reflection.Metadata.Ecma335; + +namespace ProjectCar; /// /// /// @@ -28,16 +30,16 @@ public class DrawningGasMachine /// /// Ширина прорисовки газ автомобиля /// - private readonly int _drawningMachineWidth = 0; + private readonly int _drawningMachineWidth = 180; /// /// Высота прорисовки газ автомобиля /// - private readonly int _drawningMachineHeight = 0; + private readonly int _drawningMachineHeight = 70; - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool model, bool gas, bool wheels) + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool gas, bool beacon) { EntityMachine = new EntityMachine(); - EntityMachine.Init(speed, weight, bodyColor, additionalColor, model, gas, wheels); + EntityMachine.Init(speed, weight, bodyColor, additionalColor, gas, beacon); _pictureWidth = null; _pictureHeight = null; _startPosX = null; @@ -49,27 +51,65 @@ public class DrawningGasMachine /// Ширина поля /// Высота поля /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих границах - public bool SetPictureSize(int width, int height) + public bool SetPictureSize(int width, int height) { - - _pictureWidth = width; - _pictureHeight = height; - return true; + if (EntityMachine == null) + { + return false; + } + + if (width >= _drawningMachineWidth && height >= _drawningMachineHeight) + { + _pictureWidth = width; + _pictureHeight = height; + + if (_startPosX.HasValue && _startPosY.HasValue) + { + if (_startPosX + _drawningMachineWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningMachineWidth; + } + if (_startPosY + _drawningMachineHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningMachineHeight; + } + } + return true; + } + return false; } /// /// Установка позиции /// /// Координата X /// Координата Y - public void SetPosition(int x, int y) + public void SetPosition(int x, int y) { - if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) { return; } - _startPosX = x; _startPosY = y; + if (x < 0) + { + _startPosX = 0; + } + + if (y < 0) + { + _startPosY = 0; + } + + if (x + _drawningMachineWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningMachineWidth; + } + + if (y + _drawningMachineHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningMachineHeight; + } } public bool MoveTransport(DirectionType direction) { @@ -92,13 +132,13 @@ public class DrawningGasMachine } return true; case DirectionType.Right: - if (_startPosX.Value + EntityMachine.Step < _pictureWidth) + if (_startPosX.Value + EntityMachine.Step + _drawningMachineWidth < _pictureWidth) { _startPosX += (int)EntityMachine.Step; } return true; case DirectionType.Down: - if (_startPosY.Value + EntityMachine.Step < _pictureHeight) + if (_startPosY.Value + EntityMachine.Step + _drawningMachineHeight < _pictureHeight) { _startPosY += (int)EntityMachine.Step; } @@ -117,6 +157,51 @@ public class DrawningGasMachine { return; } - } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(EntityMachine.AdditionalColor); + + //бак с газом + if (EntityMachine.Gas) + { + g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 10, 80, 30); + + g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 10, 80, 30); + } + //границы автомобиля + g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 40, 110, 10); + g.DrawRectangle(pen, _startPosX.Value + 81, _startPosY.Value + 10, 30, 30); + + + + //кузов + Brush br = new SolidBrush(EntityMachine.BodyColor); + g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 40, 110, 10); + + + //кабина + g.FillRectangle(br, _startPosX.Value + 81, _startPosY.Value + 10, 30, 30); + + + //маяк сигнальный + if (EntityMachine.Beacon) + { + g.DrawRectangle(pen, _startPosX.Value + 95, _startPosY.Value, 10, 10); + g.FillRectangle(additionalBrush, _startPosX.Value + 95, _startPosY.Value, 10, 10); + } + + + //колеса + + + g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 50, 20, 20); + g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 50, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 50, 20, 20); + g.FillEllipse(additionalBrush, _startPosX.Value + 90, _startPosY.Value + 50, 20, 20); + + + + + + } } diff --git a/ProjectCar/ProjectCar/EntityMachine.cs b/ProjectCar/ProjectCar/EntityMachine.cs index 3bc8383..412ff74 100644 --- a/ProjectCar/ProjectCar/EntityMachine.cs +++ b/ProjectCar/ProjectCar/EntityMachine.cs @@ -21,17 +21,13 @@ public class EntityMachine /// public Color AdditionalColor { get; private set; } /// - /// Модель машины - /// - public bool Model { get; private set; } - /// /// Признак (опция) наличия бака для бензина /// public bool Gas { get; private set; } /// - /// Признак (опция) наличия колес + /// Признак (опция) наличия колес(2) /// - public bool Wheels { get; private set; } + public bool Beacon { get; private set; } /// /// Шаг перемещения машины /// @@ -43,17 +39,15 @@ public class EntityMachine /// Вес машины /// Основной цвет /// Дополнительный цвет - /// Модель /// Признак наличия бака для бензина - /// Признак наличия колес - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool model, bool gas, bool wheels) + /// Признак наличия маяка + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool gas, bool beacon) { Speed = speed; Weight = weight; BodyColor = bodyColor; AdditionalColor = additionalColor; - Model = model; Gas = gas; - Wheels = wheels; + Beacon = beacon; } } diff --git a/ProjectCar/ProjectCar/FormGasMachine.Designer.cs b/ProjectCar/ProjectCar/FormGasMachine.Designer.cs index 582171c..e7e4146 100644 --- a/ProjectCar/ProjectCar/FormGasMachine.Designer.cs +++ b/ProjectCar/ProjectCar/FormGasMachine.Designer.cs @@ -34,6 +34,8 @@ buttonRight = new Button(); buttonUp = new Button(); buttonDown = new Button(); + pictureBoxGasMachine = new PictureBox(); + ((System.ComponentModel.ISupportInitialize)pictureBoxGasMachine).BeginInit(); SuspendLayout(); // // buttonCreate @@ -45,7 +47,7 @@ buttonCreate.TabIndex = 1; buttonCreate.Text = "Создать"; buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreate.Click += ButtonCreate_Click; // // buttonLeft // @@ -57,7 +59,7 @@ buttonLeft.Size = new Size(35, 35); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; - buttonLeft.Click += buttonLeft_Click; + buttonLeft.Click += ButtonMove_Click; // // buttonRight // @@ -69,6 +71,7 @@ buttonRight.Size = new Size(35, 35); buttonRight.TabIndex = 3; buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += ButtonMove_Click; // // buttonUp // @@ -80,6 +83,7 @@ buttonUp.Size = new Size(35, 35); buttonUp.TabIndex = 4; buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += ButtonMove_Click; // // buttonDown // @@ -91,6 +95,17 @@ buttonDown.Size = new Size(35, 35); buttonDown.TabIndex = 5; buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += ButtonMove_Click; + // + // pictureBoxGasMachine + // + pictureBoxGasMachine.Dock = DockStyle.Fill; + pictureBoxGasMachine.Location = new Point(0, 0); + pictureBoxGasMachine.Name = "pictureBoxGasMachine"; + pictureBoxGasMachine.Size = new Size(988, 528); + pictureBoxGasMachine.TabIndex = 0; + pictureBoxGasMachine.TabStop = false; + pictureBoxGasMachine.Click += ButtonMove_Click; // // FormGasMachine // @@ -102,9 +117,10 @@ Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonCreate); + Controls.Add(pictureBoxGasMachine); Name = "FormGasMachine"; Text = "Газовоз"; - Load += FormGasMachine_Load; + ((System.ComponentModel.ISupportInitialize)pictureBoxGasMachine).EndInit(); ResumeLayout(false); } @@ -114,5 +130,6 @@ private Button buttonRight; private Button buttonUp; private Button buttonDown; + private PictureBox pictureBoxGasMachine; } } \ No newline at end of file diff --git a/ProjectCar/ProjectCar/FormGasMachine.cs b/ProjectCar/ProjectCar/FormGasMachine.cs index fb3f876..498548c 100644 --- a/ProjectCar/ProjectCar/FormGasMachine.cs +++ b/ProjectCar/ProjectCar/FormGasMachine.cs @@ -12,21 +12,92 @@ namespace ProjectCar { public partial class FormGasMachine : Form { - private DrawningGasMachine? _drawningGasMachine; + private DrawningGasMachine? _drawningGasMachine; + + /// + /// конструктор формы + /// public FormGasMachine() { InitializeComponent(); } + + /// + /// метод прорисовки машины + /// + private void Draw() + { + if (_drawningGasMachine == null) + { + return; + } + + + Bitmap bmp = new(pictureBoxGasMachine.Width, pictureBoxGasMachine.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningGasMachine.DrawTransport(gr); + pictureBoxGasMachine.Image = bmp; + } + + + /// + /// обработка нажатия кнопки "создать" + /// + /// + /// private void ButtonCreate_Click(object sender, EventArgs e) { Random random = new(); _drawningGasMachine = new DrawningGasMachine(); - _drawningGasMachine.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)); - _drawningGasMachine.SetPictureSize(pictureBoxGasMachine.Width) + _drawningGasMachine.Init(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), 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), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + _drawningGasMachine.SetPictureSize(pictureBoxGasMachine.Width, pictureBoxGasMachine.Height); + _drawningGasMachine.SetPosition(random.Next(10, 100), random.Next(10, 100)); + + Draw(); } + + /// + /// перемещение объекта по форме + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningGasMachine == null) + { + return; + } + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonUp": + result = _drawningGasMachine.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = _drawningGasMachine.MoveTransport(DirectionType.Down); + break; + case "buttonRight": + result = _drawningGasMachine.MoveTransport(DirectionType.Right); + break; + case "buttonLeft": + result = _drawningGasMachine.MoveTransport(DirectionType.Left); + break; + } + if (result) + { + Draw(); + } + + + + + } + + } } diff --git a/ProjectCar/ProjectCar/Program.cs b/ProjectCar/ProjectCar/Program.cs index daed97b..87de37b 100644 --- a/ProjectCar/ProjectCar/Program.cs +++ b/ProjectCar/ProjectCar/Program.cs @@ -11,7 +11,7 @@ namespace ProjectCar // 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 FormGasMachine()); } } } \ No newline at end of file diff --git a/ProjectCar/ProjectCar/ProjectCar - Backup.csproj b/ProjectCar/ProjectCar/ProjectCar - Backup.csproj new file mode 100644 index 0000000..af03d74 --- /dev/null +++ b/ProjectCar/ProjectCar/ProjectCar - Backup.csproj @@ -0,0 +1,26 @@ + + + + WinExe + net8.0-windows + enable + true + enable + + + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + \ No newline at end of file diff --git a/ProjectCar/ProjectCar/ProjectCar.csproj b/ProjectCar/ProjectCar/ProjectCar.csproj index af03d74..8913e92 100644 --- a/ProjectCar/ProjectCar/ProjectCar.csproj +++ b/ProjectCar/ProjectCar/ProjectCar.csproj @@ -8,6 +8,11 @@ enable + + + + + True