This commit is contained in:
asoc1al 2023-10-07 10:46:14 +04:00
parent 241a0e3ca4
commit 6986461248
3 changed files with 68 additions and 32 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace DumpTruck namespace DumpTruck
{ {
internal enum Direction internal enum DirectionType
{ {
Up = 1, Up = 1,
Down = 2, Down = 2,

View File

@ -9,58 +9,65 @@ namespace DumpTruck
{ {
internal class DrawingTruck internal class DrawingTruck
{ {
public EntityTruck Truck { get; private set; } public EntityTruck? EntityTruck { get; private set; }
private float _startPosX; private float _startPosX;
private float _startPosY; private float _startPosY;
private int? _pictureWidth = null; private int? _pictureWidth;
private int? _pictureHeight = null; private int? _pictureHeight;
protected readonly int _truckWidth = 110; protected readonly int _truckWidth = 110;
protected readonly int _truckHeight = 60; protected readonly int _truckHeight = 60;
public void Init(int speed, float weight, Color bodyColor) public bool Init(int speed, float weight, Color bodyColor, int width, int height)
{ {
Truck = new EntityTruck(); _pictureWidth = width;
Truck.Init(speed, weight, bodyColor); _pictureHeight = height;
EntityTruck = new EntityTruck();
EntityTruck.Init(speed, weight, bodyColor);
return true;
} }
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
public void MoveTransport(Direction direction) public void MoveTransport(DirectionType direction)
{ {
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) if (EntityTruck == null)
{ {
return; return;
} }
switch (direction) switch (direction)
{ {
case Direction.Left: case DirectionType.Left:
if (_startPosX - Truck.Step > 0) if (_startPosX - EntityTruck.Step > 0)
{ {
_startPosX -= Truck.Step; _startPosX -= (int)EntityTruck.Step;
} }
break; break;
case Direction.Right: //вверх
if (_startPosX + _truckWidth + Truck.Step < _pictureWidth) case DirectionType.Up:
if (_startPosY - EntityTruck.Step > 0)
{ {
_startPosX += Truck.Step; _startPosY -= (int)EntityTruck.Step;
}
break;
case Direction.Up:
if (_startPosY - Truck.Step > 0)
{
_startPosY -= Truck.Step;
} }
break; break;
case Direction.Down: //вправо
if (_startPosY + _truckHeight + Truck.Step < _pictureHeight) case DirectionType.Right:
if (_startPosX + EntityTruck.Step + _truckWidth < _pictureWidth)
{ {
_startPosY += Truck.Step; _startPosX += (int)EntityTruck.Step;
}
break;
case DirectionType.Down:
if (_startPosY + EntityTruck.Step + _truckHeight < _pictureHeight)
{
_startPosY += (int)EntityTruck.Step;
} }
break; break;
} }
} }
public void DrawTransport(Graphics g) public void DrawTransport(Graphics g)
{ {
@ -70,11 +77,11 @@ namespace DumpTruck
} }
Brush br = new SolidBrush(Truck?.BodyColor ?? Color.Black); Brush br = new SolidBrush(EntityTruck?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX + 80, _startPosY, 20, 30); g.FillRectangle(br, _startPosX + 80, _startPosY, 20, 30);
Brush brBrown = new SolidBrush(Color.FromArgb(200, 150, 40)); Brush brBodyRandom = new SolidBrush(Color.FromArgb(0, 255, 128));
g.FillRectangle(brBrown, _startPosX, _startPosY + 30, 100, 5); g.FillRectangle(brBodyRandom, _startPosX, _startPosY + 30, 100, 5);
Brush brBlack = new SolidBrush(Color.Black); Brush brBlack = new SolidBrush(Color.Black);
g.FillEllipse(brBlack, _startPosX, _startPosY + 35, 20, 20); g.FillEllipse(brBlack, _startPosX, _startPosY + 35, 20, 20);
@ -93,6 +100,13 @@ namespace DumpTruck
g.DrawEllipse(pen, _startPosX, _startPosY + 35, 20, 20); g.DrawEllipse(pen, _startPosX, _startPosY + 35, 20, 20);
g.DrawEllipse(pen, _startPosX + 22, _startPosY + 35, 20, 20); g.DrawEllipse(pen, _startPosX + 22, _startPosY + 35, 20, 20);
g.DrawEllipse(pen, _startPosX + 80, _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);
Pen pen1 = new Pen(Color.Black);
g.DrawRectangle(pen1, _startPosX + 0, _startPosY, 70, 30);
} }
} }
} }

View File

@ -15,8 +15,10 @@ namespace DumpTruck
private DrawingTruck? _drawningTruck; private DrawingTruck? _drawningTruck;
private void Draw() private void Draw()
{ {
if (_drawningTruck == null) if (_drawningTruck == null)
{ {
return; return;
@ -37,14 +39,34 @@ namespace DumpTruck
{ {
Random random = new(); Random random = new();
_drawningTruck = new DrawingTruck(); _drawningTruck = new DrawingTruck();
_drawningTruck.Init(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256))); _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, 100), random.Next(1, 100)); _drawningTruck.SetPosition(random.Next(1, pictureBoxTruck.Width), random.Next(1, pictureBoxTruck.Height));
Draw(); Draw();
} }
private void btnMove_Click(object sender, EventArgs e) private void btnMove_Click(object sender, EventArgs e)
{ {
if (_drawningTruck == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "btnUp":
_drawningTruck.MoveTransport(DirectionType.Up);
break;
case "btnDown":
_drawningTruck.MoveTransport(DirectionType.Down);
break;
case "btnLeft":
_drawningTruck.MoveTransport(DirectionType.Left);
break;
case "btnRight":
_drawningTruck.MoveTransport(DirectionType.Right);
break;
}
Draw();
} }
} }
} }