изменения

This commit is contained in:
cyxarukk 2024-03-10 12:07:45 +04:00
parent ae1b6816f6
commit 5a2601f0fa
7 changed files with 235 additions and 37 deletions

View File

@ -1,4 +1,6 @@
namespace ProjectCar; using System.Reflection.Metadata.Ecma335;
namespace ProjectCar;
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -28,16 +30,16 @@ public class DrawningGasMachine
/// <summary> /// <summary>
/// Ширина прорисовки газ автомобиля /// Ширина прорисовки газ автомобиля
/// </summary> /// </summary>
private readonly int _drawningMachineWidth = 0; private readonly int _drawningMachineWidth = 180;
/// <summary> /// <summary>
/// Высота прорисовки газ автомобиля /// Высота прорисовки газ автомобиля
/// </summary> /// </summary>
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 = new EntityMachine();
EntityMachine.Init(speed, weight, bodyColor, additionalColor, model, gas, wheels); EntityMachine.Init(speed, weight, bodyColor, additionalColor, gas, beacon);
_pictureWidth = null; _pictureWidth = null;
_pictureHeight = null; _pictureHeight = null;
_startPosX = null; _startPosX = null;
@ -49,27 +51,65 @@ public class DrawningGasMachine
/// <param name="width">Ширина поля</param> /// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param> /// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих границах</returns> /// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих границах</returns>
public bool SetPictureSize(int width, int height) public bool SetPictureSize(int width, int height)
{ {
if (EntityMachine == null)
_pictureWidth = width; {
_pictureHeight = height; return false;
return true; }
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;
} }
/// <summary> /// <summary>
/// Установка позиции /// Установка позиции
/// </summary> /// </summary>
/// <param name="x">Координата X</param> /// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param> /// <param name="y">Координата Y</param>
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; return;
} }
_startPosX = x; _startPosX = x;
_startPosY = y; _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) public bool MoveTransport(DirectionType direction)
{ {
@ -92,13 +132,13 @@ public class DrawningGasMachine
} }
return true; return true;
case DirectionType.Right: case DirectionType.Right:
if (_startPosX.Value + EntityMachine.Step < _pictureWidth) if (_startPosX.Value + EntityMachine.Step + _drawningMachineWidth < _pictureWidth)
{ {
_startPosX += (int)EntityMachine.Step; _startPosX += (int)EntityMachine.Step;
} }
return true; return true;
case DirectionType.Down: case DirectionType.Down:
if (_startPosY.Value + EntityMachine.Step < _pictureHeight) if (_startPosY.Value + EntityMachine.Step + _drawningMachineHeight < _pictureHeight)
{ {
_startPosY += (int)EntityMachine.Step; _startPosY += (int)EntityMachine.Step;
} }
@ -117,6 +157,51 @@ public class DrawningGasMachine
{ {
return; 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);
}
} }

View File

@ -21,17 +21,13 @@ public class EntityMachine
/// </summary> /// </summary>
public Color AdditionalColor { get; private set; } public Color AdditionalColor { get; private set; }
/// <summary> /// <summary>
/// Модель машины
/// </summary>
public bool Model { get; private set; }
/// <summary>
/// Признак (опция) наличия бака для бензина /// Признак (опция) наличия бака для бензина
/// </summary> /// </summary>
public bool Gas { get; private set; } public bool Gas { get; private set; }
/// <summary> /// <summary>
/// Признак (опция) наличия колес /// Признак (опция) наличия колес(2)
/// </summary> /// </summary>
public bool Wheels { get; private set; } public bool Beacon { get; private set; }
/// <summary> /// <summary>
/// Шаг перемещения машины /// Шаг перемещения машины
/// </summary> /// </summary>
@ -43,17 +39,15 @@ public class EntityMachine
/// <param name="weight">Вес машины</param> /// <param name="weight">Вес машины</param>
/// <param name="bodyColor">Основной цвет</param> /// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param> /// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="model">Модель</param>
/// <param name="gas">Признак наличия бака для бензина</param> /// <param name="gas">Признак наличия бака для бензина</param>
/// <param name="wheels">Признак наличия колес</param> /// <param name="beacon">Признак наличия маяка</param>
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; Speed = speed;
Weight = weight; Weight = weight;
BodyColor = bodyColor; BodyColor = bodyColor;
AdditionalColor = additionalColor; AdditionalColor = additionalColor;
Model = model;
Gas = gas; Gas = gas;
Wheels = wheels; Beacon = beacon;
} }
} }

