Первая лабораторная работа.

This commit is contained in:
Кашин Максим 2022-09-07 11:11:00 +04:00
parent bdcc4d3512
commit f1c9bfed5f
3 changed files with 164 additions and 68 deletions

View File

@ -13,8 +13,8 @@ namespace GasolineTanker
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 _gasolineTankerWidth = 80; private readonly int _gasolineTankerWidth = 160;
private readonly int _gasolineTankerHeight = 50; private readonly int _gasolineTankerHeight = 55;
public void Init(int speed, float weight, Color bodyColor) public void Init(int speed, float weight, Color bodyColor)
{ {
@ -24,12 +24,14 @@ namespace GasolineTanker
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
// Check if (x >= 0 && x + _gasolineTankerWidth <= width && y >= 0 && y + _gasolineTankerHeight <= height)
{
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
} }
}
public void MoveTransport(Direction direction) public void MoveTransport(Direction direction)
{ {
@ -48,11 +50,17 @@ namespace GasolineTanker
break; break;
//влево //влево
case Direction.Left: case Direction.Left:
// TODO: Продумать логику if (_startPosX - GasolineTanker.Step > 0)
{
_startPosX -= GasolineTanker.Step;
}
break; break;
//вверх //вверх
case Direction.Up: case Direction.Up:
// TODO: Продумать логику if (_startPosY - GasolineTanker.Step > 0)
{
_startPosY -= GasolineTanker.Step;
}
break; break;
//вниз //вниз
case Direction.Down: case Direction.Down:
@ -65,7 +73,34 @@ namespace GasolineTanker
} }
public void DrawTransport(Graphics g) public void DrawTransport(Graphics g)
{ {
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush brBlack = new SolidBrush(Color.Black);
g.FillEllipse(brBlack, _startPosX + 130, _startPosY + 35, 20, 20);
g.FillEllipse(brBlack, _startPosX + 10, _startPosY + 35, 20, 20);
g.FillEllipse(brBlack, _startPosX + 30, _startPosY + 35, 20, 20);
Brush brRed = new SolidBrush(Color.Red);
g.FillEllipse(brRed, _startPosX+5, _startPosY + 35, 10, 10);
Brush brYellow = new SolidBrush(Color.Yellow);
g.FillEllipse(brYellow, _startPosX + 140, _startPosY + 30, 10, 10);
Brush br = new SolidBrush(GasolineTanker?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX + 115, _startPosY+5, 40, 40);
g.FillRectangle(br, _startPosX + 10, _startPosY + 35, 140, 10);
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 120, _startPosY + 10, 25, 25);
g.DrawRectangle(pen, _startPosX + 120, _startPosY + 10, 25, 25);
g.DrawRectangle(pen, _startPosX + 115, _startPosY + 5, 40, 40);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 35, 105, 10);
} }
public void ChangeBorders(int width, int height) public void ChangeBorders(int width, int height)
{ {

View File

@ -34,10 +34,10 @@
this.toolStripStatusWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.buttonCreate = new System.Windows.Forms.Button(); this.buttonCreate = new System.Windows.Forms.Button();
this.KeyDown = new System.Windows.Forms.Button(); this.keyDown = new System.Windows.Forms.Button();
this.KeyUp = new System.Windows.Forms.Button(); this.keyUp = new System.Windows.Forms.Button();
this.KeyLeft = new System.Windows.Forms.Button(); this.keyLeft = new System.Windows.Forms.Button();
this.KeyRight = new System.Windows.Forms.Button(); this.keyRight = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).BeginInit();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@ -46,103 +46,121 @@
// //
this.pictureBoxGasolineTanker.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxGasolineTanker.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxGasolineTanker.Location = new System.Drawing.Point(0, 0); this.pictureBoxGasolineTanker.Location = new System.Drawing.Point(0, 0);
this.pictureBoxGasolineTanker.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBoxGasolineTanker.Name = "pictureBoxGasolineTanker"; this.pictureBoxGasolineTanker.Name = "pictureBoxGasolineTanker";
this.pictureBoxGasolineTanker.Size = new System.Drawing.Size(800, 428); this.pictureBoxGasolineTanker.Size = new System.Drawing.Size(922, 574);
this.pictureBoxGasolineTanker.TabIndex = 0; this.pictureBoxGasolineTanker.TabIndex = 0;
this.pictureBoxGasolineTanker.TabStop = false; this.pictureBoxGasolineTanker.TabStop = false;
// //
// statusStrip1 // statusStrip1
// //
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusSpeed, this.toolStripStatusSpeed,
this.toolStripStatusWeight, this.toolStripStatusWeight,
this.toolStripStatusBodyColor}); this.toolStripStatusBodyColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 428); this.statusStrip1.Location = new System.Drawing.Point(0, 574);
this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 22); this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 16, 0);
this.statusStrip1.Size = new System.Drawing.Size(922, 26);
this.statusStrip1.TabIndex = 1; this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1"; this.statusStrip1.Text = "statusStrip1";
// //
// toolStripStatusSpeed // toolStripStatusSpeed
// //
this.toolStripStatusSpeed.Name = "toolStripStatusSpeed"; this.toolStripStatusSpeed.Name = "toolStripStatusSpeed";
this.toolStripStatusSpeed.Size = new System.Drawing.Size(39, 17); this.toolStripStatusSpeed.Size = new System.Drawing.Size(51, 20);
this.toolStripStatusSpeed.Text = "Speed"; this.toolStripStatusSpeed.Text = "Speed";
this.toolStripStatusSpeed.Click += new System.EventHandler(this.toolStripStatusLabel1_Click);
// //
// toolStripStatusWeight // toolStripStatusWeight
// //
this.toolStripStatusWeight.Name = "toolStripStatusWeight"; this.toolStripStatusWeight.Name = "toolStripStatusWeight";
this.toolStripStatusWeight.Size = new System.Drawing.Size(45, 17); this.toolStripStatusWeight.Size = new System.Drawing.Size(56, 20);
this.toolStripStatusWeight.Text = "Weight"; this.toolStripStatusWeight.Text = "Weight";
// //
// toolStripStatusBodyColor // toolStripStatusBodyColor
// //
this.toolStripStatusBodyColor.Name = "toolStripStatusBodyColor"; this.toolStripStatusBodyColor.Name = "toolStripStatusBodyColor";
this.toolStripStatusBodyColor.Size = new System.Drawing.Size(36, 17); this.toolStripStatusBodyColor.Size = new System.Drawing.Size(45, 20);
this.toolStripStatusBodyColor.Text = "Color"; this.toolStripStatusBodyColor.Text = "Color";
// //
// buttonCreate // buttonCreate
// //
this.buttonCreate.Location = new System.Drawing.Point(12, 393); this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(14, 524);
this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(75, 23); this.buttonCreate.Size = new System.Drawing.Size(86, 31);
this.buttonCreate.TabIndex = 2; this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Create"; this.buttonCreate.Text = "Create";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click_1);
// //
// KeyDown // keyDown
// //
this.KeyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown; this.keyDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.KeyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown;
this.KeyDown.Location = new System.Drawing.Point(624, 386); this.keyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.KeyDown.Name = "KeyDown"; this.keyDown.Location = new System.Drawing.Point(827, 515);
this.KeyDown.Size = new System.Drawing.Size(30, 30); this.keyDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.KeyDown.TabIndex = 3; this.keyDown.Name = "keyDown";
this.KeyDown.UseVisualStyleBackColor = true; this.keyDown.Size = new System.Drawing.Size(34, 40);
this.keyDown.TabIndex = 3;
this.keyDown.UseVisualStyleBackColor = true;
this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// KeyUp // keyUp
// //
this.KeyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp; this.keyUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.KeyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp;
this.KeyUp.Location = new System.Drawing.Point(624, 350); this.keyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.KeyUp.Name = "KeyUp"; this.keyUp.Location = new System.Drawing.Point(827, 467);
this.KeyUp.Size = new System.Drawing.Size(30, 30); this.keyUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.KeyUp.TabIndex = 4; this.keyUp.Name = "keyUp";
this.KeyUp.UseVisualStyleBackColor = true; this.keyUp.Size = new System.Drawing.Size(34, 40);
this.keyUp.TabIndex = 4;
this.keyUp.UseVisualStyleBackColor = true;
this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// KeyLeft // keyLeft
// //
this.KeyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft; this.keyLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.KeyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft;
this.KeyLeft.Location = new System.Drawing.Point(588, 386); this.keyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.KeyLeft.Name = "KeyLeft"; this.keyLeft.Location = new System.Drawing.Point(786, 515);
this.KeyLeft.Size = new System.Drawing.Size(30, 30); this.keyLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.KeyLeft.TabIndex = 5; this.keyLeft.Name = "keyLeft";
this.KeyLeft.UseVisualStyleBackColor = true; this.keyLeft.Size = new System.Drawing.Size(34, 40);
this.keyLeft.TabIndex = 5;
this.keyLeft.UseVisualStyleBackColor = true;
this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// KeyRight // keyRight
// //
this.KeyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight; this.keyRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.KeyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.keyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight;
this.KeyRight.Location = new System.Drawing.Point(660, 386); this.keyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.KeyRight.Name = "KeyRight"; this.keyRight.Location = new System.Drawing.Point(868, 515);
this.KeyRight.Size = new System.Drawing.Size(30, 30); this.keyRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.KeyRight.TabIndex = 6; this.keyRight.Name = "keyRight";
this.KeyRight.UseVisualStyleBackColor = true; this.keyRight.Size = new System.Drawing.Size(34, 40);
this.keyRight.TabIndex = 6;
this.keyRight.UseVisualStyleBackColor = true;
this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// FormGasolineTanker // FormGasolineTanker
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
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(922, 600);
this.Controls.Add(this.KeyRight); this.Controls.Add(this.keyRight);
this.Controls.Add(this.KeyLeft); this.Controls.Add(this.keyLeft);
this.Controls.Add(this.KeyUp); this.Controls.Add(this.keyUp);
this.Controls.Add(this.KeyDown); this.Controls.Add(this.keyDown);
this.Controls.Add(this.buttonCreate); this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxGasolineTanker); this.Controls.Add(this.pictureBoxGasolineTanker);
this.Controls.Add(this.statusStrip1); this.Controls.Add(this.statusStrip1);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormGasolineTanker"; this.Name = "FormGasolineTanker";
this.Text = "Gasoline tanker"; this.Text = "Gasoline tanker";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGasolineTanker)).EndInit();
@ -161,9 +179,9 @@
private ToolStripStatusLabel toolStripStatusWeight; private ToolStripStatusLabel toolStripStatusWeight;
private ToolStripStatusLabel toolStripStatusBodyColor; private ToolStripStatusLabel toolStripStatusBodyColor;
private Button buttonCreate; private Button buttonCreate;
private Button KeyDown; private Button keyDown;
private Button KeyUp; private Button keyUp;
private Button KeyLeft; private Button keyLeft;
private Button KeyRight; private Button keyRight;
} }
} }

