Добавление родителя и ввод конструктора
This commit is contained in:
parent
e89bd91679
commit
392c1f4428
@ -1,4 +1,4 @@
|
|||||||
namespace Battleship;
|
namespace Battleship.Drawings;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// класс, отвечающий за лево/право/верх/низ
|
/// класс, отвечающий за лево/право/верх/низ
|
||||||
/// </summary>
|
/// </summary>
|
108
Battleship/Battleship/Drawings/DrawingBattleship.cs
Normal file
108
Battleship/Battleship/Drawings/DrawingBattleship.cs
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
using Battleship.Entities;
|
||||||
|
//using System.Drawings;
|
||||||
|
|
||||||
|
namespace Battleship.Drawings;
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, отвечающий за отрисовку и перемещение объекта-сущности
|
||||||
|
/// </summary>
|
||||||
|
public class DrawingBattleship : DrawingWarship
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||||
|
/// <param name="bodyDeck">Признак наличия палубы</param>
|
||||||
|
/// <param name="compartment">Признак наличия отсека для ракет</param>
|
||||||
|
/// <param name="tower">Признак наличия башни</param>
|
||||||
|
///
|
||||||
|
|
||||||
|
public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyDeck, bool compartment, bool tower) : base (129, 60)
|
||||||
|
{
|
||||||
|
//Warship = new EntityBattleship(speed, weight, bodyColor, compartment, tower, bodyDeck, additionalColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (EntityWarship == null || EntityWarship is not EntityBattleship battleship || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pen pen = new(Color.Black);
|
||||||
|
Brush additionalBrush = new SolidBrush(battleship.AdditionalColor);
|
||||||
|
|
||||||
|
//отсек для ракет
|
||||||
|
if (battleship.BodyDeck)
|
||||||
|
{
|
||||||
|
//заливка линкора
|
||||||
|
Brush br = new SolidBrush(EntityWarship.BodyColor);
|
||||||
|
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value, 80, 40);
|
||||||
|
//границы линкора
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value, 80, 40); //ширина высота (сама палуба)
|
||||||
|
|
||||||
|
//заливка двигателей (2 фигни сзади)
|
||||||
|
Brush brBlack = new SolidBrush(Color.Black);
|
||||||
|
g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value + 7, 5, 10); //верхний
|
||||||
|
g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value + 22, 5, 10); //нижний
|
||||||
|
//границы двигателей
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 7, 5, 10); //двигатель верхний
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 22, 5, 10); //двигатель нижний
|
||||||
|
|
||||||
|
//прямоугольник левый на палубе (горизонтальный)
|
||||||
|
Brush brLightSteelBlue = new SolidBrush(Color.LightSteelBlue);
|
||||||
|
g.FillRectangle(brLightSteelBlue, _startPosX.Value + 32, _startPosY.Value + 13, 21, 12);
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 13, 21, 12);
|
||||||
|
|
||||||
|
//прямоугольник правый на палубе (вертикальный)
|
||||||
|
Brush brGray = new SolidBrush(Color.Gray);
|
||||||
|
g.FillRectangle(brGray, _startPosX.Value + 53, _startPosY.Value + 7, 16, 24);
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 53, _startPosY.Value + 7, 16, 24);
|
||||||
|
|
||||||
|
//линии для треугольника
|
||||||
|
Point[] pointsNOS =
|
||||||
|
{
|
||||||
|
new Point(_startPosX.Value + 90, _startPosY.Value),
|
||||||
|
new Point(_startPosX.Value + 120, _startPosY.Value + 20),
|
||||||
|
new Point(_startPosX.Value + 90, _startPosY.Value + 40),
|
||||||
|
|
||||||
|
};
|
||||||
|
g.DrawPolygon(pen, pointsNOS);
|
||||||
|
g.FillPolygon(br, pointsNOS);
|
||||||
|
|
||||||
|
//заливка круга
|
||||||
|
Brush brLightSlateGray = new SolidBrush(Color.LightSlateGray);
|
||||||
|
g.FillEllipse(brLightSlateGray, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
||||||
|
//круг на палубе
|
||||||
|
g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
base.DrawTransport(g);
|
||||||
|
|
||||||
|
//башня
|
||||||
|
if (battleship.Tower)
|
||||||
|
{
|
||||||
|
//заливка башни
|
||||||
|
g.FillEllipse(additionalBrush, _startPosX.Value + 98, _startPosY.Value + 15, 10, 10);
|
||||||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 107, _startPosY.Value + 17, 30, 5);
|
||||||
|
|
||||||
|
//границы башни
|
||||||
|
g.DrawEllipse(pen, _startPosX.Value + 98, _startPosY.Value + 15, 10, 10);
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 107, _startPosY.Value + 17, 30, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
//отсек
|
||||||
|
if (battleship.Compartment)
|
||||||
|
{
|
||||||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 5, 27, 5); //верхний отсек для ракет
|
||||||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 28, 27, 5); //нижний отсек для ракет
|
||||||
|
|
||||||
|
//границы отсеков
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 15, _startPosY.Value + 5, 27, 5); //верхний отсек
|
||||||
|
g.DrawRectangle(pen, _startPosX.Value + 15, _startPosY.Value + 28, 27, 5); //нижний отсек
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,13 +1,13 @@
|
|||||||
namespace Battleship;
|
using Battleship.Entities;
|
||||||
/// <summary>
|
|
||||||
/// Класс, отвечающий за отрисовку и перемещение объекта-сущности
|
namespace Battleship.Drawings;
|
||||||
/// </summary>
|
|
||||||
public class DrawingBattleship
|
public class DrawingWarship
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность
|
/// Класс-сущность
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public EntityBattleship? EntityBattleship { get; private set; }
|
public EntityWarship? EntityWarship { get; protected set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина
|
/// Ширина
|
||||||
@ -20,45 +20,57 @@ public class DrawingBattleship
|
|||||||
private int? _pictureHeight;
|
private int? _pictureHeight;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Левая координата прорисовки линкора
|
/// Левая координата прорисовки военного корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int? _startPosX;
|
protected int? _startPosX;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Верхняя координата прорисовки линкора
|
/// Верхняя координата прорисовки военного корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int? _startPosY;
|
protected int? _startPosY;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина прорисовки линкора
|
/// Ширина прорисовки военного корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _drawingBattleshipWidth = 129;
|
private readonly int _drawingWarshipWidth = 129;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Высота прорисовки линкора
|
/// Высота прорисовки военного корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _drawingBattleshipHeight = 40;
|
private readonly int _drawingWarshipHeight = 40;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
/// Пустой конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="speed">Скорость</param>
|
private DrawingWarship()
|
||||||
/// <param name="weight">Вес</param>
|
|
||||||
/// <param name="bodyColor">Основной цвет</param>
|
|
||||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
|
||||||
/// <param name="bodyDeck">Признак наличия палубы</param>
|
|
||||||
/// <param name="compartment">Признак наличия отсека для ракет</param>
|
|
||||||
/// <param name="tower">Признак наличия башни</param>
|
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyDeck, bool compartment, bool tower)
|
|
||||||
{
|
{
|
||||||
EntityBattleship = new EntityBattleship();
|
|
||||||
EntityBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower);
|
|
||||||
_pictureWidth = null;
|
_pictureWidth = null;
|
||||||
_pictureHeight = null;
|
_pictureHeight = null;
|
||||||
_startPosX = null;
|
_startPosX = null;
|
||||||
_startPosY = null;
|
_startPosY = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="speed">Скорость</param>
|
||||||
|
/// <param name="weight">Вес</param>
|
||||||
|
/// <param name="bodyColor">Основной цвет</param>
|
||||||
|
public DrawingWarship(int speed, double weight, Color bodyColor) : this()
|
||||||
|
{
|
||||||
|
EntityWarship = new EntityWarship(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор для наследников
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="drawingWarshipWidth">Ширина прорисовки военного корабля</param>
|
||||||
|
/// <param name="drawingWarshipHeight">Высота прорисовки военного корабля</param>
|
||||||
|
protected DrawingWarship(int drawingWarshipWidth, int drawingWarshipHeight) : this()
|
||||||
|
{
|
||||||
|
_drawingWarshipWidth = drawingWarshipWidth;
|
||||||
|
_drawingWarshipHeight = drawingWarshipHeight;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
@ -71,25 +83,25 @@ public class DrawingBattleship
|
|||||||
{
|
{
|
||||||
//TODO проверка, что объект "влезает" в размеры поля
|
//TODO проверка, что объект "влезает" в размеры поля
|
||||||
//если влезает, сохраняем границы и корректируем позицию объекта, если она уже была установлена
|
//если влезает, сохраняем границы и корректируем позицию объекта, если она уже была установлена
|
||||||
|
|
||||||
if (_drawingBattleshipWidth > width || _drawingBattleshipHeight > height)
|
if (_drawingWarshipWidth > width || _drawingWarshipHeight > height)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
_pictureWidth = width;
|
_pictureWidth = width;
|
||||||
_pictureHeight = height;
|
_pictureHeight = height;
|
||||||
|
|
||||||
if (_startPosX.HasValue || _startPosY.HasValue)
|
if (_startPosX.HasValue || _startPosY.HasValue)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (_startPosX + _drawingBattleshipWidth > _pictureWidth)
|
if (_startPosX + _drawingWarshipWidth > _pictureWidth)
|
||||||
{
|
{
|
||||||
_startPosX = _pictureWidth - _drawingBattleshipWidth;
|
_startPosX = _pictureWidth - _drawingWarshipWidth;
|
||||||
}
|
}
|
||||||
else if (_startPosX < 0) _startPosX = 0;
|
else if (_startPosX < 0) _startPosX = 0;
|
||||||
if (_startPosY + _drawingBattleshipHeight > _pictureHeight)
|
if (_startPosY + _drawingWarshipHeight > _pictureHeight)
|
||||||
{
|
{
|
||||||
_startPosY = _pictureHeight - _drawingBattleshipHeight;
|
_startPosY = _pictureHeight - _drawingWarshipHeight;
|
||||||
}
|
}
|
||||||
else if (_startPosY < 0) _startPosY = 0;
|
else if (_startPosY < 0) _startPosY = 0;
|
||||||
}
|
}
|
||||||
@ -102,7 +114,7 @@ public class DrawingBattleship
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">Координата X</param>
|
/// <param name="x">Координата X</param>
|
||||||
/// <param name="y">Координата Y</param>
|
/// <param name="y">Координата Y</param>
|
||||||
|
|
||||||
public void SetPosition(int x, int y, int width, int height)
|
public void SetPosition(int x, int y, int width, int height)
|
||||||
{
|
{
|
||||||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||||||
@ -112,17 +124,17 @@ public class DrawingBattleship
|
|||||||
|
|
||||||
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
||||||
// то надо изменить координаты, чтобы он оставался в этих границах
|
// то надо изменить координаты, чтобы он оставался в этих границах
|
||||||
|
|
||||||
if (x + _drawingBattleshipWidth > _pictureWidth)
|
if (x + _drawingWarshipWidth > _pictureWidth)
|
||||||
{
|
{
|
||||||
_startPosX = _pictureWidth - _drawingBattleshipWidth;
|
_startPosX = _pictureWidth - _drawingWarshipWidth;
|
||||||
}
|
}
|
||||||
else if (x < 0) _startPosX = 0;
|
else if (x < 0) _startPosX = 0;
|
||||||
else _startPosX = x;
|
else _startPosX = x;
|
||||||
|
|
||||||
if (y + _drawingBattleshipHeight > _pictureHeight)
|
if (y + _drawingWarshipHeight > _pictureHeight)
|
||||||
{
|
{
|
||||||
_startPosY = _pictureHeight - _drawingBattleshipHeight;
|
_startPosY = _pictureHeight - _drawingWarshipHeight;
|
||||||
}
|
}
|
||||||
else if (y < 0) _startPosY = 0;
|
else if (y < 0) _startPosY = 0;
|
||||||
else _startPosY = y;
|
else _startPosY = y;
|
||||||
@ -137,49 +149,49 @@ public class DrawingBattleship
|
|||||||
|
|
||||||
public bool MoveTransport(DirectionType direction)
|
public bool MoveTransport(DirectionType direction)
|
||||||
{
|
{
|
||||||
if (EntityBattleship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
if (EntityWarship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (direction)
|
switch (direction)
|
||||||
{
|
{
|
||||||
//влево
|
//влево
|
||||||
case DirectionType.Left:
|
case DirectionType.Left:
|
||||||
if (_startPosX.Value - EntityBattleship.Step > 0)
|
if (_startPosX.Value - EntityWarship.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosX -= (int)EntityBattleship.Step;
|
_startPosX -= (int)EntityWarship.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
//вправо
|
//вправо
|
||||||
case DirectionType.Right:
|
case DirectionType.Right:
|
||||||
if (_startPosX.Value + EntityBattleship.Step + _drawingBattleshipWidth < _pictureWidth)
|
if (_startPosX.Value + EntityWarship.Step + _drawingWarshipWidth < _pictureWidth)
|
||||||
{
|
{
|
||||||
_startPosX += (int)EntityBattleship.Step;
|
_startPosX += (int)EntityWarship.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
//вверх
|
//вверх
|
||||||
case DirectionType.Up:
|
case DirectionType.Up:
|
||||||
if (_startPosY.Value - EntityBattleship.Step > 0)
|
if (_startPosY.Value - EntityWarship.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosY -= (int)EntityBattleship.Step;
|
_startPosY -= (int)EntityWarship.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
//вниз
|
//вниз
|
||||||
case DirectionType.Down:
|
case DirectionType.Down:
|
||||||
if (_startPosY.Value + EntityBattleship.Step + _drawingBattleshipHeight < _pictureHeight) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
if (_startPosY.Value + EntityWarship.Step + _drawingWarshipHeight < _pictureHeight) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
{
|
{
|
||||||
_startPosY += (int)EntityBattleship.Step;
|
_startPosY += (int)EntityWarship.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -187,18 +199,17 @@ public class DrawingBattleship
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="g"></param>
|
/// <param name="g"></param>
|
||||||
|
|
||||||
public void DrawTransport(Graphics g)
|
public virtual void DrawTransport(Graphics g)
|
||||||
{
|
{
|
||||||
if (EntityBattleship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
if (EntityWarship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Pen pen = new(Color.Black);
|
Pen pen = new(Color.Black);
|
||||||
Brush additionalBrush = new SolidBrush(EntityBattleship.AdditionalColor);
|
|
||||||
|
|
||||||
//заливка линкора
|
//заливка линкора
|
||||||
Brush br = new SolidBrush(EntityBattleship.BodyColor);
|
Brush br = new SolidBrush(EntityWarship.BodyColor);
|
||||||
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value, 80, 40);
|
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value, 80, 40);
|
||||||
//границы линкора
|
//границы линкора
|
||||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value, 80, 40); //ширина высота (сама палуба)
|
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value, 80, 40); //ширина высота (сама палуба)
|
||||||
@ -227,7 +238,7 @@ public class DrawingBattleship
|
|||||||
new Point(_startPosX.Value + 90, _startPosY.Value),
|
new Point(_startPosX.Value + 90, _startPosY.Value),
|
||||||
new Point(_startPosX.Value + 120, _startPosY.Value + 20),
|
new Point(_startPosX.Value + 120, _startPosY.Value + 20),
|
||||||
new Point(_startPosX.Value + 90, _startPosY.Value + 40),
|
new Point(_startPosX.Value + 90, _startPosY.Value + 40),
|
||||||
|
|
||||||
};
|
};
|
||||||
g.DrawPolygon(pen, pointsNOS);
|
g.DrawPolygon(pen, pointsNOS);
|
||||||
g.FillPolygon(br, pointsNOS);
|
g.FillPolygon(br, pointsNOS);
|
||||||
@ -237,32 +248,5 @@ public class DrawingBattleship
|
|||||||
g.FillEllipse(brLightSlateGray, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
g.FillEllipse(brLightSlateGray, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
||||||
//круг на палубе
|
//круг на палубе
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
||||||
|
|
||||||
//ПРОВЕРКА
|
|
||||||
|
|
||||||
if (EntityBattleship.Compartment)
|
|
||||||
{
|
|
||||||
//отсеки для ракет
|
|
||||||
//заливка отсеков
|
|
||||||
|
|
||||||
g.FillRectangle(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 5, 27, 5); //верхний отсек для ракет
|
|
||||||
g.FillRectangle(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 28, 27, 5); //нижний отсек для ракет
|
|
||||||
|
|
||||||
//границы отсеков
|
|
||||||
g.DrawRectangle(pen, _startPosX.Value + 15, _startPosY.Value + 5, 27, 5); //верхний отсек
|
|
||||||
g.DrawRectangle(pen, _startPosX.Value + 15, _startPosY.Value + 28, 27, 5); //нижний отсек
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (EntityBattleship.Tower)
|
|
||||||
{
|
|
||||||
//заливка башни
|
|
||||||
g.FillEllipse(additionalBrush, _startPosX.Value + 98, _startPosY.Value + 15, 10, 10);
|
|
||||||
g.FillRectangle(additionalBrush, _startPosX.Value + 107, _startPosY.Value + 17, 30, 5);
|
|
||||||
|
|
||||||
//границы башни
|
|
||||||
g.DrawEllipse(pen, _startPosX.Value + 98, _startPosY.Value + 15, 10, 10);
|
|
||||||
g.DrawRectangle(pen, _startPosX.Value + 107, _startPosY.Value + 17, 30, 5);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,14 +1,18 @@
|
|||||||
namespace Battleship;
|
namespace Battleship.Entities;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность Линкор
|
/// Класс-сущность Линкор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class EntityBattleship
|
internal class EntityBattleship : EntityWarship
|
||||||
{
|
{
|
||||||
|
public EntityBattleship(int speed, double weight, Color bodyColor) : base(speed, weight, bodyColor)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Скорость
|
/// Скорость
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Speed { get; private set; }
|
public int Speed { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вес
|
/// Вес
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -18,41 +22,44 @@ public class EntityBattleship
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Color BodyColor { get; private set; }
|
public Color BodyColor { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Дополнительный цвет (для опциональных элементов)
|
/// Перемещение военного корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Color AdditionalColor { get; private set; }
|
/// /// <summary>
|
||||||
/// <summary>
|
|
||||||
/// Признак (опция) наличия палубы
|
|
||||||
/// </summary>
|
|
||||||
public bool BodyDeck { get; private set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Признак (опция) отсека для ракет
|
/// Признак (опция) отсека для ракет
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Compartment { get; private set; }
|
public bool Compartment { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Признак (опция) наличия башни
|
/// Признак (опция) наличия башни
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Tower { get; private set; }
|
public bool Tower { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Признак (опция) наличия палубы
|
||||||
|
/// </summary>
|
||||||
|
public bool BodyDeck { get; private set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Дополнительный цвет (для опциональных элементов)
|
||||||
|
/// </summary>
|
||||||
|
public Color AdditionalColor { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Перемещение линкора
|
/// Перемещение линкора
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double Step => Speed * 100 / Weight;
|
public double Step => Speed * 100 / Weight;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Инициализация полей объекта-класса линкора
|
||||||
|
/// </summary>
|
||||||
/// <param name="speed"></param>
|
/// <param name="speed"></param>
|
||||||
/// <param name="weight"></param>
|
/// <param name="weight"></param>
|
||||||
/// <param name="bodyColor"></param>
|
/// <param name="bodyColor"></param>
|
||||||
/// <param name="additionalColor"></param>
|
public void EntityWarship(int speed, double weight, Color bodyColor, bool compartment, bool tower, bool bodyDeck, Color additionalColor)
|
||||||
/// <param name="bodyDeck"></param>
|
|
||||||
/// <param name="compartment"></param>
|
|
||||||
/// <param name="tower"></param>
|
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyDeck, bool compartment, bool tower)
|
|
||||||
{
|
{
|
||||||
Speed = speed;
|
Speed = speed;
|
||||||
Weight = weight;
|
Weight = weight;
|
||||||
BodyColor = bodyColor;
|
BodyColor = bodyColor;
|
||||||
AdditionalColor = additionalColor;
|
|
||||||
BodyDeck = bodyDeck;
|
|
||||||
Compartment = compartment;
|
Compartment = compartment;
|
||||||
Tower = tower;
|
Tower = tower;
|
||||||
|
BodyDeck = bodyDeck;
|
||||||
|
AdditionalColor = additionalColor;
|
||||||
}
|
}
|
||||||
}
|
}
|
42
Battleship/Battleship/Entities/EntityWarship.cs
Normal file
42
Battleship/Battleship/Entities/EntityWarship.cs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Battleship.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;
|
||||||
|
}
|
||||||
|
}
|
18
Battleship/Battleship/FormBattleship.Designer.cs
generated
18
Battleship/Battleship/FormBattleship.Designer.cs
generated
@ -34,6 +34,7 @@ partial class FormBattleship
|
|||||||
buttonDown = new Button();
|
buttonDown = new Button();
|
||||||
buttonRight = new Button();
|
buttonRight = new Button();
|
||||||
buttonUp = new Button();
|
buttonUp = new Button();
|
||||||
|
buttonCreateWarshit = new Button();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -50,9 +51,9 @@ partial class FormBattleship
|
|||||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
buttonCreate.Location = new Point(12, 518);
|
buttonCreate.Location = new Point(12, 518);
|
||||||
buttonCreate.Name = "buttonCreate";
|
buttonCreate.Name = "buttonCreate";
|
||||||
buttonCreate.Size = new Size(94, 29);
|
buttonCreate.Size = new Size(267, 29);
|
||||||
buttonCreate.TabIndex = 0;
|
buttonCreate.TabIndex = 0;
|
||||||
buttonCreate.Text = "Создать";
|
buttonCreate.Text = "Создать линкор";
|
||||||
buttonCreate.UseVisualStyleBackColor = true;
|
buttonCreate.UseVisualStyleBackColor = true;
|
||||||
buttonCreate.Click += buttonCreate_Click;
|
buttonCreate.Click += buttonCreate_Click;
|
||||||
//
|
//
|
||||||
@ -104,11 +105,23 @@ partial class FormBattleship
|
|||||||
buttonUp.UseVisualStyleBackColor = true;
|
buttonUp.UseVisualStyleBackColor = true;
|
||||||
buttonUp.Click += buttonMove_Click;
|
buttonUp.Click += buttonMove_Click;
|
||||||
//
|
//
|
||||||
|
// buttonCreateWarshit
|
||||||
|
//
|
||||||
|
buttonCreateWarshit.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
|
buttonCreateWarshit.Location = new Point(316, 518);
|
||||||
|
buttonCreateWarshit.Name = "buttonCreateWarshit";
|
||||||
|
buttonCreateWarshit.Size = new Size(267, 29);
|
||||||
|
buttonCreateWarshit.TabIndex = 5;
|
||||||
|
buttonCreateWarshit.Text = "Создать военный корабль";
|
||||||
|
buttonCreateWarshit.UseVisualStyleBackColor = true;
|
||||||
|
buttonCreateWarshit.Click += buttonCreateWarshit_Click;
|
||||||
|
//
|
||||||
// FormBattleship
|
// FormBattleship
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(957, 559);
|
ClientSize = new Size(957, 559);
|
||||||
|
Controls.Add(buttonCreateWarshit);
|
||||||
Controls.Add(buttonUp);
|
Controls.Add(buttonUp);
|
||||||
Controls.Add(buttonRight);
|
Controls.Add(buttonRight);
|
||||||
Controls.Add(buttonDown);
|
Controls.Add(buttonDown);
|
||||||
@ -129,4 +142,5 @@ partial class FormBattleship
|
|||||||
private Button buttonRight;
|
private Button buttonRight;
|
||||||
private Button buttonUp;
|
private Button buttonUp;
|
||||||
private Button buttonLeft;
|
private Button buttonLeft;
|
||||||
|
private Button buttonCreateWarshit;
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
namespace Battleship;
|
using Battleship.Drawings;
|
||||||
|
|
||||||
|
namespace Battleship;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Форма работы с объектом Линкор
|
/// Форма работы с объектом Линкор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -7,7 +9,7 @@ public partial class FormBattleship : Form
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Поле/объект для прорисовки объекта
|
/// Поле/объект для прорисовки объекта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private DrawingBattleship? _drawingBattleship; //поля с нижнего подчеркивания
|
private DrawingWarship? _drawingWarship; //поля с нижнего подчеркивания
|
||||||
|
|
||||||
public FormBattleship()
|
public FormBattleship()
|
||||||
{
|
{
|
||||||
@ -19,36 +21,67 @@ public partial class FormBattleship : Form
|
|||||||
//создаем отдельный метод
|
//создаем отдельный метод
|
||||||
private void Draw()
|
private void Draw()
|
||||||
{
|
{
|
||||||
if (_drawingBattleship == null)
|
if (_drawingWarship == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bmp = new(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
Bitmap bmp = new(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||||
Graphics gr = Graphics.FromImage(bmp);
|
Graphics gr = Graphics.FromImage(bmp);
|
||||||
_drawingBattleship.DrawTransport(gr);
|
_drawingWarship.DrawTransport(gr);
|
||||||
pictureBoxBattleship.Image = bmp;
|
pictureBoxBattleship.Image = bmp;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buttonCreate_Click(object sender, EventArgs e)
|
private void CreateObject(string type)
|
||||||
{
|
{
|
||||||
Random random = new();
|
Random random = new();
|
||||||
_drawingBattleship = new DrawingBattleship();
|
switch (type)
|
||||||
_drawingBattleship.Init(random.Next(100, 300), random.Next(1000, 3000),
|
{
|
||||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
case nameof(DrawingWarship):
|
||||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
_drawingWarship = new DrawingWarship(random.Next(100, 300), random.Next(1000, 3000),
|
||||||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
||||||
_drawingBattleship.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
break;
|
||||||
_drawingBattleship.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
case nameof(DrawingBattleship):
|
||||||
|
_drawingWarship = new DrawingBattleship(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)), Convert.ToBoolean(random.Next(0, 2)));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_drawingWarship.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||||
|
_drawingWarship.SetPosition(random.Next(10, 100), random.Next(10, 100), pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||||
Draw();
|
Draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// обработка нажатия кнопки "создать линкор"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void buttonCreate_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
CreateObject(nameof(DrawingBattleship));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// обработка нажатия кнопки "создать военный корабль"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
|
private void buttonCreateWarshit_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
CreateObject(nameof(DrawingWarship));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buttonMove_Click(object sender, EventArgs e)
|
private void buttonMove_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (_drawingBattleship == null)
|
if (_drawingWarship == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -58,16 +91,16 @@ public partial class FormBattleship : Form
|
|||||||
switch (name)
|
switch (name)
|
||||||
{
|
{
|
||||||
case "buttonUp":
|
case "buttonUp":
|
||||||
result = _drawingBattleship.MoveTransport(DirectionType.Up);
|
result = _drawingWarship.MoveTransport(DirectionType.Up);
|
||||||
break;
|
break;
|
||||||
case "buttonDown":
|
case "buttonDown":
|
||||||
result = _drawingBattleship.MoveTransport(DirectionType.Down);
|
result = _drawingWarship.MoveTransport(DirectionType.Down);
|
||||||
break;
|
break;
|
||||||
case "buttonLeft":
|
case "buttonLeft":
|
||||||
result = _drawingBattleship.MoveTransport(DirectionType.Left);
|
result = _drawingWarship.MoveTransport(DirectionType.Left);
|
||||||
break;
|
break;
|
||||||
case "buttonRight":
|
case "buttonRight":
|
||||||
result = _drawingBattleship.MoveTransport(DirectionType.Right);
|
result = _drawingWarship.MoveTransport(DirectionType.Right);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user