This commit is contained in:
rakhaliullov 2024-03-07 14:27:50 +03:00
parent 18c3e57349
commit 8d308dab11
5 changed files with 209 additions and 256 deletions

View File

@ -10,7 +10,7 @@ namespace Stormtrooper.Drawnings;
public class DrawningAircraft public class DrawningAircraft
{ {
// Класс-сущность // Класс-сущность
public EntityAircraft? EntityAircraft { get; private set; } // объявление 1 public EntityAircraft? EntityAircraft { get; protected set; } // объявление 1
// Ширина окна // Ширина окна
@ -66,6 +66,99 @@ public class DrawningAircraft
/// Прорисовка объекта /// Прорисовка объекта
/// <param name="g"></param> /// <param name="g"></param>
/// // Установка границ поля
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
public bool SetPictureSize(int width, int height)
{
if (_drawningAircraftHeight > height || _drawningAircraftWidth > width)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
SetPosition(_startPosX.Value, _startPosY.Value);
}
return true;
}
// Установка позиции
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
if (x < 0 || x + _drawningAircraftWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningAircraftWidth;
}
else
{
_startPosX = x;
}
if (y < 0 || y + _drawningAircraftHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningAircraftHeight;
}
else
{
_startPosY = y;
}
}
// Изменение направления перемещения
public bool MoveTransport(DirectionType direction)
{
if (EntityAircraft == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityAircraft.Step > 0)
{
_startPosX -= (int)EntityAircraft.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityAircraft.Step > 0)
{
_startPosY -= (int)EntityAircraft.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityAircraft.Step + _drawningAircraftWidth < _pictureWidth)
{
_startPosX += (int)EntityAircraft.Step;
return true;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityAircraft.Step + _drawningAircraftHeight < _pictureHeight)
{
_startPosY += (int)EntityAircraft.Step;
}
return true;
default:
return false;
}
}
public virtual void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
if (EntityAircraft == null || !_startPosX.HasValue || !_startPosY.HasValue) if (EntityAircraft == null || !_startPosX.HasValue || !_startPosY.HasValue)

View File

