Переход на конструкторы

This commit is contained in:
Кашин Максим 2022-09-20 21:09:37 +04:00
parent f1c9bfed5f
commit 5e7e145d60
4 changed files with 23 additions and 32 deletions

View File

@ -16,10 +16,9 @@ namespace GasolineTanker
private readonly int _gasolineTankerWidth = 160;
private readonly int _gasolineTankerHeight = 55;
public void Init(int speed, float weight, Color bodyColor)
public DrawingGasolineTanker(int speed, float weight, Color bodyColor)
{
GasolineTanker = new EnityGasolineTanker();
GasolineTanker.Init(speed, weight, bodyColor);
GasolineTanker = new EnityGasolineTanker(speed, weight, bodyColor);
}
public void SetPosition(int x, int y, int width, int height)

View File

@ -12,7 +12,7 @@ namespace GasolineTanker
public float Weight { get; private set; }
public Color BodyColor { get; private set; }
public float Step => Speed * 100 / Weight;
public void Init(int speed, float weight, Color bodyColor)
public EnityGasolineTanker(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.Next(50, 100) : speed;

View File

@ -46,9 +46,8 @@
//
this.pictureBoxGasolineTanker.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxGasolineTanker.Location = new System.Drawing.Point(0, 0);
this.pictureBoxGasolineTanker.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBoxGasolineTanker.Name = "pictureBoxGasolineTanker";
this.pictureBoxGasolineTanker.Size = new System.Drawing.Size(922, 574);
this.pictureBoxGasolineTanker.Size = new System.Drawing.Size(807, 428);
this.pictureBoxGasolineTanker.TabIndex = 0;
this.pictureBoxGasolineTanker.TabStop = false;
//
@ -59,38 +58,36 @@
this.toolStripStatusSpeed,
this.toolStripStatusWeight,
this.toolStripStatusBodyColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 574);
this.statusStrip1.Location = new System.Drawing.Point(0, 428);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 16, 0);
this.statusStrip1.Size = new System.Drawing.Size(922, 26);
this.statusStrip1.Size = new System.Drawing.Size(807, 22);
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusSpeed
//
this.toolStripStatusSpeed.Name = "toolStripStatusSpeed";
this.toolStripStatusSpeed.Size = new System.Drawing.Size(51, 20);
this.toolStripStatusSpeed.Size = new System.Drawing.Size(39, 17);
this.toolStripStatusSpeed.Text = "Speed";
//
// toolStripStatusWeight
//
this.toolStripStatusWeight.Name = "toolStripStatusWeight";
this.toolStripStatusWeight.Size = new System.Drawing.Size(56, 20);
this.toolStripStatusWeight.Size = new System.Drawing.Size(45, 17);
this.toolStripStatusWeight.Text = "Weight";
//
// toolStripStatusBodyColor
//
this.toolStripStatusBodyColor.Name = "toolStripStatusBodyColor";
this.toolStripStatusBodyColor.Size = new System.Drawing.Size(45, 20);
this.toolStripStatusBodyColor.Size = new System.Drawing.Size(36, 17);
this.toolStripStatusBodyColor.Text = "Color";
//
// buttonCreate
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(14, 524);
this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonCreate.Location = new System.Drawing.Point(12, 393);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(86, 31);
this.buttonCreate.Size = new System.Drawing.Size(75, 23);
this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Create";
this.buttonCreate.UseVisualStyleBackColor = true;
@ -101,10 +98,9 @@
this.keyDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown;
this.keyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyDown.Location = new System.Drawing.Point(827, 515);
this.keyDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyDown.Location = new System.Drawing.Point(724, 386);
this.keyDown.Name = "keyDown";
this.keyDown.Size = new System.Drawing.Size(34, 40);
this.keyDown.Size = new System.Drawing.Size(30, 30);
this.keyDown.TabIndex = 3;
this.keyDown.UseVisualStyleBackColor = true;
this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click);
@ -114,10 +110,9 @@
this.keyUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp;
this.keyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyUp.Location = new System.Drawing.Point(827, 467);
this.keyUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyUp.Location = new System.Drawing.Point(724, 350);
this.keyUp.Name = "keyUp";
this.keyUp.Size = new System.Drawing.Size(34, 40);
this.keyUp.Size = new System.Drawing.Size(30, 30);
this.keyUp.TabIndex = 4;
this.keyUp.UseVisualStyleBackColor = true;
this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click);
@ -127,10 +122,9 @@
this.keyLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft;
this.keyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyLeft.Location = new System.Drawing.Point(786, 515);
this.keyLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyLeft.Location = new System.Drawing.Point(688, 386);
this.keyLeft.Name = "keyLeft";
this.keyLeft.Size = new System.Drawing.Size(34, 40);
this.keyLeft.Size = new System.Drawing.Size(30, 30);
this.keyLeft.TabIndex = 5;
this.keyLeft.UseVisualStyleBackColor = true;
this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click);
@ -140,19 +134,18 @@
this.keyRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight;
this.keyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyRight.Location = new System.Drawing.Point(868, 515);
this.keyRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyRight.Location = new System.Drawing.Point(760, 386);
this.keyRight.Name = "keyRight";
this.keyRight.Size = new System.Drawing.Size(34, 40);
this.keyRight.Size = new System.Drawing.Size(30, 30);
this.keyRight.TabIndex = 6;
this.keyRight.UseVisualStyleBackColor = true;
this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// FormGasolineTanker
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(922, 600);
this.ClientSize = new System.Drawing.Size(807, 450);
this.Controls.Add(this.keyRight);
this.Controls.Add(this.keyLeft);
this.Controls.Add(this.keyUp);
@ -160,9 +153,9 @@
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxGasolineTanker);
this.Controls.Add(this.statusStrip1);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormGasolineTanker";
this.Text = "Gasoline tanker";
this.Load += new System.EventHandler(this.FormGasolineTanker_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();

View File

@ -23,8 +23,7 @@ namespace GasolineTanker
private void buttonCreate_Click_1(object sender, EventArgs e)
{
Random rnd = new();
_gasolineTanker = new DrawingGasolineTanker();
_gasolineTanker.Init(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);
toolStripStatusSpeed.Text = $"Speed {_gasolineTanker.GasolineTanker.Speed}";
toolStripStatusWeight.Text = $"Weight {_gasolineTanker.GasolineTanker.Weight}";