Отрисовка

This commit is contained in:
Данила Селяев 2022-09-25 20:58:24 +04:00
parent dac3253be4
commit c5b3864190
5 changed files with 161 additions and 20 deletions

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class DrawningFastLoco : DrawningLocomative
{
public DrawningFastLoco(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool rockettoobe) :
base(speed, weight, bodyColor, 110, 60)
{
Locomative = new EntityFastLocomative(speed, weight, bodyColor, dopColor, bodyKit, wing, rockettoobe);
}
public override void DrawTransport(Graphics g)
{
if(Locomative is not EntityFastLocomative fastLocomative)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(fastLocomative.BodyColor);
Brush dopBrush = new SolidBrush(fastLocomative.DopColor);
if (fastLocomative.BodyKit)
{
//кисти
Brush brGray = new SolidBrush(Color.Gray);
Brush brBlack = new SolidBrush(Color.Black);
//Платформа поезда
g.FillRectangle(brBlack, _startPosX, _startPosY, 90, 40);
//котёл
g.FillRectangle(dopBrush, _startPosX + 30, _startPosY + 10, 60, 20);
g.DrawLine(pen, _startPosX + 30, _startPosY + 10, _startPosX + 70, _startPosY + 10);
g.DrawLine(pen, _startPosX + 70, _startPosY + 10, _startPosX + 70, _startPosY + 30);
g.DrawLine(pen, _startPosX + 70, _startPosY + 30, _startPosX + 30, _startPosY + 30);
g.DrawLine(pen, _startPosX + 30, _startPosY + 30, _startPosX + 30, _startPosY + 10);
//крыша поезда
g.FillRectangle(bodyBrush, _startPosX - 2, _startPosY - 2, 34, 44);
g.DrawLine(pen, _startPosX - 2, _startPosY - 2, _startPosX + 32, _startPosY - 2);
g.DrawLine(pen, _startPosX + 32, _startPosY - 2, _startPosX + 32, _startPosY + 42);
g.DrawLine(pen, _startPosX + 32, _startPosY + 42, _startPosX - 2, _startPosY + 42);
g.DrawLine(pen, _startPosX - 2, _startPosY + 42, _startPosX - 2, _startPosY - 2);
//трубы
g.FillEllipse(brGray, _startPosX + 36, _startPosY + 16, 8, 8);
g.FillEllipse(brBlack, _startPosX + 35, _startPosY + 15, 10, 10);
g.FillEllipse(brGray, _startPosX + 56, _startPosY + 16, 8, 8);
g.FillEllipse(brBlack, _startPosX + 55, _startPosY + 15, 10, 10);
g.FillEllipse(brGray, _startPosX + 70, _startPosY + 10, 20, 20);
g.FillEllipse(brBlack, _startPosX + 73, _startPosY + 13, 14, 14);
//бампер
Point point1 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY));
Point point2 = new Point(Convert.ToInt32(_startPosX + 110), Convert.ToInt32(_startPosY + 20));
Point point3 = new Point(Convert.ToInt32(_startPosX + 90), Convert.ToInt32(_startPosY + 40));
Point[] points = { point1, point2, point3 };
g.FillPolygon(brGray, points);
}
//ракетная труба сзади
if (fastLocomative.RocketToobe)
{
Brush brGray = new SolidBrush(Color.Black);
g.FillRectangle(brGray, _startPosX-20, _startPosY, 20, 40);
}
//бамперы слева и справа
if (fastLocomative.RocketToobe)
{
//слева
g.DrawLine(pen, _startPosX, _startPosY-5, _startPosX + 80, _startPosY-5);
g.DrawLine(pen, _startPosX+10, _startPosY - 5, _startPosX+10, _startPosY -2);
g.DrawLine(pen, _startPosX+70, _startPosY - 5, _startPosX + 70, _startPosY);
//справа
g.DrawLine(pen, _startPosX, _startPosY+45, _startPosX + 80, _startPosY+45);
g.DrawLine(pen, _startPosX + 10, _startPosY + 45, _startPosX + 10, _startPosY + 42);
g.DrawLine(pen, _startPosX + 70, _startPosY + 45, _startPosX + 70, _startPosY + 40);
}
}
}
}

View File

