0

两天前我问了一个关于国际象棋游戏的问题,一位朋友向我推荐了下面的代码,我对此有疑问。就是这个链接

请看:私图Displayimage;

我不知道我应该如何将国际象棋游戏中的图像放入其中,我应该将它放在哪里?类 PiecePosition {

    public enum ChessColor
    {
        White,
        Black,
    }
    public class ChessPiece
    {
        private Image DisplayedImage;
        private ChessColor DisplayedColor;
        private Point CurrentSquare;
        private Point[] ValidMoves;
        public ChessPiece(Image image, ChessColor color)
        {
            DisplayedImage = image;
            DisplayedColor = color;
        }
    }
    public class KingPiece : ChessPiece
    {

        public KingPiece(Image image, ChessColor color)
            : base(image, color)
        {
            ValidMoves[0] = new Point(0, -1);    //  Up 1
            ValidMoves[1] = new Point(1, -1);    //  Up 1, Right 1
            ValidMoves[2] = new Point(1, 0);     //  Right 1

            ValidMoves[7] = new Point(-1, -1);  //  Left 1, Up 1
        }

    }
    public class Board
    {

        private ChessPiece[,] square;
        private int SquareWidth;    //  Number of pixels wide
        private int SquareHeight;    //  Number of pixels high



    }
}
4

2 回答 2

3

如果您想知道如何将图像与源代码一起编译然后访问它们,最简单的方法是使用Resources将图像添加到您的项目中。这使您可以轻松地将外部文件添加为项目中的嵌入式资源,这些资源将直接编译到您的可执行文件中。

要将资源添加到您的项目,请按照下列步骤操作:

  1. 在解决方案资源管理器中,右键单击要向其中添加资源的项目。选择“属性”选项,然后单击“资源”选项卡。
  2. 查看资源窗口顶部的工具栏,第一个按钮允许您选择要在项目中添加或编辑的资源类型。在您的情况下,您想要添加图像,因此从下拉菜单的选项列表中选择“图像”。
  3. 然后单击“添加资源”按钮旁边的下拉箭头。在此处,您可以添加新图像(可以在 Visual Studio 中绘制和编辑)或添加计算机上已有的现有图像。

现在您已将资源添加到项目文件中,您可以像这样在代码中使用它们(所有访问细节都由 ResourceManager 类自动处理):

System.Drawing.Bitmap kingImage = MyChessGame.Properties.Resources.KingImage;
KingPiece kingPiece = new KingPiece(kingImage, ChessColor.White);
于 2010-11-06T11:22:47.230 回答
1

您必须指定图像位置(最好是资源)。
首先,将图像添加到您的资源中。查看MSDN 中的此链接以获取更多信息。然后执行以下操作:

var KingImage = WindowsFormsApplication1.Properties.Resources.KingImage;
var kingPiece = new KingPiece(KingImage, Color.Black);
于 2010-11-06T11:08:19.180 回答