From c6f29d563986eb4a30affbcb04d2fd369e7f16a2 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Fri, 23 Sep 2022 18:38:14 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.=D0=98=D1=81?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B0=20=D0=B2=20=D0=BD=D0=B0=D0=B7=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B8=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/Liner/DrawingLiner.cs | 110 ---------------- Liner/Liner/DrawingShip.cs | 123 ++++++++++++++++++ Liner/Liner/{EntityLiner.cs => EntityShip.cs} | 2 +- Liner/Liner/FormLiner.cs | 62 --------- ...Liner.Designer.cs => FormShip.Designer.cs} | 36 ++--- Liner/Liner/FormShip.cs | 62 +++++++++ Liner/Liner/{FormLiner.resx => FormShip.resx} | 0 Liner/Liner/Program.cs | 2 +- 8 files changed, 205 insertions(+), 192 deletions(-) delete mode 100644 Liner/Liner/DrawingLiner.cs create mode 100644 Liner/Liner/DrawingShip.cs rename Liner/Liner/{EntityLiner.cs => EntityShip.cs} (94%) delete mode 100644 Liner/Liner/FormLiner.cs rename Liner/Liner/{FormLiner.Designer.cs => FormShip.Designer.cs} (89%) create mode 100644 Liner/Liner/FormShip.cs rename Liner/Liner/{FormLiner.resx => FormShip.resx} (100%) diff --git a/Liner/Liner/DrawingLiner.cs b/Liner/Liner/DrawingLiner.cs deleted file mode 100644 index 794e2f5..0000000 --- a/Liner/Liner/DrawingLiner.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Liner -{ - internal class DrawingLiner - { - public EntityLiner Liner { get; set; } - private float _startPosx; - private float _startPosy; - private int? _pictureWidth = null; - private int? _pictureHeight = null; - protected readonly int _LinerWidth = 120; - protected readonly int _LinerHeight = 40; - public void Init(int speed, float weight, Color bodycolor) - { - Liner = new EntityLiner(); - Liner.Init(speed, weight, bodycolor); - } - - public void SetPosition(int x, int y, int width, int height) - { - if (width <= _LinerWidth + x || height <= _LinerHeight + y || x<0 || y<0) - { - _pictureWidth = null; - _pictureHeight = null; - return; - } - _startPosx = x; - _startPosy = y; - _pictureWidth = width; - _pictureHeight = height; - } - - public void MoveTransport(Direction direction) - { - if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) - { - return; - } - switch (direction) - { - case Direction.Right: - if (_startPosx + _LinerWidth + Liner.Step < _pictureWidth) - { - _startPosx += Liner.Step; - - } - break; - case Direction.Left: - if (_startPosx - Liner.Step > 0) - { - _startPosx -= Liner.Step; - } - break; - case Direction.Up: - if (_startPosy - Liner.Step > 0) - { - _startPosy -= Liner.Step; - } - break; - case Direction.Down: - if (_startPosy + _LinerHeight + Liner.Step < _pictureHeight) - { - _startPosy += Liner.Step; - } - break; - - } - } - - public void DrawTransport(Graphics g) - { - if (_startPosx < 0 || _startPosy < 0 || !_pictureWidth.HasValue || !_pictureHeight.HasValue) - { - return; - } - Pen pen = new(Color.Black); - g.DrawRectangle(pen, _startPosx+40, _startPosy, 30 * 2, 10 * 2); - g.DrawLine(pen, _startPosx, _startPosy+20, _startPosx + 60 * 2, _startPosy+20); - g.DrawLine(pen, _startPosx+100, _startPosy+20*2, _startPosx + 120, _startPosy + 10 * 2); - g.DrawLine(pen, _startPosx, _startPosy+20, _startPosx + 20, _startPosy + 40); - g.DrawLine(pen, _startPosx + 20, _startPosy + 40, _startPosx + 100, _startPosy + 40); - } - - public void ChangeBorders(int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureWidth <= _LinerWidth || _pictureHeight <= _LinerHeight) - { - _pictureWidth = null; - _pictureHeight = null; - return; - } - if (_startPosx + _LinerWidth > _pictureWidth) - { - _startPosx = _pictureWidth.Value - _LinerWidth; - } - if (_startPosy + _LinerHeight > _pictureHeight) - { - _startPosy = _pictureHeight.Value - _LinerHeight; - } - - } - } -} diff --git a/Liner/Liner/DrawingShip.cs b/Liner/Liner/DrawingShip.cs new file mode 100644 index 0000000..36c8ff3 --- /dev/null +++ b/Liner/Liner/DrawingShip.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Liner +{ + internal class DrawingShip + { + public EntityShip Ship { get; set; } + private float _startPosX; + private float _startPosY; + private int? _pictureWidth = null; + private int? _pictureHeight = null; + protected readonly int _ShipWidth = 120; + protected readonly int _ShipHeight = 40; + public void Init(int speed, float weight, Color bodycolor) + { + Ship = new EntityShip(); + Ship.Init(speed, weight, bodycolor); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (width <= _ShipWidth + x || height <= _ShipHeight + y || x<0 || y<0) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + + public void MoveTransport(Direction direction) + { + if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) + { + return; + } + switch (direction) + { + case Direction.Right: + if (_startPosX + _ShipWidth + Ship.Step < _pictureWidth) + { + _startPosX += Ship.Step; + + } + break; + case Direction.Left: + if (_startPosX - Ship.Step > 0) + { + _startPosX -= Ship.Step; + } + break; + case Direction.Up: + if (_startPosY - Ship.Step > 0) + { + _startPosY -= Ship.Step; + } + break; + case Direction.Down: + if (_startPosY + _ShipHeight + Ship.Step < _pictureHeight) + { + _startPosY += Ship.Step; + } + break; + + } + } + + public void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 || !_pictureWidth.HasValue || !_pictureHeight.HasValue) + { + return; + } + Pen pen = new(Color.Black); + Brush br = new SolidBrush(Ship?.BodyColor ?? Color.Black); + g.FillRectangle(br, _startPosX + 40, _startPosY, 30 * 2, 10 * 2); + g.DrawRectangle(pen, _startPosX + 40, _startPosY, 30 * 2, 10 * 2); + Point[] pointsdown = { + new Point((int)_startPosX, (int)_startPosY+20), + new Point((int)_startPosX + 60 * 2, (int)_startPosY+20), + new Point((int)_startPosX+100, (int)_startPosY+20*2), + new Point((int)_startPosX + 20, (int)_startPosY + 40), + new Point((int)_startPosX, (int)_startPosY+20), + + }; + g.FillPolygon(br, pointsdown); + g.DrawLine(pen, _startPosX, _startPosY+20, _startPosX + 60 * 2, _startPosY+20); + g.DrawLine(pen, _startPosX+100, _startPosY+20*2, _startPosX + 120, _startPosY + 10 * 2); + g.DrawLine(pen, _startPosX, _startPosY+20, _startPosX + 20, _startPosY + 40); + g.DrawLine(pen, _startPosX + 20, _startPosY + 40, _startPosX + 100, _startPosY + 40); + + + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _ShipWidth || _pictureHeight <= _ShipHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _ShipWidth > _pictureWidth) + { + _startPosX = _pictureWidth.Value - _ShipWidth; + } + if (_startPosY + _ShipHeight > _pictureHeight) + { + _startPosY = _pictureHeight.Value - _ShipHeight; + } + + } + } +} diff --git a/Liner/Liner/EntityLiner.cs b/Liner/Liner/EntityShip.cs similarity index 94% rename from Liner/Liner/EntityLiner.cs rename to Liner/Liner/EntityShip.cs index 4bdf1ad..46613d6 100644 --- a/Liner/Liner/EntityLiner.cs +++ b/Liner/Liner/EntityShip.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Liner { - internal class EntityLiner + internal class EntityShip { public int Speed { get; private set; } diff --git a/Liner/Liner/FormLiner.cs b/Liner/Liner/FormLiner.cs deleted file mode 100644 index 4bcf27a..0000000 --- a/Liner/Liner/FormLiner.cs +++ /dev/null @@ -1,62 +0,0 @@ -namespace Liner -{ - - public partial class FormLiner : Form - { - - private DrawingLiner _liner; - - public FormLiner() - { - InitializeComponent(); - } - - private void Draw() - { - Bitmap bmp = new Bitmap(pictureBoxLiner.Width, pictureBoxLiner.Height); - Graphics gr = Graphics.FromImage(bmp); - _liner?.DrawTransport(gr); - pictureBoxLiner.Image = bmp; - - } - - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - _liner = new DrawingLiner(); - _liner.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - _liner.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLiner.Width, pictureBoxLiner.Height); - toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_liner.Liner?.Speed}"; - toolStripStatusLabelWeight.Text = $"Âåñ: {_liner.Liner?.Weight}"; - toolStripStatusLabelColor.Text = $"Öâåò: {_liner.Liner?.BodyColor.Name}"; - Draw(); - } - private void ButtonMove_Click(object sender, EventArgs e) - { - string name = ((Button)sender)?.Name ?? string.Empty; - switch (name) - { - case "buttonUp": - _liner?.MoveTransport(Direction.Up); - break; - case "buttonDown": - _liner?.MoveTransport(Direction.Down); - break; - case "buttonRight": - _liner?.MoveTransport(Direction.Right); - break; - case "buttonLeft": - _liner?.MoveTransport(Direction.Left); - break; - - } - Draw(); - } - - private void pictureBoxLiner_Resize(object sender, EventArgs e) - { - _liner?.ChangeBorders(pictureBoxLiner.Width, pictureBoxLiner.Height); - Draw(); - } - } -} \ No newline at end of file diff --git a/Liner/Liner/FormLiner.Designer.cs b/Liner/Liner/FormShip.Designer.cs similarity index 89% rename from Liner/Liner/FormLiner.Designer.cs rename to Liner/Liner/FormShip.Designer.cs index 94b493c..d1d72f9 100644 --- a/Liner/Liner/FormLiner.Designer.cs +++ b/Liner/Liner/FormShip.Designer.cs @@ -1,6 +1,6 @@ namespace Liner { - partial class FormLiner + partial class FormShip { /// /// Required designer variable. @@ -28,7 +28,7 @@ /// private void InitializeComponent() { - this.pictureBoxLiner = new System.Windows.Forms.PictureBox(); + this.pictureBoxShip = new System.Windows.Forms.PictureBox(); this.statusStrip = new System.Windows.Forms.StatusStrip(); this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); @@ -38,20 +38,20 @@ this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLiner)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); this.statusStrip.SuspendLayout(); this.SuspendLayout(); // - // pictureBoxLiner + // pictureBoxShip // - this.pictureBoxLiner.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxLiner.Location = new System.Drawing.Point(0, 0); - this.pictureBoxLiner.Name = "pictureBoxLiner"; - this.pictureBoxLiner.Size = new System.Drawing.Size(800, 424); - this.pictureBoxLiner.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; - this.pictureBoxLiner.TabIndex = 0; - this.pictureBoxLiner.TabStop = false; - this.pictureBoxLiner.Resize += new System.EventHandler(this.pictureBoxLiner_Resize); + this.pictureBoxShip.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxShip.Location = new System.Drawing.Point(0, 0); + this.pictureBoxShip.Name = "pictureBoxShip"; + this.pictureBoxShip.Size = new System.Drawing.Size(800, 424); + this.pictureBoxShip.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; + this.pictureBoxShip.TabIndex = 0; + this.pictureBoxShip.TabStop = false; + this.pictureBoxShip.Resize += new System.EventHandler(this.PictureBoxShip_Resize); // // statusStrip // @@ -143,7 +143,7 @@ this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // - // FormLiner + // FormShip // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; @@ -153,11 +153,11 @@ this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonCreate); - this.Controls.Add(this.pictureBoxLiner); + this.Controls.Add(this.pictureBoxShip); this.Controls.Add(this.statusStrip); - this.Name = "FormLiner"; - this.Text = "Лайнер"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLiner)).EndInit(); + this.Name = "FormShip"; + this.Text = "Корабль"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit(); this.statusStrip.ResumeLayout(false); this.statusStrip.PerformLayout(); this.ResumeLayout(false); @@ -167,7 +167,7 @@ #endregion - private PictureBox pictureBoxLiner; + private PictureBox pictureBoxShip; private StatusStrip statusStrip; private ToolStripStatusLabel toolStripStatusLabelSpeed; private ToolStripStatusLabel toolStripStatusLabelWeight; diff --git a/Liner/Liner/FormShip.cs b/Liner/Liner/FormShip.cs new file mode 100644 index 0000000..4eb0604 --- /dev/null +++ b/Liner/Liner/FormShip.cs @@ -0,0 +1,62 @@ +namespace Liner +{ + + public partial class FormShip : Form + { + + private DrawingShip _ship; + + public FormShip() + { + InitializeComponent(); + } + + private void Draw() + { + Bitmap bmp = new Bitmap(pictureBoxShip.Width, pictureBoxShip.Height); + Graphics gr = Graphics.FromImage(bmp); + _ship?.DrawTransport(gr); + pictureBoxShip.Image = bmp; + + } + + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random rnd = new(); + _ship = new DrawingShip(); + _ship.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + _ship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxShip.Width, pictureBoxShip.Height); + toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_ship.Ship?.Speed}"; + toolStripStatusLabelWeight.Text = $"Âåñ: {_ship.Ship?.Weight}"; + toolStripStatusLabelColor.Text = $"Öâåò: {_ship.Ship?.BodyColor.Name}"; + Draw(); + } + private void ButtonMove_Click(object sender, EventArgs e) + { + string name = ((Button)sender)?.Name ?? string.Empty; + switch (name) + { + case "buttonUp": + _ship?.MoveTransport(Direction.Up); + break; + case "buttonDown": + _ship?.MoveTransport(Direction.Down); + break; + case "buttonRight": + _ship?.MoveTransport(Direction.Right); + break; + case "buttonLeft": + _ship?.MoveTransport(Direction.Left); + break; + + } + Draw(); + } + + private void PictureBoxShip_Resize(object sender, EventArgs e) + { + _ship?.ChangeBorders(pictureBoxShip.Width, pictureBoxShip.Height); + Draw(); + } + } +} \ No newline at end of file diff --git a/Liner/Liner/FormLiner.resx b/Liner/Liner/FormShip.resx similarity index 100% rename from Liner/Liner/FormLiner.resx rename to Liner/Liner/FormShip.resx diff --git a/Liner/Liner/Program.cs b/Liner/Liner/Program.cs index d4dc383..4cb7185 100644 --- a/Liner/Liner/Program.cs +++ b/Liner/Liner/Program.cs @@ -11,7 +11,7 @@ namespace Liner // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormLiner()); + Application.Run(new FormShip()); } } } \ No newline at end of file