C#国际象棋棋盘 [英] C# Drawing Chess Board

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

问题描述

我正在尝试使用C#绘制8x8的国际象棋棋盘.这是我第一次绘制它.它不会画图,我也找不到我所缺少的东西.

I'm trying to draw a 8x8 chess board using C#. Here's my first attempt to draw it. It won't draw the board and I haven't found what I'm missing.

     public void Form1_Load(object sender, EventArgs e)
     {
         Bitmap bm = new Bitmap(8 * 100, 8 * 100);
         Graphics g = Graphics.FromImage(bm);
         Color color1, color2;
         for (int i = 0; i < 8; i++)
         {
             if (i % 2 == 0)
             {
                 color1 = Color.Black;
                 color2 = Color.White;
             }
             else
             {
                 color1 = Color.White;
                 color2 = Color.Black;
             }
             SolidBrush blackBrush = new SolidBrush(color1);
             SolidBrush whiteBrush = new SolidBrush(color2);

             for (int j = 0; j < 8; j++)
             {
                 if (j % 2 == 0)
                     g.FillRectangle(blackBrush, i * 100, j * 100, 100, 100);
                 else
                     g.FillRectangle(whiteBrush, i * 100, j * 100, 100, 100);
             }
         }

         g.DrawImage(bm, 150, 200);
     }  

推荐答案

在代码底部添加 BackgroundImage = bm; .

您可以很好地绘制电路板,只是不显示位图...

You are drawing the board fine, just not displaying the bitmap...

我不确定您是否有兴趣,但是我重写了这段代码.

im not sure if you're interested, but i rewrote this code.

Bitmap bm = new Bitmap(800, 800);
using (Graphics g = Graphics.FromImage(bm))
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
using (SolidBrush whiteBrush = new SolidBrush(Color.White))
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if ((j % 2 == 0 && i % 2 == 0) || (j % 2 != 0 && i % 2 != 0))
                g.FillRectangle(blackBrush, i * 100, j * 100, 100, 100);
            else if ((j % 2 == 0 && i % 2 != 0) || (j % 2 != 0 && i % 2 == 0))
                g.FillRectangle(whiteBrush, i * 100, j * 100, 100, 100);
        }
    }
    BackgroundImage = bm;
}

如果您想制作国际象棋游戏,该项目也可能会有所帮助: http://www.codeproject.com/Articles/20736/CC-CLI-Micro-Chess-Huo-Chess

also this project could help if you want to make a chess game: http://www.codeproject.com/Articles/20736/C-C-CLI-Micro-Chess-Huo-Chess

这篇关于C#国际象棋棋盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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