половина 2 лабы

This commit is contained in:
cyxarukk 2024-03-10 16:16:18 +04:00
parent 9bdf486696
commit 7f9117622a
11 changed files with 332 additions and 201 deletions

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectCar", "ProjectCar\ProjectCar.csproj", "{0BAD5252-A074-4558-8F3E-7CDB84331644}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjectGasMachine", "ProjectCar\ProjectGasMachine.csproj", "{0BAD5252-A074-4558-8F3E-7CDB84331644}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,4 +1,4 @@
namespace ProjectCar;
namespace ProjectGasMachine.Drawnings;
/// <summary>
/// Направление перемещения
/// </summary>

View File

@ -0,0 +1,61 @@
using ProjectGasMachine.Entities;
namespace ProjectGasMachine.Drawnings;
/// <summary>
///
/// </summary>
public class DrawningGasMachine : DrawningMachine
{
/// <summary>
/// конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес машины</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="gas">Признак наличия бака для бензина</param>
/// <param name="beacon">Признак наличия маяка</param>
public DrawningGasMachine(int speed, double weight, Color bodyColor, Color additionalColor, bool gas, bool beacon) : base(115, 70)
{
EntityGas = new EntityMachine(speed, weight, bodyColor, additionalColor, gas, beacon);
}
public override void DrawTransport(Graphics g)
{
if (EntityGas == null || EntityGas is not EntityMachine gasmachine || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(gasmachine.AdditionalColor);
//маяк сигнальный
if (gasmachine.Beacon)
{
g.DrawRectangle(pen, _startPosX.Value + 95, _startPosY.Value, 10, 10);
g.FillRectangle(additionalBrush, _startPosX.Value + 95, _startPosY.Value, 10, 10);
}
//бак с газом
if (gasmachine.Gas)
{
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 10, 80, 30);
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 10, 80, 30);
}
_startPosX += 5;
base.DrawTransport(g);
_startPosX -= 5;
}
}

View File

@ -1,15 +1,18 @@
using System.Reflection.Metadata.Ecma335;
using ProjectGasMachine.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCar;
/// <summary>
///
/// </summary>
public class DrawningGasMachine
namespace ProjectGasMachine.Drawnings;
public class DrawningMachine
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityMachine? EntityMachine { get; private set; }
public EntityGas? EntityGas { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
@ -22,29 +25,55 @@ public class DrawningGasMachine
/// <summary>
/// Левая координата прорисовки газ автомобиля
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки газ автомобиля
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки газ автомобиля
/// </summary>
private readonly int _drawningMachineWidth = 115;
private readonly int _drawningMachineWidth = 110;
/// <summary>
/// Высота прорисовки газ автомобиля
/// </summary>
private readonly int _drawningMachineHeight = 70;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool gas, bool beacon)
/// <summary>
/// пустой конструктор
/// </summary>
private DrawningMachine()
{
EntityMachine = new EntityMachine();
EntityMachine.Init(speed, weight, bodyColor, additionalColor, gas, beacon);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес машины</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningMachine(int speed, double weight, Color bodyColor) : this()
{
EntityGas = new EntityGas(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningMachineWidth">Ширина прорисовки газ автомобиля</param>
/// <param name="drawningMachineHeight">Высота прорисовки газ автомобиля</param>
protected DrawningMachine(int drawningMachineWidth, int drawningMachineHeight) : this()
{
_drawningMachineWidth = drawningMachineWidth;
_pictureHeight = drawningMachineHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
@ -53,7 +82,7 @@ public class DrawningGasMachine
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих границах</returns>
public bool SetPictureSize(int width, int height)
{
if (EntityMachine == null)
if (EntityGas == null)
{
return false;
}
@ -111,36 +140,36 @@ public class DrawningGasMachine
_startPosY = _pictureHeight - _drawningMachineHeight;
}
}
public bool MoveTransport(DirectionType direction)
public bool MoveTransport(DirectionType direction)
{
if (EntityMachine == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityGas == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
switch (direction)
{
case DirectionType.Left:
if (_startPosX.Value - EntityMachine.Step > 0)
if (_startPosX.Value - EntityGas.Step > 0)
{
_startPosX -= (int)EntityMachine.Step;
_startPosX -= (int)EntityGas.Step;
}
return true;
case DirectionType.Up:
if (_startPosY.Value - EntityMachine.Step > 0)
if (_startPosY.Value - EntityGas.Step > 0)
{
_startPosY -= (int)EntityMachine.Step;
_startPosY -= (int)EntityGas.Step;
}
return true;
case DirectionType.Right:
if (_startPosX.Value + EntityMachine.Step + _drawningMachineWidth < _pictureWidth)
if (_startPosX.Value + EntityGas.Step + _drawningMachineWidth < _pictureWidth)
{
_startPosX += (int)EntityMachine.Step;
_startPosX += (int)EntityGas.Step;
}
return true;
case DirectionType.Down:
if (_startPosY.Value + EntityMachine.Step + _drawningMachineHeight < _pictureHeight)
if (_startPosY.Value + EntityGas.Step + _drawningMachineHeight < _pictureHeight)
{
_startPosY += (int)EntityMachine.Step;
_startPosY += (int)EntityGas.Step;
}
return true;
default:
@ -151,54 +180,41 @@ public class DrawningGasMachine
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityMachine == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityGas == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
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);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 40, 110, 10);
g.DrawRectangle(pen, _startPosX.Value + 76, _startPosY.Value + 10, 30, 30);
//кузов
Brush br = new SolidBrush(EntityMachine.BodyColor);
g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 40, 110, 10);
Brush br = new SolidBrush(EntityGas.BodyColor);
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 40, 110, 10);
//кабина
g.FillRectangle(br, _startPosX.Value + 81, _startPosY.Value + 10, 30, 30);
g.FillRectangle(br, _startPosX.Value + 76, _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);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 50, 20, 20);
g.FillEllipse(br, _startPosX.Value + 10, _startPosY.Value + 50, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 85, _startPosY.Value + 50, 20, 20);
g.FillEllipse(br, _startPosX.Value + 85, _startPosY.Value + 50, 20, 20);

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectGasMachine.Entities;
/// <summary>
/// Класс-сущность "машина для перевозки газа"
/// </summary>
public class EntityGas
{
/// <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 double Step => Speed * 100 / Weight;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес машины</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityGas(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,30 @@
namespace ProjectGasMachine.Entities;
/// <summary>
/// Класс-сущность "бензовоз"
/// </summary>
public class EntityMachine : EntityGas
{
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличия бака для бензина
/// </summary>
public bool Gas { get; private set; }
/// <summary>
/// Признак (опция) наличия маяка
/// </summary>
public bool Beacon { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="gas">Признак наличия бака для бензина</param>
/// <param name="beacon">Признак наличия маяка</param>
public EntityMachine(int speed, double weight, Color bodyColor, Color additionalColor, bool gas, bool beacon) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Gas = gas;
Beacon = beacon;
}
}

View File

@ -1,53 +0,0 @@
namespace ProjectCar;
/// <summary>
/// Класс-сущность "бензовоз"
/// </summary>
public class EntityMachine
{
/// <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 Gas { get; private set; }
/// <summary>
/// Признак (опция) наличия колес(2)
/// </summary>
public bool Beacon { 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="gas">Признак наличия бака для бензина</param>
/// <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;
Gas = gas;
Beacon = beacon;
}
}

View File

@ -35,6 +35,7 @@
buttonUp = new Button();
buttonDown = new Button();
pictureBoxGasMachine = new PictureBox();
buttonCreateMachine = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxGasMachine).BeginInit();
SuspendLayout();
//
@ -43,9 +44,9 @@
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 493);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(75, 23);
buttonCreate.Size = new Size(216, 23);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.Text = "Создать газовоз";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
//
@ -107,11 +108,23 @@
pictureBoxGasMachine.TabStop = false;
pictureBoxGasMachine.Click += ButtonMove_Click;
//
// buttonCreateMachine
//
buttonCreateMachine.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateMachine.Location = new Point(245, 493);
buttonCreateMachine.Name = "buttonCreateMachine";
buttonCreateMachine.Size = new Size(216, 23);
buttonCreateMachine.TabIndex = 6;
buttonCreateMachine.Text = "Создать орнамент на колесах";
buttonCreateMachine.UseVisualStyleBackColor = true;
buttonCreateMachine.Click += buttonCreateMachine_Click;
//
// FormGasMachine
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(988, 528);
Controls.Add(buttonCreateMachine);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
@ -131,5 +144,6 @@
private Button buttonUp;
private Button buttonDown;
private PictureBox pictureBoxGasMachine;
private Button buttonCreateMachine;
}
}

