1 лабораторная классная работа

This commit is contained in:
EgorRomanov 2022-09-12 11:00:02 +04:00
parent d291a03651
commit fa7476cde9
5 changed files with 64 additions and 58 deletions

View File

@ -9,31 +9,32 @@ namespace Bulldozer
internal class DrawingBulldozer internal class DrawingBulldozer
{ {
public EntityBulldozer Bulldozer { get; private set; }
private float _startPosX; private float _startPosX;
private float _startPosY; private float _startPosY;
private int? _pictureWidth = null; private int? _pictureWidth = null;
private int? _pictureHeight = null; private int? _pictureHeight = null;
private readonly int _bulldozerWidth = 80; 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); Bulldozer.Init(speed, weight, bodyColor);
} }
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
// TODO checks
_startPosX = x; if (x < 0 || x + _bulldozerWidth >= width || y < 0 || y + _bulldozerHeight >= height) return;
_startPosY = y; _startPosX = x;
_pictureWidth = width; _startPosY = y;
_pictureHeight = height; _pictureWidth = width;
_pictureHeight = height;
} }
public void MoveTransport(Direction direction) public void MoveTransport(Direction direction, EntityBulldozer Bulldozer)
{ {
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{ {
@ -41,28 +42,24 @@ namespace Bulldozer
} }
switch (direction) switch (direction)
{ {
// вправо
case Direction.Right: case Direction.Right:
if (_startPosX + _bulldozerWidth + Bulldozer.Step < _pictureWidth) if (_startPosX + _bulldozerWidth + Bulldozer.Step < _pictureWidth)
{ {
_startPosX += Bulldozer.Step; _startPosX += Bulldozer.Step;
} }
break; break;
//влево
case Direction.Left: case Direction.Left:
if (_startPosX - Bulldozer.Step > 0) if (_startPosX - Bulldozer.Step > 0)
{ {
_startPosX -= Bulldozer.Step; _startPosX -= Bulldozer.Step;
} }
break; break;
//вверх
case Direction.Up: case Direction.Up:
if (_startPosY - Bulldozer.Step > 0) if (_startPosY - Bulldozer.Step > 0)
{ {
_startPosY -= Bulldozer.Step; _startPosY -= Bulldozer.Step;
} }
break; break;
//вниз
case Direction.Down: case Direction.Down:
if (_startPosY + _bulldozerHeight + Bulldozer.Step < _pictureHeight) if (_startPosY + _bulldozerHeight + Bulldozer.Step < _pictureHeight)
{ {
@ -71,7 +68,7 @@ namespace Bulldozer
break; break;
} }
} }
public void DrawTransport(Graphics g) public void DrawTransport(Graphics g, EntityBulldozer Bulldozer)
{ {
if (_startPosX < 0 || _startPosY < 0 if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue) || !_pictureHeight.HasValue || !_pictureWidth.HasValue)

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
namespace Bulldozer namespace Bulldozer
{ {
internal class EntityBulldozer internal class EntityBulldozer
{ {
public int Speed { get; private set; } public int Speed { get; private set; }

View File

@ -33,7 +33,9 @@
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBulldozer)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBulldozer)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.BackgroundImage = global::Bulldozer.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -43,7 +45,9 @@
this.buttonUp.TabIndex = 0; this.buttonUp.TabIndex = 0;
this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); 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.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.BackgroundImage = global::Bulldozer.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -53,7 +57,9 @@
this.buttonLeft.TabIndex = 1; this.buttonLeft.TabIndex = 1;
this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); 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.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.BackgroundImage = global::Bulldozer.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -63,7 +69,9 @@
this.buttonRight.TabIndex = 2; this.buttonRight.TabIndex = 2;
this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); 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.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.BackgroundImage = global::Bulldozer.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -73,7 +81,9 @@
this.buttonDown.TabIndex = 3; this.buttonDown.TabIndex = 3;
this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); 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.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.Location = new System.Drawing.Point(12, 393);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Name = "buttonCreate";
@ -82,7 +92,9 @@
this.buttonCreate.Text = "Создать"; this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabelSpeed, this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight, this.toolStripStatusLabelWeight,
@ -92,27 +104,40 @@
this.statusStrip1.Size = new System.Drawing.Size(800, 22); this.statusStrip1.Size = new System.Drawing.Size(800, 22);
this.statusStrip1.TabIndex = 5; this.statusStrip1.TabIndex = 5;
this.statusStrip1.Text = "statusStrip1"; this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 17); this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 17);
this.toolStripStatusLabelSpeed.Text = "Скорость:"; this.toolStripStatusLabelSpeed.Text = "Скорость:";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(29, 17); this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(29, 17);
this.toolStripStatusLabelWeight.Text = "Вес:"; this.toolStripStatusLabelWeight.Text = "Вес:";
//
// toolStripStatusLabelBodyColor
//
this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor";
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17); this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17);
this.toolStripStatusLabelBodyColor.Text = "Цвет:"; this.toolStripStatusLabelBodyColor.Text = "Цвет:";
//
// pictureBoxBulldozer
//
this.pictureBoxBulldozer.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.pictureBoxBulldozer.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxBulldozer.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxBulldozer.Location = new System.Drawing.Point(0, 0); this.pictureBoxBulldozer.Location = new System.Drawing.Point(0, 0);
this.pictureBoxBulldozer.Name = "pictureBoxBulldozer"; this.pictureBoxBulldozer.Name = "pictureBoxBulldozer";
this.pictureBoxBulldozer.Size = new System.Drawing.Size(800, 450); this.pictureBoxBulldozer.Size = new System.Drawing.Size(800, 450);
this.pictureBoxBulldozer.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxBulldozer.TabIndex = 6; this.pictureBoxBulldozer.TabIndex = 6;
this.pictureBoxBulldozer.TabStop = false; 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.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);

