Заполнение класса DrawningAirBomber

This commit is contained in:
malimova 2023-10-01 23:21:45 +04:00
parent ff4b2f7e71
commit c9489b66f7
4 changed files with 208 additions and 125 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -22,30 +23,23 @@ namespace AirBomber
/// <summary>
/// Ширина прорисовки бомбардировщика
/// </summary>
private readonly int _bomberWidth = 110;
private readonly int _bomberWidth = 150;
/// <summary>
/// Высота прорисовки бомбардировщика
/// </summary>
private readonly int _bomberHeight = 60;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="fuelTanks">Признак наличия топливных баков</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
private readonly int _bomberHeight = 120;
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bombs, bool fuelTanks, int width, int height)
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bombs, Color bombsColor, bool fuelTanks, int width, int height)
{
// TODO: Продумать проверки
_pictureWidth = width;
_pictureHeight = height;
if (width < _bomberWidth || height < _bomberHeight)
{
return false;
}
EntityAirBomber = new EntityAirBomber();
EntityAirBomber.Init(speed, weight, bodyColor, additionalColor, bombs, fuelTanks);
EntityAirBomber.Init(speed, weight, bodyColor, additionalColor, bombs, bombsColor, fuelTanks);
return true;
}
/// <summary>
@ -55,7 +49,14 @@ namespace AirBomber
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
// TODO: Изменение x, y
if (x < 0 || x + _bomberWidth > _pictureWidth)
{
x = 15;
}
if (y < 0 || y + _bomberHeight > _pictureHeight)
{
y = 15;
}
_startPosX = x;
_startPosY = y;
}
@ -71,140 +72,140 @@ namespace AirBomber
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityAirBomber.Step > 0)
{
_startPosX -= (int)EntityAirBomber.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityAirBomber.Step > 0)
{
_startPosY -= (int)EntityAirBomber.Step;
}
break;
// вправо
case DirectionType.Right:
// TODO: Продумать логику
if (_startPosX + EntityAirBomber.Step + _bomberWidth < _pictureWidth)
{
_startPosX += (int)EntityAirBomber.Step;
}
break;
//вниз
case DirectionType.Down:
// TODO: Продумать логику
if (_startPosY + EntityAirBomber.Step + _bomberHeight < _pictureHeight)
{
_startPosY += (int)EntityAirBomber.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public void DrawBomber(Graphics g)
{
if (EntityAirBomber == null)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new
SolidBrush(EntityAirBomber.AdditionalColor);
// обвесы
Brush additionalBrush = new SolidBrush(EntityAirBomber.AdditionalColor);
Brush bodyColor = new SolidBrush(EntityAirBomber.BodyColor);
Brush bombsColor = new SolidBrush(EntityAirBomber.BombsColor);
Brush wingsColor = new SolidBrush(Color.DeepPink);
// bombs
if (EntityAirBomber.Bombs)
{
g.DrawEllipse(pen, _startPosX + 90, _startPosY, 20, 20);
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 40, 20,
20);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 10,
20, 40);
g.DrawRectangle(pen, _startPosX + 90, _startPosY, 15,
15);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 45,
15, 15);
g.FillEllipse(additionalBrush, _startPosX + 90,
_startPosY, 20, 20);
g.FillEllipse(additionalBrush, _startPosX + 90,
_startPosY + 40, 20, 20);
g.FillRectangle(additionalBrush, _startPosX + 90,
_startPosY + 10, 20, 40);
g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY + 1, 15, 15);
g.FillRectangle(additionalBrush, _startPosX + 90,
_startPosY + 45, 15, 15);
g.DrawEllipse(pen, _startPosX, _startPosY, 20, 20);
g.DrawEllipse(pen, _startPosX, _startPosY + 40, 20, 20);
g.DrawRectangle(pen, _startPosX, _startPosY + 10, 20,
40);
g.DrawRectangle(pen, _startPosX + 5, _startPosY, 14,
15);
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 45,
14, 15);
g.FillEllipse(additionalBrush, _startPosX, _startPosY,
20, 20);
g.FillEllipse(additionalBrush, _startPosX, _startPosY +
40, 20, 20);
g.FillRectangle(additionalBrush, _startPosX + 1,
_startPosY + 10, 25, 40);
g.FillRectangle(additionalBrush, _startPosX + 5,
_startPosY + 1, 15, 15);
g.FillRectangle(additionalBrush, _startPosX + 5,
_startPosY + 45, 15, 15);
g.DrawRectangle(pen, _startPosX + 35, _startPosY, 39,
15);
g.DrawRectangle(pen, _startPosX + 35, _startPosY + 45,
39, 15);
g.FillRectangle(additionalBrush, _startPosX + 35,
_startPosY + 1, 40, 15);
g.FillRectangle(additionalBrush, _startPosX + 35,
_startPosY + 45, 40, 15);
g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 20, 15, 29);
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 20, 15, 29);
g.FillEllipse(bombsColor, _startPosX + 90, _startPosY + 70, 15, 29);
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 70, 15, 29);
}
//границы автомобиля
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 5, 20, 20);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 35, 20, 20);
g.DrawEllipse(pen, _startPosX + 80, _startPosY + 5, 20, 20);
g.DrawEllipse(pen, _startPosX + 80, _startPosY + 35, 20, 20);
g.DrawRectangle(pen, _startPosX + 9, _startPosY + 15, 10, 30);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 15, 10,
30);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 70, 52);
//задние фары
Brush brRed = new SolidBrush(Color.Red);
g.FillEllipse(brRed, _startPosX + 10, _startPosY + 5, 20, 20);
g.FillEllipse(brRed, _startPosX + 10, _startPosY + 35, 20,
20);
//передние фары
Brush brYellow = new SolidBrush(Color.Yellow);
g.FillEllipse(brYellow, _startPosX + 80, _startPosY + 5, 20,
20);
g.FillEllipse(brYellow, _startPosX + 80, _startPosY + 35, 20,
20);
//кузов
Brush br = new SolidBrush(EntityAirBomber.BodyColor);
g.FillRectangle(br, _startPosX + 10, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 90, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 20, _startPosY + 5, 70, 50);
//стекла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 70, _startPosY + 10, 5,
40);
g.FillRectangle(brBlue, _startPosX + 30, _startPosY + 10, 5,
40);
g.FillRectangle(brBlue, _startPosX + 35, _startPosY + 8, 35,
2);
g.FillRectangle(brBlue, _startPosX + 35, _startPosY + 51, 35,
2);
//выделяем рамкой крышу
g.DrawRectangle(pen, _startPosX + 35, _startPosY + 10, 35,
40);
g.DrawRectangle(pen, _startPosX + 75, _startPosY + 15, 25,
30);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 15, 15,
30);
// крыло
//границы бомбера
g.FillPolygon(additionalBrush, new Point[] //nose
{
new Point(_startPosX + 19, _startPosY + 50),
new Point(_startPosX + 19, _startPosY + 69),
new Point(_startPosX + 1, _startPosY + 59),
}
);
g.FillRectangle(bodyColor, _startPosX + 20, _startPosY + 50, 120, 20); //korpus
g.FillPolygon(additionalBrush, new Point[] //up left wing
{
new Point(_startPosX + 36, _startPosY + 49),
new Point(_startPosX + 36, _startPosY + 1),
new Point(_startPosX + 45, _startPosY + 1),
new Point(_startPosX + 55, _startPosY + 49),
}
);
g.FillPolygon(additionalBrush, new Point[] //down left wing
{
new Point(_startPosX + 36, _startPosY + 71),
new Point(_startPosX + 36, _startPosY + 116),
new Point(_startPosX + 45, _startPosY + 116),
new Point(_startPosX + 54, _startPosY + 71),
}
);
g.FillPolygon(wingsColor, new Point[] //up right wing
{
new Point(_startPosX + 120, _startPosY + 49),
new Point(_startPosX + 120, _startPosY + 42),
new Point(_startPosX + 140, _startPosY + 7),
new Point(_startPosX + 140, _startPosY + 49),
}
);
g.FillPolygon(wingsColor, new Point[] //down right wing
{
new Point(_startPosX + 120, _startPosY + 70),
new Point(_startPosX + 120, _startPosY + 77),
new Point(_startPosX + 140, _startPosY + 112),
new Point(_startPosX + 140, _startPosY + 70),
}
);
g.DrawPolygon(pen, new Point[] //nose
{
new Point(_startPosX + 20, _startPosY + 49),
new Point(_startPosX + 20, _startPosY + 70),
new Point(_startPosX, _startPosY + 59),
}
);
g.DrawRectangle(pen, _startPosX + 19, _startPosY + 49, 121, 21); //корпус
g.DrawPolygon(pen, new Point[] //верхнее left крыло
{
new Point(_startPosX + 35, _startPosY + 49),
new Point(_startPosX + 35, _startPosY),
new Point(_startPosX + 45, _startPosY),
new Point(_startPosX + 55, _startPosY + 49),
}
);
g.DrawPolygon(pen, new Point[] //нижнее left крыло
{
new Point(_startPosX + 36, _startPosY + 71),
new Point(_startPosX + 36, _startPosY + 116),
new Point(_startPosX + 45, _startPosY + 116),
new Point(_startPosX + 54, _startPosY + 71),
}
);
g.DrawPolygon(pen, new Point[] //up right крыло
{
new Point(_startPosX + 120, _startPosY + 49),
new Point(_startPosX + 120, _startPosY + 42),
new Point(_startPosX + 140, _startPosY + 7),
new Point(_startPosX + 140, _startPosY + 49),
}
);
g.DrawPolygon(pen, new Point[] //down right крыло
{
new Point(_startPosX + 120, _startPosY + 70),
new Point(_startPosX + 120, _startPosY + 77),
new Point(_startPosX + 140, _startPosY + 112),
new Point(_startPosX + 140, _startPosY + 70),
}
);
if (EntityAirBomber.FuelTanks)
{
g.FillRectangle(additionalBrush, _startPosX, _startPosY
+ 5, 10, 50);
g.DrawRectangle(pen, _startPosX, _startPosY + 5, 10,
50);
g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 34, 20, 15);
g.DrawRectangle(pen, _startPosX + 63, _startPosY + 34, 20, 15);
g.FillRectangle(additionalBrush, _startPosX + 63, _startPosY + 70, 20, 15);
g.DrawRectangle(pen, _startPosX + 63, _startPosY + 70, 20, 15);
}
}
}

