From 9b74a54dbfa8c877ebe55be666bd7b4eb2b6cf2f Mon Sep 17 00:00:00 2001 From: Oleg Date: Sat, 17 Sep 2022 20:19:26 +0400 Subject: [PATCH] Intermediate 2 --- Trolleybus/Trolleybus/DrawingTrolleybus.cs | 32 +++++--- Trolleybus/Trolleybus/Form1.Designer.cs | 96 +++++++++++----------- Trolleybus/Trolleybus/Form1.cs | 30 ++++++- 3 files changed, 101 insertions(+), 57 deletions(-) diff --git a/Trolleybus/Trolleybus/DrawingTrolleybus.cs b/Trolleybus/Trolleybus/DrawingTrolleybus.cs index 5ba3a9d..e50e156 100644 --- a/Trolleybus/Trolleybus/DrawingTrolleybus.cs +++ b/Trolleybus/Trolleybus/DrawingTrolleybus.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; +using System.Windows.Forms; namespace Trolleybus { @@ -22,6 +23,14 @@ namespace Trolleybus /// private float _startPosY; /// + /// левая координата начала отрисовки + /// + private float _startX; + /// + /// Верхняя координата начала отрисовки + /// + private float _startY; + /// /// Ширина окна отрисовки /// private int? _pictureWidth = null; @@ -32,11 +41,11 @@ namespace Trolleybus /// /// Ширина отрисовки /// - protected readonly int _trolleybusWidth = 0; + protected readonly int _trolleybusWidth = 200; /// /// Высота отрисовки /// - protected readonly int _trolleybusHeight = 0; + protected readonly int _trolleybusHeight = 50; /// /// Инициализация свойств /// @@ -55,10 +64,13 @@ namespace Trolleybus /// Координата Y /// Ширина картинки /// Высота картинки - public void SetPosition(int x, int y, int width, int height) +// public void SetPosition(int x, int y, int startX, int startY, int width, int height) + public void SetPosition(int x, int y, int startX, int startY, int width, int height) { _startPosX = x; _startPosY = y; + _startX = startX; + _startY = startY; _pictureWidth = width; _pictureHeight = height; } @@ -76,30 +88,30 @@ namespace Trolleybus { //вправо case Direction.Right: - if(_startPosX + _trolleybusWidth + Trolleybus.Step< _pictureWidth) + if(_startPosX + _trolleybusWidth + Trolleybus.Step < _pictureWidth) { _startPosX += Trolleybus.Step; } break; //влево case Direction.Left: - if (_startPosX + _trolleybusWidth - Trolleybus.Step < _pictureWidth) + if (_startPosX > _startX) { - _startPosX += Trolleybus.Step; + _startPosX -= Trolleybus.Step; } break; //вверх case Direction.Up: - if (_startPosX + _trolleybusWidth - Trolleybus.Step < _pictureHeight) + if (_startPosY > _startY) { - _startPosX += Trolleybus.Step; + _startPosY -= Trolleybus.Step; } break; //вниз case Direction.Down: - if (_startPosX + _trolleybusWidth + Trolleybus.Step < _pictureHeight) + if (_startPosY + _trolleybusHeight + Trolleybus.Step < _pictureHeight) { - _startPosX += Trolleybus.Step; + _startPosY += Trolleybus.Step; } break; } diff --git a/Trolleybus/Trolleybus/Form1.Designer.cs b/Trolleybus/Trolleybus/Form1.Designer.cs index 753eeea..7160c1d 100644 --- a/Trolleybus/Trolleybus/Form1.Designer.cs +++ b/Trolleybus/Trolleybus/Form1.Designer.cs @@ -34,10 +34,10 @@ namespace Trolleybus this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); this.buttonCreate = new System.Windows.Forms.Button(); - this.button5 = new System.Windows.Forms.Button(); - this.button4 = new System.Windows.Forms.Button(); - this.button3 = new System.Windows.Forms.Button(); - this.button2 = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); this.pictureBoxTrolleybus = new System.Windows.Forms.PictureBox(); this.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTrolleybus)).BeginInit(); @@ -83,51 +83,55 @@ namespace Trolleybus this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); // - // button5 + // buttonUp // - this.button5.BackgroundImage = global::Trolleybus.Properties.Resources.up30; - this.button5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.button5.Location = new System.Drawing.Point(729, 347); - this.button5.Name = "button5"; - this.button5.Size = new System.Drawing.Size(30, 30); - this.button5.TabIndex = 6; - this.button5.UseVisualStyleBackColor = true; + this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(729, 347); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 6; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); // - // button4 + // buttonRight // - this.button4.BackgroundImage = global::Trolleybus.Properties.Resources.right30; - this.button4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.button4.Location = new System.Drawing.Point(765, 383); - this.button4.Name = "button4"; - this.button4.Size = new System.Drawing.Size(30, 30); - this.button4.TabIndex = 5; - this.button4.UseVisualStyleBackColor = true; + this.buttonRight.BackgroundImage = global::Trolleybus.Properties.Resources.right30; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(765, 383); + 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); // - // button3 + // buttonLeft // - this.button3.BackgroundImage = global::Trolleybus.Properties.Resources.left30; - this.button3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.button3.Location = new System.Drawing.Point(693, 383); - this.button3.Name = "button3"; - this.button3.Size = new System.Drawing.Size(30, 30); - this.button3.TabIndex = 4; - this.button3.UseVisualStyleBackColor = true; + this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(693, 383); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 4; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // - // button2 + // buttonDown // - this.button2.BackgroundImage = global::Trolleybus.Properties.Resources.down30; - this.button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.button2.Location = new System.Drawing.Point(729, 383); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(30, 30); - this.button2.TabIndex = 3; - this.button2.UseVisualStyleBackColor = true; + this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(729, 383); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 3; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // // pictureBoxTrolleybus // - this.pictureBoxTrolleybus.Location = new System.Drawing.Point(0, 0); + this.pictureBoxTrolleybus.Location = new System.Drawing.Point(12, 12); this.pictureBoxTrolleybus.Name = "pictureBoxTrolleybus"; - this.pictureBoxTrolleybus.Size = new System.Drawing.Size(800, 425); + this.pictureBoxTrolleybus.Size = new System.Drawing.Size(776, 413); this.pictureBoxTrolleybus.TabIndex = 0; this.pictureBoxTrolleybus.TabStop = false; // @@ -136,10 +140,10 @@ namespace Trolleybus this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.button5); - this.Controls.Add(this.button4); - this.Controls.Add(this.button3); - this.Controls.Add(this.button2); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonCreate); this.Controls.Add(this.statusStrip1); this.Controls.Add(this.pictureBoxTrolleybus); @@ -161,10 +165,10 @@ namespace Trolleybus private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelWeight; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelBodyColor; private System.Windows.Forms.Button buttonCreate; - private System.Windows.Forms.Button button2; - private System.Windows.Forms.Button button3; - private System.Windows.Forms.Button button4; - private System.Windows.Forms.Button button5; + private System.Windows.Forms.Button buttonDown; + private System.Windows.Forms.Button buttonLeft; + private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.Button buttonUp; } } diff --git a/Trolleybus/Trolleybus/Form1.cs b/Trolleybus/Trolleybus/Form1.cs index 7455084..07959cc 100644 --- a/Trolleybus/Trolleybus/Form1.cs +++ b/Trolleybus/Trolleybus/Form1.cs @@ -40,11 +40,39 @@ namespace Trolleybus Random rnd = new Random(); _trolleybus = new DrawingTrolleybus(); _trolleybus.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); + _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), Location.X, Location.Y, pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); +// _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}"; toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}"; toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}"; Draw(); } + + private void ButtonMove_Click(object sender, EventArgs e) + { + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + switch (name) + { + case "buttonUp": + _trolleybus?.MoveTransport(Direction.Up); + break; + case "buttonDown": + _trolleybus?.MoveTransport(Direction.Down); + break; + case "buttonLeft": + _trolleybus?.MoveTransport(Direction.Left); + break; + case "buttonRight": + _trolleybus?.MoveTransport(Direction.Right); + break; + } + Draw(); + } + private void PictureBoxTrolleybus_Resize(object sender, EventArgs e) + { + _trolleybus?.ChangeBorders(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); + Draw(); + } } }