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

This commit is contained in:
Кашин Максим 2022-09-20 22:42:52 +04:00
parent 5e7e145d60
commit 588e670457
5 changed files with 114 additions and 8 deletions

View File

@ -8,9 +8,9 @@ namespace GasolineTanker
{ {
internal class DrawingGasolineTanker internal class DrawingGasolineTanker
{ {
public EnityGasolineTanker GasolineTanker { get; private set; } public EnityGasolineTanker GasolineTanker { get; protected set; }
private float _startPosX; protected float _startPosX;
private float _startPosY; protected float _startPosY;
private int? _pictureWidth = null; private int? _pictureWidth = null;
private int? _pictureHeight = null; private int? _pictureHeight = null;
private readonly int _gasolineTankerWidth = 160; private readonly int _gasolineTankerWidth = 160;
@ -21,6 +21,13 @@ namespace GasolineTanker
GasolineTanker = new EnityGasolineTanker(speed, weight, bodyColor); GasolineTanker = new EnityGasolineTanker(speed, weight, bodyColor);
} }
protected DrawingGasolineTanker(int speed, float weight, Color bodyColor, int gasolineTankerWidth, int gasolineTankerHeight) :
this(speed, weight, bodyColor)
{
_gasolineTankerWidth = gasolineTankerWidth;
_gasolineTankerHeight = gasolineTankerHeight;
}
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
if (x >= 0 && x + _gasolineTankerWidth <= width && y >= 0 && y + _gasolineTankerHeight <= height) if (x >= 0 && x + _gasolineTankerWidth <= width && y >= 0 && y + _gasolineTankerHeight <= height)
@ -70,7 +77,7 @@ namespace GasolineTanker
break; break;
} }
} }
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,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class DrawingImprovedGasolineTanker : DrawingGasolineTanker
{
public DrawingImprovedGasolineTanker(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool ornamentWheels) :
base(speed, weight, bodyColor, 160, 55)
{
GasolineTanker = new EntityImprovedGasolineTanker(speed, weight, bodyColor, dopColor, bodyKit, ornamentWheels);
}
public override void DrawTransport(Graphics g)
{
if (GasolineTanker is not EntityImprovedGasolineTanker improvedGasolineTanker)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(improvedGasolineTanker.DopColor);
if (improvedGasolineTanker.BodyKit)
{
Brush brOrange = new SolidBrush(Color.Orange);
g.FillRectangle(brOrange, _startPosX + 25, _startPosY + 5, 100, 35);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (improvedGasolineTanker.OrnamentWheels)
{
Pen penGray = new(Color.Gray);
Brush brBlack = new SolidBrush(Color.Gray);
g.DrawEllipse(penGray, _startPosX + 140, _startPosY + 50, 20, 5);
g.DrawEllipse(penGray, _startPosX + 40, _startPosY + 50, 20, 5);
g.DrawEllipse(penGray, _startPosX + 20, _startPosY + 50, 20, 5);
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class EntityImprovedGasolineTanker : EnityGasolineTanker
{
public Color DopColor { get; private set; }
public bool BodyKit { get; private set; }
public bool OrnamentWheels { get; private set; }
public EntityImprovedGasolineTanker(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool ornamentWheels) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
BodyKit = bodyKit;
OrnamentWheels = ornamentWheels;
}
}
}

View File

@ -38,6 +38,7 @@
this.keyUp = new System.Windows.Forms.Button(); this.keyUp = new System.Windows.Forms.Button();
this.keyLeft = new System.Windows.Forms.Button(); this.keyLeft = new System.Windows.Forms.Button();
this.keyRight = new System.Windows.Forms.Button(); this.keyRight = new System.Windows.Forms.Button();
this.ButtonCreateImproved = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).BeginInit();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@ -141,11 +142,22 @@
this.keyRight.UseVisualStyleBackColor = true; this.keyRight.UseVisualStyleBackColor = true;
this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click); this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// ButtonCreateImproved
//
this.ButtonCreateImproved.Location = new System.Drawing.Point(93, 393);
this.ButtonCreateImproved.Name = "ButtonCreateImproved";
this.ButtonCreateImproved.Size = new System.Drawing.Size(75, 23);
this.ButtonCreateImproved.TabIndex = 7;
this.ButtonCreateImproved.Text = "Improved";
this.ButtonCreateImproved.UseVisualStyleBackColor = true;
this.ButtonCreateImproved.Click += new System.EventHandler(this.ButtonCreateImproved_Click);
//
// FormGasolineTanker // FormGasolineTanker
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(807, 450); this.ClientSize = new System.Drawing.Size(807, 450);
this.Controls.Add(this.ButtonCreateImproved);
this.Controls.Add(this.keyRight); this.Controls.Add(this.keyRight);
this.Controls.Add(this.keyLeft); this.Controls.Add(this.keyLeft);
this.Controls.Add(this.keyUp); this.Controls.Add(this.keyUp);
@ -155,7 +167,6 @@
this.Controls.Add(this.statusStrip1); this.Controls.Add(this.statusStrip1);
this.Name = "FormGasolineTanker"; this.Name = "FormGasolineTanker";
this.Text = "Gasoline tanker"; this.Text = "Gasoline tanker";
this.Load += new System.EventHandler(this.FormGasolineTanker_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit();
this.statusStrip1.ResumeLayout(false); this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout(); this.statusStrip1.PerformLayout();
@ -176,5 +187,6 @@
private Button keyUp; private Button keyUp;
private Button keyLeft; private Button keyLeft;
private Button keyRight; private Button keyRight;
private Button ButtonCreateImproved;
} }
} }

View File

@ -14,6 +14,14 @@ namespace GasolineTanker
_gasolineTanker?.DrawTransport(gr); _gasolineTanker?.DrawTransport(gr);
pictureBoxGasolineTanker.Image = bmp; pictureBoxGasolineTanker.Image = bmp;
} }
private void SetData()
{
Random rnd = new();
_gasolineTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
toolStripStatusSpeed.Text = $"Speed {_gasolineTanker.GasolineTanker.Speed}";
toolStripStatusWeight.Text = $"Weight {_gasolineTanker.GasolineTanker.Weight}";
toolStripStatusBodyColor.Text = $"Color {_gasolineTanker.GasolineTanker.BodyColor.Name}";
}
private void PictureBoxCar_Resize(object sender, EventArgs e) private void PictureBoxCar_Resize(object sender, EventArgs e)
{ {
_gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height); _gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
@ -25,9 +33,7 @@ namespace GasolineTanker
Random rnd = new(); Random rnd = new();
_gasolineTanker = new DrawingGasolineTanker(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); _gasolineTanker = new DrawingGasolineTanker(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_gasolineTanker.SetPosition(rnd.Next(10, 100),rnd.Next(50, 100), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height); _gasolineTanker.SetPosition(rnd.Next(10, 100),rnd.Next(50, 100), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
toolStripStatusSpeed.Text = $"Speed {_gasolineTanker.GasolineTanker.Speed}"; SetData();
toolStripStatusWeight.Text = $"Weight {_gasolineTanker.GasolineTanker.Weight}";
toolStripStatusBodyColor.Text = $"Color {_gasolineTanker.GasolineTanker.BodyColor.Name}";
Draw(); Draw();
} }
@ -58,5 +64,16 @@ namespace GasolineTanker
_gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height); _gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Draw(); Draw();
} }
private void ButtonCreateImproved_Click(object sender, EventArgs e)
{
Random rnd = new();
_gasolineTanker = new DrawingImprovedGasolineTanker(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)));
SetData();
Draw();
}
} }
} }