рабочий код

This commit is contained in:
Nikita Potapov 2022-10-12 12:40:12 +04:00
parent 060bf98a1f
commit 827a7457b4
8 changed files with 231 additions and 204 deletions

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
/// <summary>
/// Направление перемещения
/// </summary>
internal enum Direction
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
}
}

View File

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
enum Directions
{
Up,
Down,
Left,
Right
}
}

View File

@ -1,122 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
class DrawCatamaran
{
public EntityCatamaran Catamaran { get; private set; }
private int _StartPosX;
private int _StartPosY;
public int? _PictureWidth = null;
public int? _PictureHeight = null;
private readonly int _CatamaranWidth = 80;
private readonly int _CatamaranHeight = 50;
public void Init(float w, Color c, Directions d, float v)
{
EntityCatamaran entityCatamaran = new EntityCatamaran();
entityCatamaran.Init(w, c, d, v);
Catamaran = entityCatamaran;
}
public void SetPosition(int x, int y, int? width, int height)
{
//todo проверки
_StartPosX = x;
_StartPosY = y;
_PictureWidth = width;
_PictureHeight = height;
}
public void MoveTransport(Directions direction)
{
if (!_PictureWidth.HasValue || !_PictureHeight.HasValue)
{
return;
}
switch (direction)
{
case Directions.Up:
if (_StartPosY - Catamaran.Step > 0)
{
_StartPosY -= (int)Catamaran.Step;
}
break;
case Directions.Down:
if (_StartPosY + _CatamaranHeight + Catamaran.Step <= _PictureHeight)
{
_StartPosY += (int)Catamaran.Step;
}
break;
case Directions.Left:
if (_StartPosX - Catamaran.Step > 0)
{
_StartPosX -= (int)Catamaran.Step;
}
break;
case Directions.Right:
if (_StartPosX + _CatamaranWidth + Catamaran.Step < _PictureWidth)
{
_StartPosX += (int)Catamaran.Step;
}
break;
default:
break;
}
}
public void DrawTransport(Graphics g)
{
int nx = _StartPosX;
int ny = _StartPosY;
// Корпус катамарана
g.DrawLine(Pens.Black, nx, ny, nx + _CatamaranWidth - 10, ny);
g.DrawLine(Pens.Black, nx, ny + _CatamaranHeight, nx + _CatamaranWidth - 10, ny + _CatamaranHeight);
g.DrawLine(Pens.Black, nx, ny, nx, ny + _CatamaranHeight);
g.DrawLine(Pens.Black, nx + _CatamaranWidth, ny + 10, nx + _CatamaranWidth, ny + _CatamaranHeight - 10);
g.DrawLine(Pens.Black, nx + _CatamaranWidth - 10, ny, nx + _CatamaranWidth, ny + 10);
g.DrawLine(Pens.Black, nx + _CatamaranWidth - 10, ny + _CatamaranHeight, nx + _CatamaranWidth, ny + _CatamaranHeight - 10);
//
g.DrawEllipse(Pens.Black, nx + 10, ny + 10, _CatamaranWidth - 20, _CatamaranHeight - 20);
}
public void ChangeBorders(int nwidth, int nheight)
{
_PictureWidth = nwidth;
_PictureHeight = nheight;
if (_PictureWidth <= _CatamaranWidth || _PictureHeight <= _CatamaranHeight)
{
_PictureWidth = null;
_PictureHeight = null;
return;
}
if (_StartPosX + _CatamaranWidth > _PictureWidth)
{
_StartPosX = _PictureWidth.Value - _CatamaranWidth;
}
if (_StartPosY + _CatamaranHeight > _PictureHeight)
{
_StartPosY = _PictureHeight.Value - _CatamaranHeight;
}
}
public string GetStatus()
{
string status = "";
status += "Pos: " + _StartPosX + ", " + _StartPosY;
return status;
}
}
}

View File

