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

This commit is contained in:
Sem730 2022-09-27 10:48:19 +03:00
parent 5669b4e535
commit 9436161a1a
5 changed files with 204 additions and 21 deletions

View File

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class DrawningElectroLocomotive : DrawningLocomotive
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="electroLines">Признак наличия "рогов" для подключения</param>
/// <param name="electroBattery">Признак наличия отсека электро-батарей</param>
public DrawningElectroLocomotive(int speed, float weight, Color bodyColor, Color
dopColor, bool electroLines, bool electroBattery) :
base(speed, weight, bodyColor, 110, 60)
{
Locomotivе = new EntityElectricLocomotive(speed, weight, bodyColor, dopColor, electroLines,
electroBattery);
}
public override void DrawTransport(Graphics g)
{
if (Locomotivе is not EntityElectricLocomotive elLocc)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(elLocc.DopColor);
if (elLocc.ElectroLines)
{
g.DrawLine(pen, _startPosX + 20, _startPosY, _startPosX + 5, _startPosY - 12);
g.DrawLine(pen, _startPosX + 20, _startPosY, _startPosX + 35, _startPosY - 12);
g.DrawLine(pen, _startPosX + 70, _startPosY, _startPosX + 55, _startPosY - 12);
g.DrawLine(pen, _startPosX + 70, _startPosY, _startPosX + 85, _startPosY - 12);
// g.DrawEllipse(pen, _startPosX + 90, _startPosY, 20, 20);
// g.DrawEllipse(pen, _startPosX + 90, _startPosY + 40, 20, 20);
// g.DrawRectangle(pen, _startPosX + 90, _startPosY + 10, 20, 40);
// g.DrawRectangle(pen, _startPosX + 90, _startPosY, 15, 15);
// g.DrawRectangle(pen, _startPosX + 90, _startPosY + 45, 15, 15);
// g.FillEllipse(dopBrush, _startPosX + 90, _startPosY, 20, 20);
// g.FillEllipse(dopBrush, _startPosX + 90, _startPosY + 40, 20, 20);
// g.FillRectangle(dopBrush, _startPosX + 90, _startPosY + 10, 20,
//40);
// g.FillRectangle(dopBrush, _startPosX + 90, _startPosY + 1, 15,
//15);
// g.FillRectangle(dopBrush, _startPosX + 90, _startPosY + 45, 15,
//15);
// g.DrawEllipse(pen, _startPosX, _startPosY, 20, 20);
// g.DrawEllipse(pen, _startPosX, _startPosY + 40, 20, 20);
// g.DrawRectangle(pen, _startPosX, _startPosY + 10, 20, 40);
// g.DrawRectangle(pen, _startPosX + 5, _startPosY, 14, 15);
// g.DrawRectangle(pen, _startPosX + 5, _startPosY + 45, 14, 15);
// g.FillEllipse(dopBrush, _startPosX, _startPosY, 20, 20);
// g.FillEllipse(dopBrush, _startPosX, _startPosY + 40, 20, 20);
// g.FillRectangle(dopBrush, _startPosX + 1, _startPosY + 10, 25,
//40);
// g.FillRectangle(dopBrush, _startPosX + 5, _startPosY + 1, 15, 15);
// g.FillRectangle(dopBrush, _startPosX + 5, _startPosY + 45, 15,
//15);
// g.DrawRectangle(pen, _startPosX + 35, _startPosY, 39, 15);
// g.DrawRectangle(pen, _startPosX + 35, _startPosY + 45, 39, 15);
// g.FillRectangle(dopBrush, _startPosX + 35, _startPosY + 1, 40,
//15);
// g.FillRectangle(dopBrush, _startPosX + 35, _startPosY + 45, 40,
//15);
}
//_startPosX += 10;
//_startPosY += 5;
base.DrawTransport(g);
//_startPosX -= 10;
//_startPosY -= 5;
if (elLocc.ElectroBattery)
{
Brush brblack = new SolidBrush(Color.Black);
g.FillRectangle(brblack, _startPosX + 40, _startPosY + 25, 15, 5);
g.FillRectangle(brblack, _startPosX + 60, _startPosY + 25, 15, 5);
g.FillRectangle(brblack, _startPosX + 5, _startPosY + 25, 15, 5);
//g.FillRectangle(brblack, _startPosX + 70, _startPosY + 3, 5, 10);
//g.FillRectangle(dopBrush, _startPosX, _startPosY + 5, 10, 50);
//g.DrawRectangle(pen, _startPosX, _startPosY + 5, 10, 50);
}
}
}
}

View File

