国际象棋项目VB.net

国际象棋项目VB.net,vb.net,input,logic,chess,Vb.net,Input,Logic,Chess,我正在用vb 2010制作一个国际象棋游戏,下面是我的代码更新 Imports System.Drawing.Image Public Class Form1 Dim MD As Boolean Dim BLACK(5) As Image Dim WHITE(5) As Image Dim corX1st As String Dim corY1st As String Dim corX2nd As String Dim corY2nd As String Dim PIC As Image Di

我正在用vb 2010制作一个国际象棋游戏,下面是我的代码更新

Imports System.Drawing.Image
Public Class Form1
Dim MD As Boolean
Dim BLACK(5) As Image
Dim WHITE(5) As Image
Dim corX1st As String
Dim corY1st As String
Dim corX2nd As String
Dim corY2nd As String

Dim PIC As Image
Dim box As PictureBox

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    BLACK(0) = My.Resources.PB
    BLACK(1) = My.Resources.RB
    BLACK(2) = My.Resources.HB
    BLACK(3) = My.Resources.BB
    BLACK(4) = My.Resources.QB
    BLACK(5) = My.Resources.KB

    WHITE(0) = My.Resources.PW
    WHITE(1) = My.Resources.RW
    WHITE(2) = My.Resources.HW
    WHITE(3) = My.Resources.BW
    WHITE(4) = My.Resources.QW
    WHITE(5) = My.Resources.KW

    a1.Image = BLACK(1)
    b1.Image = BLACK(2)
    c1.Image = BLACK(3)
    d1.Image = BLACK(4)
    e1.Image = BLACK(5)
    f1.Image = BLACK(3)
    g1.Image = BLACK(2)
    h1.Image = BLACK(1)
    a2.Image = BLACK(0)
    b2.Image = BLACK(0)
    c2.Image = BLACK(0)
    d2.Image = BLACK(0)
    e2.Image = BLACK(0)
    f2.Image = BLACK(0)
    g2.Image = BLACK(0)
    h2.Image = BLACK(0)

    a8.Image = WHITE(1)
    b8.Image = WHITE(2)
    c8.Image = WHITE(3)
    d8.Image = WHITE(4)
    e8.Image = WHITE(5)
    f8.Image = WHITE(3)
    g8.Image = WHITE(2)
    h8.Image = WHITE(1)
    a7.Image = WHITE(0)
    b7.Image = WHITE(0)
    c7.Image = WHITE(0)
    d7.Image = WHITE(0)
    e7.Image = WHITE(0)
    f7.Image = WHITE(0)
    g7.Image = WHITE(0)
    h7.Image = WHITE(0)

    For Each a As Control In Me.Controls
        If TypeOf a Is PictureBox Then
            AddHandler a.MouseDown, AddressOf PictureBox_MouseDown
            AddHandler a.MouseMove, AddressOf PictureBox_MouseMove
            AddHandler a.DragEnter, AddressOf PictureBox_DragEnter
            AddHandler a.DragDrop, AddressOf PictureBox_DragDrop
            a.AllowDrop = True
        End If
    Next
End Sub
Private Sub PictureBox_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
    Dim a As PictureBox = DirectCast(sender, PictureBox)
    PIC = a.Image
    box = a
    corX1st = a.Location.X.ToString
    corY1st = a.Location.Y.ToString
    txt1.Text = corX1st
    txt2.Text = corY1st
    If Not a.Image Is Nothing Then
        MD = True
        TextBox1.Text = MD
        If a.Image Is BLACK(0) Then
        End If
    Else
        MD = False
        TextBox1.Text = MD
    End If
End Sub
Private Sub PictureBox_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
    If MD = True Then
        box.DoDragDrop(PIC, DragDropEffects.Move)
        'MsgBox("tae0")
    End If
    MD = False

End Sub
Private Sub PictureBox_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs)
    If e.Data.GetDataPresent(DataFormats.Bitmap) Then
        e.Effect = DragDropEffects.Move
    Else
        e.Effect = DragDropEffects.None
    End If
