Выполнение всех требований по второй лабораторной

This commit is contained in:
AparyanLuiza 2023-11-23 01:37:55 +04:00
parent e9c0b5160f
commit 87de7c65b3
25 changed files with 721 additions and 296 deletions

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBulldozer
{
internal enum Direction
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
}
}

View File

@ -0,0 +1,12 @@

namespace ProjectBulldozer
{
public enum DirectionType
{
Up = 1,
Down = 2,
Left = 3,
Right = 4,
}
}

View File

@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bulldozer;
using ProjectBulldozer.Entities;
using ProjectBulldozer.Properties;
namespace ProjectBulldozer.Drawings
{
public class DrawingTractor
{
public EntityTractor? EntityTractor { get; protected set; }
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private int tractWidth;
protected readonly int _tractWidth = 140;
protected readonly int _tractHeight = 123;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _tractWidth;
public int GetHeight => _tractHeight;
public DrawingTractor(int speed, double weight, Color bodyColor, int width, int heigth)
{
if (width < _tractWidth || heigth < _tractHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = heigth;
EntityTractor = new EntityTractor(speed, weight, bodyColor);
}
protected DrawingTractor(int speed, double weight, Color bodyColor, int width,
int height, int tractWidth, int tractHeight)
{
if (width < _tractWidth || height < _tractHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_tractWidth = tractWidth;
_tractHeight = tractHeight;
EntityTractor = new EntityTractor(speed, weight, bodyColor);
}
//Установка позиции
public void SetPosition(int x, int y)
{
if (x < 0 || x + _tractWidth > _pictureWidth)
{
x = _pictureWidth - tractWidth;
}
if (y < 0 || y + _tractHeight > _pictureHeight)
{
y = _pictureHeight - _tractHeight;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntityTractor == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntityTractor.Step > 0)
{
_startPosX -= (int)EntityTractor.Step;
}
break;
case DirectionType.Up:
if (_startPosY - EntityTractor.Step > 0)
{
_startPosY -= (int)EntityTractor.Step;
}
break;
case DirectionType.Right:
if (_startPosX + EntityTractor.Step + _tractWidth < _pictureWidth)
{
_startPosX += (int)EntityTractor.Step;
}
break;
case DirectionType.Down:
if (_startPosY + EntityTractor.Step + _tractHeight < _pictureHeight)
{
_startPosY += (int)EntityTractor.Step;
}
break;
}
}
public virtual void DrawTransport(Graphics g)
{
{
if (EntityTractor == null) return;
}
Pen pen = new(Color.Black);
Brush brownBrush = new SolidBrush(Color.Brown);
Brush windows = new SolidBrush(Color.LightYellow);
Brush bodyColor = new SolidBrush(EntityTractor.BodyColor);
Brush grayBrush = new SolidBrush(Color.Gray);
//основное тело
g.FillRectangle(bodyColor, _startPosX + 20, _startPosY + 40, 100, 60);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 40, 100, 60);
//кабина водителя
g.FillRectangle(windows, _startPosX + 20, _startPosY, 40, 40);
g.DrawRectangle(pen, _startPosX + 20, _startPosY, 40, 40);
//колеса
g.FillEllipse(grayBrush, _startPosX + 20, _startPosY + 74, 47, 47);
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 74, 47, 47);
g.FillEllipse(grayBrush, _startPosX + 80, _startPosY + 85, 35, 35);
g.DrawEllipse(pen, _startPosX + 80, _startPosY + 85, 35, 35);
//выхлопная труба
g.FillRectangle(brownBrush, _startPosX + 90, _startPosY, 15, 40);
g.DrawRectangle(pen, _startPosX + 90, _startPosY, 15, 40);
}
public bool CanMove(DirectionType direction)
{
if (EntityTractor == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityTractor.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityTractor.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityTractor.Step < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityTractor.Step < _pictureHeight,
};
}
}
}

View File