@ -14,15 +14,15 @@ namespace ProjectLocomotive
/// <summary> /// <summary>
/// Класс-сущность /// Класс-сущность
/// </summary> /// </summary>
public EntityLocomotive Locomotivе { private set; get; } public EntityLocomotive Locomotivе { get; protected set; }
/// <summary> /// <summary>
/// Левая координата отрисовки локомотива /// Левая координата отрисовки локомотива
/// </summary> /// </summary>
private float _startPosX; protected float _startPosX;
/// <summary> /// <summary>
/// Верхняя кооридната отрисовки локомотива /// Верхняя кооридната отрисовки локомотива
/// </summary> /// </summary>
private float _startPosY; protected float _startPosY;
/// <summary> /// <summary>
/// Ширина окна отрисовки /// Ширина окна отрисовки
/// </summary> /// </summary>
@ -50,6 +50,21 @@ namespace ProjectLocomotive
Locomotivе = new EntityLocomotive(speed, weight, bodyColor); Locomotivе = new EntityLocomotive(speed, weight, bodyColor);
} }
/// <summary> /// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="carWidth">Ширина отрисовки автомобиля</param>
/// <param name="carHeight">Высота отрисовки автомобиля</param>
protected DrawningLocomotive(int speed, float weight, Color bodyColor, int
carWidth, int carHeight) :
this(speed, weight, bodyColor)
{
_LocWidth = carWidth;
_LocHeight = carHeight;
}
/// <summary>
/// Установка позиции автомобиля /// Установка позиции автомобиля
/// </summary> /// </summary>
/// <param name="x">Координата X</param> /// <param name="x">Координата X</param>
@ -118,7 +133,7 @@ namespace ProjectLocomotive
/// Отрисовка автомобиля /// Отрисовка автомобиля
/// </summary> /// </summary>
/// <param name="g"></param> /// <param name="g"></param>
public void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
if (_startPosX < 0 || _startPosY < 0 if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue) || !_pictureHeight.HasValue || !_pictureWidth.HasValue)

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
internal class EntityElectricLocomotive : EntityLocomotive
{
/// <summary>
/// Дополнительный цвет
/// </summary>
public Color DopColor { get; private set; }
/// <summary>
/// Признак наличия "рогов" для подключения
/// </summary>
public bool ElectroLines { get; private set; }
/// <summary>
/// Признак наличия отсека электро-батарей
/// </summary>
public bool ElectroBattery { get; private set; }
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="electroLines">Признак наличия "рогов" для подключения</param>
/// <param name="electroBattery">Признак наличия отсека электро-батарей</param>
public EntityElectricLocomotive(int speed, float weight, Color bodyColor, Color
dopColor, bool electroLines, bool electroBattery) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
ElectroLines = electroLines = true;
ElectroBattery = electroBattery = true;
}
}
}

View File

@ -38,6 +38,7 @@
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.statusStrip = new System.Windows.Forms.StatusStrip(); this.statusStrip = new System.Windows.Forms.StatusStrip();
this.buttonCreateModif = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotive)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotive)).BeginInit();
this.statusStrip.SuspendLayout(); this.statusStrip.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@ -88,19 +89,6 @@
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonLeft.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize); this.buttonLeft.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
// //
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ProjectLocomotive.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(711, 375);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonRight.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
//
// buttonDown // buttonDown
// //
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -114,6 +102,19 @@
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonDown.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize); this.buttonDown.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
// //
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ProjectLocomotive.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(711, 375);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonRight.Resize += new System.EventHandler(this.PictureBoxLocomotive_Resize);
//
// toolStripStatusLabelSpeed // toolStripStatusLabelSpeed
// //
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
@ -144,11 +145,22 @@
this.statusStrip.Size = new System.Drawing.Size(800, 32); this.statusStrip.Size = new System.Drawing.Size(800, 32);
this.statusStrip.TabIndex = 1; this.statusStrip.TabIndex = 1;
// //
// buttonCreateModif
//
this.buttonCreateModif.Location = new System.Drawing.Point(120, 375);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(138, 30);
this.buttonCreateModif.TabIndex = 7;
this.buttonCreateModif.Text = "Модификация";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
//
// FormLocomotive // FormLocomotive
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonLeft);
@ -178,5 +190,6 @@
private ToolStripStatusLabel toolStripStatusLabelWeight; private ToolStripStatusLabel toolStripStatusLabelWeight;
private ToolStripStatusLabel toolStripStatusLabelBodyColor; private ToolStripStatusLabel toolStripStatusLabelBodyColor;
private StatusStrip statusStrip; private StatusStrip statusStrip;
private Button buttonCreateModif;
} }
} }

View File

@ -20,6 +20,17 @@ namespace ProjectLocomotive
pictureBoxLocomotive.Image = bmp; pictureBoxLocomotive.Image = bmp;
} }
/// <summary> /// <summary>
/// Ìåòîä óñòàíîâêè äàííûõ
/// </summary>
private void SetData()
{
Random rnd = new();
_elloc.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_elloc.Locomotivå.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_elloc.Locomotivå.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_elloc.Locomotivå.BodyColor.Name}";
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü" /// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
@ -29,10 +40,7 @@ namespace ProjectLocomotive
{ {
Random rnd = new(); Random rnd = new();
_elloc = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); _elloc = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_elloc.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height); SetData();
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_elloc.Locomotivå.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_elloc.Locomotivå.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_elloc.Locomotivå.BodyColor.Name}";
Draw(); Draw();
} }
/// <summary> /// <summary>
@ -71,5 +79,20 @@ namespace ProjectLocomotive
_elloc?.ChangeBorders(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height); _elloc?.ChangeBorders(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
Draw(); Draw();
} }
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ìîäèôèêàöèÿ"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_elloc = new DrawningElectroLocomotive(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(1, 100), rnd.Next(1, 100), rnd.Next(1, 100)),
Convert.ToBoolean(rnd.Next(0, 1)), Convert.ToBoolean(rnd.Next(0, 1)));
SetData();
Draw();
}
} }
} }