Продвинутый объект

This commit is contained in:
Nap 2022-10-03 23:32:31 +04:00
parent 880d59a6e6
commit a273654d6e
5 changed files with 180 additions and 17 deletions

View File

@ -14,15 +14,15 @@ namespace Battleship
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBattleship Battleship { get; private set; }
public EntityBattleship Battleship { get; protected set; }
/// <summary>
/// Левая координата отрисовки боевого корабля
/// </summary>
private float _startPosX;
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки боевого корабля
/// </summary>
private float _startPosY;
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -34,7 +34,7 @@ namespace Battleship
/// <summary>
/// Ширина отрисовки боевого корабля
/// </summary>
private readonly int _battleshipWidth = 80;
private readonly int _battleshipWidth = 120;
/// <summary>
/// Высота отрисовки боевого корабля
/// </summary>
@ -50,6 +50,20 @@ namespace Battleship
Battleship = new EntityBattleship(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес военного корабля</param>
/// <param name="bodyColor">Цвет корпуса корабля</param>
/// <param name="carWidth">Ширина отрисовки военного корабля</param>
/// <param name="carHeight">Высота отрисовки военного корабля</param>
protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight) :
this(speed, weight, bodyColor)
{
_battleshipWidth = battleshipWidth;
_battleshipHeight = battleshipHeight;
}
/// <summary>
/// Установка позиции боевого корабля
/// </summary>
/// <param name="x">Координата X</param>
@ -116,7 +130,7 @@ namespace Battleship
/// Отрисовка боевого корабля
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -130,10 +144,10 @@ namespace Battleship
g.DrawRectangle(pen, _startPosX - 4, _startPosY + 4, 4, 10);
g.DrawRectangle(pen, _startPosX - 4, _startPosY + 16 , 4, 10);
g.DrawRectangle(pen, _startPosX + 40, _startPosY + 4, 10, 20);
g.DrawRectangle(pen, _startPosX , _startPosY, 70, 30);
PointF point_triangle_1 = new PointF(_startPosX + 70, _startPosY);
PointF point_triangle_2 = new PointF(_startPosX + 90, _startPosY + 15);
PointF point_triangle_3 = new PointF(_startPosX + 70, _startPosY + 30);
g.DrawRectangle(pen, _startPosX , _startPosY, 110, 30);
PointF point_triangle_1 = new PointF(_startPosX + 110, _startPosY);
PointF point_triangle_2 = new PointF(_startPosX + 130, _startPosY + 15);
PointF point_triangle_3 = new PointF(_startPosX + 110, _startPosY + 30);
PointF[] assembled_triangle =
{
point_triangle_1,
@ -150,7 +164,7 @@ namespace Battleship
//палуба корабля
Brush br = new SolidBrush(Battleship?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX, _startPosY, 70, 30);
g.FillRectangle(br, _startPosX, _startPosY, 110, 30);
g.FillPolygon(br, assembled_triangle);
//отсек корабля

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
internal class DrawningGunBattleship : DrawningBattleship
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес военного корабля</param>
/// <param name="bodyColor">Цвет корпуса корабля</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="compartmentRocket">Признак наличия отсека для ракет</param>
/// <param name="gunTower">Признак наличия орудийной башни</param>
/// <param name="sternFence">Признак наличия забора на корме</param>
public DrawningGunBattleship(int speed, float weight, Color bodyColor, Color dopColor, bool compartmentRocket, bool gunTower, bool sternFence) :
base(speed, weight, bodyColor, 110, 60)
{
Battleship = new EntityGunBattleship(speed, weight, bodyColor, dopColor, compartmentRocket, gunTower, sternFence);
}
public override void DrawTransport(Graphics g)
{
if (Battleship is not EntityGunBattleship gunBattleship)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(gunBattleship.DopColor);
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (gunBattleship.CompartmentRocket)
{
g.FillRectangle(dopBrush, _startPosX + 85, _startPosY + 10, 15, 20);
g.DrawRectangle(pen, _startPosX + 85, _startPosY + 10, 15, 20);
}
if (gunBattleship.GunTower)
{
g.FillEllipse(dopBrush, _startPosX + 105, _startPosY + 12, 17, 17);
g.FillRectangle(dopBrush, _startPosX + 121, _startPosY + 17, 11, 7);
g.DrawEllipse(pen, _startPosX + 105, _startPosY + 12, 17, 17);
g.DrawRectangle(pen, _startPosX + 121, _startPosY + 17, 11, 7);
}
if (gunBattleship.SternFence)
{
g.FillRectangle(dopBrush, _startPosX + 13, _startPosY + 8, 5, 25);
g.DrawRectangle(pen, _startPosX + 13, _startPosY + 8, 5, 25);
}
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
/// <summary>
/// Класс-сущность "Военный корабль с оружием"
/// </summary>
internal class EntityGunBattleship : EntityBattleship
{
/// <summary>
/// Дополнительный цвет
/// </summary>
public Color DopColor { get; private set; }
/// <summary>
/// Признак наличия отсека для ракет
/// </summary>
public bool CompartmentRocket { get; private set; }
/// <summary>
/// Признак наличия орудийной башни
/// </summary>
public bool GunTower { get; private set; }
/// <summary>
/// Признак наличия забора на корме
/// </summary>
public bool SternFence { get; private set; }
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес военного корабля</param>
/// <param name="bodyColor">Цвет корпуса корабля</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="compartmentRocket">Признак наличия отсека для ракет</param>
/// <param name="gunTower">Признак наличия орудийной башни</param>
/// <param name="sternFence">Признак наличия забора на корме</param>
public EntityGunBattleship(int speed, float weight, Color bodyColor, Color dopColor, bool compartmentRocket, bool gunTower, bool sternFence) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
CompartmentRocket = compartmentRocket;
GunTower = gunTower;
SternFence = sternFence;
}
}
}

View File

@ -38,6 +38,7 @@
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonCreateModif = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).BeginInit();
this.statusStripBattleship.SuspendLayout();
this.SuspendLayout();
@ -138,12 +139,23 @@
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonCreateModif
//
this.buttonCreateModif.Location = new System.Drawing.Point(93, 397);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(110, 23);
this.buttonCreateModif.TabIndex = 7;
this.buttonCreateModif.Text = "Модификация";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
//
// FormBattleship
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp);
@ -173,5 +185,6 @@
private Button buttonUp;
private Button buttonRight;
private Button buttonLeft;
private Button buttonCreateModif;
}
}

View File

@ -20,19 +20,26 @@ namespace Battleship
pictureBoxBattleship.Image = bmp;
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// Ìåòîä óñòàíîâêè äàííûõ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
private void SetData()
{
Random rnd = new();
_battleship = new DrawningBattleship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_battleship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxBattleship.Width, pictureBoxBattleship.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_battleship.Battleship.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_battleship.Battleship.Weight}";
toolStripStatusLabeBodylColor.Text = $"Öâåò: {_battleship.Battleship.BodyColor.Name}";
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_battleship = new DrawningBattleship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData();
Draw();
}
/// <summary>
@ -71,5 +78,20 @@ namespace Battleship
_battleship?.ChangeBorders(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ìîäèôèêàöèÿ"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_battleship = new DrawningGunBattleship(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData();
Draw();
}
}
}