View File

@ -2,19 +2,62 @@ namespace GasolineTanker
{ {
public partial class FormGasolineTanker : Form public partial class FormGasolineTanker : Form
{ {
private DrawingGasolineTanker _gasolineTanker;
public FormGasolineTanker() public FormGasolineTanker()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw()
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
{ {
Bitmap bmp = new(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Graphics gr = Graphics.FromImage(bmp);
_gasolineTanker?.DrawTransport(gr);
pictureBoxGasolineTanker.Image = bmp;
}
private void PictureBoxCar_Resize(object sender, EventArgs e)
{
_gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Draw();
} }
private void ButtonCreate_Click(object sender, EventArgs e) private void buttonCreate_Click_1(object sender, EventArgs e)
{ {
Random rnd = new();
_gasolineTanker = new DrawingGasolineTanker();
_gasolineTanker.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_gasolineTanker.SetPosition(rnd.Next(10, 100),rnd.Next(50, 100), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
toolStripStatusSpeed.Text = $"Speed {_gasolineTanker.GasolineTanker.Speed}";
toolStripStatusWeight.Text = $"Weight {_gasolineTanker.GasolineTanker.Weight}";
toolStripStatusBodyColor.Text = $"Color {_gasolineTanker.GasolineTanker.BodyColor.Name}";
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
//ïîëó÷àåì èìÿ êíîïêè
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "keyUp":
_gasolineTanker?.MoveTransport(Direction.Up);
break;
case "keyDown":
_gasolineTanker?.MoveTransport(Direction.Down);
break;
case "keyLeft":
_gasolineTanker?.MoveTransport(Direction.Left);
break;
case "keyRight":
_gasolineTanker?.MoveTransport(Direction.Right);
break;
}
Draw();
}
private void PictureBoxGasolineTanker_Resize(object sender, EventArgs e)
{
_gasolineTanker?.ChangeBorders(pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
Draw();
} }
} }
} }