@ -0,0 +1,75 @@
using ProjectBulldozer.Entities;
namespace ProjectBulldozer.Drawings
{
public class DrawingBulldozer : DrawingTractor
{
public DrawingBulldozer(int speed, double weight, Color bodyColor, Color additionalColor,
bool otval, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 129, 120)
{
if (EntityTractor != null)
{
EntityTractor = new EntityBulldozer(speed, width, bodyColor, additionalColor, otval, seifBatteries);
}
}
public override void DrawTransport(Graphics g)
{
if (EntityTractor is not EntityBulldozer Bulldozer)
{
return;
}
Pen pen = new(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black);
Brush windows = new SolidBrush(Color.LightBlue);
Brush bodyColor = new SolidBrush(Bulldozer.BodyColor);
Brush additionalBrush = new SolidBrush(Bulldozer.AdditionalColor);
Brush grayBrush = new SolidBrush(Color.Gray);
if (Bulldozer.Otval)
{
//otval
Point[] Otval =
{
new Point(_startPosX + 122, _startPosY + 55),
new Point(_startPosX + 142, _startPosY + 115),
new Point(_startPosX+ 122, _startPosY + 115),
};
g.FillPolygon(blackBrush, Otval);
g.DrawPolygon(pen, Otval);
}
//гусеницы
Brush gg = new SolidBrush(Color.LightGray);
g.FillEllipse(gg, _startPosX + 19, _startPosY + 62, 100, 65);
g.DrawEllipse(pen, _startPosX + 19, _startPosY + 62, 100, 65);
g.FillEllipse(grayBrush, _startPosX + 65, _startPosY + 100, 15, 15);
g.DrawEllipse(pen, _startPosX + 65, _startPosY + 100, 15, 15);
Point[] Ttt =
{
new Point(_startPosX + 18 , _startPosY + 80),
new Point(_startPosX + 18, _startPosY + 110),
new Point(_startPosX, _startPosY + 50),
};
g.FillPolygon(blackBrush, Ttt);
g.DrawPolygon(pen, Ttt);
if (Bulldozer.SeifBatteries)
{
g.FillRectangle(blackBrush, _startPosX + 78, _startPosY + 30, 10, 10);
}
base.DrawTransport(g);
}
}
}

View File

@ -1,209 +0,0 @@

