Сделал первую часть второй лабораторной.

This commit is contained in:
Egor_Shtyrkin 2024-04-14 19:11:40 +04:00
parent 5b52aa4e07
commit f465b4ada8
8 changed files with 428 additions and 312 deletions

View File

@ -1,252 +0,0 @@
namespace ProjectAircraftCarrier;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningAircraftCarrier
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAircraftCarrier? EntityAircraftCarrier { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки авианосца
/// </summary>
private int? _startPosX;
/// <summary>
/// Верхняя координата отрисовки авианосца
/// </summary>
private int? _startPosY;
/// <summary>
/// Ширина прорисовки авианосца
/// </summary>
private readonly int _drawningAircraftCarrierWidth = 145;
/// <summary>
/// Высота прорисовки авианосца
/// </summary>
private readonly int _drawningAircraftCarrierHeight = 55;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="aircraftDeck">Признак наличия взлётно-посадочной полосы</param>
/// <param name="controlRoom">Признак наличия рубки управления</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom)
{
EntityAircraftCarrier = new EntityAircraftCarrier();
EntityAircraftCarrier.Init(speed, weight, bodyColor, additionalColor, aircraftDeck, controlRoom);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (width < _drawningAircraftCarrierWidth || height < _drawningAircraftCarrierHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawningAircraftCarrierWidth > _pictureWidth)
{
_startPosX = -_drawningAircraftCarrierWidth + _pictureWidth;
}
else if (_startPosX < 0)
{
_startPosX = 0;
}
if (_startPosY + _drawningAircraftCarrierHeight > _pictureHeight)
{
_startPosY = -_drawningAircraftCarrierHeight + _pictureHeight;
}
else if (_startPosY < 0)
{
_startPosY = 0;
}
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <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 + _drawningAircraftCarrierWidth > _pictureWidth)
{
x = 0;
}
if(y < 0 || y + _drawningAircraftCarrierHeight > _pictureHeight)
{
y = 0;
}
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Измнение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if(EntityAircraftCarrier == null || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityAircraftCarrier.Step > 0)
{
_startPosX -= (int)EntityAircraftCarrier.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityAircraftCarrier.Step > 0)
{
_startPosY -= (int)EntityAircraftCarrier.Step;
}
return true;
//вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningAircraftCarrierWidth + EntityAircraftCarrier.Step < _pictureWidth)
{
_startPosX += (int)EntityAircraftCarrier.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningAircraftCarrierHeight + EntityAircraftCarrier.Step < _pictureHeight)
{
_startPosY += (int)EntityAircraftCarrier.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityAircraftCarrier == null || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityAircraftCarrier.AdditionalColor);
//границы авианосца
Point[] FramePoints = {
new Point(_startPosX.Value + 5, _startPosY.Value),
new Point(_startPosX.Value + 105, _startPosY.Value),
new Point(_startPosX.Value + 145, _startPosY.Value + 25),
new Point(_startPosX.Value + 105, _startPosY.Value + 50),
new Point(_startPosX.Value + 5, _startPosY.Value + 50),
};
g.DrawPolygon(pen, FramePoints);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 5, 5, 18);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 27, 5, 18);
//корпус авианосца
Brush br = new SolidBrush(EntityAircraftCarrier.BodyColor);
g.FillPolygon(br, FramePoints);
//отсеки авианосца
Brush brGray = new SolidBrush(Color.Gray);
g.FillRectangle(brGray, _startPosX.Value + 80, _startPosY.Value + 10, 15, 30);
g.FillRectangle(brGray, _startPosX.Value + 60, _startPosY.Value + 20, 20, 10);
g.FillEllipse(brGray, _startPosX.Value + 100, _startPosY.Value + 14, 20, 20);
//выделяем рамкой отсеки авианосца
g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 10, 15, 30);
g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value + 20, 20, 10);
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 14, 20, 20);
//двигатели авианосца
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX.Value, _startPosY.Value + 5, 5, 18);
g.FillRectangle(brBlack, _startPosX.Value, _startPosY.Value + 27, 5, 18);
//рубка управления
if (EntityAircraftCarrier.ControlRoom)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 42, 15, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 42, 15, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 46, 15, 7);
g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 46, 5, 7);
g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 46, 5, 7);
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 42, 15, 15);
g.DrawRectangle(pen, _startPosX.Value + 65, _startPosY.Value + 42, 15, 15);
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 46, 15, 7);
g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 46, 5, 7);
g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 46, 5, 7);
g.DrawRectangle(pen, _startPosX.Value + 39, _startPosY.Value + 46, 7, 7);
g.DrawRectangle(pen, _startPosX.Value + 69, _startPosY.Value + 46, 7, 7);
}
//палуба для взлёта самолётов
if (EntityAircraftCarrier.AircraftDeck)
{
Point[] DeckPoint =
{
new Point(_startPosX.Value + 5, _startPosY.Value + 25),
new Point(_startPosX.Value + 5, _startPosY.Value + 45),
new Point(_startPosX.Value + 50, _startPosY.Value + 20),
new Point(_startPosX.Value + 50, _startPosY.Value),
};
g.FillPolygon(additionalBrush, DeckPoint);
g.DrawPolygon(pen, DeckPoint);
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 35, _startPosX.Value + 50, _startPosY.Value + 10);
}
}
}

