Compare commits

..

2 Commits

Author SHA1 Message Date
KirillFirsof
0d0d57578e Доработка формы 2023-10-12 21:25:26 +04:00
KirillFirsof
8c7e0c3e99 Создание классов и формы 2023-10-12 21:23:15 +04:00
8 changed files with 441 additions and 2 deletions

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RPP_FirstLaba_Tractor
{
public enum DirectionType
{
/// <summary>
/// /// Вверх
/// /// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}
}

View File

@ -0,0 +1,180 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RPP_FirstLaba_Tractor
{
internal class DrawningTractor
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTractor? EntityTractor { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
private int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int _startPosY;
/// <summary>
/// Ширина прорисовки автомобиля
/// </summary>
private readonly int _tractorWidth = 80;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _tractorHeight = 60;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="wing">Признак наличия антикрыла</param>
/// <param name="sportLine">Признак наличия гоночной полосы</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_tractorWidth > _pictureHeight || _tractorHeight > _pictureHeight)
{
_pictureWidth = 2 * _tractorWidth;
_pictureHeight = 2 * _tractorHeight;
}
EntityTractor = new EntityTractor();
EntityTractor.Init(speed, weight, bodyColor);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
if ((x + _tractorWidth > _pictureWidth) || (y + _pictureHeight > _pictureHeight))
{
Random random = new();
x = random.Next(0, _pictureWidth);
x = random.Next(0, _pictureHeight);
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
///
public void MoveTransport(DirectionType direction)
{
if (EntityTractor == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityTractor.Step > 0)
{
_startPosX -= (int)EntityTractor.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityTractor.Step > 0)
{
_startPosY -= (int)EntityTractor.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + _tractorWidth + EntityTractor.Step < _pictureWidth)
{
_startPosX += (int)EntityTractor.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + _tractorHeight + EntityTractor.Step < _pictureHeight)
{
_startPosY += (int)EntityTractor.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityTractor == null)
{
return;
}
Pen pen = new(Color.Black);
Brush brGray = new SolidBrush(Color.Gray);
Brush brBlack = new SolidBrush(Color.Black);
Brush additionalBrush = new
SolidBrush(EntityTractor.BodyColor);
//Гусеницы
g.FillEllipse(brGray, _startPosX, _startPosY + 41, 25, 25);
g.FillEllipse(brGray, _startPosX + 55, _startPosY + 41, 25, 25);
g.FillRectangle(brGray, _startPosX + 13, _startPosY + 41, 54, 25);
//колеса
g.FillEllipse(brBlack, _startPosX, _startPosY + 45, 15, 15);
g.FillEllipse(brBlack, _startPosX + 65, _startPosY + 45, 15, 15);
g.FillEllipse(brBlack, _startPosX + 35, _startPosY + 55, 10, 10);
g.FillEllipse(brBlack, _startPosX + 20, _startPosY + 55, 10, 10);
g.FillEllipse(brBlack, _startPosX + 50, _startPosY + 55, 10, 10);
g.FillEllipse(brBlack, _startPosX + 25, _startPosY + 40, 10, 10);
g.FillEllipse(brBlack, _startPosX + 45, _startPosY + 40, 10, 10);
//кузов
g.FillRectangle(additionalBrush, _startPosX, _startPosY + 20, 80, 20);
g.FillRectangle(additionalBrush, _startPosX + 60, _startPosY, 10, 20);
g.FillRectangle(additionalBrush, _startPosX, _startPosY, 40, 20);
//Окно
Brush brBlue = new SolidBrush(Color.Blue);
g.FillRectangle(brBlue, _startPosX + 10, _startPosY + 3, 25, 15);
//Колеса
g.FillEllipse(additionalBrush, _startPosX + 2, _startPosY + 47, 11, 11);
g.FillEllipse(additionalBrush, _startPosX + 67, _startPosY + 47, 11, 11);
g.FillEllipse(additionalBrush, _startPosX + 37, _startPosY + 57, 6, 6);
g.FillEllipse(additionalBrush, _startPosX + 22, _startPosY + 57, 6, 6);
g.FillEllipse(additionalBrush, _startPosX + 52, _startPosY + 57, 6, 6);
g.FillEllipse(additionalBrush, _startPosX + 27, _startPosY + 42, 6, 6);
g.FillEllipse(additionalBrush, _startPosX + 47, _startPosY + 42, 6, 6);
//границы трактора
g.DrawRectangle(pen, _startPosX, _startPosY + 20, 80, 20);
g.DrawRectangle(pen, _startPosX + 60, _startPosY, 10, 20);
g.DrawRectangle(pen, _startPosX, _startPosY, 40, 20);
}
}
}

View File

@ -1,9 +1,196 @@
namespace RPP_FirstLaba_Tractor
{
/// <summary>
/// Ôîðìà ðàáîòû ñ îáúåêòîì "Ñïîðòèâíûé àâòîìîáèëü"
/// </summary>
public partial class FormTractor : Form
{
private PictureBox pictureBoxTractor;
private Button buttonCreate;
private Button buttonTop;
private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
public int getPictureWidth()
{
return pictureBoxTractor.Width;
}
public int getPictureHeight()
{
return pictureBoxTractor.Height;
}
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawningTractor? _drawningTractor;
/// <summary>
/// Èíèöèàëèçàöèÿ ôîðìû
/// </summary>
public FormTractor()
{
InitializeComponent();
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ìàøèíû
/// </summary>
private void Draw()
{
if (_drawningTractor == null)
{
return;
}
Bitmap bmp = new(pictureBoxTractor.Width,
pictureBoxTractor.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTractor.DrawTransport(gr);
pictureBoxTractor.Image = bmp;
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningTractor = new DrawningTractor();
_drawningTractor.Init(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxTractor.Width, pictureBoxTractor.Height);
_drawningTractor.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
}
/// <summary>
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningTractor == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonTop":
_drawningTractor.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningTractor.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningTractor.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningTractor.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void InitializeComponent()
{
this.pictureBoxTractor = new System.Windows.Forms.PictureBox();
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonTop = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxTractor)).BeginInit();
this.SuspendLayout();
//
// pictureBoxTractor
//
this.pictureBoxTractor.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxTractor.Location = new System.Drawing.Point(0, 0);
this.pictureBoxTractor.Name = "pictureBoxTractor";
this.pictureBoxTractor.Size = new System.Drawing.Size(891, 490);
this.pictureBoxTractor.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxTractor.TabIndex = 0;
this.pictureBoxTractor.TabStop = false;
//
// buttonCreate
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(12, 440);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(94, 29);
this.buttonCreate.TabIndex = 1;
this.buttonCreate.Text = "Ñîçäàòü";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
//
// buttonTop
//
this.buttonTop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonTop.BackgroundImage = global::RPP_FirstLaba_Tractor.Properties.Resources.arrowUp;
this.buttonTop.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonTop.Location = new System.Drawing.Point(813, 412);
this.buttonTop.Name = "buttonTop";
this.buttonTop.Size = new System.Drawing.Size(30, 30);
this.buttonTop.TabIndex = 2;
this.buttonTop.UseVisualStyleBackColor = true;
this.buttonTop.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::RPP_FirstLaba_Tractor.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(777, 448);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 3;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::RPP_FirstLaba_Tractor.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(813, 448);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 4;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::RPP_FirstLaba_Tractor.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(849, 448);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// FormTractor
//
this.ClientSize = new System.Drawing.Size(891, 490);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonTop);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.pictureBoxTractor);
this.Name = "FormTractor";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxTractor)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RPP_FirstLaba_Tractor
{
public class EntityTractor
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="wing">Признак наличия антикрыла</param>
/// <param name="sportLine">Признак наличия гоночной полосы</param>
public void Init(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB