Добавление родительских классов и конструкторов

This commit is contained in:
grishazagidulin 2024-03-02 20:46:38 +04:00
parent a902a068b3
commit 35221daf04
9 changed files with 311 additions and 185 deletions

View File

@ -1,126 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship;
// класс прорисовки и пермещения
public class DrawningBattleship
{
#region [Variables Initialization]
public EntityBattleship? EntityBattleship { get; private set; } //класс сущность
private int? _pictureWidth; //ширина окна
private int? _pictureHeight; //высота окна
private int? _startX; // Х начальной позиции
private int? _startY; // Y начальной позиции
private readonly int _drawningShipWidth = 143; // длина корабля
private readonly int _drawningShipHeight = 75; // высота корабля
#endregion
public void Init(EntityBattleship entityBattleship) //инициализация свойств корабля и параметров окна, позиции
{
EntityBattleship = entityBattleship;
_pictureWidth = null;
_pictureHeight = null;
_startX = null;
_startY = null;
}
public bool SetPictureSize(int width, int height) // установка размеров окна
{
if (width > _drawningShipWidth && height > _drawningShipHeight) //если размеры окна позволяют вместить корабль, то присваиваем
{
if (_startX.HasValue && _startY.HasValue)
{
if (_drawningShipHeight + _startY.Value > height)
{
_startY = height - _drawningShipHeight;
}
if (_drawningShipWidth + _startX.Value > width)
{
_startX = width - _drawningShipWidth;
}
}
_pictureWidth = width;
_pictureHeight = height;
return true;
}
return false;
}
public void SetPosition(int x, int y) // установка позиции корабля
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) // проверка инициализации размеров окна
return;
if (x < 0 || x + _drawningShipWidth > _pictureWidth) // если корабль не вмещается по Х - присваиваем 0
x = 0;
if (y < 0 || y + _drawningShipHeight > _pictureHeight) // если корабль не вмещается по Y - присваиваем 0
y = 0;
_startX = x;
_startY = y;
}
public bool MoveTransport(DirectionType direction) //метод движения корабля
{
if (EntityBattleship == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции
return false;
switch (direction)
{
case DirectionType.Left: // Влево
if (_startX.Value - EntityBattleship.Step > 0) // Проверяем, есть ли место для шага (аналогично далее)
_startX -= (int)EntityBattleship.Step;
return true;
case DirectionType.Right: // Вправо
if (_startX.Value + EntityBattleship.Step + _drawningShipWidth < _pictureWidth)
_startX += (int)EntityBattleship.Step;
return true;
case DirectionType.Up: // Вверх
if (_startY.Value - EntityBattleship.Step > 0)
_startY -= (int)EntityBattleship.Step;
return true;
case DirectionType.Down: // Вниз
if (_startY.Value + _drawningShipHeight + EntityBattleship.Step < _pictureHeight)
_startY += (int)EntityBattleship.Step;
return true;
default: return false;
}
}
public void DrawTransport(Graphics g) //метод рисования корабля
{
if (EntityBattleship == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции
return;
Pen pen = new(Color.Black, 3);
Brush addBr = new SolidBrush(EntityBattleship.AdditionalColor);
Brush br = new SolidBrush(EntityBattleship.BodyColor);
#region [Drawing]
// Гриницы Линкора
g.DrawLine(pen, _startX.Value + 3, _startY.Value + 15, _startX.Value + 93, _startY.Value + 15);
g.DrawLine(pen, _startX.Value + 93, _startY.Value + 15, _startX.Value + 143, _startY.Value + 15 + 30);
g.DrawLine(pen, _startX.Value + 143, _startY.Value + 15 + 30, _startX.Value + 93, _startY.Value + 15 + 60);
g.DrawLine(pen, _startX.Value + 93, _startY.Value + 15 + 60, _startX.Value + 3, _startY.Value + 15 + 60);
g.DrawLine(pen, _startX.Value + 3, _startY.Value + 15 + 60, _startX.Value + 3, _startY.Value + 15);
//Заливка
g.FillRectangle(br, _startX.Value + 3, _startY.Value + 15, 90, 60);
g.FillPolygon(br, new PointF[] { new PointF(_startX.Value + 93, _startY.Value + 15), new PointF(_startX.Value + 143, _startY.Value + 15 + 30), new PointF(_startX.Value + 93, _startY.Value + 15 + 60) });
//Элементы палубы
g.DrawEllipse(pen, _startX.Value + 83, _startY.Value + 15 + 20, 20, 20);
g.DrawRectangle(pen, _startX.Value + 63, _startY.Value + 15 + 15, 15, 30);
g.DrawRectangle(pen, _startX.Value + 43, _startY.Value + 15 + 25, 20, 10);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startX.Value, _startY.Value + 15 + 10, 3, 15);
g.FillRectangle(brBlack, _startX.Value, _startY.Value + 15 + 35, 3, 15);
// Орудийная башня
if (EntityBattleship.Weapon)
{
g.FillPie(addBr, _startX.Value + 8, _startY.Value + 15, 30, 25, 180, 180);
g.FillRectangle(addBr, _startX.Value + 18, _startY.Value, 10, 20);
}
//Ракеты
if (EntityBattleship.Rockets)
{
g.FillRectangle(addBr, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35);
g.DrawRectangle(pen, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35);
g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 25, 10, 10);
g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 25, 10, 10);
g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 40, 10, 10);
g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 40, 10, 10);
}
#endregion
}
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Battleship; namespace Battleship.Drawnings;
public enum DirectionType public enum DirectionType
{ {
Up = 1, //Вверх Up = 1, //Вверх

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Battleship.Entities;
namespace Battleship.Drawnings;
// класс прорисовки и пермещения
public class DrawningBattleship : DrawningShip
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="entityBattleship">объект класса-сущности</param>
public DrawningBattleship(EntityBattleship entityBattleship)
{
EntityShip = entityBattleship;
}
public override void DrawTransport(Graphics g)
{
if (EntityShip == null || EntityShip is not EntityBattleship entityBattleship || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции
return;
_startY += 15;
base.DrawTransport(g);
_startY -= 15;
#region Прорисовка доп. элеменетов
Pen pen = new(Color.Black, 3); //!!! ЗАДАТЬ ВОПРОС ПРО PEN
Brush addBr = new SolidBrush(entityBattleship.AdditionalColor);
// Орудийная башня
if (entityBattleship.Weapon)
{
g.FillPie(addBr, _startX.Value + 8, _startY.Value + 15, 30, 25, 180, 180);
g.FillRectangle(addBr, _startX.Value + 18, _startY.Value, 10, 20);
}
//Ракеты
if (entityBattleship.Rockets)
{
g.FillRectangle(addBr, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35);
g.DrawRectangle(pen, _startX.Value + 8, _startY.Value + 15 + 20, 30, 35);
g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 25, 10, 10);
g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 25, 10, 10);
g.DrawEllipse(pen, _startX.Value + 23, _startY.Value + 15 + 40, 10, 10);
g.DrawEllipse(pen, _startX.Value + 13, _startY.Value + 15 + 40, 10, 10);
}
#endregion
}
}

