Готовая лаба

This commit is contained in:
Павел Ладягин 2024-03-23 15:58:03 +04:00
parent ef5352ab0c
commit b039442fea
4 changed files with 157 additions and 43 deletions

View File

@ -5,6 +5,11 @@
/// </summary> /// </summary>
public enum DirectionType public enum DirectionType
{ {
/// <summary>
/// Неизвестное направление
/// </summary>
Unknow = -1,
/// <summary> /// <summary>
/// Вверх /// Вверх
/// </summary> /// </summary>

View File

@ -34,6 +34,9 @@
buttonRight = new Button(); buttonRight = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonUp = new Button(); buttonUp = new Button();
ButtonCreateAirplane = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -41,21 +44,19 @@
// //
pictureBoxAirplaneWithRadar.Dock = DockStyle.Fill; pictureBoxAirplaneWithRadar.Dock = DockStyle.Fill;
pictureBoxAirplaneWithRadar.Location = new Point(0, 0); pictureBoxAirplaneWithRadar.Location = new Point(0, 0);
pictureBoxAirplaneWithRadar.Margin = new Padding(3, 4, 3, 4);
pictureBoxAirplaneWithRadar.Name = "pictureBoxAirplaneWithRadar"; pictureBoxAirplaneWithRadar.Name = "pictureBoxAirplaneWithRadar";
pictureBoxAirplaneWithRadar.Size = new Size(1128, 636); pictureBoxAirplaneWithRadar.Size = new Size(987, 477);
pictureBoxAirplaneWithRadar.TabIndex = 0; pictureBoxAirplaneWithRadar.TabIndex = 0;
pictureBoxAirplaneWithRadar.TabStop = false; pictureBoxAirplaneWithRadar.TabStop = false;
// //
// buttonCreate // buttonCreate
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(14, 589); buttonCreate.Location = new Point(12, 442);
buttonCreate.Margin = new Padding(3, 4, 3, 4);
buttonCreate.Name = "buttonCreate"; buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(86, 31); buttonCreate.Size = new Size(199, 23);
buttonCreate.TabIndex = 1; buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать Самолет с радаром";
buttonCreate.UseVisualStyleBackColor = true; buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click; buttonCreate.Click += ButtonCreate_Click;
// //
@ -64,10 +65,9 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.Стрелка_влево; buttonLeft.BackgroundImage = Properties.Resources.Стрелка_влево;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(981, 573); buttonLeft.Location = new Point(858, 430);
buttonLeft.Margin = new Padding(3, 4, 3, 4);
buttonLeft.Name = "buttonLeft"; buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(40, 47); buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 2; buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click; buttonLeft.Click += ButtonMove_Click;
@ -77,10 +77,9 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.Стрелка_вправо; buttonRight.BackgroundImage = Properties.Resources.Стрелка_вправо;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch; buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(1074, 573); buttonRight.Location = new Point(940, 430);
buttonRight.Margin = new Padding(3, 4, 3, 4);
buttonRight.Name = "buttonRight"; buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(40, 47); buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 3; buttonRight.TabIndex = 3;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click; buttonRight.Click += ButtonMove_Click;
@ -90,10 +89,9 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.Стрелка_вниз; buttonDown.BackgroundImage = Properties.Resources.Стрелка_вниз;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch; buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(1027, 573); buttonDown.Location = new Point(899, 430);
buttonDown.Margin = new Padding(3, 4, 3, 4);
buttonDown.Name = "buttonDown"; buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(40, 47); buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 4; buttonDown.TabIndex = 4;
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click; buttonDown.Click += ButtonMove_Click;
@ -103,26 +101,58 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.Стрелка_вверх; buttonUp.BackgroundImage = Properties.Resources.Стрелка_вверх;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch; buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(1027, 519); buttonUp.Location = new Point(899, 389);
buttonUp.Margin = new Padding(3, 4, 3, 4);
buttonUp.Name = "buttonUp"; buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(40, 47); buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 5; buttonUp.TabIndex = 5;
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click; buttonUp.Click += ButtonMove_Click;
// //
// ButtonCreateAirplane
//
ButtonCreateAirplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateAirplane.Location = new Point(230, 442);
ButtonCreateAirplane.Name = "ButtonCreateAirplane";
ButtonCreateAirplane.Size = new Size(199, 23);
ButtonCreateAirplane.TabIndex = 6;
ButtonCreateAirplane.Text = "Создать Самолет";
ButtonCreateAirplane.UseVisualStyleBackColor = true;
ButtonCreateAirplane.Click += ButtonCreateAirplane_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(854, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 7;
//
// buttonStrategyStep
//
buttonStrategyStep.Location = new Point(900, 41);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(75, 23);
buttonStrategyStep.TabIndex = 8;
buttonStrategyStep.Text = "Шаг";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += buttonStrategyStep_Click;
//
// FormAirplaneWithRadar // FormAirplaneWithRadar
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1128, 636); ClientSize = new Size(987, 477);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(ButtonCreateAirplane);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonCreate); Controls.Add(buttonCreate);
Controls.Add(pictureBoxAirplaneWithRadar); Controls.Add(pictureBoxAirplaneWithRadar);
Margin = new Padding(3, 4, 3, 4);
Name = "FormAirplaneWithRadar"; Name = "FormAirplaneWithRadar";
Text = "Самолет с радаром"; Text = "Самолет с радаром";
((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).EndInit();
@ -137,5 +167,8 @@
private Button buttonRight; private Button buttonRight;
private Button buttonDown; private Button buttonDown;
private Button buttonUp; private Button buttonUp;
private Button ButtonCreateAirplane;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStep;
} }
} }