View File

@ -14,6 +14,7 @@ namespace AirBomber
public Color BodyColor { get; private set; }
public Color AdditionalColor { get; private set; }
public bool Bombs { get; private set; }
public Color BombsColor { get; private set; }
public bool FuelTanks { get; private set; }
public double Step => (double)Speed * 100 / Weight;
/// <summary>
@ -26,13 +27,14 @@ namespace AirBomber
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="fuelTanks">Признак наличия топливных баков</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool bombs, bool fuelTanks)
additionalColor, bool bombs, Color bombsColor, bool fuelTanks)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Bombs = bombs;
BombsColor = bombsColor;
FuelTanks = fuelTanks;
}

View File

@ -33,6 +33,8 @@
buttonLeft = new Button();
buttonRight = new Button();
buttonUp = new Button();
pictureBoxAirBomber = new PictureBox();
((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit();
SuspendLayout();
//
// buttonCreate
@ -44,6 +46,7 @@
buttonCreate.TabIndex = 0;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
//
// buttonDown
//
@ -55,6 +58,7 @@
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 1;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click;
//
// buttonLeft
//
@ -66,6 +70,7 @@
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click;
//
// buttonRight
//
@ -77,6 +82,7 @@
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 3;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click;
//
// buttonUp
//
@ -88,6 +94,17 @@
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 4;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click;
//
// pictureBoxAirBomber
//
pictureBoxAirBomber.BackColor = SystemColors.Control;
pictureBoxAirBomber.Dock = DockStyle.Fill;
pictureBoxAirBomber.Location = new Point(0, 0);
pictureBoxAirBomber.Name = "pictureBoxAirBomber";
pictureBoxAirBomber.Size = new Size(986, 540);
pictureBoxAirBomber.TabIndex = 5;
pictureBoxAirBomber.TabStop = false;
//
// FormAirBomber
//
@ -99,8 +116,10 @@
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonCreate);
Controls.Add(pictureBoxAirBomber);
Name = "FormAirBomber";
Text = "Form1";
Text = "Бомбардировщик";
((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).EndInit();
ResumeLayout(false);
}
@ -111,5 +130,6 @@
private Button buttonLeft;
private Button buttonRight;
private Button buttonUp;
private PictureBox pictureBoxAirBomber;
}
}

View File

@ -1,10 +1,70 @@
namespace AirBomber
namespace AirBomber
{
public partial class FormAirBomber : Form
{
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningAirBomber? _drawningAirBomber;
public FormAirBomber()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningAirBomber == null)
{
return;
}
Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAirBomber.DrawBomber(gr);
pictureBoxAirBomber.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirBomber = new DrawningAirBomber();
_drawningAirBomber.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)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); _drawningAirBomber.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 (_drawningAirBomber == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningAirBomber.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningAirBomber.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningAirBomber.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningAirBomber.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
}
}