using ProjectBulldozer.Entities;
namespace ProjectBulldozer
{
internal class DrawningBulldozer
{
public EntityBulldozer? EntityBulldozer { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _BulldozerWidth = 180;
private readonly int _BulldozerHeight = 140;
public bool Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool additionalEngine, bool additionalCompartment, int width, int height)
{
if (_pictureWidth < _BulldozerWidth | _pictureHeight < _BulldozerHeight)
{
_pictureWidth = width;
_pictureHeight = height;
EntityBulldozer = new EntityBulldozer();
EntityBulldozer.Init(speed, weight, bodyColor, additionalColor,
additionalEngine, additionalCompartment);
return true;
}
else return false;
}
public void SetPosition(int x, int y)
{
if (_startPosY + _BulldozerHeight < _pictureHeight)
_startPosY = y;
else
_startPosY = _pictureHeight - _BulldozerHeight;
if (_startPosX + _BulldozerWidth < _pictureWidth)
_startPosX = x;
else
_startPosX = _pictureWidth - _BulldozerWidth;
}
public void MoveTransport(Direction direction)
{
if (EntityBulldozer == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityBulldozer.Step > 0)
{
_startPosX -= (int)EntityBulldozer.Step;
}
else _startPosX = 0;
break;
//вверх
case Direction.Up:
if (_startPosY - EntityBulldozer.Step > 0)
{
_startPosY -= (int)EntityBulldozer.Step;
}
else _startPosY = 0;
break;
// вправо
case Direction.Right:
if (_startPosX + _BulldozerWidth + EntityBulldozer.Step < _pictureWidth)
{
_startPosX += (int)EntityBulldozer.Step;
}
else _startPosX = _pictureWidth - _BulldozerWidth;
break;
//вниз
case Direction.Down:
if (_startPosY + _BulldozerHeight + EntityBulldozer.Step < _pictureHeight)
{
_startPosY += (int)EntityBulldozer.Step;
}
else _startPosY = _pictureHeight - _BulldozerHeight;
break;
}
}
public void DrawTransport(Graphics g)
{
if (EntityBulldozer == null)
{
return;
}
Pen pen = new(Color.Black);
Brush brush = new SolidBrush(Color.Black);
Brush bl = new SolidBrush(EntityBulldozer.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntityBulldozer.BodyColor);
Brush bodyBrush2 = new SolidBrush(EntityBulldozer.AdditionalColor);
//основное тело
g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 40, 120, 60);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 40, 120, 60);
//Гусеницы
Brush gg = new SolidBrush(Color.LightGray);
g.FillEllipse(gg, _startPosX + 23, _startPosY + 101, 118, 35);
g.DrawEllipse(pen, _startPosX + 23, _startPosY + 101, 118, 35);
g.DrawEllipse(pen, _startPosX + 26, _startPosY + 103, 110, 30);
//катки в гусеницах
Brush gr = new SolidBrush(Color.Gray);
g.FillEllipse(gr, _startPosX + 40, _startPosY + 108, 20, 20);
g.DrawEllipse(pen, _startPosX + 40, _startPosY + 108, 20, 20);
g.FillEllipse(gr, _startPosX + 65, _startPosY + 110, 20, 20);
g.DrawEllipse(pen, _startPosX + 65, _startPosY + 110, 20, 20);
g.FillEllipse(gr, _startPosX + 115, _startPosY + 110, 15, 15);
g.DrawEllipse(pen, _startPosX + 115, _startPosY + 110, 15, 15);
g.FillEllipse(gr, _startPosX + 90, _startPosY + 110, 20, 20);
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 110, 20, 20);
//кабина водителя
g.FillRectangle(bodyBrush2, _startPosX + 20, _startPosY, 40, 40);
g.DrawRectangle(pen, _startPosX + 20, _startPosY, 40, 40);
//выхлопная труба
Brush brBr = new SolidBrush(Color.Brown);
g.FillRectangle(brBr, _startPosX + 110, _startPosY, 15, 40);
g.DrawRectangle(pen, _startPosX + 110, _startPosY, 15, 40);
//Brush bl = new SolidBrush(Color.LightYellow);
/////////отвал
///
Point[] Otval =
{
new Point(_startPosX + 142, _startPosY + 70),
new Point(_startPosX + 172, _startPosY + 130),
new Point(_startPosX+ 142, _startPosY + 130),
};
g.FillPolygon(bl, Otval);
g.DrawPolygon(pen, Otval);
Brush black = new SolidBrush(Color.Black);
Point[] Rihl =
{
new Point(_startPosX + 18 , _startPosY + 60),
new Point(_startPosX + 18, _startPosY + 80),
new Point(_startPosX, _startPosY + 120),
};
g.FillPolygon(black, Rihl);
g.DrawPolygon(pen, Rihl);
Point[] Ttt =
{
new Point(_startPosX + 18 , _startPosY + 80),
new Point(_startPosX + 18, _startPosY + 120),
new Point(_startPosX, _startPosY + 50),
};
g.FillPolygon(black, Ttt);
g.DrawPolygon(pen, Ttt);
}
}
}

View File

