ISEbd-12_Platonov_A.M._LabWork1_Simple #1

Closed
cyxaruk wants to merge 5 commits from LabWork01 into main
7 changed files with 235 additions and 37 deletions
Showing only changes of commit 5a2601f0fa - Show all commits

View File

@ -1,4 +1,6 @@
namespace ProjectCar;
using System.Reflection.Metadata.Ecma335;
namespace ProjectCar;
/// <summary>
///
/// </summary>
@ -28,16 +30,16 @@ public class DrawningGasMachine
/// <summary>
/// Ширина прорисовки газ автомобиля
/// </summary>
private readonly int _drawningMachineWidth = 0;
private readonly int _drawningMachineWidth = 180;
/// <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.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
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих границах</returns>
public bool SetPictureSize(int width, int height)
public bool SetPictureSize(int width, int height)
{
Review

Нет проверки, что объект можно "вписать" в размеры формы

Нет проверки, что объект можно "вписать" в размеры формы
_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;
}
Outdated
Review

Нет корректировки координат, если при текущих объект "выходит" за границы

Нет корректировки координат, если при текущих объект "выходит" за границы
if (_startPosY + _drawningMachineHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningMachineHeight;
}
}
return true;
}
return false;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</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;
}
_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)
Outdated
Review

А где код прорисовки?

А где код прорисовки?
{
@ -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);
}
}

View File

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

View File

@ -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;
}
}

View File

@ -12,21 +12,92 @@ namespace ProjectCar
{
public partial class FormGasMachine : Form
{
private DrawningGasMachine? _drawningGasMachine;
private DrawningGasMachine? _drawningGasMachine;
/// <summary>
/// конструктор формы
/// </summary>
public FormGasMachine()
{
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)
{
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();
}
/// <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,
// see https://aka.ms/applicationconfiguration.
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>
</PropertyGroup>
<ItemGroup>
<None Include="ProjectCar.csproj" />
<None Include="ProjectCar.csproj.user" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>