Дополнение формы (логики, элементов)

This commit is contained in:
malimova 2023-10-06 23:12:58 +04:00
parent 5c75d533a1
commit 5890fcbba7
3 changed files with 94 additions and 28 deletions

View File

@ -6,7 +6,30 @@ using System.Threading.Tasks;
namespace AirBomber
{
internal class DrawningObjectAirPlane
public class DrawningObjectAirPlane : IMoveableObject
{
private readonly DrawningAirPlane? _drawningAirPlane = null;
public DrawningObjectAirPlane(DrawningAirPlane drawningAirPlane)
{
_drawningAirPlane = drawningAirPlane;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningAirPlane == null || _drawningAirPlane.EntityAirPlane ==
null)
{
return null;
}
return new ObjectParameters(_drawningAirPlane.GetPosX,
_drawningAirPlane.GetPosY, _drawningAirPlane.GetWidth, _drawningAirPlane.GetHeight);
}
}
public int GetStep => (int)(_drawningAirPlane?.EntityAirPlane?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningAirPlane?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningAirPlane?.MoveTransport(direction);
}
}

View File

@ -28,25 +28,27 @@
/// </summary>
private void InitializeComponent()
{
buttonCreate = new Button();
buttonCreateAirBomber = new Button();
buttonDown = new Button();
buttonLeft = new Button();
buttonRight = new Button();
buttonUp = new Button();
pictureBoxAirBomber = new PictureBox();
comboBoxStrategy = new ComboBox();
buttonCreateAirPlane = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit();
SuspendLayout();
//
// buttonCreate
// buttonCreateAirBomber
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(29, 479);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(148, 34);
buttonCreate.TabIndex = 0;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
buttonCreateAirBomber.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateAirBomber.Location = new Point(29, 447);
buttonCreateAirBomber.Name = "buttonCreateAirBomber";
buttonCreateAirBomber.Size = new Size(191, 66);
buttonCreateAirBomber.TabIndex = 0;
buttonCreateAirBomber.Text = "Создать бомбардировщик";
buttonCreateAirBomber.UseVisualStyleBackColor = true;
buttonCreateAirBomber.Click += buttonCreateAirBomber_Click;
//
// buttonDown
//
@ -106,16 +108,38 @@
pictureBoxAirBomber.TabIndex = 5;
pictureBoxAirBomber.TabStop = false;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToRightEdge" });
comboBoxStrategy.Location = new Point(755, 26);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(219, 33);
comboBoxStrategy.TabIndex = 6;
//
// buttonCreateAirPlane
//
buttonCreateAirPlane.Location = new Point(249, 447);
buttonCreateAirPlane.Name = "buttonCreateAirPlane";
buttonCreateAirPlane.Size = new Size(191, 65);
buttonCreateAirPlane.TabIndex = 7;
buttonCreateAirPlane.Text = "Создать самолёт";
buttonCreateAirPlane.UseVisualStyleBackColor = true;
buttonCreateAirPlane.Click += buttonCreateAirPlane_Click;
//
// FormAirBomber
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(986, 540);
Controls.Add(buttonCreateAirPlane);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateAirBomber);
Controls.Add(pictureBoxAirBomber);
Name = "FormAirBomber";
Text = "Бомбардировщик";
@ -125,11 +149,13 @@
#endregion
private Button buttonCreate;
private Button buttonCreateAirBomber;
private Button buttonDown;
private Button buttonLeft;
private Button buttonRight;
private Button buttonUp;
private PictureBox pictureBoxAirBomber;
private ComboBox comboBoxStrategy;
private Button buttonCreateAirPlane;
}
}

View File

@ -2,37 +2,52 @@
{
public partial class FormAirBomber : Form
{
private DrawningAirBomber? _drawningAirBomber;
private DrawningAirPlane? _drawningAirPlane;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _abstractStrategy;
public FormAirBomber()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningAirBomber == null)
if (_drawningAirPlane == null)
{
return;
}
Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAirBomber.DrawBomber(gr);
_drawningAirPlane.DrawPlane(gr);
pictureBoxAirBomber.Image = bmp;
}
private void buttonCreate_Click(object sender, EventArgs e)
private void buttonCreateAirBomber_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirBomber = new DrawningAirBomber();
_drawningAirBomber.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)),
_drawningAirPlane = new DrawningAirBomber(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)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawningAirBomber.SetPosition(random.Next(10, 100), random.Next(10, 100));
Convert.ToBoolean(random.Next(0, 2)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateAirPlane_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirPlane = new DrawningAirPlane(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningAirBomber == null)
if (_drawningAirPlane == null)
{
return;
}
@ -40,19 +55,21 @@
switch (name)
{
case "buttonUp":
_drawningAirBomber.MoveTransport(DirectionType.Up);
_drawningAirPlane.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningAirBomber.MoveTransport(DirectionType.Down);
_drawningAirPlane.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningAirBomber.MoveTransport(DirectionType.Left);
_drawningAirPlane.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningAirBomber.MoveTransport(DirectionType.Right);
_drawningAirPlane.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
}
}