@ -8,28 +8,34 @@ namespace Locomative
{
internal class DrawningLocomative
{
public EntityLocomative Locomative { get; private set; }
private float _startPosX;
private float _startPosY;
public EntityLocomative Locomative { get; protected set; }
protected float _startPosX;
protected float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
protected readonly int _locomativeWidth = 110;
protected readonly int _locomativeHeight = 40;
protected readonly int _locoWidth = 100;
protected readonly int _locoHeight = 40;
public DrawningLocomative(int speed, float weight, Color bodyColor)
{
Locomative = new EntityLocomative(speed, weight, bodyColor);
}
protected DrawningLocomative(int speed, float weight, Color bodyColor, int locoWidth, int locoHeight)
: this(speed, weight, bodyColor)
{
_locoHeight = locoHeight;
_locoWidth = locoWidth;
}
public void SetPosition(int x, int y, int width, int height)
{
Random rnd1 = new();
if (width < 120 || height < 50) return;
if (x > width - _locomativeWidth)
if (x > width - _locoWidth)
{
x = rnd1.Next(10, width - _locomativeWidth - 1);
x = rnd1.Next(10, width - _locoWidth - 1);
}
if( y > height- _locomativeHeight)
if( y > height- _locoHeight)
{
y = rnd1.Next(10, height - _locomativeHeight - 1);
y = rnd1.Next(10, height - _locoHeight - 1);
}
_startPosX = x;
_startPosY = y;
@ -41,7 +47,7 @@ namespace Locomative
switch (direction)
{
case Direction.Right:
if (_startPosX + _locomativeWidth + Locomative.Step < _pictureWidth)
if (_startPosX + _locoWidth + Locomative.Step < _pictureWidth)
{
_startPosX += Locomative.Step;
}
@ -59,14 +65,14 @@ namespace Locomative
}
break;
case Direction.Down:
if (_startPosY + _locomativeHeight + Locomative.Step < _pictureHeight)
if (_startPosY + _locoHeight + Locomative.Step < _pictureHeight)
{
_startPosY += Locomative.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX <= 0 || _startPosY <= 0 || !_pictureWidth.HasValue || !_pictureHeight.HasValue) return;
//кисти
@ -107,19 +113,19 @@ namespace Locomative
{
_pictureWidth = width;
_pictureHeight = height;
if(_pictureWidth<=_locomativeWidth || _pictureHeight <= _locomativeHeight)
if(_pictureWidth<=_locoWidth || _pictureHeight <= _locoHeight)
{
_pictureHeight = null;
_pictureWidth = null;
return;
}
if (_startPosX + _locomativeWidth > _pictureWidth)
if (_startPosX + _locoWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _locomativeWidth;
_startPosX = _pictureWidth.Value - _locoWidth;
}
if (_startPosY + _locomativeHeight > _pictureHeight)
if (_startPosY + _locoHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _locomativeHeight;
_startPosY = _pictureHeight.Value - _locoHeight;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomative
{
internal class EntityFastLocomative : EntityLocomative
{
public Color DopColor { get; private set; }
public bool Wing { get; private set; }
public bool BodyKit { get; private set; }
public bool RocketToobe { get; private set; }
public EntityFastLocomative(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool rockettoobe) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
BodyKit = bodyKit;
Wing = wing;
RocketToobe = rockettoobe;
}
}
}

View File

@ -38,6 +38,7 @@
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.ButtonFastCreate = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomative)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
@ -142,11 +143,23 @@
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
//
// ButtonFastCreate
//
this.ButtonFastCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonFastCreate.Location = new System.Drawing.Point(101, 393);
this.ButtonFastCreate.Name = "ButtonFastCreate";
this.ButtonFastCreate.Size = new System.Drawing.Size(97, 23);
this.ButtonFastCreate.TabIndex = 7;
this.ButtonFastCreate.Text = "Модификация";
this.ButtonFastCreate.UseVisualStyleBackColor = true;
this.ButtonFastCreate.Click += new System.EventHandler(this.ButtonFastCreate_Click);
//
// LocomativeForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.ButtonFastCreate);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonСreate);
this.Controls.Add(this.buttonRight);
@ -177,5 +190,6 @@
private Button buttonLeft;
private Button buttonRight;
private Button buttonUp;
private Button ButtonFastCreate;
}
}

View File

@ -14,6 +14,14 @@ namespace Locomative
_loco?.DrawTransport(gr);
pictureBoxLocomative.Image = bmp;
}
public void SetData()
{
Random rnd = new();
_loco.SetPosition(rnd.Next(10, 300), rnd.Next(10, 300), pictureBoxLocomative.Width, pictureBoxLocomative.Height);
toolStripStatusLabelColor.Text = $"Öâåò:{_loco.Locomative?.BodyColor.Name}";
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü:{_loco.Locomative?.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ:{_loco.Locomative?.Weight}";
}
private void LocomativeForm_Load(object sender, EventArgs e)
{
@ -24,9 +32,7 @@ namespace Locomative
Random rnd = new();
_loco = new DrawningLocomative(rnd.Next(1, 200), rnd.Next(10, 300), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_loco.SetPosition(rnd.Next(10, 300), rnd.Next(10, 300), pictureBoxLocomative.Width, pictureBoxLocomative.Height);
toolStripStatusLabelColor.Text = $"Öâåò:{_loco.Locomative?.BodyColor.Name}";
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü:{_loco.Locomative?.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ:{_loco.Locomative?.Weight}";
SetData();
Draw();
}
@ -66,5 +72,16 @@ namespace Locomative
_loco?.ChangeBorders(pictureBoxLocomative.Width, pictureBoxLocomative.Height);
Draw();
}
private void ButtonFastCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_loco = new DrawningFastLoco(rnd.Next(1, 200), rnd.Next(100, 300),
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)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData();
Draw();
}
}
}