View File

@ -1,5 +1,5 @@
using ProjectAirplaneWithRadar.Drawnings; using ProjectAirplaneWithRadar.Drawnings;
using ProjectAirplaneWithRadar.Entities; using ProjectAirplaneWithRadar.MovementStrategy;
namespace ProjectAirplaneWithRadar namespace ProjectAirplaneWithRadar
{ {
@ -11,7 +11,12 @@ namespace ProjectAirplaneWithRadar
/// <summary> /// <summary>
/// Поле-объект для происовки объект /// Поле-объект для происовки объект
/// </summary> /// </summary>
private DrawingAirplaneWithRadar? _drawingAirplaneWithRadar; private DrawningAirplane? _drawingAirplane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary> /// <summary>
/// Конструктор формы /// Конструктор формы
@ -19,39 +24,67 @@ namespace ProjectAirplaneWithRadar
public FormAirplaneWithRadar() public FormAirplaneWithRadar()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null;
} }
/// <summary> /// <summary>
/// Обработка нажатия кнопки "Создать" /// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningAirplane):
_drawingAirplane = new DrawningAirplane(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(DrawingAirplaneWithRadar):
_drawingAirplane = new DrawingAirplaneWithRadar(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)));
break;
default:
return;
}
_drawingAirplane.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
_drawingAirplane.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
UpdatePlane();
}
/// <summary>
/// Обработка нажатия кнопки "Создать Самолет с радаром"
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e) private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingAirplaneWithRadar));
{
Random random = new();
_drawingAirplaneWithRadar = new DrawingAirplaneWithRadar();
_drawingAirplaneWithRadar.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)));
_drawingAirplaneWithRadar.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
_drawingAirplaneWithRadar.SetPosition(random.Next(10, 100), random.Next(10, 100));
UpdatePlane(); /// <summary>
} /// Обработка нажатия кнопки "Создать Самолет"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateAirplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirplane));
/// <summary> /// <summary>
/// Метод прорисовки самолета /// Метод прорисовки самолета
/// </summary> /// </summary>
private void UpdatePlane() private void UpdatePlane()
{ {
if (_drawingAirplaneWithRadar == null) if (_drawingAirplane == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); Bitmap bmp = new(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawingAirplaneWithRadar?.DrawTransport(gr); _drawingAirplane?.DrawTransport(gr);
pictureBoxAirplaneWithRadar.Image = bmp; pictureBoxAirplaneWithRadar.Image = bmp;
} }
@ -62,7 +95,7 @@ namespace ProjectAirplaneWithRadar
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawingAirplaneWithRadar == null) if (_drawingAirplane == null)
{ {
return; return;
} }
@ -90,10 +123,53 @@ namespace ProjectAirplaneWithRadar
return; return;
} }
_drawingAirplaneWithRadar.MoveTransport(result); _drawingAirplane.MoveTransport(result);
UpdatePlane(); UpdatePlane();
} }
} }
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawingAirplane == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveablePlane(_drawingAirplane), pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
UpdatePlane();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
} }
} }

View File

@ -58,7 +58,7 @@ namespace ProjectAirplaneWithRadar.MovementStrategy
MovementDirection.Right => DirectionType.Right, MovementDirection.Right => DirectionType.Right,
MovementDirection.Up => DirectionType.Up, MovementDirection.Up => DirectionType.Up,
MovementDirection.Down => DirectionType.Down, MovementDirection.Down => DirectionType.Down,
_ => DirectionType.Unknow, _ => DirectionType.Unknow
}; };
} }
} }