mirror of
https://github.com/jie65535/ChineseChess.git
synced 2024-07-27 18:55:00 +08:00
1. 增加 自绘棋盘方法
This commit is contained in:
parent
a088f3f448
commit
b5760ff8eb
@ -5,6 +5,16 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ChessMove
|
public class ChessMove
|
||||||
{
|
{
|
||||||
|
public ChessMove(ChessCamp camp, ChessType chess, ChessType? killed, ChessboardPosition start, ChessboardPosition end, string text)
|
||||||
|
{
|
||||||
|
Camp=camp;
|
||||||
|
Chess=chess;
|
||||||
|
Killed=killed;
|
||||||
|
Start=start;
|
||||||
|
End=end;
|
||||||
|
Text=text;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 阵营
|
/// 阵营
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -50,13 +50,29 @@ namespace ChineseChess
|
|||||||
|
|
||||||
protected override void OnClick(EventArgs e)
|
protected override void OnClick(EventArgs e)
|
||||||
{
|
{
|
||||||
Chessman selected = null;
|
try
|
||||||
if (_CurrMouseOverPos.HasValue)
|
|
||||||
selected = _Game.Chessboard.GetChessmanByPos(_CurrMouseOverPos.Value);
|
|
||||||
if (_CurrSelectedChessman != selected)
|
|
||||||
{
|
{
|
||||||
_CurrSelectedChessman = selected;
|
Chessman selected = null;
|
||||||
Invalidate();
|
if (_CurrMouseOverPos.HasValue)
|
||||||
|
selected = _Game.Chessboard.GetChessmanByPos(_CurrMouseOverPos.Value);
|
||||||
|
if (_CurrSelectedChessman == null)
|
||||||
|
{
|
||||||
|
if (selected != null)
|
||||||
|
{
|
||||||
|
_CurrSelectedChessman = selected;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (_CurrSelectedChessman != selected)
|
||||||
|
{
|
||||||
|
_Game.Chessboard.PushMove(new ChessMove(_CurrSelectedChessman.Camp, _CurrSelectedChessman.Type, selected?.Type, _CurrSelectedChessman.Position, _CurrMouseOverPos.Value, string.Empty));
|
||||||
|
_CurrSelectedChessman = null;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
base.OnClick(e);
|
base.OnClick(e);
|
||||||
}
|
}
|
||||||
@ -71,6 +87,95 @@ namespace ChineseChess
|
|||||||
DrawImageByCentre(g, _ResHelper.GetChessmanBitmap(chessman.Type, chessman.Camp), GetChessboardGridPoint(pos));
|
DrawImageByCentre(g, _ResHelper.GetChessmanBitmap(chessman.Type, chessman.Camp), GetChessboardGridPoint(pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void DrawChessborad(Graphics g, Rectangle rect, Pen pen)
|
||||||
|
{
|
||||||
|
Size blockCount = new Size(8, 9);
|
||||||
|
Size cellSize = new Size(rect.Width / blockCount.Width, rect.Height / blockCount.Height);
|
||||||
|
Point begin = default, end = default;
|
||||||
|
|
||||||
|
// 绘制横线
|
||||||
|
begin.X = rect.Left;
|
||||||
|
end.X = rect.Right;
|
||||||
|
for (int i = 0; i <= blockCount.Height; i++)
|
||||||
|
{
|
||||||
|
end.Y = begin.Y = rect.Top + cellSize.Height * i;
|
||||||
|
g.DrawLine(pen, begin, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绘制纵线
|
||||||
|
begin.Y = rect.Top;
|
||||||
|
end.Y = rect.Bottom;
|
||||||
|
g.DrawLine(pen, rect.Left, begin.Y, rect.Left, end.Y);
|
||||||
|
g.DrawLine(pen, rect.Right, begin.Y, rect.Right, end.Y);
|
||||||
|
int upEndY = rect.Top + cellSize.Height * (blockCount.Height / 2);
|
||||||
|
int dnBegY = rect.Top + cellSize.Height * (blockCount.Height / 2 + 1);
|
||||||
|
for (int i = 1; i < blockCount.Width; i++)
|
||||||
|
{
|
||||||
|
begin.X = end.X = rect.Left + cellSize.Width * i;
|
||||||
|
g.DrawLine(pen, begin.X, begin.Y, end.X, upEndY);
|
||||||
|
g.DrawLine(pen, begin.X, dnBegY, end.X, end.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绘制斜线
|
||||||
|
Point centerPoint = new Point(rect.Left + rect.Width / 2, rect.Top + cellSize.Height);
|
||||||
|
int left = centerPoint.X - cellSize.Width;
|
||||||
|
int right = centerPoint.X + cellSize.Width;
|
||||||
|
int up = centerPoint.Y - cellSize.Height;
|
||||||
|
int down = centerPoint.Y + cellSize.Height;
|
||||||
|
g.DrawLine(pen, left, up, right, down);
|
||||||
|
g.DrawLine(pen, right, up, left, down);
|
||||||
|
|
||||||
|
centerPoint.Y = rect.Bottom - cellSize.Height;
|
||||||
|
up = centerPoint.Y - cellSize.Height;
|
||||||
|
down = centerPoint.Y + cellSize.Height;
|
||||||
|
g.DrawLine(pen, left, up, right, down);
|
||||||
|
g.DrawLine(pen, right, up, left, down);
|
||||||
|
|
||||||
|
// 绘制对位
|
||||||
|
Size offsetSize = new Size(cellSize.Width / 10, cellSize.Height / 10);
|
||||||
|
Point[] begOffset = new Point[4]
|
||||||
|
{
|
||||||
|
new Point(-offsetSize.Width, -offsetSize.Height),
|
||||||
|
new Point(offsetSize.Width, -offsetSize.Height),
|
||||||
|
new Point(offsetSize.Width, offsetSize.Height),
|
||||||
|
new Point(-offsetSize.Width, offsetSize.Height),
|
||||||
|
};
|
||||||
|
Size lineSize = new Size(cellSize.Width / 5, cellSize.Height / 5);
|
||||||
|
Point[,] endOffsets = new Point[4, 2]
|
||||||
|
{
|
||||||
|
{ new Point(0, -lineSize.Height), new Point(-lineSize.Width, 0) },
|
||||||
|
{ new Point(0, -lineSize.Height), new Point(lineSize.Width, 0) },
|
||||||
|
{ new Point(0, lineSize.Height), new Point(lineSize.Width, 0) },
|
||||||
|
{ new Point(0, lineSize.Height), new Point(-lineSize.Width, 0) },
|
||||||
|
};
|
||||||
|
|
||||||
|
void drawTarget(Point point)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
begin = new Point(point.X + begOffset[i].X, point.Y + begOffset[i].Y);
|
||||||
|
for (int j = 0; j < 2; j++)
|
||||||
|
{
|
||||||
|
end = new Point(begin.X + endOffsets[i, j].X, begin.Y + endOffsets[i, j].Y);
|
||||||
|
if (rect.Contains(begin) && rect.Contains(end))
|
||||||
|
g.DrawLine(pen, begin, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 画卒线 兵位
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
drawTarget(new Point(rect.Left + i*2 * cellSize.Width, rect.Top + 3 * cellSize.Height));
|
||||||
|
drawTarget(new Point(rect.Left + i*2 * cellSize.Width, rect.Top + 6 * cellSize.Height));
|
||||||
|
}
|
||||||
|
// 画三线 炮位
|
||||||
|
drawTarget(new Point(rect.Left + cellSize.Width, rect.Top + 2 * cellSize.Height));
|
||||||
|
drawTarget(new Point(rect.Right - cellSize.Width, rect.Top + 2 * cellSize.Height));
|
||||||
|
drawTarget(new Point(rect.Left + cellSize.Width, rect.Bottom - 2 * cellSize.Height));
|
||||||
|
drawTarget(new Point(rect.Right - cellSize.Width, rect.Bottom - 2 * cellSize.Height));
|
||||||
|
}
|
||||||
|
|
||||||
private Point GetChessboardGridPoint(ChessboardPosition position)
|
private Point GetChessboardGridPoint(ChessboardPosition position)
|
||||||
{
|
{
|
||||||
return new Point(position.Col * _ResHelper.ChessboardCellSize.Width + _ResHelper.ChessboardOffset.Width,
|
return new Point(position.Col * _ResHelper.ChessboardCellSize.Width + _ResHelper.ChessboardOffset.Width,
|
||||||
@ -98,6 +203,8 @@ namespace ChineseChess
|
|||||||
|
|
||||||
protected override void OnPaintBackground(PaintEventArgs pevent)
|
protected override void OnPaintBackground(PaintEventArgs pevent)
|
||||||
{
|
{
|
||||||
|
//pevent.Graphics.FillRectangle(Brushes.White, pevent.ClipRectangle);
|
||||||
|
//DrawChessborad(pevent.Graphics, new Rectangle(((Point)_ResHelper.ChessboardOffset), _ResHelper.ChessboardGridSize), Pens.Black);
|
||||||
pevent.Graphics.DrawImage(_ChessboardBitmap, 0, 0, _ChessboardBitmap.Width, _ChessboardBitmap.Height);
|
pevent.Graphics.DrawImage(_ChessboardBitmap, 0, 0, _ChessboardBitmap.Width, _ChessboardBitmap.Height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user