国际象棋游戏项目VB.net
我在vb 2010中制作了一个国际象棋游戏,这里是我的代码UPDATED
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
末班
UPDATE ....
GOT THE PAWN LOGIC我的问题是一个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代表只是成像PICTUREBOXES作为棋盘均匀间隔和大小70,70 ...这就是为什么我加号和减号76 corY1st和corX1st到达第二坐标...
CHESS PIECE BLOCKING是一个痛苦......稍后会更新
您可以处理图像的OnClick事件,然后编写代码以突出显示它。 一旦你知道哪个部分被选中,然后处理Onclick,以便在其中放置所选片段。 国际象棋游戏的实际逻辑应该在其自己的课堂内。
链接地址: http://www.djcxy.com/p/14815.html