готовая лаб работа 2

This commit is contained in:
cyxarukk 2024-03-12 12:14:52 +04:00
parent 40448cbf4f
commit 626b613b39
2 changed files with 82 additions and 1 deletions

View File

@ -35,6 +35,8 @@
buttonDown = new Button();
pictureBoxGasMachine = new PictureBox();
buttonCreateMachine = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxGasMachine).BeginInit();
SuspendLayout();
//
@ -117,11 +119,33 @@
buttonCreateMachine.UseVisualStyleBackColor = true;
buttonCreateMachine.Click += ButtonCreateMachine_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(858, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 7;
//
// buttonStrategyStep
//
buttonStrategyStep.Location = new Point(901, 41);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(75, 23);
buttonStrategyStep.TabIndex = 8;
buttonStrategyStep.Text = "Шаг";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += ButtonStrategyStep_Click;
//
// FormGasMachine
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(988, 528);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateMachine);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
@ -143,5 +167,7 @@
private Button buttonDown;
private PictureBox pictureBoxGasMachine;
private Button buttonCreateMachine;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStep;
}
}

View File

@ -1,17 +1,27 @@
using ProjectGasMachine.Drawnings;
using ProjectGasMachine.MovementStrategy;
namespace ProjectGasMachine;
public partial class FormGasMachine : Form
{
/// <summary>
/// поле-объект для прорисовки объекта
/// </summary>
private DrawningMachine? _drawningMachine;
/// <summary>
/// стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// конструктор формы
/// </summary>
public FormGasMachine()
{
InitializeComponent();
_strategy = null;
}
@ -57,7 +67,8 @@ public partial class FormGasMachine : Form
_drawningMachine.SetPictureSize(pictureBoxGasMachine.Width, pictureBoxGasMachine.Height);
_drawningMachine.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
@ -116,6 +127,50 @@ public partial class FormGasMachine : Form
{
Draw();
}
}
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningMachine == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableMachine(_drawningMachine), pictureBoxGasMachine.Width, pictureBoxGasMachine.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}