国王在国际象棋游戏中的动作 [英] king movements on chess game

查看:139
本文介绍了国王在国际象棋游戏中的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我对下面的代码有疑问.该代码用于国际象棋游戏中的国王棋子,我希望此代码中有有效的国王移动和国王在棋盘上的形象,但它不起作用.你能帮我这个忙吗?我有两个问题.

1.我应该为validmoves工作定义什么,因为编译器在validmoves上给出错误:由于其保护级别而无法访问吗?

2.显示国王的图像该怎么办(si​​yahsah1是国王图像的名称),或者我不显示它的图像又有什么错误呢?

如果下面的代码无济于事,我是否有权上传和链接我的项目?

谢谢

Hi friends, I have a question about the code below that I have. This code is for the king piece in chess game and I want to have valid moves of king and the image of the king on the board in this codes, but it doesnt work. Can you please help me about this? I have two questions.

1.Should I define anything for validmoves to work because the compiler gives error on validmoves:inaccessible due to its protection level?

2.what should I do for showing the image of king(siyahsah1 is the name of the kingimage) or what is my mistake that it doesnt show the image of it?

If the code below below doesnt help have do I permission to upload and link my project?

Thanks

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

      System.Drawing.Bitmap siyahsah1 = chess6.Properties.Resources.siyahsah1;
      KingPiece kingPiece = new KingPiece(siyahsah1, ChessColor.White);
  }

推荐答案

1)将ValidMoves的定义从private更改为protected-这使派生类可以看到它.
有关详细信息,请参见此处 [
为您的Form(或面板-您想要在其上显示的物件)添加事件处理程序,然后在PaintEventArgs Graphics参数上使用Graphics.DrawImage:
1) Change the definition of ValidMoves from privateto protected - this allows it to be visible to derived classes.
See here for details[^]

2) You will have to derive ChessPiece from a control, and handle the paint event to display the image, or modify your form code to display the appropriate pieces.
I would go with the first option, since the chess piece is something that will be interacted with by the user.

"how can i handle the paint event to display image?thanks - arashmobileboy 4 mins ago"

Add an event handler for your Form (or panel - what ever you want to display the piece on) and then use Graphics.DrawImage on the PaintEventArgs Graphics parameter:
private void panel1_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.DrawImage(myChessPeiceImage, myChessPeiceLocation);
    }



很抱歉,您的答案花了很长时间.我应该在您的代码中放置siaysah1(kingimage的名称)而不是myChessPeiceImage吗?因为我放置了siayhsah1,但是它说siaysah1在当前上下文中不存在(我添加了siyahsah1图片至资源):



"sorry that it took long time to comment on your answer. should i put siaysah1(the name of kingimage) instead of myChessPeiceImage in your code?because i put the siayhsah1 but it says siaysah1 doesnt exist in the current context(i added the siyahsah1 pic to resources):

private void ChessForm_Paint(object sender, PaintEventArgs e)
   {
    e.Graphics.DrawImage(siyahsah1,chess6.Properties.Resources.siyahsah1);
   }

-5分钟前arashmobileboy"

假设您已将图像文件设置为名为"myPicture"的嵌入式资源,请使用:

- arashmobileboy 5 mins ago"

Assuming you have set an image file as an embedded resource called "myPicture", use:

Image myImage = Properties.Resources.myPicture;
e.Graphics.DrawImage(myImage, new Point(0, 0));


这篇关于国王在国际象棋游戏中的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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