Приведено к виду с практического занятия
This commit is contained in:
parent
98281c3db4
commit
c4de4279c9
@ -33,28 +33,83 @@ namespace Trolleybus
|
||||
}
|
||||
public Bitmap MoveObject(Direction direction)
|
||||
{
|
||||
// TODO проверка, что объект может переместится в требуемом направлении
|
||||
if (true)
|
||||
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
|
||||
|
||||
for (int i = 0; i < _map.GetLength(0); i++)
|
||||
{
|
||||
_drawningObject.MoveObject(direction);
|
||||
for (int j = 0; j < _map.GetLength(1); j++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case Direction.Up:
|
||||
if (_size_y * (j + 1) >= topY - _drawningObject.Step && _size_y * (j + 1) < topY && _size_x * (i + 1) > leftX
|
||||
&& _size_x * (i + 1) <= rightX)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
break;
|
||||
case Direction.Down:
|
||||
if (_size_y * j <= bottomY + _drawningObject.Step && _size_y * j > bottomY && _size_x * (i + 1) > leftX
|
||||
&& _size_x * (i + 1) <= rightX)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
break;
|
||||
case Direction.Left:
|
||||
if (_size_x * (i + 1) >= leftX - _drawningObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY
|
||||
&& _size_y * (j + 1) >= topY)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
break;
|
||||
case Direction.Right:
|
||||
if (_size_x * i <= rightX + _drawningObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY
|
||||
&& _size_y * (j + 1) >= topY)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_drawningObject.MoveObject(direction);
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
private bool SetObjectOnMap()
|
||||
{
|
||||
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
|
||||
if (_drawningObject == null || _map == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
float airplaneWidth = rightX - leftX;
|
||||
float airplaneHeight = bottomY - topY;
|
||||
|
||||
int x = _random.Next(0, 10);
|
||||
int y = _random.Next(0, 10);
|
||||
for (int i = 0; i < _map.GetLength(0); ++i)
|
||||
{
|
||||
for (int j = 0; j < _map.GetLength(1); ++j)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
if (x + airplaneWidth >= _size_x * i && x <= _size_x * i && y + airplaneHeight > _size_y * j && y <= _size_y * j)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_drawningObject.SetObject(x, y, _width, _height);
|
||||
// TODO првоерка, что объект не "накладывается" на закрытые участки
|
||||
return true;
|
||||
}
|
||||
private Bitmap DrawMapWithObject()
|
||||
{
|
||||
Bitmap bmp = new Bitmap(_width, _height);
|
||||
Bitmap bmp = new(_width, _height);
|
||||
if (_drawningObject == null || _map == null)
|
||||
{
|
||||
return bmp;
|
||||
@ -82,4 +137,4 @@ namespace Trolleybus
|
||||
protected abstract void DrawRoadPart(Graphics g, int i, int j);
|
||||
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
||||
}
|
||||
}
|
||||
}
|
@ -9,10 +9,10 @@ namespace Trolleybus
|
||||
{
|
||||
internal class DrawningSmallTrolleybus : DrawingTrolleybus
|
||||
{
|
||||
public DrawningSmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool sportLine) :
|
||||
public DrawningSmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns, bool battary) :
|
||||
base(speed, weight, bodyColor, 110, 60)
|
||||
{
|
||||
Trolleybus = new EntitySmallTrolleybus(speed, weight, bodyColor, dopColor, bodyKit, wing, sportLine);
|
||||
Trolleybus = new EntitySmallTrolleybus(speed, weight, bodyColor, dopColor, bodyKit, wing, horns, battary);
|
||||
}
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
@ -29,8 +29,8 @@ namespace Trolleybus
|
||||
Brush bWhite = new SolidBrush(Color.White);
|
||||
Pen WinBlue = new Pen(Color.Blue);
|
||||
|
||||
_startPosY = _startPosY + 100;
|
||||
|
||||
//_startPosY = _startPosY + 100;
|
||||
base.DrawTransport(g);
|
||||
if (smallTrolleybus.BodyKit)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX, _startPosY, 200, 50);
|
||||
@ -55,11 +55,11 @@ namespace Trolleybus
|
||||
g.DrawEllipse(pen, _startPosX + 170, _startPosY + 40, 30, 30);
|
||||
}
|
||||
|
||||
_startPosX += 10;
|
||||
_startPosY += 5;
|
||||
base.DrawTransport(g);
|
||||
_startPosX -= 10;
|
||||
_startPosY -= 5;
|
||||
//_startPosX += 10;
|
||||
//_startPosY += 5;
|
||||
//base.DrawTransport(g);
|
||||
//_startPosX -= 10;
|
||||
//_startPosY -= 5;
|
||||
|
||||
if (smallTrolleybus.Horns)
|
||||
{
|
||||
@ -69,6 +69,11 @@ namespace Trolleybus
|
||||
g.DrawRectangle(pen, _startPosX + 50, _startPosY - 10, 100, 10);
|
||||
}
|
||||
|
||||
if (smallTrolleybus.Battary)
|
||||
{
|
||||
g.DrawRectangle(pen, _startPosX + 100, _startPosY, 10, 10);
|
||||
}
|
||||
|
||||
if (smallTrolleybus.Wing)
|
||||
{
|
||||
//g.FillRectangle(dopBrush, _startPosX, _startPosY + 5, 10, 50);
|
||||
|
@ -29,6 +29,11 @@ namespace Trolleybus
|
||||
_trolleybus?.MoveTransport(direction);
|
||||
}
|
||||
|
||||
public void nothing()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SetObject(int x, int y, int width, int height)
|
||||
{
|
||||
// _trolleybus.SetPosition(x, y, startX, startY, width, height);
|
||||
|
46
Trolleybus/Trolleybus/DrawningObjectTrolleybus.cs
Normal file
46
Trolleybus/Trolleybus/DrawningObjectTrolleybus.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Trolleybus
|
||||
{
|
||||
internal class DrawningObjectTrolleybus : IDrawningObject
|
||||
{
|
||||
private DrawingTrolleybus _trolleybus = null;
|
||||
|
||||
public DrawningObjectTrolleybus(DrawingTrolleybus trolleybus)
|
||||
{
|
||||
_trolleybus = trolleybus;
|
||||
}
|
||||
|
||||
public float Step => _trolleybus?.Trolleybus?.Step ?? 0;
|
||||
|
||||
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
|
||||
{
|
||||
return _trolleybus?.GetCurrentPosition() ?? default;
|
||||
}
|
||||
|
||||
public void MoveObject(Direction direction)
|
||||
{
|
||||
_trolleybus?.MoveTransport(direction);
|
||||
}
|
||||
|
||||
public void nothing()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SetObject(int x, int y, int width, int height)
|
||||
{
|
||||
_trolleybus.SetPosition(x, y, width, height);
|
||||
}
|
||||
|
||||
void IDrawningObject.DrawningObject(Graphics g)
|
||||
{
|
||||
_trolleybus.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
@ -25,6 +25,7 @@ namespace Trolleybus
|
||||
/// Признак наличия гоночной полосы
|
||||
/// </summary>
|
||||
public bool Horns { get; private set; }
|
||||
public bool Battary { get; private set; }
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// </summary>
|
||||
@ -35,13 +36,15 @@ namespace Trolleybus
|
||||
/// <param name="bodyKit">Признак наличия обвеса</param>
|
||||
/// <param name="wing">Признак наличия антикрыла</param>
|
||||
/// <param name="horns">Признак наличия гоночной полосы</param>
|
||||
public EntitySmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns) :
|
||||
/// /// <param name="battary">Признак наличия гоночной полосы</param>
|
||||
public EntitySmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns, bool battary) :
|
||||
base(speed, weight, bodyColor)
|
||||
{
|
||||
DopColor = dopColor;
|
||||
BodyKit = bodyKit;
|
||||
Wing = wing;
|
||||
Horns = horns;
|
||||
Battary = battary;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,10 +31,15 @@ namespace Trolleybus
|
||||
|
||||
private void SetData()
|
||||
{
|
||||
Bitmap bmp = new Bitmap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_trolleybus?.DrawTransport(gr);
|
||||
pictureBoxTrolleybus.Image = bmp;
|
||||
Random rnd = new Random();
|
||||
//Bitmap bmp = new Bitmap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||
//Graphics gr = Graphics.FromImage(bmp);
|
||||
//_trolleybus?.DrawTransport(gr);
|
||||
//pictureBoxTrolleybus.Image = bmp;
|
||||
_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||
toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}";
|
||||
toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}";
|
||||
toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -47,11 +52,12 @@ namespace Trolleybus
|
||||
{
|
||||
Random rnd = new Random();
|
||||
_trolleybus = new DrawingTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
|
||||
// _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), Location.X, Location.Y, pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||
_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||
toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}";
|
||||
toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}";
|
||||
toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}";
|
||||
// _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), Location.X, Location.Y, pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||
//_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
|
||||
//toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}";
|
||||
//toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}";
|
||||
//toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}";
|
||||
SetData();
|
||||
Draw();
|
||||
}
|
||||
|
||||
@ -88,7 +94,7 @@ namespace Trolleybus
|
||||
_trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000),
|
||||
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)));
|
||||
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
|
||||
SetData();
|
||||
Draw();
|
||||
}
|
||||
|
@ -4,17 +4,16 @@ using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
|
||||
namespace Trolleybus
|
||||
{
|
||||
public partial class FormMap : Form
|
||||
{
|
||||
private AbstractMap _abstractMap;
|
||||
|
||||
public FormMap()
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -30,7 +29,7 @@ namespace Trolleybus
|
||||
toolStripStatusLabelWeight.Text = $"Вес: {trolleybus.Trolleybus.Weight}";
|
||||
toolStripStatusLabelBodyColor.Text = $"Цвет: {trolleybus.Trolleybus.BodyColor.Name}";
|
||||
pictureBoxTrolleybus.Image = _abstractMap.CreateMap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height,
|
||||
new DrawningObject(trolleybus));
|
||||
new DrawningObjectTrolleybus(trolleybus));
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
@ -81,7 +80,7 @@ namespace Trolleybus
|
||||
var trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000),
|
||||
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)));
|
||||
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
|
||||
SetData(trolleybus);
|
||||
}
|
||||
/// <summary>
|
||||
|
@ -12,7 +12,7 @@ namespace Trolleybus
|
||||
/// <summary>
|
||||
/// Шаг перемещения объекта
|
||||
/// </summary>
|
||||
float Step { get; }
|
||||
public float Step { get; }
|
||||
/// <summary>
|
||||
/// Установка позиции объекта
|
||||
/// </summary>
|
||||
@ -36,6 +36,7 @@ namespace Trolleybus
|
||||
/// Получение текущей позиции объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
void nothing();
|
||||
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ namespace Trolleybus
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new Form1());
|
||||
Application.Run(new FormMap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,6 +52,7 @@
|
||||
<Compile Include="DrawingSmallTrolleybus.cs" />
|
||||
<Compile Include="DrawingTrolleybus.cs" />
|
||||
<Compile Include="DrawningObject.cs" />
|
||||
<Compile Include="DrawningObjectTrolleybus.cs" />
|
||||
<Compile Include="EntitySmallTrolleybus.cs" />
|
||||
<Compile Include="EntityTrolleybus.cs" />
|
||||
<Compile Include="Form1.cs">
|
||||
|
Loading…
Reference in New Issue
Block a user