@ -1,6 +1,6 @@
namespace ProjectBulldozer namespace Bulldozer
{ {
partial class FormBulldozer partial class Bulldozer
{ {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
@ -28,13 +28,15 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormBulldozer));
pictureBoxBulldozer = new PictureBox(); pictureBoxBulldozer = new PictureBox();
buttonCreate = new Button(); buttonCreateBulldozer = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonDown = new Button(); buttonDown = new Button();
comboBoxStrategy = new ComboBox();
buttonCreateTractor = new Button();
buttonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -42,31 +44,35 @@
// //
pictureBoxBulldozer.Dock = DockStyle.Fill; pictureBoxBulldozer.Dock = DockStyle.Fill;
pictureBoxBulldozer.Location = new Point(0, 0); pictureBoxBulldozer.Location = new Point(0, 0);
pictureBoxBulldozer.Margin = new Padding(2);
pictureBoxBulldozer.Name = "pictureBoxBulldozer"; pictureBoxBulldozer.Name = "pictureBoxBulldozer";
pictureBoxBulldozer.Size = new Size(884, 461); pictureBoxBulldozer.Size = new Size(869, 307);
pictureBoxBulldozer.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxBulldozer.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxBulldozer.TabIndex = 0; pictureBoxBulldozer.TabIndex = 0;
pictureBoxBulldozer.TabStop = false; pictureBoxBulldozer.TabStop = false;
// //
// buttonCreate // buttonCreateBulldozer
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 426); buttonCreateBulldozer.Font = new Font("Times New Roman", 11.25F, FontStyle.Regular, GraphicsUnit.Point);
buttonCreate.Name = "buttonCreate"; buttonCreateBulldozer.Location = new Point(131, 249);
buttonCreate.Size = new Size(75, 23); buttonCreateBulldozer.Margin = new Padding(2);
buttonCreate.TabIndex = 1; buttonCreateBulldozer.Name = "buttonCreateBulldozer";
buttonCreate.Text = "Создать"; buttonCreateBulldozer.Size = new Size(100, 47);
buttonCreate.UseVisualStyleBackColor = true; buttonCreateBulldozer.TabIndex = 1;
buttonCreate.Click += buttonCreate_Click; buttonCreateBulldozer.Text = "Создать булльдозер";
buttonCreateBulldozer.UseVisualStyleBackColor = true;
buttonCreateBulldozer.Click += buttonCreateBulldozer_Click;
// //
// buttonLeft // buttonLeft
// //
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = (Image)resources.GetObject("buttonLeft.BackgroundImage"); buttonLeft.BackgroundImage = ProjectBulldozer.Properties.Resources.left1;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(769, 419); buttonLeft.Location = new Point(764, 271);
buttonLeft.Margin = new Padding(2);
buttonLeft.Name = "buttonLeft"; buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30); buttonLeft.Size = new Size(26, 25);
buttonLeft.TabIndex = 2; buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click; buttonLeft.Click += buttonMove_Click;
@ -74,24 +80,25 @@
// buttonUp // buttonUp
// //
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = (Image)resources.GetObject("buttonUp.BackgroundImage"); buttonUp.BackgroundImage = ProjectBulldozer.Properties.Resources.up1;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom; buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(805, 383); buttonUp.Location = new Point(792, 240);
buttonUp.Margin = new Padding(2);
buttonUp.Name = "buttonUp"; buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30); buttonUp.Size = new Size(30, 27);
buttonUp.TabIndex = 3; buttonUp.TabIndex = 3;
buttonUp.TextAlign = ContentAlignment.BottomRight;
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click; buttonUp.Click += buttonMove_Click;
// //
// buttonRight // buttonRight
// //
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = (Image)resources.GetObject("buttonRight.BackgroundImage"); buttonRight.BackgroundImage = ProjectBulldozer.Properties.Resources.right1;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom; buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(841, 419); buttonRight.Location = new Point(826, 271);
buttonRight.Margin = new Padding(2);
buttonRight.Name = "buttonRight"; buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30); buttonRight.Size = new Size(27, 25);
buttonRight.TabIndex = 4; buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click; buttonRight.Click += buttonMove_Click;
@ -99,28 +106,74 @@
// buttonDown // buttonDown
// //
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = (Image)resources.GetObject("buttonDown.BackgroundImage"); buttonDown.BackgroundImage = ProjectBulldozer.Properties.Resources.down1;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom; buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(805, 419); buttonDown.Location = new Point(794, 271);
buttonDown.Margin = new Padding(2);
buttonDown.Name = "buttonDown"; buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30); buttonDown.Size = new Size(30, 25);
buttonDown.TabIndex = 5; buttonDown.TabIndex = 5;
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click; buttonDown.Click += buttonMove_Click;
// //
// FormBulldozer // comboBoxStrategy
//
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.Font = new Font("Times New Roman", 11.25F, FontStyle.Regular, GraphicsUnit.Point);
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "В угол" });
comboBoxStrategy.Location = new Point(693, 10);
comboBoxStrategy.Margin = new Padding(2);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(150, 25);
comboBoxStrategy.TabIndex = 6;
comboBoxStrategy.SelectedIndexChanged += comboBoxStrategy_SelectedIndexChanged;
//
// buttonCreateTractor
//
buttonCreateTractor.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTractor.Font = new Font("Times New Roman", 11.25F, FontStyle.Regular, GraphicsUnit.Point);
buttonCreateTractor.Location = new Point(11, 249);
buttonCreateTractor.Margin = new Padding(2);
buttonCreateTractor.Name = "buttonCreateTractor";
buttonCreateTractor.Size = new Size(106, 47);
buttonCreateTractor.TabIndex = 7;
buttonCreateTractor.Text = "Создать трактор";
buttonCreateTractor.UseVisualStyleBackColor = true;
buttonCreateTractor.Click += buttonCreateTractor_Click;
//
// buttonStep
//
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonStep.Font = new Font("Times New Roman", 11.25F, FontStyle.Regular, GraphicsUnit.Point);
buttonStep.Location = new Point(755, 39);
buttonStep.Margin = new Padding(2);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(88, 34);
buttonStep.TabIndex = 8;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// Bulldozer
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461); ClientSize = new Size(869, 307);
Controls.Add(buttonStep);
Controls.Add(buttonCreateTractor);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonCreate); Controls.Add(buttonCreateBulldozer);
Controls.Add(pictureBoxBulldozer); Controls.Add(pictureBoxBulldozer);
Name = "FormBulldozer"; Margin = new Padding(2);
Text = "FormBulldozer"; Name = "Bulldozer";
StartPosition = FormStartPosition.CenterScreen;
Text = "Bulldozer";
((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).EndInit();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
@ -129,10 +182,13 @@
#endregion #endregion
private PictureBox pictureBoxBulldozer; private PictureBox pictureBoxBulldozer;
private Button buttonCreate; private Button buttonCreateBulldozer;
private Button buttonLeft; private Button buttonLeft;
private Button buttonUp; private Button buttonUp;
private Button buttonRight; private Button buttonRight;
private Button buttonDown; private Button buttonDown;
private ComboBox comboBoxStrategy;
private Button buttonCreateTractor;
private Button buttonStep;
} }
} }

