如何在国际象棋游戏中实现运动

如何在国际象棋游戏中实现运动

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

问题描述

我正在学习使用Windows窗体C#制作国际象棋游戏的一个小变种,该游戏仅包括双方的棋子,我已经画过棋盘并在其中放置了棋子,但老实说我不知道​​如何开始执行移动时,先在块上单击鼠标,然后单击要移动的位置。

I am learning to make a small variant of chess game using windows forms C#, the game includes only the pawns of both sides, i have drew the board and organized the pieces on there places, but i honestly do not know how to start implementing the moves by clicking the mouse on the piece and then the location where i want to move it.

作为参考,黑卒子被命名为片断,白卒子被称为命名为pieceW

as references the black pawn is named piece, and white pawn is named 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;
        }
    }


推荐答案

一个非常简单的实现,希望您不要介意我从头开始。

Here's a really simple implementation, I hope you won't mind that I did it from scratch.

显然,它非常简单,没有拖放操作,也没有动画,但是可以实现您的要求。

Obviously it's very simple, there's no drag and drop and no animation but it fulfills your requirement.

我将详细介绍每个部分

InitializeGame

InitializeGame


  • 您确实设置了图片尺寸(显然它们应该相同)

  • 您在字典中添加了片段类型/颜色与位图之间的关系

注意:网格将被缩放因此您可以抛出任意大小的位图

Note : the grid will be scaled so you can throw any size of bitmap you like

CreateBoard,DrawGame,DrawPieces

CreateBoard, DrawGame, DrawPieces

这里没有什么例外,请注意,为了保持简单,我每次用户单击时都会这样做,但这不应该是一个大问题,毕竟不是孤岛危机:D

Nothing exceptional in there, note that for keeping things simple I do that every time a user clicks but it shouldn't be much of an issue, it's not Crysis after all :D

PickOrDropPiece

PickOrDropPiece

这是拾取/删除操作的逻辑,这确实很简单

This is the logic where picking/dropping happens, it's really trivial and I'll let you take a look by yourself.

您的代码之间的差异

我创建了一个 Board 类型,该类型可以保存碎片,并且您可以轻松地对其进行更新。

I've created a Board type which holds the pieces and that you can easily update.

注意:请勿删除 Piece 中的等式成员,它们在这里是为了帮助字典。

Note : do not remove the equality members in Piece they are here to help the dictionary.

请务必使用32具有透明边框的位图

Make sure to use 32-bit bitmaps with transparent borders

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.MouseDown += pictureBox1_MouseDown;
        }

        #region Properties

        private Board Board { get; set; }
        private Piece CurrentPiece { get; set; }
        private Dictionary<Piece, Bitmap> PieceBitmaps { get; set; }
        private int TileWidth { get; set; }
        private int TileHeight { get; set; }

        #endregion

        #region Events

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeGame();
            DrawGame();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            PickOrDropPiece(e);
            DrawGame();
        }

        #endregion

        #region Methods

        private void InitializeGame()
        {
            TileWidth = 64;
            TileHeight = 64;

            Board = new Board();

            PieceBitmaps = new Dictionary<Piece, Bitmap>();
            PieceBitmaps.Add(new Piece(PieceType.Pawn, PieceColor.Black), new Bitmap("pawnblack.png"));
            PieceBitmaps.Add(new Piece(PieceType.Pawn, PieceColor.White), new Bitmap("pawnwhite.png"));
        }

        private void DrawGame()
        {
            var tileSize = new Size(TileWidth, TileHeight);
            Bitmap bitmap = CreateBoard(tileSize);
            DrawPieces(bitmap);
            pictureBox1.Image = bitmap;
        }

        private Bitmap CreateBoard(Size tileSize)
        {
            int tileWidth = tileSize.Width;
            int tileHeight = tileSize.Height;
            var bitmap = new Bitmap(tileWidth*8, tileHeight*8);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                for (int x = 0; x < 8; x++)
                {
                    for (int y = 0; y < 8; y++)
                    {
                        Brush brush = (x%2 == 0 && y%2 == 0) || (x%2 != 0 && y%2 != 0) ? Brushes.Black : Brushes.White;
                        graphics.FillRectangle(brush, new Rectangle(x*tileWidth, y*tileHeight, tileWidth, tileHeight));
                    }
                }
            }
            return bitmap;
        }

        private void DrawPieces(Bitmap bitmap)
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                Board board = Board;
                for (int x = 0; x < 8; x++)
                {
                    for (int y = 0; y < 8; y++)
                    {
                        Piece piece = board.GetPiece(x, y);
                        if (piece != null)
                        {
                            Bitmap bitmap1 = PieceBitmaps[piece];

                            graphics.DrawImageUnscaled(bitmap1, new Point(x*TileWidth, y*TileHeight));
                        }
                    }
                }
            }
        }

        private void PickOrDropPiece(MouseEventArgs e)
        {
            Point location = e.Location;
            int x = location.X/TileWidth;
            int y = location.Y/TileHeight;
            bool pickOrDrop = CurrentPiece == null;
            if (pickOrDrop)
            {
                // Pick a piece
                Piece piece = Board.GetPiece(x, y);
                Board.SetPiece(x, y, null);
                if (piece != null)
                {
                    label1.Text = string.Format("You picked a {0} {1} at location {2},{3}", piece.Color, piece.Type, x,
                        y);
                }
                else
                {
                    label1.Text = "Nothing there !";
                }
                CurrentPiece = piece;
            }
            else
            {
                // Drop picked piece
                Board.SetPiece(x, y, CurrentPiece);
                label1.Text = string.Format("You dropped a {0} {1} at location {2},{3}", CurrentPiece.Color,
                    CurrentPiece.Type, x,
                    y);
                CurrentPiece = null;
            }
        }

        #endregion
    }

    public class Board
    {
        private readonly Piece[] _pieces;

        public Board()
        {
            _pieces = new Piece[8*8];
            PopulatePieces();
        }

        public Piece GetPiece(int x, int y)
        {
            int i = y*8 + x;
            return _pieces[i];
        }

        public void SetPiece(int x, int y, Piece piece)
        {
            int i = y*8 + x;
            _pieces[i] = piece;
        }

        private void PopulatePieces()
        {
            for (int i = 0; i < 8; i++)
            {
                SetPiece(i, 1, new Piece(PieceType.Pawn, PieceColor.Black));
                SetPiece(i, 7, new Piece(PieceType.Pawn, PieceColor.White));
            }
        }
    }

    public class Piece
    {
        private readonly PieceColor _color;
        private readonly PieceType _type;

        public Piece(PieceType type, PieceColor color)
        {
            _type = type;
            _color = color;
        }

        public PieceType Type
        {
            get { return _type; }
        }

        public PieceColor Color
        {
            get { return _color; }
        }

        protected bool Equals(Piece other)
        {
            return _color == other._color && _type == other._type;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != GetType()) return false;
            return Equals((Piece) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((int) _color*397) ^ (int) _type;
            }
        }

        public static bool operator ==(Piece left, Piece right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(Piece left, Piece right)
        {
            return !Equals(left, right);
        }
    }

    public enum PieceType
    {
        Pawn
    }

    public enum PieceColor
    {
        Black,
        White
    }
}

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

08-23 05:29