View File

@ -34,6 +34,8 @@
buttonRight = new Button(); buttonRight = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonDown = new Button(); buttonDown = new Button();
pictureBoxGasMachine = new PictureBox();
((System.ComponentModel.ISupportInitialize)pictureBoxGasMachine).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// buttonCreate // buttonCreate
@ -45,7 +47,7 @@
buttonCreate.TabIndex = 1; buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true; buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click; buttonCreate.Click += ButtonCreate_Click;
// //
// buttonLeft // buttonLeft
// //
@ -57,7 +59,7 @@
buttonLeft.Size = new Size(35, 35); buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 2; buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonLeft_Click; buttonLeft.Click += ButtonMove_Click;
// //
// buttonRight // buttonRight
// //
@ -69,6 +71,7 @@
buttonRight.Size = new Size(35, 35); buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 3; buttonRight.TabIndex = 3;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
// //
// buttonUp // buttonUp
// //
@ -80,6 +83,7 @@
buttonUp.Size = new Size(35, 35); buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 4; buttonUp.TabIndex = 4;
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
// //
// buttonDown // buttonDown
// //
@ -91,6 +95,17 @@
buttonDown.Size = new Size(35, 35); buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 5; buttonDown.TabIndex = 5;
buttonDown.UseVisualStyleBackColor = true; 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 // FormGasMachine
// //
@ -102,9 +117,10 @@
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonCreate); Controls.Add(buttonCreate);
Controls.Add(pictureBoxGasMachine);
Name = "FormGasMachine"; Name = "FormGasMachine";
Text = "Газовоз"; Text = "Газовоз";
Load += FormGasMachine_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxGasMachine).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -114,5 +130,6 @@
private Button buttonRight; private Button buttonRight;
private Button buttonUp; private Button buttonUp;
private Button buttonDown; private Button buttonDown;
private PictureBox pictureBoxGasMachine;
} }
} }

View File

@ -12,21 +12,92 @@ namespace ProjectCar
{ {
public partial class FormGasMachine : Form public partial class FormGasMachine : Form
{ {
private DrawningGasMachine? _drawningGasMachine; private DrawningGasMachine? _drawningGasMachine;
/// <summary>
/// конструктор формы
/// </summary>
public FormGasMachine() public FormGasMachine()
{ {
InitializeComponent(); InitializeComponent();
} }
/// <summary>
/// метод прорисовки машины
/// </summary>
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;
}
/// <summary>
/// обработка нажатия кнопки "создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e) private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawningGasMachine = new DrawningGasMachine(); _drawningGasMachine = new DrawningGasMachine();
_drawningGasMachine.Init(random.Next(100, 300), random.Next(1000, 3000)), _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), 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), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0,2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)); Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawningGasMachine.SetPictureSize(pictureBoxGasMachine.Width) _drawningGasMachine.SetPictureSize(pictureBoxGasMachine.Width, pictureBoxGasMachine.Height);
_drawningGasMachine.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 (_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();
}
}
} }
} }

View File

@ -11,7 +11,7 @@ namespace ProjectCar
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new Form1()); Application.Run(new FormGasMachine());
} }
} }
} }

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

View File

@ -8,6 +8,11 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Include="ProjectCar.csproj" />
<None Include="ProjectCar.csproj.user" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="Properties\Resources.Designer.cs"> <Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>