Первая лабораторная работа.Исправлена ошибка в названии базового класса.

This commit is contained in:
Павел Сорокин 2022-09-23 18:38:14 +04:00
parent 5e6dae1ae7
commit c6f29d5639
8 changed files with 205 additions and 192 deletions

View File

@ -1,110 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Liner
{
internal class DrawingLiner
{
public EntityLiner Liner { get; set; }
private float _startPosx;
private float _startPosy;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
protected readonly int _LinerWidth = 120;
protected readonly int _LinerHeight = 40;
public void Init(int speed, float weight, Color bodycolor)
{
Liner = new EntityLiner();
Liner.Init(speed, weight, bodycolor);
}
public void SetPosition(int x, int y, int width, int height)
{
if (width <= _LinerWidth + x || height <= _LinerHeight + y || x<0 || y<0)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
_startPosx = x;
_startPosy = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
case Direction.Right:
if (_startPosx + _LinerWidth + Liner.Step < _pictureWidth)
{
_startPosx += Liner.Step;
}
break;
case Direction.Left:
if (_startPosx - Liner.Step > 0)
{
_startPosx -= Liner.Step;
}
break;
case Direction.Up:
if (_startPosy - Liner.Step > 0)
{
_startPosy -= Liner.Step;
}
break;
case Direction.Down:
if (_startPosy + _LinerHeight + Liner.Step < _pictureHeight)
{
_startPosy += Liner.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (_startPosx < 0 || _startPosy < 0 || !_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
Pen pen = new(Color.Black);
g.DrawRectangle(pen, _startPosx+40, _startPosy, 30 * 2, 10 * 2);
g.DrawLine(pen, _startPosx, _startPosy+20, _startPosx + 60 * 2, _startPosy+20);
g.DrawLine(pen, _startPosx+100, _startPosy+20*2, _startPosx + 120, _startPosy + 10 * 2);
g.DrawLine(pen, _startPosx, _startPosy+20, _startPosx + 20, _startPosy + 40);
g.DrawLine(pen, _startPosx + 20, _startPosy + 40, _startPosx + 100, _startPosy + 40);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _LinerWidth || _pictureHeight <= _LinerHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosx + _LinerWidth > _pictureWidth)
{
_startPosx = _pictureWidth.Value - _LinerWidth;
}
if (_startPosy + _LinerHeight > _pictureHeight)
{
_startPosy = _pictureHeight.Value - _LinerHeight;
}
}
}
}

123
Liner/Liner/DrawingShip.cs Normal file
View File

@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Liner
{
internal class DrawingShip
{
public EntityShip Ship { get; set; }
private float _startPosX;
private float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
protected readonly int _ShipWidth = 120;
protected readonly int _ShipHeight = 40;
public void Init(int speed, float weight, Color bodycolor)
{
Ship = new EntityShip();
Ship.Init(speed, weight, bodycolor);
}
public void SetPosition(int x, int y, int width, int height)
{
if (width <= _ShipWidth + x || height <= _ShipHeight + y || x<0 || y<0)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
case Direction.Right:
if (_startPosX + _ShipWidth + Ship.Step < _pictureWidth)
{
_startPosX += Ship.Step;
}
break;
case Direction.Left:
if (_startPosX - Ship.Step > 0)
{
_startPosX -= Ship.Step;
}
break;
case Direction.Up:
if (_startPosY - Ship.Step > 0)
{
_startPosY -= Ship.Step;
}
break;
case Direction.Down:
if (_startPosY + _ShipHeight + Ship.Step < _pictureHeight)
{
_startPosY += Ship.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0 || !_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush br = new SolidBrush(Ship?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX + 40, _startPosY, 30 * 2, 10 * 2);
g.DrawRectangle(pen, _startPosX + 40, _startPosY, 30 * 2, 10 * 2);
Point[] pointsdown = {
new Point((int)_startPosX, (int)_startPosY+20),
new Point((int)_startPosX + 60 * 2, (int)_startPosY+20),
new Point((int)_startPosX+100, (int)_startPosY+20*2),
new Point((int)_startPosX + 20, (int)_startPosY + 40),
new Point((int)_startPosX, (int)_startPosY+20),
};
g.FillPolygon(br, pointsdown);
g.DrawLine(pen, _startPosX, _startPosY+20, _startPosX + 60 * 2, _startPosY+20);
g.DrawLine(pen, _startPosX+100, _startPosY+20*2, _startPosX + 120, _startPosY + 10 * 2);
g.DrawLine(pen, _startPosX, _startPosY+20, _startPosX + 20, _startPosY + 40);
g.DrawLine(pen, _startPosX + 20, _startPosY + 40, _startPosX + 100, _startPosY + 40);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _ShipWidth || _pictureHeight <= _ShipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _ShipWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _ShipWidth;
}
if (_startPosY + _ShipHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _ShipHeight;
}
}
}
}

View File

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

View File

@ -1,62 +0,0 @@
namespace Liner
{
public partial class FormLiner : Form
{
private DrawingLiner _liner;
public FormLiner()
{
InitializeComponent();
}
private void Draw()
{
Bitmap bmp = new Bitmap(pictureBoxLiner.Width, pictureBoxLiner.Height);
Graphics gr = Graphics.FromImage(bmp);
_liner?.DrawTransport(gr);
pictureBoxLiner.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_liner = new DrawingLiner();
_liner.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_liner.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLiner.Width, pictureBoxLiner.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_liner.Liner?.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_liner.Liner?.Weight}";
toolStripStatusLabelColor.Text = $"Öâåò: {_liner.Liner?.BodyColor.Name}";
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_liner?.MoveTransport(Direction.Up);
break;
case "buttonDown":
_liner?.MoveTransport(Direction.Down);
break;
case "buttonRight":
_liner?.MoveTransport(Direction.Right);
break;
case "buttonLeft":
_liner?.MoveTransport(Direction.Left);
break;
}
Draw();
}
private void pictureBoxLiner_Resize(object sender, EventArgs e)
{
_liner?.ChangeBorders(pictureBoxLiner.Width, pictureBoxLiner.Height);
Draw();
}
}
}