View File

@ -0,0 +1,131 @@
using Battleship.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship.Drawnings;
public class DrawningShip
{
#region [Variables Initialization]
public EntityShip? EntityShip { get; protected set; } //класс сущность
private int? _pictureWidth; //ширина окна
private int? _pictureHeight; //высота окна
protected int? _startX; // Х начальной позиции
protected int? _startY; // Y начальной позиции
private readonly int _drawningShipWidth = 143; // длина корабля
private readonly int _drawningShipHeight = 60; // высота корабля
#endregion
/// <summary>
/// Пустой конструктор
/// </summary>
public DrawningShip()
{
_pictureWidth = null;
_pictureHeight = null;
_startX = null;
_startY = null;
}
/// <summary>
/// Конструктор прорисовки
/// </summary>
/// <param name="entityShip">Объект класса-сущности</param>
public DrawningShip(EntityShip entityShip) : this()
{
EntityShip = entityShip;
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningShipWidth"></param>
/// <param name="drawningShipHeight"></param>
protected DrawningShip(int drawningShipWidth, int drawningShipHeight) : this()
{
_drawningShipHeight = drawningShipHeight;
_drawningShipWidth = drawningShipWidth;
}
public bool SetPictureSize(int width, int height) // установка размеров окна
{
if (width > _drawningShipWidth && height > _drawningShipHeight) //если размеры окна позволяют вместить корабль, то присваиваем
{
if (_startX.HasValue && _startY.HasValue)
{
if (_drawningShipHeight + _startY.Value > height)
{
_startY = height - _drawningShipHeight;
}
if (_drawningShipWidth + _startX.Value > width)
{
_startX = width - _drawningShipWidth;
}
}
_pictureWidth = width;
_pictureHeight = height;
return true;
}
return false;
}
public void SetPosition(int x, int y) // установка позиции корабля
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) // проверка инициализации размеров окна
return;
if (x < 0 || x + _drawningShipWidth > _pictureWidth) // если корабль не вмещается по Х - присваиваем 0
x = 0;
if (y < 0 || y + _drawningShipHeight > _pictureHeight) // если корабль не вмещается по Y - присваиваем 0
y = 0;
_startX = x;
_startY = y;
}
public bool MoveTransport(DirectionType direction) //метод движения корабля
{
if (EntityShip == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции
return false;
switch (direction)
{
case DirectionType.Left: // Влево
if (_startX.Value - EntityShip.Step > 0) // Проверяем, есть ли место для шага (аналогично далее)
_startX -= (int)EntityShip.Step;
return true;
case DirectionType.Right: // Вправо
if (_startX.Value + EntityShip.Step + _drawningShipWidth < _pictureWidth)
_startX += (int)EntityShip.Step;
return true;
case DirectionType.Up: // Вверх
if (_startY.Value - EntityShip.Step > 0)
_startY -= (int)EntityShip.Step;
return true;
case DirectionType.Down: // Вниз
if (_startY.Value + _drawningShipHeight + EntityShip.Step < _pictureHeight)
_startY += (int)EntityShip.Step;
return true;
default: return false;
}
}
public virtual void DrawTransport(Graphics g) //метод рисования корабля
{
if (EntityShip == null || !_startX.HasValue || !_startY.HasValue) //проверка наличия корабля и инициализации начальной позиции
return;
Pen pen = new(Color.Black, 3);
Brush br = new SolidBrush(EntityShip.BodyColor);
#region [Drawing]
// Гриницы Линкора
g.DrawLine(pen, _startX.Value + 3, _startY.Value, _startX.Value + 93, _startY.Value);
g.DrawLine(pen, _startX.Value + 93, _startY.Value, _startX.Value + 143, _startY.Value + 30);
g.DrawLine(pen, _startX.Value + 143, _startY.Value + 30, _startX.Value + 93, _startY.Value + 60);
g.DrawLine(pen, _startX.Value + 93, _startY.Value + 60, _startX.Value + 3, _startY.Value + 60);
g.DrawLine(pen, _startX.Value + 3, _startY.Value + 60, _startX.Value + 3, _startY.Value);
//Заливка
g.FillRectangle(br, _startX.Value + 3, _startY.Value, 90, 60);
g.FillPolygon(br, new PointF[] { new PointF(_startX.Value + 93, _startY.Value), new PointF(_startX.Value + 143, _startY.Value + 30), new PointF(_startX.Value + 93, _startY.Value + 60) });
//Элементы палубы
g.DrawEllipse(pen, _startX.Value + 83, _startY.Value + 20, 20, 20);
g.DrawRectangle(pen, _startX.Value + 63, _startY.Value + 15, 15, 30);
g.DrawRectangle(pen, _startX.Value + 43, _startY.Value + 25, 20, 10);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startX.Value, _startY.Value + 10, 3, 15);
g.FillRectangle(brBlack, _startX.Value, _startY.Value + 35, 3, 15);
#endregion
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship.Entities;
// класс-сущность "Боевой корабль"
public class EntityBattleship : EntityShip
{
public Color AdditionalColor; // дополнительный цвет
public bool Weapon { get; private set; } // оружие
public bool Rockets { get; set; } // рокеты
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodycolor">Основной цвет</param>
/// <param name="additionalcolor">Дополнительный цвет</param>
/// <param name="weapon">Наличие оружейной башни</param>
/// <param name="rockets">Наличие рокет</param>
public EntityBattleship(int speed, double weight, Color bodycolor, Color additionalcolor, bool weapon, bool rockets) : base(speed, weight, bodycolor) // инициализация полей объекта-класса военного корабля
{
AdditionalColor = additionalcolor;
Weapon = weapon;
Rockets = rockets;
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Battleship.Entities;
/// <summary>
/// Класс-сущность "Корабль"
/// </summary>
public class EntityShip
{
public int Speed; // скорость
public double Weight; //вес
public Color BodyColor; /*основной цвет*/
public double Step => Speed * 100 / Weight; // шаг перемещения корабля
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodycolor">Цвет</param>
public EntityShip(int speed, double weight, Color bodycolor) {
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
}
}

View File

@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship;
// класс-сущность "Боевой корабль"
public class EntityBattleship
{
public int Speed; // скорость
public double Weight; //вес
public Color BodyColor; /*основной цвет*/
public Color AdditionalColor; // дополнительный цвет
public bool Weapon { get; private set; } // оружие
public bool Rockets { get; set; } // рокеты
public double Step => Speed * 100 / Weight; // шаг перемещения корабля
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, bool weapon, bool rockets) // инициализация полей объекта-класса военного корабля
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
Weapon = weapon;
Rockets = rockets;
}
}

View File

@ -32,26 +32,27 @@
buttonDown = new Button(); buttonDown = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonCreateSimple = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// pictureBoxBattleship // pictureBoxBattleship
// //
pictureBoxBattleship.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; pictureBoxBattleship.Dock = DockStyle.Fill;
pictureBoxBattleship.Location = new Point(0, 0); pictureBoxBattleship.Location = new Point(0, 0);
pictureBoxBattleship.Name = "pictureBoxBattleship"; pictureBoxBattleship.Name = "pictureBoxBattleship";
pictureBoxBattleship.Size = new Size(1259, 803); pictureBoxBattleship.Size = new Size(1641, 945);
pictureBoxBattleship.TabIndex = 0; pictureBoxBattleship.TabIndex = 0;
pictureBoxBattleship.TabStop = false; pictureBoxBattleship.TabStop = false;
// //
// buttonCreate // buttonCreate
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 745); buttonCreate.Location = new Point(12, 887);
buttonCreate.Name = "buttonCreate"; buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(150, 46); buttonCreate.Size = new Size(316, 46);
buttonCreate.TabIndex = 1; buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать боевой корабль";
buttonCreate.UseVisualStyleBackColor = true; buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click; buttonCreate.Click += buttonCreate_Click;
// //
@ -60,7 +61,7 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.right_arrow; buttonUp.BackgroundImage = Properties.Resources.right_arrow;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch; buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(1152, 699); buttonUp.Location = new Point(1534, 841);
buttonUp.Name = "buttonUp"; buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(40, 40); buttonUp.Size = new Size(40, 40);
buttonUp.TabIndex = 2; buttonUp.TabIndex = 2;
@ -72,7 +73,7 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.right_arrow_2; buttonDown.BackgroundImage = Properties.Resources.right_arrow_2;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch; buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(1152, 745); buttonDown.Location = new Point(1534, 887);
buttonDown.Name = "buttonDown"; buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(40, 40); buttonDown.Size = new Size(40, 40);
buttonDown.TabIndex = 3; buttonDown.TabIndex = 3;
@ -84,7 +85,7 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.right_arrow_4; buttonLeft.BackgroundImage = Properties.Resources.right_arrow_4;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(1106, 745); buttonLeft.Location = new Point(1488, 887);
buttonLeft.Name = "buttonLeft"; buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(40, 40); buttonLeft.Size = new Size(40, 40);
buttonLeft.TabIndex = 4; buttonLeft.TabIndex = 4;
@ -96,18 +97,30 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.right_arrow_3; buttonRight.BackgroundImage = Properties.Resources.right_arrow_3;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch; buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(1198, 745); buttonRight.Location = new Point(1580, 887);
buttonRight.Name = "buttonRight"; buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(40, 40); buttonRight.Size = new Size(40, 40);
buttonRight.TabIndex = 5; buttonRight.TabIndex = 5;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click; buttonRight.Click += ButtonMove_Click;
// //
// buttonCreateSimple
//
buttonCreateSimple.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateSimple.Location = new Point(334, 887);
buttonCreateSimple.Name = "buttonCreateSimple";
buttonCreateSimple.Size = new Size(316, 46);
buttonCreateSimple.TabIndex = 6;
buttonCreateSimple.Text = "Создать корабль";
buttonCreateSimple.UseVisualStyleBackColor = true;
buttonCreateSimple.Click += buttonCreateSimple_Click;
//
// FormBattleship // FormBattleship
// //
AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleDimensions = new SizeF(13F, 32F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1259, 803); ClientSize = new Size(1641, 945);
Controls.Add(buttonCreateSimple);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonDown); Controls.Add(buttonDown);
@ -127,5 +140,6 @@
private Button buttonDown; private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private Button buttonRight; private Button buttonRight;
private Button buttonCreateSimple;
} }
} }

