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

This commit is contained in:
Almaz 2024-04-09 11:53:13 +04:00
parent 32dc88b8a9
commit 45691d205f
7 changed files with 236 additions and 92 deletions

View File

@ -4,17 +4,17 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser;
namespace ProjectCruiser.Drawings;
/// <summary>
/// Направление перемещения
/// </summary>
public enum DirectionType
{
public enum DirectionType
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
Up = 1,
/// <summary>
/// Вниз
/// </summary>
@ -27,5 +27,5 @@ namespace ProjectCruiser;
/// Вправо
/// </summary>
Right = 4
}
}

View File

@ -3,15 +3,16 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.Entities;
namespace ProjectCruiser;
namespace ProjectCruiser.Drawings;
public class DrawingCruiser
public class DrawningCruiser
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityCruiser? EntityCruiser { get; private set; }
public EntityCruiser? EntityCruiser { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
@ -23,41 +24,53 @@ public class DrawingCruiser
/// <summary>
/// Левая координата прорисовки крейсера
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки крейсера
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки крейсера
/// </summary>
private readonly int _drawningCruiserWidth = 110;
private readonly int _drawningCruiserWidth = 90;
/// <summary>
/// Высота прорисовки крейсера
/// </summary>
private readonly int _drawingCruiserHeight = 60;
private readonly int _drawingCruiserHeight = 50;
/// <summary>
/// Инициализация полей объекта класса крейсера
/// Пустой конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="armor">Признак наличия брони</param>
/// <param name="weapon">Признак наличия оружия</param>
public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon)
{
EntityCruiser = new EntityCruiser();
EntityCruiser.Init(speed, weigth, bodyColor, additionalColor, bodyKit, armor, weapon);
private DrawningCruiser() {
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Констуктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
public DrawningCruiser(int speed, double weigth, Color bodyColor): this()
{
EntityCruiser = new EntityCruiser(speed, weigth, bodyColor);
}
/// <summary>
/// Констуктор для наследников
/// </summary>
/// <param name="drawningCruiserWidth">Ширина прорисовки крейсера</param>
/// <param name="drawingCruiserHeight">Высота прорисовки крейсера</param>
protected DrawningCruiser(int drawningCruiserWidth, int drawingCruiserHeight) : this()
{
_drawningCruiserWidth = drawningCruiserWidth;
_drawingCruiserHeight = drawingCruiserHeight;
}
public bool SetPictireSize(int width, int height)
{
_pictureWidth = width;
@ -115,7 +128,7 @@ public class DrawingCruiser
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
@ -124,31 +137,21 @@ public class DrawingCruiser
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor);
//Границы крейсера
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 35, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 9, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70, 52);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value, 20, 20);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 30, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 30, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 1, _startPosY.Value + 10, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 10, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 1, 70, 52);
//кузов крейсера
Brush br = new SolidBrush(EntityCruiser.BodyColor);
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70, 50);
// оружие крейсера
if (EntityCruiser.Weapon)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 23, 25, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 23, 35, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 23, 20, 15);
}
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 10, 10, 30);
g.FillRectangle(br, _startPosX.Value + 80, _startPosY.Value + 10, 10, 30);
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value, 70, 50);
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.Entities;
namespace ProjectCruiser.Drawings;
public class DrawningMilitaryCruiser: DrawningCruiser
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="armor">Признак наличия брони</param>
/// <param name="weapon">Признак наличия оружия</param>
public DrawningMilitaryCruiser(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon): base(110, 60)
{
EntityCruiser = new EntityMilitaryCruiser(speed, weigth, bodyColor, additionalColor, bodyKit, armor, weapon);
}
public override void DrawTransport(Graphics g)
{
if (EntityCruiser == null || EntityCruiser is not EntityMilitaryCruiser militaryCruiser || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(militaryCruiser.AdditionalColor);
//Границы крейсера
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 35, 20, 20);
g.DrawRectangle(pen, _startPosX.Value + 9, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70, 52);
//кузов крейсера
Brush br = new SolidBrush(militaryCruiser.BodyColor);
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10, 30);
g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70, 50);
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
// оружие крейсера
if (militaryCruiser.Weapon)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 23, 25, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 23, 35, 15);
g.FillRectangle(additionalBrush, _startPosX.Value + 10, _startPosY.Value + 23, 20, 15);
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.Entities;
/// <summary>
/// Класс-сущность "Крейсер" Вариант 18
/// </summary>
public class EntityCruiser
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; protected set; }
/// <summary>
/// Вес
/// </summary>
public double Weigth { get; protected set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; protected set; }
/// <summary>
/// Шаг перемещения крейсера
/// </summary>
public double Step => Speed * 100 / Weigth;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weigth">Вес крейсера</param>
/// <param name="bodyColor">Скорость</param>
public EntityCruiser(int speed, double weigth, Color bodyColor)
{
Speed = speed;
Weigth = weigth;
BodyColor = bodyColor;
}
}

View File

