我正在学习使用windows forms C#制作一个小型的国际象棋游戏,该游戏只包含双方的兵。我已经画好了棋盘并将棋子组织到了相应位置,但是我不知道如何开始通过点击鼠标来实现移动棋子到目标位置。
参考黑色兵为"piece",白色兵为"pieceW"。
以下是我的棋盘代码。
参考黑色兵为"piece",白色兵为"pieceW"。
以下是我的棋盘代码。
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 AIchess
{
public partial class Form1 : Form
{
static System.Drawing.Bitmap piece = AIchess.Properties.Resources.piece;
ChessPiece Piece = new ChessPiece(piece, ChessColor.Black);
static System.Drawing.Bitmap pieceW = AIchess.Properties.Resources.pieceW;
ChessPiece PieceW = new ChessPiece(pieceW, ChessColor.White);
Square[,] square = new Square[8, 8];
public Form1()
{
InitializeComponent();
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
this.square[i, j] = new Square();
this.square[i, j].BackColor = System.Drawing.SystemColors.ActiveCaption;
this.square[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.square[i, j].Location = new System.Drawing.Point(57 + i * 60, 109 + j * 60);
this.square[i, j].Name = i.ToString()+j.ToString();
this.square[i, j].Size = new System.Drawing.Size(60, 60);
this.square[i, j].TabIndex = 2;
this.square[i, j].TabStop = false;
this.Controls.Add(this.square[i, j]);
if (j == 1)
{
this.square[i, j].Image = piece;
this.square[i, j].AllocatedBy = "black";
}
if (j == 6)
{
this.square[i, j].Image = pieceW;
this.square[i, j].AllocatedBy = "white";
}
if (((i+j) % 2) ==0)
this.square[i, j].BackColor = Color.RoyalBlue;
else
this.square[i, j].BackColor = Color.LightBlue;
}
}
}
}
public enum ChessColor
{
White,
Black,
};
class ChessPiece
{
private Image DisplayedImage;
private ChessColor DisplayedColor;
private Point CurrentSquare;
public ChessPiece(Image image, ChessColor color)
{
DisplayedImage = image;
DisplayedColor = color;
}
}
class Square:PictureBox
{
private bool color;
public string AllocatedBy;
}
}
ChessPiece
类是什么样子的? - Batu.Khan}; 类 ChessPiece { private Image DisplayedImage; private ChessColor DisplayedColor; private Point CurrentSquare; public ChessPiece(Image image, ChessColor color) { DisplayedImage = image; DisplayedColor = color; } }
- Thair AbdallaBoard
和Piece
这样不含有 UI 元素引用的类。然后编写代码将数据模型渲染到屏幕上。将逻辑与 UI 分离是一个通常很好的想法,如果你尝试实现人工智能,这也将变得必要。 - default.kramer