75 lines
2.5 KiB
C#
75 lines
2.5 KiB
C#
namespace Battleship
|
|
{
|
|
public partial class Battleship : Form
|
|
{
|
|
/// <summary>
|
|
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
|
|
/// </summary>
|
|
private DrawningBattleship? _drawningBattleship;
|
|
/// <summary>
|
|
/// Èíèöèàëèçàöèÿ ôîðìû
|
|
/// </summary>
|
|
public Battleship()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
/// Ìåòîä ïðîðèñîâêè ìàøèíû
|
|
/// </summary>
|
|
private void Draw()
|
|
{
|
|
if (_drawningBattleship == null)
|
|
{
|
|
return;
|
|
}
|
|
Bitmap bmp = new(pictureBoxBattleship.Width,
|
|
pictureBoxBattleship.Height);
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
_drawningBattleship.DrawTransport(gr);
|
|
pictureBoxBattleship.Image = bmp;
|
|
}
|
|
|
|
private void buttonCreate_Click(object sender, EventArgs e)
|
|
{
|
|
Random random = new();
|
|
_drawningBattleship = new DrawningBattleship();
|
|
_drawningBattleship.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)), Convert.ToBoolean(random.Next(0, 2)),
|
|
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
|
_drawningBattleship.SetPosition(random.Next(10, 100),
|
|
random.Next(10, 100));
|
|
Draw();
|
|
}
|
|
|
|
private void buttonMove_Click(object sender, EventArgs e)
|
|
{
|
|
if (_drawningBattleship == null)
|
|
{
|
|
return;
|
|
}
|
|
string name = ((Button)sender)?.Name ?? string.Empty;
|
|
switch (name)
|
|
{
|
|
case "buttonUp":
|
|
_drawningBattleship.MoveTransport(DirectionType.Up);
|
|
break;
|
|
case "buttonDown":
|
|
_drawningBattleship.MoveTransport(DirectionType.Down);
|
|
break;
|
|
case "buttonLeft":
|
|
_drawningBattleship.MoveTransport(DirectionType.Left);
|
|
break;
|
|
case "buttonRight":
|
|
_drawningBattleship.MoveTransport(DirectionType.Right);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
|
|
}
|
|
} |