lab2 done
This commit is contained in:
parent
58de90de09
commit
95676d3037
150
Sailboat/AbstractMap.cs
Normal file
150
Sailboat/AbstractMap.cs
Normal file
@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Sailboat
|
||||
{
|
||||
internal abstract class AbstractMap
|
||||
{
|
||||
IDrawingObject _drawingObject = null;
|
||||
protected int[,] _map = null;
|
||||
//ширина и высота карты
|
||||
protected int _width;
|
||||
protected int _height;
|
||||
//ширина и высота ячейки
|
||||
protected float _size_x;
|
||||
protected float _size_y;
|
||||
protected readonly Random _random = new();
|
||||
protected readonly int _freeRoad = 0;
|
||||
protected readonly int _barrier = 1;
|
||||
|
||||
public Bitmap CreateMap(int width, int height, IDrawingObject drawningObject)
|
||||
{
|
||||
_width = width;
|
||||
_height = height;
|
||||
_drawingObject = drawningObject;
|
||||
GenerateMap();
|
||||
while (!SetObjectOnMap())
|
||||
{
|
||||
GenerateMap();
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
public Bitmap MoveObject(Direction direction)
|
||||
{
|
||||
(float leftX, float topY, float rightX, float bottomY) = _drawingObject.GetCurrentPosition();
|
||||
|
||||
float boatWidth = rightX - leftX;
|
||||
float boatHeight = bottomY - topY;
|
||||
for (int i = 0; i < _map.GetLength(0); i++)
|
||||
{
|
||||
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 - _drawingObject.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 + _drawingObject.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 - _drawingObject.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 + _drawingObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY
|
||||
&& _size_y * (j + 1) >= topY)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (true)
|
||||
{
|
||||
_drawingObject.MoveObject(direction);
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
|
||||
private bool SetObjectOnMap()
|
||||
{
|
||||
(float leftX, float topY, float rightX, float bottomY) = _drawingObject.GetCurrentPosition();
|
||||
if (_drawingObject == null || _map == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
float boatWidth = rightX - leftX;
|
||||
float boatHeight = 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 + boatWidth >= _size_x * i && x <= _size_x * i && y + boatHeight > _size_y * j && y <= _size_y * j)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_drawingObject.SetObject(x, y, _width, _height);
|
||||
return true;
|
||||
}
|
||||
private Bitmap DrawMapWithObject()
|
||||
{
|
||||
Bitmap bmp = new(_width, _height);
|
||||
if (_drawingObject == null || _map == null)
|
||||
{
|
||||
return bmp;
|
||||
}
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
for (int i = 0; i < _map.GetLength(0); ++i)
|
||||
{
|
||||
for (int j = 0; j < _map.GetLength(1); ++j)
|
||||
{
|
||||
if (_map[i, j] == _freeRoad)
|
||||
{
|
||||
DrawRoadPart(gr, i, j);
|
||||
}
|
||||
else if (_map[i, j] == _barrier)
|
||||
{
|
||||
DrawBarrierPart(gr, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
_drawingObject.DrawingObject(gr);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
|
||||
protected abstract void GenerateMap();
|
||||
protected abstract void DrawRoadPart(Graphics g, int i, int j);
|
||||
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
||||
|
||||
}
|
||||
}
|
@ -84,18 +84,6 @@ namespace Sailboat
|
||||
_boat?.DrawTransport(gr);
|
||||
pictureBoxBoat.Image = bmp;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Изменение размеров формы
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void PictureBoxCar_Resize(object sender, EventArgs e)
|
||||
{
|
||||
_boat?.ChangeBorders(pictureBoxBoat.Width, pictureBoxBoat.Height);
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки машины
|
||||
/// </summary>
|
||||
@ -104,8 +92,7 @@ namespace Sailboat
|
||||
Random rnd = new();
|
||||
toolStripLabel_color.Text = $"Скорость: {_boat.Boat.Speed}";
|
||||
toolStripLabel_weight.Text = $"Вес: {_boat.Boat.Weight}";
|
||||
toolStripLabel_color.Text = $"Цвет: { _boat.Boat.BodyColor.Name}";
|
||||
|
||||
toolStripLabel_color.Text = $"Цвет: { _boat.Boat.BodyColor.Name}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,10 +11,11 @@ namespace Sailboat
|
||||
/// </summary>
|
||||
internal enum Direction
|
||||
{
|
||||
None = 0,
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
Left = 3,
|
||||
Right = 4
|
||||
Right = 4
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,13 +35,13 @@ namespace Sailboat
|
||||
/// <summary>
|
||||
/// Ширина отрисовки автомобиля
|
||||
/// </summary>
|
||||
private readonly int _boatWidth = 120;
|
||||
private readonly int _boatWidth = 125;
|
||||
/// <summary>
|
||||
/// Высота отрисовки автомобиля
|
||||
/// </summary>
|
||||
private readonly int _boatHeight = 40;
|
||||
/// <summary>
|
||||
/// Инициализация свойств (метод инит принимал готовые параметры от боат
|
||||
/// Инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
@ -66,14 +66,13 @@ namespace Sailboat
|
||||
/// <param name="height">Высота картинки</param>
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if (x > 0 && y > 0 && x < width - _boatWidth && y < height - _boatHeight)
|
||||
if (x >= 0 && y >= 0 && x < width - _boatWidth && y < height - _boatHeight)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
@ -118,7 +117,7 @@ namespace Sailboat
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Отрисовка автомобиля
|
||||
/// Отрисовка лодки
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
|
@ -24,7 +24,7 @@ namespace Sailboat
|
||||
|
||||
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return _boat?.GetCurrentPosition() ?? default;
|
||||
}
|
||||
|
||||
void IDrawingObject.MoveObject(Direction direction)
|
||||
|
211
Sailboat/FormMap.Designer.cs
generated
Normal file
211
Sailboat/FormMap.Designer.cs
generated
Normal file
@ -0,0 +1,211 @@
|
||||
|
||||
namespace Sailboat
|
||||
{
|
||||
partial class FormMap
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
|
||||
this.toolStripLabel_speed = new System.Windows.Forms.ToolStripStatusLabel();
|
||||
this.toolStripLabel_weight = new System.Windows.Forms.ToolStripStatusLabel();
|
||||
this.toolStripLabel_color = new System.Windows.Forms.ToolStripStatusLabel();
|
||||
this.pictureBoxBoat = new System.Windows.Forms.PictureBox();
|
||||
this.btn_create = new System.Windows.Forms.Button();
|
||||
this.btn_right = new System.Windows.Forms.Button();
|
||||
this.btn_down = new System.Windows.Forms.Button();
|
||||
this.btn_up = new System.Windows.Forms.Button();
|
||||
this.btn_left = new System.Windows.Forms.Button();
|
||||
this.btn_create_sailboat = new System.Windows.Forms.Button();
|
||||
this.comboBoxMapSelector = new System.Windows.Forms.ComboBox();
|
||||
this.statusStrip1.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// statusStrip1
|
||||
//
|
||||
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
|
||||
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.toolStripLabel_speed,
|
||||
this.toolStripLabel_weight,
|
||||
this.toolStripLabel_color});
|
||||
this.statusStrip1.Location = new System.Drawing.Point(0, 424);
|
||||
this.statusStrip1.Name = "statusStrip1";
|
||||
this.statusStrip1.Size = new System.Drawing.Size(800, 26);
|
||||
this.statusStrip1.TabIndex = 0;
|
||||
this.statusStrip1.Text = "statusStrip1";
|
||||
//
|
||||
// toolStripLabel_speed
|
||||
//
|
||||
this.toolStripLabel_speed.Name = "toolStripLabel_speed";
|
||||
this.toolStripLabel_speed.Size = new System.Drawing.Size(76, 20);
|
||||
this.toolStripLabel_speed.Text = "Скорость:";
|
||||
//
|
||||
// toolStripLabel_weight
|
||||
//
|
||||
this.toolStripLabel_weight.Name = "toolStripLabel_weight";
|
||||
this.toolStripLabel_weight.Size = new System.Drawing.Size(36, 20);
|
||||
this.toolStripLabel_weight.Text = "Вес:";
|
||||
//
|
||||
// toolStripLabel_color
|
||||
//
|
||||
this.toolStripLabel_color.Name = "toolStripLabel_color";
|
||||
this.toolStripLabel_color.Size = new System.Drawing.Size(45, 20);
|
||||
this.toolStripLabel_color.Text = "Цвет:";
|
||||
//
|
||||
// pictureBoxBoat
|
||||
//
|
||||
this.pictureBoxBoat.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pictureBoxBoat.Location = new System.Drawing.Point(0, 0);
|
||||
this.pictureBoxBoat.Name = "pictureBoxBoat";
|
||||
this.pictureBoxBoat.Size = new System.Drawing.Size(800, 424);
|
||||
this.pictureBoxBoat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
|
||||
this.pictureBoxBoat.TabIndex = 1;
|
||||
this.pictureBoxBoat.TabStop = false;
|
||||
this.pictureBoxBoat.Resize += new System.EventHandler(this.btn_move_Click);
|
||||
//
|
||||
// btn_create
|
||||
//
|
||||
this.btn_create.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btn_create.Location = new System.Drawing.Point(13, 13);
|
||||
this.btn_create.Name = "btn_create";
|
||||
this.btn_create.Size = new System.Drawing.Size(94, 29);
|
||||
this.btn_create.TabIndex = 2;
|
||||
this.btn_create.Text = "Создать";
|
||||
this.btn_create.UseVisualStyleBackColor = true;
|
||||
this.btn_create.Click += new System.EventHandler(this.btn_create_Click);
|
||||
//
|
||||
// btn_right
|
||||
//
|
||||
this.btn_right.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btn_right.BackgroundImage = global::Sailboat.Properties.Resources.right;
|
||||
this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.btn_right.Location = new System.Drawing.Point(694, 392);
|
||||
this.btn_right.Name = "btn_right";
|
||||
this.btn_right.Size = new System.Drawing.Size(94, 29);
|
||||
this.btn_right.TabIndex = 4;
|
||||
this.btn_right.UseVisualStyleBackColor = true;
|
||||
this.btn_right.Click += new System.EventHandler(this.btn_move_Click);
|
||||
//
|
||||
// btn_down
|
||||
//
|
||||
this.btn_down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btn_down.BackgroundImage = global::Sailboat.Properties.Resources.down;
|
||||
this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.btn_down.Location = new System.Drawing.Point(594, 392);
|
||||
this.btn_down.Name = "btn_down";
|
||||
this.btn_down.Size = new System.Drawing.Size(94, 29);
|
||||
this.btn_down.TabIndex = 5;
|
||||
this.btn_down.UseVisualStyleBackColor = true;
|
||||
this.btn_down.Click += new System.EventHandler(this.btn_move_Click);
|
||||
//
|
||||
// btn_up
|
||||
//
|
||||
this.btn_up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btn_up.BackgroundImage = global::Sailboat.Properties.Resources.up;
|
||||
this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.btn_up.Location = new System.Drawing.Point(594, 357);
|
||||
this.btn_up.Name = "btn_up";
|
||||
this.btn_up.Size = new System.Drawing.Size(94, 29);
|
||||
this.btn_up.TabIndex = 6;
|
||||
this.btn_up.UseVisualStyleBackColor = true;
|
||||
this.btn_up.Click += new System.EventHandler(this.btn_move_Click);
|
||||
//
|
||||
// btn_left
|
||||
//
|
||||
this.btn_left.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btn_left.BackgroundImage = global::Sailboat.Properties.Resources.left;
|
||||
this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
|
||||
this.btn_left.Location = new System.Drawing.Point(494, 391);
|
||||
this.btn_left.Name = "btn_left";
|
||||
this.btn_left.Size = new System.Drawing.Size(94, 29);
|
||||
this.btn_left.TabIndex = 7;
|
||||
this.btn_left.UseVisualStyleBackColor = true;
|
||||
this.btn_left.Click += new System.EventHandler(this.btn_move_Click);
|
||||
//
|
||||
// btn_create_sailboat
|
||||
//
|
||||
this.btn_create_sailboat.Location = new System.Drawing.Point(113, 13);
|
||||
this.btn_create_sailboat.Name = "btn_create_sailboat";
|
||||
this.btn_create_sailboat.Size = new System.Drawing.Size(150, 29);
|
||||
this.btn_create_sailboat.TabIndex = 8;
|
||||
this.btn_create_sailboat.Text = "Создать парусник";
|
||||
this.btn_create_sailboat.UseVisualStyleBackColor = true;
|
||||
this.btn_create_sailboat.Click += new System.EventHandler(this.btn_create_sailboat_Click);
|
||||
//
|
||||
// comboBoxMapSelector
|
||||
//
|
||||
this.comboBoxMapSelector.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
this.comboBoxMapSelector.FormattingEnabled = true;
|
||||
this.comboBoxMapSelector.Items.AddRange(new object[] {
|
||||
"Простая карта",
|
||||
"Водная карта"});
|
||||
this.comboBoxMapSelector.Location = new System.Drawing.Point(269, 14);
|
||||
this.comboBoxMapSelector.Name = "comboBoxMapSelector";
|
||||
this.comboBoxMapSelector.Size = new System.Drawing.Size(151, 28);
|
||||
this.comboBoxMapSelector.TabIndex = 9;
|
||||
this.comboBoxMapSelector.SelectedIndexChanged += new System.EventHandler(this.comboBoxMapSelector_SelectedIndexChanged);
|
||||
//
|
||||
// FormMap
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||
this.Controls.Add(this.comboBoxMapSelector);
|
||||
this.Controls.Add(this.btn_create_sailboat);
|
||||
this.Controls.Add(this.btn_left);
|
||||
this.Controls.Add(this.btn_up);
|
||||
this.Controls.Add(this.btn_down);
|
||||
this.Controls.Add(this.btn_right);
|
||||
this.Controls.Add(this.btn_create);
|
||||
this.Controls.Add(this.pictureBoxBoat);
|
||||
this.Controls.Add(this.statusStrip1);
|
||||
this.Name = "FormMap";
|
||||
this.Click += new System.EventHandler(this.btn_move_Click);
|
||||
this.statusStrip1.ResumeLayout(false);
|
||||
this.statusStrip1.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.StatusStrip statusStrip1;
|
||||
private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_speed;
|
||||
private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_weight;
|
||||
private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_color;
|
||||
private System.Windows.Forms.PictureBox pictureBoxBoat;
|
||||
private System.Windows.Forms.Button btn_create;
|
||||
private System.Windows.Forms.Button btn_right;
|
||||
private System.Windows.Forms.Button btn_down;
|
||||
private System.Windows.Forms.Button btn_up;
|
||||
private System.Windows.Forms.Button btn_left;
|
||||
private System.Windows.Forms.Button btn_create_sailboat;
|
||||
private System.Windows.Forms.ComboBox comboBoxMapSelector;
|
||||
}
|
||||
}
|
99
Sailboat/FormMap.cs
Normal file
99
Sailboat/FormMap.cs
Normal file
@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Sailboat
|
||||
{
|
||||
public partial class FormMap : Form
|
||||
{
|
||||
private AbstractMap _abstractMap;
|
||||
public FormMap()
|
||||
{
|
||||
InitializeComponent();
|
||||
_abstractMap = new SimpleMap();
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void btn_create_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new();
|
||||
var boat = new DrawingBoat(rnd.Next(100, 300), rnd.Next(1000, 2000),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
|
||||
SetData(boat);
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать парусник"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void btn_create_sailboat_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new();
|
||||
var boat = new DrawingSailboat(rnd.Next(100, 300), rnd.Next(1000, 2000),
|
||||
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)));
|
||||
SetData(boat);
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия стрелок
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void btn_move_Click(object sender, EventArgs e)
|
||||
{
|
||||
//получаем имя кнопки
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
Direction dir = Direction.None;
|
||||
switch (name)
|
||||
{
|
||||
case "btn_up":
|
||||
dir = Direction.Up;
|
||||
break;
|
||||
case "btn_down":
|
||||
dir = Direction.Down;
|
||||
break;
|
||||
case "btn_left":
|
||||
dir = Direction.Left;
|
||||
break;
|
||||
case "btn_right":
|
||||
dir = Direction.Right;
|
||||
break;
|
||||
}
|
||||
pictureBoxBoat.Image = _abstractMap?.MoveObject(dir);
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки машины
|
||||
/// </summary>
|
||||
private void SetData(DrawingBoat boat)
|
||||
{
|
||||
Random rnd = new();
|
||||
toolStripLabel_color.Text = $"Скорость: {boat?.Boat.Speed}";
|
||||
toolStripLabel_weight.Text = $"Вес: {boat?.Boat.Weight}";
|
||||
toolStripLabel_color.Text = $"Цвет: { boat?.Boat.BodyColor.Name}";
|
||||
pictureBoxBoat.Image = _abstractMap.CreateMap(pictureBoxBoat.Width, pictureBoxBoat.Height,
|
||||
new DrawingObjectBoat(boat));
|
||||
}
|
||||
|
||||
private void comboBoxMapSelector_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
switch (comboBoxMapSelector.Text)
|
||||
{
|
||||
case "Простая карта":
|
||||
_abstractMap = new SimpleMap();
|
||||
break;
|
||||
case "Водная карта":
|
||||
_abstractMap = new WaterMap();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
63
Sailboat/FormMap.resx
Normal file
63
Sailboat/FormMap.resx
Normal file
@ -0,0 +1,63 @@
|
||||
<root>
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
</root>
|
@ -17,7 +17,7 @@ namespace Sailboat
|
||||
Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new BoatForm());
|
||||
Application.Run(new FormMap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
57
Sailboat/SimpleMap.cs
Normal file
57
Sailboat/SimpleMap.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Sailboat
|
||||
{
|
||||
/// <summary>
|
||||
/// Простая реализация абсрактного класса AbstractMap
|
||||
/// </summary>
|
||||
internal class SimpleMap : AbstractMap
|
||||
{
|
||||
/// <summary>
|
||||
/// Цвет участка закрытого
|
||||
/// </summary>
|
||||
private readonly Brush barrierColor = new SolidBrush(Color.Black);
|
||||
/// <summary>
|
||||
/// Цвет участка открытого
|
||||
/// </summary>
|
||||
private readonly Brush roadColor = new SolidBrush(Color.Gray);
|
||||
|
||||
protected override void DrawBarrierPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
}
|
||||
protected override void DrawRoadPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
}
|
||||
protected override void GenerateMap()
|
||||
{
|
||||
_map = new int[100, 100];
|
||||
_size_x = (float)_width / _map.GetLength(0);
|
||||
_size_y = (float)_height / _map.GetLength(1);
|
||||
int counter = 0;
|
||||
for (int i = 0; i < _map.GetLength(0); ++i)
|
||||
{
|
||||
for (int j = 0; j < _map.GetLength(1); ++j)
|
||||
{
|
||||
_map[i, j] = _freeRoad;
|
||||
}
|
||||
}
|
||||
while (counter < 50)
|
||||
{
|
||||
int x = _random.Next(0, 100);
|
||||
int y = _random.Next(0, 100);
|
||||
if (_map[x, y] == _freeRoad)
|
||||
{
|
||||
_map[x, y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
Sailboat/WaterMap.cs
Normal file
56
Sailboat/WaterMap.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Sailboat
|
||||
{
|
||||
internal class WaterMap : AbstractMap
|
||||
{
|
||||
private readonly Brush barrierColor = new SolidBrush(Color.Black);
|
||||
private readonly Brush wavesColor = new SolidBrush(Color.Blue);
|
||||
private readonly Brush deepColor = new SolidBrush(Color.DarkBlue);
|
||||
protected override void DrawBarrierPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
}
|
||||
protected override void DrawRoadPart(Graphics g, int i, int j)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
if (rnd.Next(0, 7) == 6)
|
||||
{
|
||||
g.FillRectangle(wavesColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
g.FillRectangle(deepColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
}
|
||||
}
|
||||
protected override void GenerateMap()
|
||||
{
|
||||
_map = new int[100, 100];
|
||||
_size_x = (float)_width / _map.GetLength(0);
|
||||
_size_y = (float)_height / _map.GetLength(1);
|
||||
int counter = 0;
|
||||
for (int i = 0; i < _map.GetLength(0); ++i)
|
||||
{
|
||||
for (int j = 0; j < _map.GetLength(1); ++j)
|
||||
{
|
||||
_map[i, j] = _freeRoad;
|
||||
}
|
||||
}
|
||||
while (counter < 50)
|
||||
{
|
||||
int x = _random.Next(0, 100);
|
||||
int y = _random.Next(0, 100);
|
||||
if (_map[x, y] == _freeRoad)
|
||||
{
|
||||
_map[x, y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user