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
{
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
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)

View File

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

View File

@ -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);

View File

@ -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;
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PictureBoxBulldozer_Resize(object sender, EventArgs e)
private void pictureBoxBulldozer_Resize(object sender, EventArgs e)
{
_bulldozer?.ChangeBorders(pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
Draw();

View File

@ -57,7 +57,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</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>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">