View File

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

View File

@ -0,0 +1,74 @@
using ProjectAircraftCarrier.Entities;
using System.Drawing;
namespace ProjectAircraftCarrier.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningAircraftCarrier : DrawningWarship
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="aircraftDeck">Признак наличия взлётно-посадочной полосы</param>
/// <param name="controlRoom">Признак наличия рубки управления</param>
public DrawningAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom) : base(145, 55)
{
EntityWarship = new EntityAircraftCarrier(speed, weight, bodyColor, additionalColor, aircraftDeck, controlRoom);
}
public override void DrawTransport(Graphics g)
{
if (EntityWarship == null || EntityWarship is not EntityAircraftCarrier warship || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(warship.AdditionalColor);
_startPosX += 5;
_startPosY += 10;
base.DrawTransport(g);
_startPosX -= 5;
_startPosX -= 10;
//рубка управления
if (warship.ControlRoom)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 42, 15, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 42, 15, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 46, 15, 7);
g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 46, 5, 7);
g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 46, 5, 7);
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 42, 15, 15);
g.DrawRectangle(pen, _startPosX.Value + 65, _startPosY.Value + 42, 15, 15);
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 46, 15, 7);
g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 46, 5, 7);
g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 46, 5, 7);
g.DrawRectangle(pen, _startPosX.Value + 39, _startPosY.Value + 46, 7, 7);
g.DrawRectangle(pen, _startPosX.Value + 69, _startPosY.Value + 46, 7, 7);
}
//палуба для взлёта самолётов
if (warship.AircraftDeck)
{
Point[] DeckPoint =
{
new Point(_startPosX.Value + 5, _startPosY.Value + 25),
new Point(_startPosX.Value + 5, _startPosY.Value + 45),
new Point(_startPosX.Value + 50, _startPosY.Value + 20),
new Point(_startPosX.Value + 50, _startPosY.Value),
};
g.FillPolygon(additionalBrush, DeckPoint);
g.DrawPolygon(pen, DeckPoint);
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 35, _startPosX.Value + 50, _startPosY.Value + 10);
}
}
}

View File