View File

@ -7,11 +7,14 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Battleship.Drawnings;
using Battleship.Entities;
namespace Battleship namespace Battleship
{ {
public partial class FormBattleship : Form public partial class FormBattleship : Form
{ {
private DrawningBattleship? _drawningBattleship; private DrawningShip? _drawningShip;
private EntityShip? _entityShip;
private EntityBattleship? _entityBattleship; private EntityBattleship? _entityBattleship;
public FormBattleship() public FormBattleship()
{ {
@ -19,50 +22,74 @@ namespace Battleship
} }
private void Draw() // метод рисования корабля private void Draw() // метод рисования корабля
{ {
if (_drawningBattleship == null) if (_drawningShip == 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);
_drawningBattleship.DrawTransport(gr); _drawningShip.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 random = new();
_drawningBattleship = new DrawningBattleship(); switch (type)
_entityBattleship = new EntityBattleship(); {
_entityBattleship.Init(random.Next(100, 300), random.Next(1000, 3000), case nameof(DrawningShip):
_entityShip = new EntityShip(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
_drawningShip = new DrawningShip(_entityShip);
_drawningShip.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
break;
case nameof(DrawningBattleship):
_entityBattleship = new EntityBattleship(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)),
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)), Convert.ToBoolean(random.Next(0, 2)));
_drawningBattleship.Init(_entityBattleship); _drawningShip = new DrawningBattleship(_entityBattleship);
_drawningBattleship.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height); _drawningShip.SetPictureSize(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
_drawningBattleship.SetPosition(random.Next(10, 100), random.Next(10, 100)); _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
break;
default:
return;
}
}
private void buttonCreate_Click(object sender, EventArgs e) // обработка кнопки "создать боевой корабль"
{
CreateObject(nameof(DrawningBattleship));
}
private void buttonCreateSimple_Click(object sender, EventArgs e) // обработка кнопки "создать корабль"
{
CreateObject(nameof(DrawningShip));
} }
private void ButtonMove_Click(object sender, EventArgs e) // обработка кнопок движения private void ButtonMove_Click(object sender, EventArgs e) // обработка кнопок движения
{ {
if (_drawningBattleship == null) if (_drawningShip == null)
return; return;
string name = ((Button)sender)?.Name ?? string.Empty; string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false; bool result = false;
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
result = _drawningBattleship.MoveTransport(DirectionType.Up); result = _drawningShip.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
result = _drawningBattleship.MoveTransport(DirectionType.Down); result = _drawningShip.MoveTransport(DirectionType.Down);
break; break;
case "buttonRight": case "buttonRight":
result = _drawningBattleship.MoveTransport(DirectionType.Right); result = _drawningShip.MoveTransport(DirectionType.Right);
break; break;
case "buttonLeft": case "buttonLeft":
result = _drawningBattleship.MoveTransport(DirectionType.Left); result = _drawningShip.MoveTransport(DirectionType.Left);
break; break;
} }
if (result) if (result)
Draw(); Draw();
} }
} }
} }