View File

@ -1,6 +1,6 @@
namespace Liner
{
partial class FormLiner
partial class FormShip
{
/// <summary>
/// Required designer variable.
@ -28,7 +28,7 @@
/// </summary>
private void InitializeComponent()
{
this.pictureBoxLiner = new System.Windows.Forms.PictureBox();
this.pictureBoxShip = new System.Windows.Forms.PictureBox();
this.statusStrip = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
@ -38,20 +38,20 @@
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLiner)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit();
this.statusStrip.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxLiner
// pictureBoxShip
//
this.pictureBoxLiner.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxLiner.Location = new System.Drawing.Point(0, 0);
this.pictureBoxLiner.Name = "pictureBoxLiner";
this.pictureBoxLiner.Size = new System.Drawing.Size(800, 424);
this.pictureBoxLiner.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxLiner.TabIndex = 0;
this.pictureBoxLiner.TabStop = false;
this.pictureBoxLiner.Resize += new System.EventHandler(this.pictureBoxLiner_Resize);
this.pictureBoxShip.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxShip.Location = new System.Drawing.Point(0, 0);
this.pictureBoxShip.Name = "pictureBoxShip";
this.pictureBoxShip.Size = new System.Drawing.Size(800, 424);
this.pictureBoxShip.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxShip.TabIndex = 0;
this.pictureBoxShip.TabStop = false;
this.pictureBoxShip.Resize += new System.EventHandler(this.PictureBoxShip_Resize);
//
// statusStrip
//
@ -143,7 +143,7 @@
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// FormLiner
// FormShip
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
@ -153,11 +153,11 @@
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxLiner);
this.Controls.Add(this.pictureBoxShip);
this.Controls.Add(this.statusStrip);
this.Name = "FormLiner";
this.Text = "Лайнер";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLiner)).EndInit();
this.Name = "FormShip";
this.Text = "Корабль";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit();
this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout();
this.ResumeLayout(false);
@ -167,7 +167,7 @@
#endregion
private PictureBox pictureBoxLiner;
private PictureBox pictureBoxShip;
private StatusStrip statusStrip;
private ToolStripStatusLabel toolStripStatusLabelSpeed;
private ToolStripStatusLabel toolStripStatusLabelWeight;

62
Liner/Liner/FormShip.cs Normal file
View File

@ -0,0 +1,62 @@
namespace Liner
{
public partial class FormShip : Form
{
private DrawingShip _ship;
public FormShip()
{
InitializeComponent();
}
private void Draw()
{
Bitmap bmp = new Bitmap(pictureBoxShip.Width, pictureBoxShip.Height);
Graphics gr = Graphics.FromImage(bmp);
_ship?.DrawTransport(gr);
pictureBoxShip.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_ship = new DrawingShip();
_ship.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_ship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxShip.Width, pictureBoxShip.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_ship.Ship?.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_ship.Ship?.Weight}";
toolStripStatusLabelColor.Text = $"Öâåò: {_ship.Ship?.BodyColor.Name}";
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_ship?.MoveTransport(Direction.Up);
break;
case "buttonDown":
_ship?.MoveTransport(Direction.Down);
break;
case "buttonRight":
_ship?.MoveTransport(Direction.Right);
break;
case "buttonLeft":
_ship?.MoveTransport(Direction.Left);
break;
}
Draw();
}
private void PictureBoxShip_Resize(object sender, EventArgs e)
{
_ship?.ChangeBorders(pictureBoxShip.Width, pictureBoxShip.Height);
Draw();
}
}
}

View File

@ -11,7 +11,7 @@ namespace Liner
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormLiner());
Application.Run(new FormShip());
}
}
}