@ -0,0 +1,236 @@
using ProjectAircraftCarrier.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAircraftCarrier.Drawnings;
public class DrawningWarship
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityWarship? EntityWarship { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки военного корабля
/// </summary>
protected int? _startPosX;
/// <summary>
/// Верхняя координата отрисовки военного корабля
/// </summary>
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки военного корабля
/// </summary>
private readonly int _drawningWarshipWidth = 145;
/// <summary>
/// Высота прорисовки военного корабля
/// </summary>
private readonly int _drawningWarshipHeight = 50;
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningWarship()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningWarship(int speed, double weight, Color bodyColor) : this()
{
EntityWarship = new EntityWarship(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningWarshipWidth">Ширина прорисовки военного корабля</param>
/// <param name="drawningWarshipHeight">Высота прорисовки военного корабля</param>
protected DrawningWarship(int drawningWarshipWidth, int drawningWarshipHeight) : this()
{
_drawningWarshipWidth = drawningWarshipWidth;
_drawningWarshipHeight = drawningWarshipHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (width < _drawningWarshipWidth || height < _drawningWarshipHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawningWarshipWidth > _pictureWidth)
{
_startPosX = -_drawningWarshipWidth + _pictureWidth;
}
else if (_startPosX < 0)
{
_startPosX = 0;
}
if (_startPosY + _drawningWarshipHeight > _pictureHeight)
{
_startPosY = -_drawningWarshipHeight + _pictureHeight;
}
else if (_startPosY < 0)
{
_startPosY = 0;
}
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <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 + _drawningWarshipWidth > _pictureWidth)
{
x = 0;
}
if (y < 0 || y + _drawningWarshipHeight > _pictureHeight)
{
y = 0;
}
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Измнение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityWarship == null || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityWarship.Step > 0)
{
_startPosX -= (int)EntityWarship.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityWarship.Step > 0)
{
_startPosY -= (int)EntityWarship.Step;
}
return true;
//вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningWarshipWidth + EntityWarship.Step < _pictureWidth)
{
_startPosX += (int)EntityWarship.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningWarshipHeight + EntityWarship.Step < _pictureHeight)
{
_startPosY += (int)EntityWarship.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityWarship == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
//границы военного корабля
Point[] FramePoints = {
new Point(_startPosX.Value + 5, _startPosY.Value),
new Point(_startPosX.Value + 105, _startPosY.Value),
new Point(_startPosX.Value + 145, _startPosY.Value + 25),
new Point(_startPosX.Value + 105, _startPosY.Value + 50),
new Point(_startPosX.Value + 5, _startPosY.Value + 50),
};
g.DrawPolygon(pen, FramePoints);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 5, 5, 18);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 27, 5, 18);
//корпус военного корабля
Brush br = new SolidBrush(EntityWarship.BodyColor);
g.FillPolygon(br, FramePoints);
//отсеки военного корабля
Brush brGray = new SolidBrush(Color.Gray);
g.FillRectangle(brGray, _startPosX.Value + 80, _startPosY.Value + 10, 15, 30);
g.FillRectangle(brGray, _startPosX.Value + 60, _startPosY.Value + 20, 20, 10);
g.FillEllipse(brGray, _startPosX.Value + 100, _startPosY.Value + 14, 20, 20);
//выделяем рамкой отсеки военного корабля
g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 10, 15, 30);
g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value + 20, 20, 10);
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 14, 20, 20);
//двигатели военного корабля
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX.Value, _startPosY.Value + 5, 5, 18);
g.FillRectangle(brBlack, _startPosX.Value, _startPosY.Value + 27, 5, 18);
}
}

View File