@ -1,25 +1,10 @@
namespace ProjectCruiser
namespace ProjectCruiser.Entities
{
/// <summary>
/// Класс-сущность "Крейсер" Вариант 18
/// Класс-сущность "Военный Крейсер" Вариант 18
/// </summary>
public class EntityCruiser
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weigth { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
public class EntityMilitaryCruiser: EntityCruiser
{
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
@ -40,11 +25,6 @@
/// </summary>
public bool Weapon { get; private set; }
/// <summary>
/// Шаг перемещения крейсера
/// </summary>
public double Step => Speed * 100 / Weigth;
/// <summary>
/// Инициализация полей объекта класса крейсера
/// </summary>
@ -55,7 +35,7 @@
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="armor">Признак наличия брони</param>
/// <param name="weapon">Признак наличия оружия</param>
public void Init(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon)
public EntityMilitaryCruiser(int speed, double weigth, Color bodyColor, Color additionalColor, bool bodyKit, bool armor, bool weapon): base(speed, weigth, bodyColor)
{
Speed = speed;
Weigth = weigth;

View File

@ -29,11 +29,12 @@
private void InitializeComponent()
{
pictureBoxCruiser = new PictureBox();
buttonCreateCruiser = new Button();
buttonCreateMilitaryCruiser = new Button();
buttonDown = new Button();
buttonUp = new Button();
buttonRight = new Button();
buttonLeft = new Button();
buttonCreateCruiser = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).BeginInit();
SuspendLayout();
//
@ -46,16 +47,16 @@
pictureBoxCruiser.TabIndex = 0;
pictureBoxCruiser.TabStop = false;
//
// buttonCreateCruiser
// buttonCreateMilitaryCruiser
//
buttonCreateCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateCruiser.Location = new Point(12, 409);
buttonCreateCruiser.Name = "buttonCreateCruiser";
buttonCreateCruiser.Size = new Size(94, 29);
buttonCreateCruiser.TabIndex = 1;
buttonCreateCruiser.Text = "Создать";
buttonCreateCruiser.UseVisualStyleBackColor = true;
buttonCreateCruiser.Click += ButtonCreateCruiser_Click;
buttonCreateMilitaryCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateMilitaryCruiser.Location = new Point(12, 409);
buttonCreateMilitaryCruiser.Name = "buttonCreateMilitaryCruiser";
buttonCreateMilitaryCruiser.Size = new Size(216, 29);
buttonCreateMilitaryCruiser.TabIndex = 1;
buttonCreateMilitaryCruiser.Text = "Создать военный крейсер";
buttonCreateMilitaryCruiser.UseVisualStyleBackColor = true;
buttonCreateMilitaryCruiser.Click += ButtonCreateMilitaryCruiser_Click;
//
// buttonDown
//
@ -105,16 +106,28 @@
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// buttonCreateCruiser
//
buttonCreateCruiser.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateCruiser.Location = new Point(234, 409);
buttonCreateCruiser.Name = "buttonCreateCruiser";
buttonCreateCruiser.Size = new Size(179, 29);
buttonCreateCruiser.TabIndex = 6;
buttonCreateCruiser.Text = "Создать крейсер";
buttonCreateCruiser.UseVisualStyleBackColor = true;
buttonCreateCruiser.Click += buttonCreateCruiser_Click;
//
// FormCruiser
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonCreateCruiser);
Controls.Add(buttonLeft);
Controls.Add(buttonRight);
Controls.Add(buttonUp);
Controls.Add(buttonDown);
Controls.Add(buttonCreateCruiser);
Controls.Add(buttonCreateMilitaryCruiser);
Controls.Add(pictureBoxCruiser);
Name = "FormCruiser";
Text = "Крейсер";
@ -125,10 +138,11 @@
#endregion
private PictureBox pictureBoxCruiser;
private Button buttonCreateCruiser;
private Button buttonCreateMilitaryCruiser;
private Button buttonDown;
private Button buttonUp;
private Button buttonRight;
private Button buttonLeft;
private Button buttonCreateCruiser;
}
}

View File

@ -1,9 +1,11 @@
namespace ProjectCruiser
using ProjectCruiser.Drawings;
namespace ProjectCruiser
{
public partial class FormCruiser : Form
{
private DrawingCruiser? _drawingCruiser;
private DrawningCruiser? _drawingCruiser;
public FormCruiser()
{
InitializeComponent();
@ -22,12 +24,20 @@
pictureBoxCruiser.Image = bmp;
}
private void ButtonCreateCruiser_Click(object sender, EventArgs e)
private void CreateObject(string type)
{
Random random = new();
_drawingCruiser = new DrawingCruiser();
_drawingCruiser.Init(
random.Next(100, 300),
switch (type)
{
case nameof(DrawningCruiser):
_drawingCruiser = new DrawningCruiser(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(DrawningMilitaryCruiser):
_drawingCruiser = new DrawningMilitaryCruiser(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)),
@ -35,24 +45,45 @@
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)));
break;
default:
return;
}
_drawingCruiser.SetPictireSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawingCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать военный крейсер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateMilitaryCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMilitaryCruiser));
/// <summary>
/// Обработка нажатия кнопки "Создать крейсер"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateCruiser_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningCruiser));
private void ButtonMove_Click(object sender, EventArgs e)
{
if(_drawingCruiser == null)
if (_drawingCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch(name) {
switch (name)
{
case "buttonUp":
result = _drawingCruiser.MoveTransport(DirectionType.Up);
result = _drawingCruiser.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawingCruiser.MoveTransport(DirectionType.Down);
@ -65,9 +96,11 @@
break;
}
if (result) {
if (result)
{
Draw();
}
}
}
}