View File

@ -1,103 +1,121 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectGasMachine.Drawnings;
namespace ProjectCar
namespace ProjectGasMachine;
public partial class FormGasMachine : Form
{
public partial class FormGasMachine : Form
private DrawningMachine? _drawningMachine;
/// <summary>
/// конструктор формы
/// </summary>
public FormGasMachine()
{
private DrawningGasMachine? _drawningGasMachine;
InitializeComponent();
}
/// <summary>
/// конструктор формы
/// </summary>
public FormGasMachine()
/// <summary>
/// метод прорисовки машины
/// </summary>
private void Draw()
{
if (_drawningMachine == null)
{
InitializeComponent();
return;
}
/// <summary>
/// метод прорисовки машины
/// </summary>
private void Draw()
Bitmap bmp = new(pictureBoxGasMachine.Width, pictureBoxGasMachine.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningMachine.DrawTransport(gr);
pictureBoxGasMachine.Image = bmp;
}
/// <summary>
/// создание объекта класса-перемещение
/// </summary>
/// <param name="type"></param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
if (_drawningGasMachine == null)
{
case nameof(DrawningMachine):
_drawningMachine = new DrawningMachine(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningGasMachine):
_drawningMachine = new DrawningGasMachine(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)));
break;
default:
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), 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));
_drawningMachine.SetPictureSize(pictureBoxGasMachine.Width, pictureBoxGasMachine.Height);
_drawningMachine.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// обработка нажатия кнопки "создать газовоз"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningGasMachine));
/// <summary>
/// обработка нажатия кнопки "создать орнамент на колесах"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateMachine_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMachine));
/// <summary>
/// перемещение объекта по форме
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningMachine == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningMachine.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningMachine.MoveTransport(DirectionType.Down);
break;
case "buttonRight":
result = _drawningMachine.MoveTransport(DirectionType.Right);
break;
case "buttonLeft":
result = _drawningMachine.MoveTransport(DirectionType.Left);
break;
}
if (result)
{
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

@ -9,8 +9,8 @@
</PropertyGroup>
<ItemGroup>
<None Include="ProjectCar.csproj" />
<None Include="ProjectCar.csproj.user" />
<None Include="ProjectGasMachine.csproj" />
<None Include="ProjectGasMachine.csproj.user" />
</ItemGroup>
<ItemGroup>

View File

@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------
namespace ProjectCar.Properties {
namespace ProjectGasMachine.Properties {
using System;
@ -39,7 +39,7 @@ namespace ProjectCar.Properties {
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ProjectCar.Properties.Resources", typeof(Resources).Assembly);
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ProjectGasMachine.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;