View File

@ -2,7 +2,8 @@ namespace Bulldozer
{ {
public partial class FormBulldozer : Form public partial class FormBulldozer : Form
{ {
private DrawingBulldozer _bulldozer; private DrawingBulldozer? _bulldozer;
private EntityBulldozer? Bulldozer { get; set; }
public FormBulldozer() public FormBulldozer()
{ {
InitializeComponent(); InitializeComponent();
@ -12,64 +13,48 @@ namespace Bulldozer
{ {
Bitmap bmp = new(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); Bitmap bmp = new(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_bulldozer?.DrawTransport(gr); _bulldozer?.DrawTransport(gr, Bulldozer);
pictureBoxBulldozer.Image = bmp; pictureBoxBulldozer.Image = bmp;
} }
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e) private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
_bulldozer = new DrawingBulldozer(); _bulldozer = new DrawingBulldozer();
Bulldozer = new EntityBulldozer();
_bulldozer.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), _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), _bulldozer.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100),
pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_bulldozer.Bulldozer.Speed}"; toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {Bulldozer.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_bulldozer.Bulldozer.Weight}"; toolStripStatusLabelWeight.Text = $"Âåñ: {Bulldozer.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_bulldozer.Bulldozer.BodyColor.Name} "; toolStripStatusLabelBodyColor.Text = $"Öâåò: {Bulldozer.BodyColor.Name} ";
Draw(); Draw();
} }
/// <summary>
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
string name = ((Button)sender)?.Name ?? string.Empty; string name = ((Button)sender)?.Name ?? string.Empty;
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_bulldozer?.MoveTransport(Direction.Up); _bulldozer?.MoveTransport(Direction.Up, Bulldozer);
break; break;
case "buttonDown": case "buttonDown":
_bulldozer?.MoveTransport(Direction.Down); _bulldozer?.MoveTransport(Direction.Down, Bulldozer);
break; break;
case "buttonLeft": case "buttonLeft":
_bulldozer?.MoveTransport(Direction.Left); _bulldozer?.MoveTransport(Direction.Left, Bulldozer);
break; break;
case "buttonRight": case "buttonRight":
_bulldozer?.MoveTransport(Direction.Right); _bulldozer?.MoveTransport(Direction.Right, Bulldozer);
break; break;
} }
Draw(); Draw();
} }
/// <summary> private void pictureBoxBulldozer_Resize(object sender, EventArgs e)
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PictureBoxBulldozer_Resize(object sender, EventArgs e)
{ {
_bulldozer?.ChangeBorders(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); _bulldozer?.ChangeBorders(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
Draw(); Draw();

View File

@ -57,7 +57,7 @@
<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>
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value> <value>17, 17</value>
</metadata> </metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">