@ -1,25 +1,10 @@
namespace ProjectAircraftCarrier;
namespace ProjectAircraftCarrier.Entities;
/// <summary>
/// Класс-сущность "Авианосец"
/// </summary>
public class EntityAircraftCarrier
public class EntityAircraftCarrier : EntityWarship
{
/// <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>
@ -51,9 +36,6 @@ public class EntityAircraftCarrier
/// <param name="controlRoom">Признак наличия рубки управления</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
AircraftDeck = aircraftDeck;
ControlRoom = controlRoom;

View File

@ -0,0 +1,40 @@
namespace ProjectAircraftCarrier.Entities;
/// <summary>
/// Класс-сущность "Военный корабль"
/// </summary>
public class EntityWarship
{
/// <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 EntityWarship(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -29,11 +29,12 @@
private void InitializeComponent()
{
pictureBoxAircraftCarrier = new PictureBox();
buttonCreate = new Button();
buttonCreateAircraftCarrier = new Button();
buttonRight = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonLeft = new Button();
buttonCreateWarship = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit();
SuspendLayout();
//
@ -48,17 +49,17 @@
pictureBoxAircraftCarrier.TabIndex = 0;
pictureBoxAircraftCarrier.TabStop = false;
//
// buttonCreate
// buttonCreateAircraftCarrier
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(14, 581);
buttonCreate.Margin = new Padding(3, 4, 3, 4);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(86, 31);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
buttonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateAircraftCarrier.Location = new Point(14, 581);
buttonCreateAircraftCarrier.Margin = new Padding(3, 4, 3, 4);
buttonCreateAircraftCarrier.Name = "buttonCreateAircraftCarrier";
buttonCreateAircraftCarrier.Size = new Size(188, 31);
buttonCreateAircraftCarrier.TabIndex = 1;
buttonCreateAircraftCarrier.Text = "Создать авианосец";
buttonCreateAircraftCarrier.UseVisualStyleBackColor = true;
buttonCreateAircraftCarrier.Click += buttonCreate_Click;
//
// buttonRight
//
@ -71,7 +72,7 @@
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 2;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click;
buttonRight.Click += ButtonMove_Click;
//
// buttonUp
//
@ -84,7 +85,7 @@
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 3;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click;
buttonUp.Click += ButtonMove_Click;
//
// buttonDown
//
@ -97,7 +98,7 @@
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 4;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click;
buttonDown.Click += ButtonMove_Click;
//
// buttonLeft
//
@ -110,18 +111,31 @@
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 5;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click;
buttonLeft.Click += ButtonMove_Click;
//
// buttonCreateWarship
//
buttonCreateWarship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateWarship.Location = new Point(208, 581);
buttonCreateWarship.Margin = new Padding(3, 4, 3, 4);
buttonCreateWarship.Name = "buttonCreateWarship";
buttonCreateWarship.Size = new Size(208, 31);
buttonCreateWarship.TabIndex = 6;
buttonCreateWarship.Text = "Создать военный корабль";
buttonCreateWarship.UseVisualStyleBackColor = true;
buttonCreateWarship.Click += ButtonCreateWarship_Click;
//
// FormAircraftCarrier
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(918, 628);
Controls.Add(buttonCreateWarship);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateAircraftCarrier);
Controls.Add(pictureBoxAircraftCarrier);
Margin = new Padding(3, 4, 3, 4);
Name = "FormAircraftCarrier";
@ -135,10 +149,11 @@
#endregion
private PictureBox pictureBoxAircraftCarrier;
private Button buttonCreate;
private Button buttonCreateAircraftCarrier;
private Button buttonRight;
private Button buttonUp;
private Button buttonDown;
private Button buttonLeft;
private Button buttonCreateWarship;
}
}

View File

@ -7,6 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectAircraftCarrier.Drawnings;
namespace ProjectAircraftCarrier;
@ -18,7 +19,7 @@ public partial class FormAircraftCarrier : Form
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningAircraftCarrier? _drawningAircraftCarrier;
private DrawningWarship? _drawningWarship;
/// <summary>
/// Конструктор формы
@ -33,43 +34,63 @@ public partial class FormAircraftCarrier : Form
/// </summary>
private void Draw()
{
if (_drawningAircraftCarrier == null)
if (_drawningWarship == null)
{
return;
}
Bitmap bmp = new(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAircraftCarrier.DrawTransport(gr);
_drawningWarship.DrawTransport(gr);
pictureBoxAircraftCarrier.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e)
private void CreateObject(string type)
{
Random random = new();
_drawningAircraftCarrier = new DrawningAircraftCarrier();
_drawningAircraftCarrier.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)));
_drawningAircraftCarrier.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
_drawningAircraftCarrier.SetPosition(random.Next(10, 100), random.Next(10, 100));
switch (type)
{
case nameof(DrawningWarship):
_drawningWarship = new DrawningWarship(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(DrawningAircraftCarrier):
_drawningWarship = new DrawningAircraftCarrier(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;
}
_drawningWarship.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
_drawningWarship.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать авианосец"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAircraftCarrier));
/// <summary>
/// Обработка нажатия кнопки "Создать военный корабль"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateWarship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarship));
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonMove_Click(object sender, EventArgs e)
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningAircraftCarrier == null)
if (_drawningWarship == null)
{
return;
}
@ -79,16 +100,16 @@ public partial class FormAircraftCarrier : Form
switch (name)
{
case "buttonUp":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Up);
result = _drawningWarship.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Down);
result = _drawningWarship.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Left);
result = _drawningWarship.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningAircraftCarrier.MoveTransport(DirectionType.Right);
result = _drawningWarship.MoveTransport(DirectionType.Right);
break;
}