View File

@ -1,51 +1,59 @@
namespace ProjectBulldozer using ProjectBulldozer;
using ProjectBulldozer.Drawings;
using ProjectBulldozer.MovementStrategy;
using System;
namespace Bulldozer
{ {
public partial class FormBulldozer : Form public partial class Bulldozer : Form
{ {
private DrawningBulldozer? _drawningBulldozer; private DrawingTractor? _drawingTractor;
public FormBulldozer() private AbstractStrategy? _abstractStrategy;
public Bulldozer()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw() private void Draw()
{ {
if (_drawningBulldozer == null) if (_drawingTractor == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxBulldozer.Width, Bitmap bmp = new(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
pictureBoxBulldozer.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningBulldozer.DrawTransport(gr); _drawingTractor.DrawTransport(gr);
pictureBoxBulldozer.Image = bmp; pictureBoxBulldozer.Image = bmp;
} }
private void buttonCreateBulldozer_Click(object sender, EventArgs e)
private void buttonCreate_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new Random();
_drawningBulldozer = new DrawningBulldozer(); _drawingTractor = new DrawingBulldozer(random.Next(100, 300), random.Next(1000, 3000),
_drawningBulldozer.Init(random.Next(100, 300), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
_drawningBulldozer.SetPosition(random.Next(10, 100), _drawingTractor.SetPosition(random.Next(10, 100), random.Next(10, 100));
random.Next(10, 100)); Draw();
}
private void buttonCreateTractor_Click(object sender, EventArgs e)
{
Random rnd = new Random();
_drawingTractor = new DrawingTractor(rnd.Next(100, 300), rnd.Next(1000, 3000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
_drawingTractor.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw(); Draw();
} }
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
{ {
if (_drawningBulldozer == null) if (_drawingTractor == null)
{ {
return; return;
} }
@ -53,20 +61,60 @@ namespace ProjectBulldozer
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawningBulldozer.MoveTransport(Direction.Up); _drawingTractor.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawningBulldozer.MoveTransport(Direction.Down); _drawingTractor.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawningBulldozer.MoveTransport(Direction.Left); _drawingTractor.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawningBulldozer.MoveTransport(Direction.Right); _drawingTractor.MoveTransport(DirectionType.Right);
break; break;
} }
Draw(); Draw();
} }
}
} private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingTractor == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToRightCorner(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawingObjectTractor(_drawingTractor), pictureBoxBulldozer.Width,
pictureBoxBulldozer.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
private void comboBoxStrategy_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

View File

@ -0,0 +1,79 @@
namespace ProjectBulldozer.MovementStrategy
{
/// <summary>
/// Абстрактный класс стратегии
/// </summary>
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public Status GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(DirectionType.Left);
protected bool MoveRight() => MoveTo(DirectionType.Right);
protected bool MoveUp() => MoveTo(DirectionType.Up);
protected bool MoveDown() => MoveTo(DirectionType.Down);
/// Параметры объекта
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion();
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,30 @@

using ProjectBulldozer.Drawings;
namespace ProjectBulldozer.MovementStrategy
{
public class DrawingObjectTractor : IMoveableObject
{
private readonly DrawingTractor? _drawningTractor = null;
public DrawingObjectTractor(DrawingTractor drawningTractor)
{
_drawningTractor = drawningTractor;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningTractor == null || _drawningTractor.EntityTractor == null)
{
return null;
}
return new ObjectParameters(_drawningTractor.GetPosX,
_drawningTractor.GetPosY, _drawningTractor.GetWidth, _drawningTractor.GetHeight);
}
}
public int GetStep => (int)(_drawningTractor?.EntityTractor?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) => _drawningTractor?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) => _drawningTractor?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,13 @@

namespace ProjectBulldozer.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,47 @@

namespace ProjectBulldozer.MovementStrategy
{
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null) return false;
return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal
+ GetStep() >= FieldWidth / 2 && objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null) return;
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,25 @@

namespace ProjectBulldozer.MovementStrategy
{
public class MoveToRightCorner : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null) return false;
return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep();
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null) return;
if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight();
if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown();
}
}
}

