From fa7476cde9d162467ca17e9801b4f2aaacdcf4a2 Mon Sep 17 00:00:00 2001 From: EgorRomanov Date: Mon, 12 Sep 2022 11:00:02 +0400 Subject: [PATCH] =?UTF-8?q?1=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bulldozer/Bulldozer/DrawingBulldozer.cs | 27 +++++----- Bulldozer/Bulldozer/EntityBulldozer.cs | 1 - Bulldozer/Bulldozer/FormBulldozer.Designer.cs | 49 ++++++++++++++----- Bulldozer/Bulldozer/FormBulldozer.cs | 43 ++++++---------- Bulldozer/Bulldozer/FormBulldozer.resx | 2 +- 5 files changed, 64 insertions(+), 58 deletions(-) diff --git a/Bulldozer/Bulldozer/DrawingBulldozer.cs b/Bulldozer/Bulldozer/DrawingBulldozer.cs index e1957d6..8f5762d 100644 --- a/Bulldozer/Bulldozer/DrawingBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawingBulldozer.cs @@ -9,31 +9,32 @@ namespace Bulldozer internal class DrawingBulldozer { - public EntityBulldozer Bulldozer { get; private set; } + private float _startPosX; private float _startPosY; private int? _pictureWidth = null; private int? _pictureHeight = null; private readonly int _bulldozerWidth = 80; - private readonly int _bulldozerHeight = 50; + private readonly int _bulldozerHeight = 80; - public void Init(int speed, float weight, Color bodyColor) + public void Init(int speed, float weight, Color bodyColor, EntityBulldozer Bulldozer) { - Bulldozer = new EntityBulldozer(); + Bulldozer.Init(speed, weight, bodyColor); } public void SetPosition(int x, int y, int width, int height) { - // TODO checks - _startPosX = x; - _startPosY = y; - _pictureWidth = width; - _pictureHeight = height; + + if (x < 0 || x + _bulldozerWidth >= width || y < 0 || y + _bulldozerHeight >= height) return; + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; } - public void MoveTransport(Direction direction) + public void MoveTransport(Direction direction, EntityBulldozer Bulldozer) { if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) { @@ -41,28 +42,24 @@ namespace Bulldozer } switch (direction) { - // вправо case Direction.Right: if (_startPosX + _bulldozerWidth + Bulldozer.Step < _pictureWidth) { _startPosX += Bulldozer.Step; } break; - //влево case Direction.Left: if (_startPosX - Bulldozer.Step > 0) { _startPosX -= Bulldozer.Step; } break; - //вверх case Direction.Up: if (_startPosY - Bulldozer.Step > 0) { _startPosY -= Bulldozer.Step; } break; - //вниз case Direction.Down: if (_startPosY + _bulldozerHeight + Bulldozer.Step < _pictureHeight) { @@ -71,7 +68,7 @@ namespace Bulldozer break; } } - public void DrawTransport(Graphics g) + public void DrawTransport(Graphics g, EntityBulldozer Bulldozer) { if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue) diff --git a/Bulldozer/Bulldozer/EntityBulldozer.cs b/Bulldozer/Bulldozer/EntityBulldozer.cs index a4d3269..5ea5077 100644 --- a/Bulldozer/Bulldozer/EntityBulldozer.cs +++ b/Bulldozer/Bulldozer/EntityBulldozer.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; namespace Bulldozer { - internal class EntityBulldozer { public int Speed { get; private set; } diff --git a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs index 7911525..3b7c469 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs @@ -33,7 +33,9 @@ this.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBulldozer)).BeginInit(); this.SuspendLayout(); - + // + // buttonUp + // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Bulldozer.Properties.Resources.up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; @@ -43,7 +45,9 @@ this.buttonUp.TabIndex = 0; this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); - + // + // buttonLeft + // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Bulldozer.Properties.Resources.left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; @@ -53,7 +57,9 @@ this.buttonLeft.TabIndex = 1; this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); - + // + // buttonRight + // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Bulldozer.Properties.Resources.right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; @@ -63,7 +69,9 @@ this.buttonRight.TabIndex = 2; this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); - + // + // buttonDown + // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Bulldozer.Properties.Resources.down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; @@ -73,7 +81,9 @@ this.buttonDown.TabIndex = 3; this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); - + // + // 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(12, 393); this.buttonCreate.Name = "buttonCreate"; @@ -82,7 +92,9 @@ this.buttonCreate.Text = "Создать"; this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); - + // + // statusStrip1 + // this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripStatusLabelSpeed, this.toolStripStatusLabelWeight, @@ -92,27 +104,40 @@ this.statusStrip1.Size = new System.Drawing.Size(800, 22); this.statusStrip1.TabIndex = 5; this.statusStrip1.Text = "statusStrip1"; - + // + // toolStripStatusLabelSpeed + // this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 17); this.toolStripStatusLabelSpeed.Text = "Скорость:"; - + // + // toolStripStatusLabelWeight + // this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(29, 17); this.toolStripStatusLabelWeight.Text = "Вес:"; - + // + // toolStripStatusLabelBodyColor + // this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17); this.toolStripStatusLabelBodyColor.Text = "Цвет:"; - + // + // pictureBoxBulldozer + // + this.pictureBoxBulldozer.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; this.pictureBoxBulldozer.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxBulldozer.Location = new System.Drawing.Point(0, 0); this.pictureBoxBulldozer.Name = "pictureBoxBulldozer"; this.pictureBoxBulldozer.Size = new System.Drawing.Size(800, 450); + this.pictureBoxBulldozer.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxBulldozer.TabIndex = 6; this.pictureBoxBulldozer.TabStop = false; - this.pictureBoxBulldozer.Click += new System.EventHandler(this.PictureBoxBulldozer_Resize); - + this.pictureBoxBulldozer.Click += new System.EventHandler(this.pictureBoxBulldozer_Resize); + this.pictureBoxBulldozer.Resize += new System.EventHandler(this.pictureBoxBulldozer_Resize); + // + // FormBulldozer + // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); diff --git a/Bulldozer/Bulldozer/FormBulldozer.cs b/Bulldozer/Bulldozer/FormBulldozer.cs index f4f74b2..9e861dc 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.cs @@ -2,7 +2,8 @@ namespace Bulldozer { public partial class FormBulldozer : Form { - private DrawingBulldozer _bulldozer; + private DrawingBulldozer? _bulldozer; + private EntityBulldozer? Bulldozer { get; set; } public FormBulldozer() { InitializeComponent(); @@ -12,64 +13,48 @@ namespace Bulldozer { Bitmap bmp = new(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); Graphics gr = Graphics.FromImage(bmp); - _bulldozer?.DrawTransport(gr); + _bulldozer?.DrawTransport(gr, Bulldozer); pictureBoxBulldozer.Image = bmp; } - /// - /// "" - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); _bulldozer = new DrawingBulldozer(); + Bulldozer = new EntityBulldozer(); + _bulldozer.Init(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)), Bulldozer); _bulldozer.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); - toolStripStatusLabelSpeed.Text = $": {_bulldozer.Bulldozer.Speed}"; - toolStripStatusLabelWeight.Text = $": {_bulldozer.Bulldozer.Weight}"; - toolStripStatusLabelBodyColor.Text = $": {_bulldozer.Bulldozer.BodyColor.Name} "; + toolStripStatusLabelSpeed.Text = $": {Bulldozer.Speed}"; + toolStripStatusLabelWeight.Text = $": {Bulldozer.Weight}"; + toolStripStatusLabelBodyColor.Text = $": {Bulldozer.BodyColor.Name} "; Draw(); } - /// - /// - /// - /// - /// - private void ButtonMove_Click(object sender, EventArgs e) { string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { case "buttonUp": - _bulldozer?.MoveTransport(Direction.Up); + _bulldozer?.MoveTransport(Direction.Up, Bulldozer); break; case "buttonDown": - _bulldozer?.MoveTransport(Direction.Down); + _bulldozer?.MoveTransport(Direction.Down, Bulldozer); break; case "buttonLeft": - _bulldozer?.MoveTransport(Direction.Left); + _bulldozer?.MoveTransport(Direction.Left, Bulldozer); break; case "buttonRight": - _bulldozer?.MoveTransport(Direction.Right); + _bulldozer?.MoveTransport(Direction.Right, Bulldozer); break; } Draw(); } - /// - /// - /// - /// - /// - - private void PictureBoxBulldozer_Resize(object sender, EventArgs e) + private void pictureBoxBulldozer_Resize(object sender, EventArgs e) { _bulldozer?.ChangeBorders(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); Draw(); diff --git a/Bulldozer/Bulldozer/FormBulldozer.resx b/Bulldozer/Bulldozer/FormBulldozer.resx index 30aae67..796091e 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.resx +++ b/Bulldozer/Bulldozer/FormBulldozer.resx @@ -57,7 +57,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + 17, 17