End Sub
Private Sub PictureBox_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs)
    If Not e.KeyState = 8 Then
        Dim a As PictureBox = DirectCast(sender, PictureBox)
        corX2nd = a.Location.X.ToString
        corY2nd = a.Location.Y.ToString
        txt1.Text = corX2nd
        txt2.Text = corY2nd

        If Not a Is box Then
            If PIC Is BLACK(0) Then
                MsgBox("pawnblack")
                If corY1st - corY2nd = 76 And corX1st = corX2nd Then
                    If a.Image Is Nothing Then
                        a.Image = e.Data.GetData(DataFormats.Bitmap)
                        box.Image = Nothing
                    Else
                        MsgBox("There is a piece there")
                    End If
                ElseIf corY1st - corY2nd = 76 And corX1st - corX2nd = 76 Or corY1st - corY2nd = 76 And corX2nd - corX1st = 76 Then
                    a.Image = e.Data.GetData(DataFormats.Bitmap)
                    box.Image = Nothing

                    MsgBox("good")
                End If

            ElseIf PIC Is BLACK(1) Then
                a.Image = e.Data.GetData(DataFormats.Bitmap)
                box.Image = Nothing
            ElseIf PIC Is BLACK(2) Then
            ElseIf PIC Is BLACK(3) Then
            ElseIf PIC Is BLACK(4) Then
            ElseIf PIC Is BLACK(5) Then
            End If

        Else
            MsgBox(" DO nothing")
        End If
        End If
End Sub
末级

更新

有典当逻辑吗 我的问题是一个骑士或根逻辑 我可以前后左右移动我的车,但是如果有一个棋子挡住了车的路怎么办。它仍然会跳到棋子上方的图片框这是我的代码

ElseIf PIC Is BLACK(1) Then
                If corX1st < corX2nd And corY1st = corY2nd Then
                    For ew As Integer = corX1st To 597
                        If corX1st <= corX2nd And corY1st = corY1st Then
                            a.Image = e.Data.GetData(DataFormats.Bitmap)
                            box.Image = Nothing
                            corX1st += 76
                            MsgBox("wee" + corX1st)
                            Exit For
                        End If
                    Next
                ElseIf corX2nd <= corX1st And corY1st = corY2nd Then
                    For aw As Integer = 65 To corX1st
                        If corX1st >= corX2nd And corY1st = corY1st Then
                            a.Image = e.Data.GetData(DataFormats.Bitmap)
                            box.Image = Nothing
                            corX1st -= 76
                            MsgBox("waa" + corX1st)
                            Exit For
                        End If
                    Next
                ElseIf corY1st <= corY2nd And corX1st = corX2nd Then
                    For ow As Integer = corY1st To 544
                        If corY1st <= corY2nd And corX1st = corX2nd Then
                            a.Image = e.Data.GetData(DataFormats.Bitmap)
                            box.Image = Nothing
                            corY1st += 76
                            MsgBox("woo" + corY1st)
                            Exit For
                        Else
                        End If
                    Next
                ElseIf corY1st >= corY2nd And corX1st = corX2nd Then
                    For iw As Integer = 12 To corY1st
                        If corY1st >= corY2nd And corX1st = corX2nd Then
                            a.Image = e.Data.GetData(DataFormats.Bitmap)
                            box.Image = Nothing
                            corY1st -= 76
                            MsgBox("wii" + corY1st)
                            Exit For
                        Else
                        End If
                    Next
                End If
如果我可以发布一个屏幕,但需要1500次重复,将图片框成像为棋盘,间隔均匀,大小为70,70。。。这就是为什么我加减76到corY1st和corX1st到第二个坐标


棋子挡是一种痛苦。。。稍后将更新

您可以处理图像的OnClick事件,然后编写代码以突出显示它。一旦您知道选择了哪一块,然后处理Onclick以确定要将所选块放置在何处。不过,国际象棋游戏的实际逻辑应该在它自己的类中

你可能需要几个类,我会,但现在我把它放在DragDrop事件上,你应该有一个函数,它接受当前的一个片段并返回所有可用的移动。这样,你只需要看看这个列表。如何检测是否有一个棋子挡住了我的方向将棋子逻辑放入DragDrop