View File

@ -0,0 +1,38 @@

namespace ProjectBulldozer.MovementStrategy
{
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
/// Левая граница
public int LeftBorder => _x;
/// Верхняя граница
public int TopBorder => _y;
/// Правая граница
public int RightBorder => _x + _width;
/// Нижняя граница
public int DownBorder => _y + _height;
/// Середина объекта по горизонтали
public int ObjectMiddleHorizontal => _x + _width / 2;
/// Середина объекта по вертикали
public int ObjectMiddleVertical => _y + _height / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -1,4 +1,4 @@
namespace ProjectBulldozer namespace Bulldozer
{ {
internal static class Program internal static class Program
{ {
@ -11,7 +11,7 @@ namespace ProjectBulldozer
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormBulldozer()); Application.Run(new Bulldozer());
} }
} }
} }

View File

@ -59,5 +59,45 @@ namespace ProjectBulldozer.Properties {
resourceCulture = value; resourceCulture = value;
} }
} }
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap down1 {
get {
object obj = ResourceManager.GetObject("down1", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap left1 {
get {
object obj = ResourceManager.GetObject("left1", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap right1 {
get {
object obj = ResourceManager.GetObject("right1", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap up1 {
get {
object obj = ResourceManager.GetObject("up1", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
} }
} }

View File

@ -117,4 +117,17 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="down1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\down1.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="left1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\left1.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="right1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\right1.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="up1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\up1.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root> </root>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

@ -0,0 +1,10 @@

namespace ProjectBulldozer.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}