@ -12,32 +12,7 @@ using Stormtrooper.Entities;
namespace Stormtrooper.Drawnings; namespace Stormtrooper.Drawnings;
public class DrawningStormtrooper : DrawningAircraft public class DrawningStormtrooper : DrawningAircraft
{ {
// Конструктор
// Ширина окна
public int? _pictureWidth;
// Высота окна
public int? _pictureHeight;
// Левая координата прорисовки штурмовика
public int? _startPosX;
// Верхняя координата прорисовки автомобиля
public int? _startPosY;
// Ширина прорисовки штурмовика
private readonly int _drawningStormtrooperWidth = 140;
// Высота прорисовки штурмовика
private readonly int _drawningStormtrooperHeight = 150;
// Инициализация свойств
/// <param name="speed">Скорость</param> /// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param> /// <param name="weight">Вес</param>
@ -47,168 +22,26 @@ public class DrawningStormtrooper : DrawningAircraft
/// <param name="bomb">Признак наличия бомбы</param> /// <param name="bomb">Признак наличия бомбы</param>
/// <param name="wing">Признак наличия крыла</param> /// <param name="wing">Признак наличия крыла</param>
public DrawningStormtrooper(int speed, double weight, Color bodyColor, Color public DrawningStormtrooper(int speed, double weight, Color bodyColor, Color
additionalColor, bool rocket, bool bomb, bool wing) additionalColor, bool rocket, bool bomb, bool wing) : base(140,150)
{ {
EntityStormtrooper = new EntityStormtrooper(); EntityAircraft = new EntityStormtrooper(speed, weight, bodyColor, additionalColor, rocket, bomb, wing);
EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rocket, bomb, wing);
} }
// Установка границ поля
/// <param name="width">Ширина поля</param> public override void DrawTransport(Graphics g)
/// <param name="height">Высота поля</param>
public bool SetPictureSize(int width, int height)
{ {
if (_drawningStormtrooperHeight > height || _drawningStormtrooperWidth > width) if (EntityAircraft == null || EntityAircraft is not EntityStormtrooper entityStormtrooper|| !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
SetPosition(_startPosX.Value, _startPosY.Value);
}
return true;
}
// Установка позиции
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{ {
return; return;
} }
if (x < 0 || x + _drawningStormtrooperWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningStormtrooperWidth;
}
else
{
_startPosX = x;
}
if (y < 0 || y + _drawningStormtrooperHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningStormtrooperHeight;
}
else
{
_startPosY = y;
}
}
// Изменение направления перемещения
public bool MoveTransport(DirectionType direction)
{
if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityStormtrooper.Step > 0)
{
_startPosX -= (int)EntityStormtrooper.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityStormtrooper.Step > 0)
{
_startPosY -= (int)EntityStormtrooper.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityStormtrooper.Step + _drawningStormtrooperWidth < _pictureWidth)
{
_startPosX += (int)EntityStormtrooper.Step;
return true;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityStormtrooper.Step + _drawningStormtrooperHeight < _pictureHeight)
{
_startPosY += (int)EntityStormtrooper.Step;
}
return true;
default:
return false;
}
}
/// Прорисовка объекта
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush bodyColorBrush = new SolidBrush(EntityStormtrooper.BodyColor); Brush bodyColorBrush = new SolidBrush(entityStormtrooper.BodyColor);
Brush additionalBrush = new SolidBrush(EntityStormtrooper.AdditionalColor); Brush additionalBrush = new SolidBrush(entityStormtrooper.AdditionalColor);
//нос штурмовика
Brush brBlack = new SolidBrush(Color.Black);
Point[] Nose = new Point[3];
Nose[0].X = _startPosX.Value + 20; Nose[0].Y = _startPosY.Value + 80;
Nose[1].X = _startPosX.Value + 20; Nose[1].Y = _startPosY.Value + 60;
Nose[2].X = _startPosX.Value; Nose[2].Y = _startPosY.Value + 70;
g.FillPolygon(brBlack, Nose);
g.DrawPolygon(pen, Nose);
//Заднии крылья штурмовика
Point[] pflybtwings = new Point[6];
pflybtwings[0].X = _startPosX.Value + 120; pflybtwings[0].Y = _startPosY.Value + 60;
pflybtwings[1].X = _startPosX.Value + 120; pflybtwings[1].Y = _startPosY.Value + 50;
pflybtwings[2].X = _startPosX.Value + 140; pflybtwings[2].Y = _startPosY.Value + 30;
pflybtwings[3].X = _startPosX.Value + 140; pflybtwings[3].Y = _startPosY.Value + 110;
pflybtwings[4].X = _startPosX.Value + 120; pflybtwings[4].Y = _startPosY.Value + 90;
pflybtwings[5].X = _startPosX.Value + 120; pflybtwings[5].Y = _startPosY.Value + 80;
g.FillPolygon(bodyColorBrush, pflybtwings);
g.DrawPolygon(pen, pflybtwings);
//Тело штурмовика
g.FillRectangle(bodyColorBrush, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20);
//Крылья штурмовика
Point[] frontwings = new Point[4];
frontwings[0].X = _startPosX.Value + 60; frontwings[0].Y = _startPosY.Value + 60;
frontwings[1].X = _startPosX.Value + 60; frontwings[1].Y = _startPosY.Value;
frontwings[2].X = _startPosX.Value + 70; frontwings[2].Y = _startPosY.Value;
frontwings[3].X = _startPosX.Value + 80; frontwings[3].Y = _startPosY.Value + 60;
g.FillPolygon(bodyColorBrush, frontwings);
g.DrawPolygon(pen, frontwings);
Point[] frontwings2 = new Point[4];
frontwings2[0].X = _startPosX.Value + 60; frontwings2[0].Y = _startPosY.Value + 80;
frontwings2[1].X = _startPosX.Value + 60; frontwings2[1].Y = _startPosY.Value + 140;
frontwings2[2].X = _startPosX.Value + 70; frontwings2[2].Y = _startPosY.Value + 140;
frontwings2[3].X = _startPosX.Value + 80; frontwings2[3].Y = _startPosY.Value + 80;
g.FillPolygon(bodyColorBrush, frontwings2);
g.DrawPolygon(pen, frontwings2);
base.DrawTransport(g);
//Ракеты штурмовика //Ракеты штурмовика
if (entityStormtrooper.Rocket)
if (EntityStormtrooper.Rocket)
{ {
Point[] rockets = new Point[3]; Point[] rockets = new Point[3];
rockets[0].X = _startPosX.Value + 45; rockets[0].Y = _startPosY.Value + 20; rockets[0].X = _startPosX.Value + 45; rockets[0].Y = _startPosY.Value + 20;
@ -233,7 +66,7 @@ public class DrawningStormtrooper : DrawningAircraft
//Бомбы бомбардировщика //Бомбы бомбардировщика
if (EntityStormtrooper.Bomb) if (entityStormtrooper.Bomb)
{ {
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 40, 10, 10); g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 40, 10, 10);
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 90, 10, 10); g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 90, 10, 10);

View File

