This commit is contained in:
Katerina881 2023-10-07 09:40:07 +04:00
parent 0e65921327
commit 241a0e3ca4
3 changed files with 135 additions and 32 deletions

View File

@ -7,26 +7,24 @@ using System.Threading.Tasks;
namespace DumpTruck namespace DumpTruck
{ {
internal class drawingTruck internal class DrawingTruck
{ {
public EntityTruck Truck { get; private set; } public EntityTruck Truck { 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;
protected readonly int _truckWidth = 100; protected readonly int _truckWidth = 110;
protected readonly int _truckHeight = 55; protected readonly int _truckHeight = 60;
public void Init(int speed, float weight, Color bodyColor) public void Init(int speed, float weight, Color bodyColor)
{ {
Truck = new EntityTruck(); Truck = new EntityTruck();
Truck.Init(speed, weight, bodyColor); Truck.Init(speed, weight, bodyColor);
} }
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y)
{ {
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
} }
public void MoveTransport(Direction direction) public void MoveTransport(Direction direction)
{ {
@ -95,26 +93,6 @@ 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);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _truckWidth || _pictureHeight <= _truckHeight)
{
_pictureHeight = null;
_pictureWidth = null;
return;
}
if (_startPosX + _truckWidth >= _pictureWidth)
{
_startPosX = _pictureWidth.Value - _truckWidth;
}
if (_startPosY + _truckHeight >= _pictureHeight)
{
_startPosY = _pictureHeight.Value - _truckHeight;
}
} }
} }
} }

View File

@ -28,12 +28,115 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container(); this.btnCreate = new System.Windows.Forms.Button();
AutoScaleMode = AutoScaleMode.Font; this.btnLeft = new System.Windows.Forms.Button();
ClientSize = new Size(800, 450); this.btnDown = new System.Windows.Forms.Button();
Text = "Form1"; this.btnRight = new System.Windows.Forms.Button();
this.btnUp = new System.Windows.Forms.Button();
this.pictureBoxTruck = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxTruck)).BeginInit();
this.SuspendLayout();
//
// btnCreate
//
this.btnCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnCreate.Location = new System.Drawing.Point(12, 426);
this.btnCreate.Name = "btnCreate";
this.btnCreate.Size = new System.Drawing.Size(75, 23);
this.btnCreate.TabIndex = 0;
this.btnCreate.Text = "Создать";
this.btnCreate.UseVisualStyleBackColor = true;
this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
//
// btnLeft
//
this.btnLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnLeft.BackgroundImage = global::DumpTruck.Properties.Resources.left;
this.btnLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btnLeft.Location = new System.Drawing.Point(766, 419);
this.btnLeft.Name = "btnLeft";
this.btnLeft.Size = new System.Drawing.Size(30, 30);
this.btnLeft.TabIndex = 1;
this.btnLeft.Text = " ";
this.btnLeft.UseVisualStyleBackColor = true;
this.btnLeft.Click += new System.EventHandler(this.btnMove_Click);
//
// btnDown
//
this.btnDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnDown.BackgroundImage = global::DumpTruck.Properties.Resources.down;
this.btnDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btnDown.Location = new System.Drawing.Point(802, 419);
this.btnDown.Name = "btnDown";
this.btnDown.Size = new System.Drawing.Size(30, 30);
this.btnDown.TabIndex = 2;
this.btnDown.Text = " ";
this.btnDown.UseVisualStyleBackColor = true;
this.btnDown.Click += new System.EventHandler(this.btnMove_Click);
//
// btnRight
//
this.btnRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnRight.BackgroundImage = global::DumpTruck.Properties.Resources.right;
this.btnRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btnRight.Location = new System.Drawing.Point(838, 419);
this.btnRight.Name = "btnRight";
this.btnRight.Size = new System.Drawing.Size(30, 30);
this.btnRight.TabIndex = 3;
this.btnRight.Text = " ";
this.btnRight.UseVisualStyleBackColor = true;
this.btnRight.Click += new System.EventHandler(this.btnMove_Click);
//
// btnUp
//
this.btnUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnUp.BackgroundImage = global::DumpTruck.Properties.Resources.up;
this.btnUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btnUp.Location = new System.Drawing.Point(802, 383);
this.btnUp.Name = "btnUp";
this.btnUp.Size = new System.Drawing.Size(30, 30);
this.btnUp.TabIndex = 4;
this.btnUp.Text = " ";
this.btnUp.UseVisualStyleBackColor = true;
this.btnUp.Click += new System.EventHandler(this.btnMove_Click);
//
// pictureBoxTruck
//
this.pictureBoxTruck.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxTruck.Location = new System.Drawing.Point(0, 0);
this.pictureBoxTruck.Name = "pictureBoxTruck";
this.pictureBoxTruck.Size = new System.Drawing.Size(884, 461);
this.pictureBoxTruck.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxTruck.TabIndex = 5;
this.pictureBoxTruck.TabStop = false;
//
// FormTruck
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(884, 461);
this.Controls.Add(this.btnUp);
this.Controls.Add(this.btnRight);
this.Controls.Add(this.btnDown);
this.Controls.Add(this.btnLeft);
this.Controls.Add(this.btnCreate);
this.Controls.Add(this.pictureBoxTruck);
this.Name = "FormTruck";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FormTruck";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxTruck)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
} }
#endregion #endregion
private Button btnCreate;
private Button btnLeft;
private Button btnDown;
private Button btnRight;
private Button btnUp;
private PictureBox pictureBoxTruck;
} }
} }

View File

@ -12,17 +12,39 @@ namespace DumpTruck
{ {
public partial class FormTruck : Form public partial class FormTruck : Form
{ {
private DrawingTruck? _drawningTruck;
private void Draw()
{
if (_drawningTruck == null)
{
return;
}
Bitmap bmp = new(pictureBoxTruck.Width,
pictureBoxTruck.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTruck.DrawTransport(gr);
pictureBoxTruck.Image = bmp;
}
public FormTruck() public FormTruck()
{ {
InitializeComponent(); InitializeComponent();
} }
private void btnCreate_Click(object sender, EventArgs e) private void btnCreate_Click(object sender, EventArgs e)
{ {
Random random = new();
_drawningTruck = new DrawingTruck();
_drawningTruck.Init(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256)));
_drawningTruck.SetPosition(random.Next(1, 100), random.Next(1, 100));
Draw();
} }
private void btnMove_Click(object sender, EventArgs e) private void btnMove_Click(object sender, EventArgs e)
{ {
} }
} }
} }