From e300a3d194759234b2120e634e2bfcb47011ce3d Mon Sep 17 00:00:00 2001 From: asoc1al Date: Sat, 7 Oct 2023 11:34:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=87=D1=82=D0=B8=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5,=20=D1=84=D0=B8=D0=BA=D1=81=D0=B0=D0=BD=D1=83=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D0=B2=D0=BE=D0=B9=D1=81=D1=82=D0=B2=D0=B0-?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=B7=D0=BD=D0=B0=D0=BA=D0=B8=20(bool=20?= =?UTF-8?q?=D0=B2=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DumpTruck/DumpTruck/DrawingTruck.cs | 39 +++++++++++++++++++---------- DumpTruck/DumpTruck/EntityTruck.cs | 4 ++- DumpTruck/DumpTruck/FormTruck.cs | 7 +++--- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/DumpTruck/DumpTruck/DrawingTruck.cs b/DumpTruck/DumpTruck/DrawingTruck.cs index 566a34e..ec65513 100644 --- a/DumpTruck/DumpTruck/DrawingTruck.cs +++ b/DumpTruck/DumpTruck/DrawingTruck.cs @@ -10,22 +10,34 @@ namespace DumpTruck internal class DrawingTruck { public EntityTruck? EntityTruck { get; private set; } - private float _startPosX; - private float _startPosY; - private int? _pictureWidth; - private int? _pictureHeight; - protected readonly int _truckWidth = 110; - protected readonly int _truckHeight = 60; - public bool Init(int speed, float weight, Color bodyColor, int width, int height) + private int _startPosX; + private int _startPosY; + private int _pictureWidth; + private int _pictureHeight; + protected readonly int _truckWidth = 100; + protected readonly int _truckHeight = 50; + public bool Init(int speed, float weight, Color bodyColor, Color additionalColor, int width, int height) { + if (width < _truckWidth || height < _truckHeight) + { + return false; + } _pictureWidth = width; _pictureHeight = height; EntityTruck = new EntityTruck(); - EntityTruck.Init(speed, weight, bodyColor); + EntityTruck.Init(speed, weight, bodyColor, additionalColor); return true; } public void SetPosition(int x, int y) { + if (x < 0 || x + _truckWidth > _pictureWidth) + { + x = _pictureWidth - _truckWidth; + } + if (y < 0 || y + _truckHeight > _pictureHeight) + { + y = _pictureHeight - _truckHeight; + } _startPosX = x; _startPosY = y; } @@ -69,9 +81,9 @@ namespace DumpTruck } - public void DrawTransport(Graphics g) + public void DrawTransport(Graphics g, Color bodyColor, Color additionalColor) { - if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue) + if (EntityTruck == null) { return; } @@ -80,7 +92,7 @@ namespace DumpTruck Brush br = new SolidBrush(EntityTruck?.BodyColor ?? Color.Black); g.FillRectangle(br, _startPosX + 80, _startPosY, 20, 30); - Brush brBodyRandom = new SolidBrush(Color.FromArgb(0, 255, 128)); + Brush brBodyRandom = new SolidBrush(bodyColor); g.FillRectangle(brBodyRandom, _startPosX, _startPosY + 30, 100, 5); Brush brBlack = new SolidBrush(Color.Black); @@ -101,8 +113,9 @@ namespace DumpTruck g.DrawEllipse(pen, _startPosX + 22, _startPosY + 35, 20, 20); g.DrawEllipse(pen, _startPosX + 80, _startPosY + 35, 20, 20); - Brush brBody = new SolidBrush(Color.FromArgb(255, 0, 0)); - g.FillRectangle(brBody, _startPosX + 0, _startPosY, 70, 30); + //Brush brBody = new SolidBrush(EntityTruck?.AdditionalColor ?? Color.Red); + Brush brBodyAdditional = new SolidBrush(additionalColor); + g.FillRectangle(brBodyAdditional, _startPosX + 0, _startPosY, 70, 30); Pen pen1 = new Pen(Color.Black); g.DrawRectangle(pen1, _startPosX + 0, _startPosY, 70, 30); diff --git a/DumpTruck/DumpTruck/EntityTruck.cs b/DumpTruck/DumpTruck/EntityTruck.cs index f0a7cd2..793c885 100644 --- a/DumpTruck/DumpTruck/EntityTruck.cs +++ b/DumpTruck/DumpTruck/EntityTruck.cs @@ -14,14 +14,16 @@ namespace DumpTruck public float Weight { get; private set; } public Color BodyColor { get; private set; } + public Color AdditionalColor { get; private set; } public float Step => Speed * 100 / Weight; - public void Init(int speed, float weight, Color bodyColor) + public void Init(int speed, float weight, Color bodyColor, Color additionalColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; + AdditionalColor = additionalColor; } } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormTruck.cs b/DumpTruck/DumpTruck/FormTruck.cs index a522920..92a926e 100644 --- a/DumpTruck/DumpTruck/FormTruck.cs +++ b/DumpTruck/DumpTruck/FormTruck.cs @@ -23,10 +23,11 @@ namespace DumpTruck { return; } + Random rnd = new Random(); Bitmap bmp = new(pictureBoxTruck.Width, pictureBoxTruck.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningTruck.DrawTransport(gr); + _drawningTruck.DrawTransport(gr, Color.FromArgb(0,255,128),Color.FromArgb(255,0,0)); pictureBoxTruck.Image = bmp; } @@ -39,8 +40,8 @@ namespace DumpTruck { Random random = new(); _drawningTruck = new DrawingTruck(); - _drawningTruck.Init(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256)), pictureBoxTruck.Width, pictureBoxTruck.Height); - _drawningTruck.SetPosition(random.Next(1, pictureBoxTruck.Width), random.Next(1, pictureBoxTruck.Height)); + _drawningTruck.Init(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256)), Color.FromArgb(random.Next(0,256)), pictureBoxTruck.Width, pictureBoxTruck.Height); + _drawningTruck.SetPosition(random.Next(1, 100), random.Next(1, 100)); Draw(); }