@ -36,9 +36,6 @@ public class EntityStormtrooper : EntityAircraft
public EntityStormtrooper(int speed, double weight, Color bodyColor, Color public EntityStormtrooper(int speed, double weight, Color bodyColor, Color
additionalColor, bool rocket, bool bomb, bool wing) : base(speed, weight, bodyColor) additionalColor, bool rocket, bool bomb, bool wing) : base(speed, weight, bodyColor)
{ {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor; AdditionalColor = additionalColor;
Rocket = rocket; Rocket = rocket;
Bomb = bomb; Bomb = bomb;

View File

@ -34,6 +34,7 @@
buttonDown = new Button(); buttonDown = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonCreateAircraft = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -50,11 +51,11 @@
// buttonCreate // buttonCreate
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 398); buttonCreate.Location = new Point(12, 395);
buttonCreate.Name = "buttonCreate"; buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29); buttonCreate.Size = new Size(205, 32);
buttonCreate.TabIndex = 1; buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать штурмовик";
buttonCreate.UseVisualStyleBackColor = true; buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click; buttonCreate.Click += ButtonCreate_Click;
// //
@ -106,11 +107,23 @@
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click; buttonRight.Click += ButtonMove_Click;
// //
// buttonCreateAircraft
//
buttonCreateAircraft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateAircraft.Location = new Point(239, 395);
buttonCreateAircraft.Name = "buttonCreateAircraft";
buttonCreateAircraft.Size = new Size(205, 32);
buttonCreateAircraft.TabIndex = 7;
buttonCreateAircraft.Text = "Создать самолет";
buttonCreateAircraft.UseVisualStyleBackColor = true;
buttonCreateAircraft.Click += buttonCreateAircraft_Click;
//
// FormStormtrooper // FormStormtrooper
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(773, 439); ClientSize = new Size(773, 439);
Controls.Add(buttonCreateAircraft);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonDown); Controls.Add(buttonDown);
@ -133,5 +146,6 @@
private Button buttonDown; private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private Button buttonRight; private Button buttonRight;
private Button buttonCreateAircraft;
} }
} }

View File

@ -9,88 +9,104 @@ using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Stormtrooper.Drawnings; using Stormtrooper.Drawnings;
namespace Stormtrooper namespace Stormtrooper;
public partial class FormStormtrooper : Form
{ {
public partial class FormStormtrooper : Form // Поле объект для прорисовки объекта
public DrawningAircraft? _drawningAircraft;
// Конструктор формы
public FormStormtrooper()
{ {
// Поле объект для прорисовки объекта InitializeComponent();
public DrawningStormtrooper? _drawningStormtrooper; }
// Конструктор формы
public FormStormtrooper()
{
InitializeComponent();
}
// Метод прорисовки машины // Метод прорисовки машины
private void FormStormtrooper_Load(object sender, EventArgs e) private void FormStormtrooper_Load(object sender, EventArgs e)
{ {
} }
/// <summary>
// Обработка нажатия кнопки Создать /// Создание объекта класса-перемещения
private void ButtonCreate_Click(object sender, EventArgs e) /// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{ {
Random random = new(); case nameof(DrawningAircraft):
_drawningStormtrooper = new DrawningStormtrooper(); _drawningAircraft = new DrawningAircraft(random.Next(100,
_drawningStormtrooper.Init(random.Next(100, 300), random.Next(1000, 3000), 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)));
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), break;
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); case nameof(DrawningStormtrooper):
_drawningStormtrooper.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); _drawningAircraft = new DrawningStormtrooper(random.Next(100, 300), random.Next(1000, 3000),
_drawningStormtrooper.SetPosition(random.Next(10, 100), random.Next(10, 100)); 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)),
Bitmap bmp = new(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
Graphics gr = Graphics.FromImage(bmp); break;
_drawningStormtrooper.DrawTransport(gr); default:
pictureBoxStormtrooper.Image = bmp;
}
private void Draw()
{
if (_drawningStormtrooper == null)
{
return; return;
}
Bitmap bmp = new(pictureBoxStormtrooper.Width,
pictureBoxStormtrooper.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningStormtrooper.DrawTransport(gr);
pictureBoxStormtrooper.Image = bmp;
} }
private void ButtonMove_Click(object sender, EventArgs e) _drawningAircraft.SetPictureSize(pictureBoxStormtrooper.Width,
pictureBoxStormtrooper.Height);
_drawningAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
// Обработка нажатия кнопки Создать штурмовик
private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningStormtrooper));
// Обработка нажатия кнопки Создать самолет
private void buttonCreateAircraft_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAircraft));
// Прорисовка
private void Draw()
{
if (_drawningAircraft == null)
{ {
if (_drawningStormtrooper == null) return;
{ }
return; Bitmap bmp = new(pictureBoxStormtrooper.Width,
} pictureBoxStormtrooper.Height);
Graphics gr = Graphics.FromImage(bmp);
string name = ((Button)sender)?.Name ?? string.Empty; _drawningAircraft.DrawTransport(gr);
bool result = false; pictureBoxStormtrooper.Image = bmp;
switch (name) }
{ // Перемещение объекта по форме
case "buttonUp": private void ButtonMove_Click(object sender, EventArgs e)
result = _drawningStormtrooper.MoveTransport(DirectionType.Up); {
break; if (_drawningAircraft == null)
case "buttonDown": {
result = _drawningStormtrooper.MoveTransport(DirectionType.Down); return;
break;
case "buttonLeft":
result = _drawningStormtrooper.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningStormtrooper.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
} }
private void pictureBoxStormtrooper_Click(object sender, EventArgs e) string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{ {
case "buttonUp":
result = _drawningAircraft.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningAircraft.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningAircraft.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningAircraft.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
} }
} }
private void pictureBoxStormtrooper_Click(object sender, EventArgs e)
{
}
} }