Chess Game Project VB.net
I am making a chess game in vb 2010 here's my codes 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
End Class
UPDATE....
GOT THE PAWN LOGIC my problem is that of a KNIGHT or ROOT logic i can move my rook forward backward left right but what if there is a chess piece blocking the way of a rook. it still goes to the picture box jumping over the chess piece here's my code
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
it would be better if i can post a screeny but need 1500 rep just imaging PICTUREBOXES as chessboard evenly spaced and size 70,70... thats is why i plus and minus 76 to corY1st and corX1st to get to the 2nd coordinates...
CHESS PIECE BLOCKING is a pain... will update later
You could handle the OnClick event for an image and then write code to highlight it. Once you know which piece is selected then handle the Onclick for where you want to put the selected piece. The actual logic for the chess game should be inside its own class though.
链接地址: http://www.djcxy.com/p/14816.html上一篇: 将国际象棋引擎连接到使用Javascript制作的现有GUI
下一篇: 国际象棋游戏项目VB.net