【问题标题】:Chess Game Project VB.net国际象棋项目VB.net
【发布时间】:2014-01-29 00:10:22
【问题描述】:

我正在 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

结束类

更新....

了解典当逻辑 我的问题是 KNIGHT 或 ROOT 逻辑 我可以将我的车向前左右向后移动,但是如果有一个棋子挡住了车的路怎么办。它仍然会跳转到跳过棋子的图片框,这是我的代码

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 的棋盘会更好...这就是为什么我对 corY1st 和 corX1st 加减 76 以到达第二个坐标...

棋子格挡很痛苦...稍后会更新

【问题讨论】:

  • 你可能需要几个
  • 我会,但现在我把它放在 DragDrop 事件中
  • 你应该有一个函数来获取当前棋子并返回所有可用的动作。这样您只需要查看此列表即可。
  • 如何检测是否有一块挡住我的路

标签: vb.net input logic chess


【解决方案1】:

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

【讨论】:

  • 将棋子逻辑放入 DragDrop
猜你喜欢
  • 2022-06-17
  • 1970-01-01
  • 2014-03-02
  • 2013-05-24
  • 2015-05-02
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多