在国际象棋游戏中将图像插入代码c# [英] inserting image into codes in chess game c#

查看:174
本文介绍了在国际象棋游戏中将图像插入代码c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2天前问了一个关于国际象棋比赛的问题,朋友建议我下面的代码,我对此有疑问。这是这个链接



请参阅:
私人图片Displayimage;



我不知道如何将棋子中的棋子放入其中,我应该把它放在哪里?
class PiecePosition
{

  public enum ChessColor 
{
White,
黑色,
}
公共类ChessPiece
{
private Image DisplayedImage;
私人ChessColor DisplayedColor;
私人Point CurrentSquare;
private Point [] ValidMoves;
public ChessPiece(图片图片,ChessColor颜色)
{
DisplayedImage = image;
DisplayedColor = color;
}
}
公共类KingPiece:ChessPiece
{

公共KingPiece(图像,ChessColor颜色)
:base(图像, 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); //右1

ValidMoves [7] =新点(-1,-1); //左1,上涨1
}

}
公共类董事会
{

私人ChessPiece [,]平方;
private int SquareWidth; //像素宽
private int SquareHeight; //像素数高



}
}


解决方案

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



要向项目中添加资源,请按照这些步骤:


  1. 在解决方案资源管理器中,
    右键单击要
    的项目添加资源到。选择
    属性选项,然后单击
    资源选项卡。

  2. 查看资源窗口顶部的工具栏,第一个按钮允许您选择要在项目中添加或编辑的资源类型。在您的情况下,您想要添加图像,因此从下拉菜单中的选项列表中选择图像。

  3. 然后单击旁边的下拉箭头添加资源按钮。从这里,您可以添加新图像(可以在Visual Studio中绘制和编辑),也可以添加计算机上已有的现有图像。

现在您已经为项目文件添加了资源,您可以在代码中使用它们(所有访问详细信息都由ResourceManager类自动处理) :

  System.Drawing.Bitmap kingImage = MyChessGame.Properties.Resources.KingImage; 
KingPiece kingPiece =新的KingPiece(kingImage,ChessColor.White);


i asked a question about chess game 2days ago,and a friend suggest me the code below,and i have question about it.it was this link

please see: private image Displayimage;

i don't know how should i put images from pieces of chess game in it,and where should i put it? class 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



    }
}

解决方案

If you're wondering how you can compile the images along with your source code and then access them, the easiest way is to add the images to your project using Resources. This allows you to easily add external files as embedded resources in your project that will be compiled directly into your executable.

To add a resource to your project, follow these steps:

  1. In the Solution Explorer, right-click the project you want to add a resource to. Select "Properties" option and click the "Resources" tab.
  2. Looking at the toolbar on the top of the Resources window, the first button allows you to select the type of resources that you want to add or edit in your project. In your case, you want to add an image, so select "Image" from the list of options in the drop-down menu.
  3. Then click the drop-down arrow next to the "Add Resource" button. From here, you can either add a new image (which you can draw and edit from within Visual Studio) or add an existing image that you already have on your computer.

Now that you've added resources to your project file, you can use them in your code like this (all of the access details are handled automatically by the ResourceManager class):

System.Drawing.Bitmap kingImage = MyChessGame.Properties.Resources.KingImage;
KingPiece kingPiece = new KingPiece(kingImage, ChessColor.White);

这篇关于在国际象棋游戏中将图像插入代码c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