Лаборатонрная№1 правки 2

This commit is contained in:
Garifullin-Farid 2024-03-12 10:11:39 +04:00
parent 2fd3a6f979
commit 57e736cf99
7 changed files with 139 additions and 136 deletions

View File

@ -3,23 +3,23 @@
/// <summary>
/// направление перемещенния
/// </summary>
public enum DicretionType
public enum DirectionType
{
/// <summary>
/// Вверх
/// </summary>
Up= 1,
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down= 2,
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left =3,
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right=4,
Right = 4,
}
}

View File

@ -2,12 +2,12 @@
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningTank
public class DrawningBattleTank
{
/// <summary>
/// класс - сущность
/// </summary>
public EntityTank? EntityTank { get; private set; }
public EntityBattleTank? EntityBattleTank { get; private set; }
/// <summary>
/// ширина окна
@ -48,9 +48,9 @@ public class DrawningTank
/// <param name="additionalColor"> Дополнительный цвет</param>
/// <param name="gun"> орудие</param>
/// <param name="machinGun"> пулемет на башне</param>
public void Init(EntityTank entityTank)
public void Init(EntityBattleTank entityBattleTank)
{
EntityTank = entityTank;
EntityBattleTank = entityBattleTank;
_pictureHeight = null;
_pictureWidth = null;
_startPosX = null;
@ -66,15 +66,16 @@ public class DrawningTank
/// <returns>true - границы заданы, false - проверка не пройдена , нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (height < _drawingTankHeight || width < _drawningTankWidth)
{
return false;
}
_pictureHeight = height;
_pictureWidth = width;
if(_startPosX != null && _startPosY!=null) {
if (_startPosX < 0) _startPosX=0;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX < 0) _startPosX = 0;
if (_startPosY < 0) _startPosY = 0;
if (_pictureHeight.Value < (_startPosY + _drawingTankHeight))
{
@ -85,15 +86,16 @@ public class DrawningTank
_startPosX = _pictureWidth - _drawningTankWidth;
}
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y){
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
@ -102,8 +104,9 @@ public class DrawningTank
_startPosY = y;
if (_startPosX < 0) _startPosX = 0;
if (_startPosY < 0) _startPosY = 0;
if (_pictureHeight.Value<(_startPosY + _drawingTankHeight)){
_startPosY = _pictureHeight-_drawingTankHeight;
if (_pictureHeight.Value < (_startPosY + _drawingTankHeight))
{
_startPosY = _pictureHeight - _drawingTankHeight;
}
if (_pictureWidth.Value < (_startPosX + _drawningTankWidth))
{
@ -116,76 +119,78 @@ public class DrawningTank
/// </summary>
/// <param name="dicretion">направление</param>
/// <returns></returns>
public bool MoveTransport(DicretionType dicretion){
if(EntityTank == null || _startPosX == null || _startPosY == null)
public bool MoveTransport(DirectionType dicretion)
{
if (EntityBattleTank == null || _startPosX == null || _startPosY == null)
{
return false;
}
switch(dicretion)
switch (dicretion)
{
//влево
case DicretionType.Left:
if(_startPosX.Value - EntityTank.Step > 0)
case DirectionType.Left:
if (_startPosX.Value - EntityBattleTank.Step > 0)
{
_startPosX -= (int)EntityTank.Step;
_startPosX -= (int)EntityBattleTank.Step;
}
return true;
//вправо
case DicretionType.Right:
if (_startPosX.Value+ _drawningTankWidth + EntityTank.Step < _pictureWidth)
case DirectionType.Right:
if (_startPosX.Value + _drawningTankWidth + EntityBattleTank.Step < _pictureWidth)
{
_startPosX += (int)EntityTank.Step;
_startPosX += (int)EntityBattleTank.Step;
}
return true;
//вверх
case DicretionType.Up:
if(_startPosY.Value - EntityTank.Step > 0)
case DirectionType.Up:
if (_startPosY.Value - EntityBattleTank.Step > 0)
{
_startPosY -= (int)EntityTank.Step;
_startPosY -= (int)EntityBattleTank.Step;
}
return true;
//вниз
case DicretionType.Down:
if(_startPosY + EntityTank.Step+ _drawingTankHeight < _pictureHeight)
case DirectionType.Down:
if (_startPosY + EntityBattleTank.Step + _drawingTankHeight < _pictureHeight)
{
_startPosY += (int)EntityTank.Step;
_startPosY += (int)EntityBattleTank.Step;
}
return true;
default:
default:
return false;
}
}
/// <summary>
/// прорисовка танка
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if(EntityTank == null || !_startPosX.HasValue || !_startPosY.HasValue) {
if (EntityBattleTank == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush addititionalBrush = new SolidBrush(EntityTank.AdditionalColor);
Brush Br = new SolidBrush(EntityTank.BodyColor);
Brush addititionalBrush = new SolidBrush(EntityBattleTank.AdditionalColor);
Brush Br = new SolidBrush(EntityBattleTank.BodyColor);
//основная часть танка
g.DrawRectangle(pen, _startPosX.Value+6, _startPosY.Value+40, 135, 20);
g.DrawRectangle(pen, _startPosX.Value+45, _startPosY.Value+20, 55, 20);
g.DrawRectangle(pen, _startPosX.Value + 6, _startPosY.Value + 40, 135, 20);
g.DrawRectangle(pen, _startPosX.Value + 45, _startPosY.Value + 20, 55, 20);
g.FillRectangle(Br, _startPosX.Value + 6, _startPosY.Value + 40, 135, 20);
g.FillRectangle(Br, _startPosX.Value + 45, _startPosY.Value + 20, 55, 20);
//гусеницы
g.DrawArc(pen, _startPosX.Value, _startPosY.Value + 60, 15,30,100,170);
g.DrawArc(pen, _startPosX.Value+135, _startPosY.Value + 60, 15, 30, -90, 170);
g.DrawLine(pen, _startPosX.Value+5, _startPosY.Value + 60, _startPosX.Value+145, _startPosY.Value + 60);
g.DrawLine(pen, _startPosX.Value+5 , _startPosY.Value + 90, _startPosX.Value + 145, _startPosY.Value + 90);
g.DrawArc(pen, _startPosX.Value, _startPosY.Value + 60, 15, 30, 100, 170);
g.DrawArc(pen, _startPosX.Value + 135, _startPosY.Value + 60, 15, 30, -90, 170);
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 60, _startPosX.Value + 145, _startPosY.Value + 60);
g.DrawLine(pen, _startPosX.Value + 5, _startPosY.Value + 90, _startPosX.Value + 145, _startPosY.Value + 90);
Pen WidePen = new(Color.Black);
WidePen.Width = 2;
g.DrawEllipse(WidePen, _startPosX.Value, _startPosY.Value+60, 24, 24);
g.DrawEllipse(WidePen, _startPosX.Value +125, _startPosY.Value + 60, 24, 24);
g.DrawEllipse(WidePen, _startPosX.Value, _startPosY.Value + 60, 24, 24);
g.DrawEllipse(WidePen, _startPosX.Value + 125, _startPosY.Value + 60, 24, 24);
//катки
g.DrawEllipse(pen, _startPosX.Value + 26, _startPosY.Value + 70, 20, 20);
@ -199,16 +204,16 @@ public class DrawningTank
g.DrawEllipse(pen, _startPosX.Value + 101, _startPosY.Value + 60, 5, 5);
//пушка
Brush AdditionalBrush = new SolidBrush(EntityTank.AdditionalColor);
if (EntityTank.Gun)
{
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value +30, 45, 5);
Brush AdditionalBrush = new SolidBrush(EntityBattleTank.AdditionalColor);
if (EntityBattleTank.Gun)
{
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 30, 45, 5);
g.FillRectangle(AdditionalBrush, _startPosX.Value, _startPosY.Value +30, 45, 5);
g.FillRectangle(AdditionalBrush, _startPosX.Value, _startPosY.Value + 30, 45, 5);
}
//пулемет
if (EntityTank.MachinGun)
if (EntityBattleTank.MachinGun)
{
g.DrawRectangle(pen, _startPosX.Value + 65, _startPosY.Value + 15, 16, 5);
g.DrawRectangle(pen, _startPosX.Value + 72, _startPosY.Value + 10, 5, 5);

View File

@ -3,7 +3,7 @@
/// <summary>
/// Класс-сущность танк
/// </summary>
public class EntityTank
public class EntityBattleTank
{
/// <summary>
/// скорость
@ -32,7 +32,7 @@
/// <summary>
/// Признак (опция) наличия пулемета
/// </summary>
public bool MachinGun { get; private set; }
public bool MachinGun { get; private set; }
/// <summary>
/// шаг
/// </summary>
@ -46,7 +46,7 @@
/// <param name="additionalColor"> Дополнительный цвет</param>
/// <param name="gun"> орудие</param>
/// <param name="machinGun"> пулемет на башне</param>
public void Init(int speed, double weight,Color bodyColor , Color additionalColor, bool gun, bool machinGun)
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool gun, bool machinGun)
{
Speed = speed;
Weight = weight;

View File

@ -1,6 +1,6 @@
namespace ProjectTank
{
partial class FormTank
partial class FormBattleTank
{
/// <summary>
/// Required designer variable.
@ -28,45 +28,45 @@
/// </summary>
private void InitializeComponent()
{
pictureBoxTank = new PictureBox();
ButtonCreate = new Button();
pictureBoxBattleTank = new PictureBox();
Создать = new Button();
buttonLeft = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonRight = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit();
buttonDown = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBattleTank).BeginInit();
SuspendLayout();
//
// pictureBoxTank
// pictureBoxBattleTank
//
pictureBoxTank.Dock = DockStyle.Fill;
pictureBoxTank.Location = new Point(0, 0);
pictureBoxTank.Name = "pictureBoxTank";
pictureBoxTank.Size = new Size(941, 462);
pictureBoxTank.TabIndex = 0;
pictureBoxTank.TabStop = false;
pictureBoxBattleTank.Dock = DockStyle.Fill;
pictureBoxBattleTank.Location = new Point(0, 0);
pictureBoxBattleTank.Name = "pictureBoxBattleTank";
pictureBoxBattleTank.Size = new Size(831, 478);
pictureBoxBattleTank.TabIndex = 0;
pictureBoxBattleTank.TabStop = false;
pictureBoxBattleTank.Click += ButtonMove_Click;
//
// ButtonCreate
// Создать
//
ButtonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreate.Location = new Point(12, 427);
ButtonCreate.Name = "ButtonCreate";
ButtonCreate.Size = new Size(75, 23);
ButtonCreate.TabIndex = 1;
ButtonCreate.Text = "создать";
ButtonCreate.UseVisualStyleBackColor = true;
ButtonCreate.Click += ButtonCreate_Click;
Создать.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
Создать.Location = new Point(12, 443);
Создать.Name = "Создать";
Создать.Size = new Size(75, 23);
Создать.TabIndex = 1;
Создать.Text = "Создать";
Создать.UseVisualStyleBackColor = true;
Создать.Click += ButtonCreate_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(786, 421);
buttonLeft.Location = new Point(672, 426);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(35, 35);
buttonLeft.Size = new Size(40, 40);
buttonLeft.TabIndex = 2;
buttonLeft.UseCompatibleTextRendering = true;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
@ -75,64 +75,61 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(827, 380);
buttonUp.Location = new Point(718, 380);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(35, 35);
buttonUp.Size = new Size(40, 40);
buttonUp.TabIndex = 3;
buttonUp.UseCompatibleTextRendering = true;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight_;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(764, 426);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(40, 40);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonDown
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(827, 421);
buttonDown.Location = new Point(718, 426);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 4;
buttonDown.UseCompatibleTextRendering = true;
buttonDown.Size = new Size(40, 40);
buttonDown.TabIndex = 5;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight_;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(868, 421);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 5;
buttonRight.UseCompatibleTextRendering = true;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// FormTank
// FormBattleTank
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(941, 462);
Controls.Add(buttonRight);
ClientSize = new Size(831, 478);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(ButtonCreate);
Controls.Add(pictureBoxTank);
Name = "FormTank";
Text = "Танк";
((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit();
Controls.Add(Создать);
Controls.Add(pictureBoxBattleTank);
Name = "FormBattleTank";
Text = "FormBattleTank";
((System.ComponentModel.ISupportInitialize)pictureBoxBattleTank).EndInit();
ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxTank;
private Button ButtonCreate;
private PictureBox pictureBoxBattleTank;
private Button Создать;
private Button buttonLeft;
private Button buttonUp;
private Button buttonDown;
private Button buttonRight;
private Button buttonDown;
}
}

View File

@ -3,38 +3,39 @@
/// <summary>
/// Форма работы с объектом "Танк"
/// </summary>
public partial class FormTank : Form
public partial class FormBattleTank : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningTank? _drawningTank;
private DrawningBattleTank? _drawningBattleTank;
/// <summary>
/// класс - сущность
/// </summary>
private EntityTank? _entityTank;
private EntityBattleTank? _entityBattleTank;
/// <summary>
/// Конструктор формы
/// </summary>
public FormTank()
public FormBattleTank()
{
InitializeComponent();
}
/// <summary>
/// Метод прорисовки танка
/// Метод прорисовки танкаx
/// </summary>
private void Draw()
{
if (_drawningTank == null)
if (_drawningBattleTank == null)
{
return;
}
Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height);
Bitmap bmp = new(pictureBoxBattleTank.Width, pictureBoxBattleTank.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTank.DrawTransport(gr);
pictureBoxTank.Image = bmp;
_drawningBattleTank.DrawTransport(gr);
pictureBoxBattleTank.Image = bmp;
}
/// <summary>
@ -45,15 +46,15 @@
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawningTank = new DrawningTank();
_entityTank = new EntityTank();
_entityTank.Init(random.Next(100, 300), random.Next(1000, 3000),
_drawningBattleTank = new DrawningBattleTank();
_entityBattleTank = new EntityBattleTank();
_entityBattleTank.Init(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawningTank.Init(_entityTank);
_drawningTank.SetPictureSize(pictureBoxTank.Width, pictureBoxTank.Height);
_drawningTank.SetPosition(random.Next(10,100),random.Next(10,100));
_drawningBattleTank.Init(_entityBattleTank);
_drawningBattleTank.SetPictureSize(pictureBoxBattleTank.Width, pictureBoxBattleTank.Height);
_drawningBattleTank.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
@ -63,7 +64,7 @@
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if(_drawningTank == null)
if (_drawningBattleTank == null)
{
return;
}
@ -72,16 +73,16 @@
switch (name)
{
case "buttonUp":
result = _drawningTank.MoveTransport(DicretionType.Up);
result = _drawningBattleTank.MoveTransport(DirectionType.Up);
break;
case "buttonLeft":
result = _drawningTank.MoveTransport(DicretionType.Left);
result = _drawningBattleTank.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTank.MoveTransport(DicretionType.Right);
result = _drawningBattleTank.MoveTransport(DirectionType.Right);
break;
case "buttonDown":
result = _drawningTank.MoveTransport(DicretionType.Down);
result = _drawningBattleTank.MoveTransport(DirectionType.Down);
break;
}
if (result)

View File

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