@ -0,0 +1,144 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using PIbd_21_Potapov_N.S._Catamaran_Base;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawingCatamaran
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityCatamaran Catamaran { protected set; get; }
/// <summary>
/// Левая координата отрисовки катамарана
/// </summary>
private float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки катамарана
/// </summary>
private float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки катамарана
/// </summary>
private readonly int _catamaranWidth = 100;
/// <summary>
/// Высота отрисовки катамарана
/// </summary>
private readonly int _catamaranHeight = 40;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес катамарана</param>
/// <param name="bodyColor">Цвет корпуса</param>
public void Init(int speed, float weight, Color bodyColor)
{
Catamaran = new EntityCatamaran();
Catamaran.Init(speed, weight, bodyColor);
}
/// <summary>
/// Установка позиции катамарана
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
// TODO проверки
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _catamaranWidth + Catamaran.Step < _pictureWidth)
{
_startPosX += Catamaran.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Catamaran.Step > 0)
{
_startPosX -= Catamaran.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Catamaran.Step > 0)
{
_startPosY -= Catamaran.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _catamaranHeight + Catamaran.Step < _pictureHeight)
{
_startPosY += Catamaran.Step;
}
break;
}
}
/// <summary>
/// Отрисовка катамарана
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
SolidBrush brush = new SolidBrush(Catamaran.BodyColor);
PointF[] bodyPoints = new PointF[5];
bodyPoints[0] = new PointF(_startPosX, _startPosY);
bodyPoints[1] = new PointF(_startPosX + _catamaranWidth - _catamaranWidth / 4, _startPosY);
bodyPoints[2] = new PointF(_startPosX + _catamaranWidth, _startPosY + _catamaranHeight / 2);
bodyPoints[3] = new PointF(_startPosX + _catamaranWidth - _catamaranWidth / 4, _startPosY + _catamaranHeight);
bodyPoints[4] = new PointF(_startPosX, _startPosY + _catamaranHeight);
// Отрисовка корпуса катамарана
g.FillPolygon(brush, bodyPoints);
g.DrawPolygon(Pens.Black, bodyPoints);
// Отрисовка головы катамарана
g.FillEllipse(Brushes.White, _startPosX + _catamaranWidth / 8, _startPosY + _catamaranHeight / 8,
_catamaranWidth / 2, _catamaranHeight - _catamaranHeight / 4);
g.DrawEllipse(Pens.Black, _startPosX + _catamaranWidth / 8, _startPosY + _catamaranHeight / 8,
_catamaranWidth / 2, _catamaranHeight - _catamaranHeight / 4);
}
}
}

View File

@ -7,22 +7,40 @@ using System.Drawing;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
class EntityCatamaran
/// <summary>
/// Класс-сущность "Автомобиль"
/// </summary>
internal class EntityCatamaran
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public float Weight { get; private set; }
public Color EntityColor { get; private set; }
public Directions Direction { get; private set; }
public float Speed { get; private set; }
public float Step { get; private set; }
public void Init(float w, Color c, Directions d, float v)
/// <summary>
/// Цвет корпуса
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения катамарана
/// </summary>
public float Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса катамарана
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public void Init(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = (v <= 0 ? rnd.Next(50, 100) : v);
Weight = (w <= 0 ? rnd.Next(40, 80) : w);
EntityColor = c;
Direction = d;
Step = Speed * 100 / Weight;
Random rnd = new();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
}
}
}

View File

@ -1,7 +1,7 @@

namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
partial class Form
partial class FormCatamaran
{
/// <summary>
/// Required designer variable.
@ -47,11 +47,11 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
//
// btn_new
//
this.btn_new.Location = new System.Drawing.Point(12, 327);
this.btn_new.Location = new System.Drawing.Point(12, 344);
this.btn_new.Name = "btn_new";
this.btn_new.Size = new System.Drawing.Size(75, 60);
this.btn_new.Size = new System.Drawing.Size(75, 63);
this.btn_new.TabIndex = 10;
this.btn_new.Text = "New";
this.btn_new.Text = "Создать";
this.btn_new.Click += new System.EventHandler(this.btn_new_Click);
//
// statusStrip
@ -61,28 +61,28 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor});
this.statusStrip.Location = new System.Drawing.Point(0, 395);
this.statusStrip.Location = new System.Drawing.Point(0, 415);
this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(1074, 24);
this.statusStrip.Size = new System.Drawing.Size(1074, 26);
this.statusStrip.TabIndex = 8;
this.statusStrip.Text = "statusStrip";
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(75, 19);
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(80, 20);
this.toolStripStatusLabelSpeed.Text = "Скорость: ";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(37, 19);
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(40, 20);
this.toolStripStatusLabelWeight.Text = "Вес: ";
//
// toolStripStatusLabelColor
//
this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor";
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(46, 19);
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(49, 20);
this.toolStripStatusLabelColor.Text = "Цвет: ";
//
// groupBox
@ -92,11 +92,11 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
this.groupBox.Controls.Add(this.btn_down);
this.groupBox.Controls.Add(this.btn_left);
this.groupBox.Controls.Add(this.btn_right);
this.groupBox.Location = new System.Drawing.Point(923, 309);
this.groupBox.Location = new System.Drawing.Point(923, 325);
this.groupBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox.Name = "groupBox";
this.groupBox.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox.Size = new System.Drawing.Size(139, 84);
this.groupBox.Size = new System.Drawing.Size(139, 88);
this.groupBox.TabIndex = 9;
this.groupBox.TabStop = false;
//
@ -107,7 +107,7 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
this.btn_up.Location = new System.Drawing.Point(51, 0);
this.btn_up.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_up.Name = "btn_up";
this.btn_up.Size = new System.Drawing.Size(40, 38);
this.btn_up.Size = new System.Drawing.Size(40, 40);
this.btn_up.TabIndex = 1;
this.btn_up.UseVisualStyleBackColor = true;
this.btn_up.Click += new System.EventHandler(this.btn_move_Click);
@ -116,10 +116,10 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
//
this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_down.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_down1;
this.btn_down.Location = new System.Drawing.Point(51, 40);
this.btn_down.Location = new System.Drawing.Point(51, 42);
this.btn_down.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_down.Name = "btn_down";
this.btn_down.Size = new System.Drawing.Size(40, 38);
this.btn_down.Size = new System.Drawing.Size(40, 40);
this.btn_down.TabIndex = 2;
this.btn_down.Text = " ";
this.btn_down.UseVisualStyleBackColor = true;
@ -129,10 +129,10 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
//
this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_left.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_left1;
this.btn_left.Location = new System.Drawing.Point(5, 40);
this.btn_left.Location = new System.Drawing.Point(5, 42);
this.btn_left.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_left.Name = "btn_left";
this.btn_left.Size = new System.Drawing.Size(40, 38);
this.btn_left.Size = new System.Drawing.Size(40, 40);
this.btn_left.TabIndex = 3;
this.btn_left.UseVisualStyleBackColor = true;
this.btn_left.Click += new System.EventHandler(this.btn_move_Click);
@ -141,10 +141,10 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
//
this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_right.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_right1;
this.btn_right.Location = new System.Drawing.Point(97, 42);
this.btn_right.Location = new System.Drawing.Point(97, 44);
this.btn_right.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_right.Name = "btn_right";
this.btn_right.Size = new System.Drawing.Size(40, 38);
this.btn_right.Size = new System.Drawing.Size(40, 40);
this.btn_right.TabIndex = 4;
this.btn_right.UseVisualStyleBackColor = true;
this.btn_right.Click += new System.EventHandler(this.btn_move_Click);
@ -155,23 +155,23 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(1074, 419);
this.pictureBox.Size = new System.Drawing.Size(1074, 441);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// Form
// FormCatamaran
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 19F);
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1074, 419);
this.ClientSize = new System.Drawing.Size(1074, 441);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.btn_new);
this.Controls.Add(this.statusStrip);
this.Controls.Add(this.pictureBox);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "Form";
this.Text = "CatamaranTest";
this.Name = "FormCatamaran";
this.Text = "Катамаран";
this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout();
this.groupBox.ResumeLayout(false);

View File

@ -10,11 +10,11 @@ using System.Windows.Forms;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
public partial class Form : System.Windows.Forms.Form
public partial class FormCatamaran : System.Windows.Forms.Form
{
DrawCatamaran catamaranDrawObj;
DrawingCatamaran catamaranDrawObj;
public Form()
public FormCatamaran()
{
InitializeComponent();
pictureBox.Image = new Bitmap(pictureBox.Width, pictureBox.Height);
@ -22,14 +22,14 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
private void btn_new_Click(object sender, EventArgs e)
{
Random rnd = new Random();
Random rnd = new Random();
catamaranDrawObj = new DrawCatamaran();
catamaranDrawObj.Init(rnd.Next(50, 100), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), Directions.Right, 20);
catamaranDrawObj.SetPosition(rnd.Next(10, 50), rnd.Next(10, 50), pictureBox.Width, pictureBox.Height);
catamaranDrawObj = new DrawingCatamaran();
catamaranDrawObj.Init(rnd.Next(50, 100), rnd.Next(50, 100), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)));
catamaranDrawObj.SetPosition(rnd.Next(10, 50), rnd.Next(10, 50), pictureBox.Width, pictureBox.Height - statusStrip.Height);
toolStripStatusLabelSpeed.Text = $"Скорость: {catamaranDrawObj.Catamaran.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {catamaranDrawObj.Catamaran.Weight}";
toolStripStatusLabelColor.Text = $"Цвет: {catamaranDrawObj.Catamaran.EntityColor.Name}";
toolStripStatusLabelColor.Text = $"Цвет: {catamaranDrawObj.Catamaran.BodyColor.Name}";
RedrawCatamaran();
}
@ -44,22 +44,22 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
case "btn_up":
{
catamaranDrawObj.MoveTransport(Directions.Up);
catamaranDrawObj.MoveTransport(Direction.Up);
}
break;
case "btn_down":
{
catamaranDrawObj.MoveTransport(Directions.Down);
catamaranDrawObj.MoveTransport(Direction.Down);
}
break;
case "btn_left":
{
catamaranDrawObj.MoveTransport(Directions.Left);
catamaranDrawObj.MoveTransport(Direction.Left);
}
break;
case "btn_right":
{
catamaranDrawObj.MoveTransport(Directions.Right);
catamaranDrawObj.MoveTransport(Direction.Right);
}
break;
default:
@ -75,21 +75,5 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
catamaranDrawObj.DrawTransport(g);
pictureBox.Invalidate();
}
private void UpdateStatus()
{
if (catamaranDrawObj == null)
return;
}
private void form_Resize(object sender, EventArgs e)
{
int oldWidth = pictureBox.Width;
int oldHeight = pictureBox.Height;
}
}
}

View File

@ -17,7 +17,7 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
Application.EnableVisualStyles();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form());
Application.Run(new FormCatamaran());
}
}
}