益智 | 用VB编程开发的围棋游戏!
VB编程易学实用、功能强大,非常有趣!
今天,我们就共同来领略用VB开发的象棋游戏如何实现。
象棋游戏实现第一步,首先是想办法用VB代码来实现上面的图形界面。
我们可以看到,棋盘界面上有方框、有图线、有文字。
这个如何来实现呢?
我们通过下面的代码来实现界面。
Option Explicit
Dim C As Byte '当前玩家
Dim cSel As Byte, osel As Byte '当前选中哪个棋子,目标哪个棋子
Dim DoCchess As Boolean '是否允许玩家选中棋子
Public a As Byte, b As Byte
Dim PubBestLocate As CHESSER '最后运算分数最高的结果
Const INITVALUE = -10000 '初始分数
Const KING = 5000
Dim ChessBoard(9, 10) As Chess '棋盘(横9列,竖10行)
Dim ChessBoardCopy(9, 10) As Chess '棋盘的复本
'Rem 根据ChessBoard数组显示棋子
Public Function DisplayImageCchess()
Dim i As Byte, j As Byte, k As Byte
k = 0
For j = 1 To 10
For i = 1 To 9
Cchess1(k).Value = ChessBoard(i, j).Value ''得知屏幕位置为何棋
If ChessBoard(i, j).Value = 0 Then
Cchess1(k).NoneCchess = True
Else
Cchess1(k).Visible = True
Cchess1(k).NoneCchess = False
Cchess1(k).Text = CchessWord(ChessBoard(i, j).Value)
If Int(ChessBoard(i, j).Value / 100) = 1 Then Cchess1(k).FillColor = vbBlue Else Cchess1(k).FillColor = vbRed ''颜色
End If
k = k + 1
Next i
Next j
End Function
单击新局按钮时,重置棋盘
Rem 重置棋盘
Private Sub SetCchess()
Call ClearChessBoard ''棋盘无子
ChessBoard(5, 1).Value = 107 ''黑帅
ChessBoard(4, 1).Value = 106 ''黑士
ChessBoard(6, 1).Value = 106 ''黑士
ChessBoard(3, 1).Value = 105 ''黑象
ChessBoard(7, 1).Value = 105 ''黑象
ChessBoard(2, 1).Value = 103 ''黑马
ChessBoard(8, 1).Value = 103 ''黑马
ChessBoard(1, 1).Value = 104 ''黑车
ChessBoard(9, 1).Value = 104 ''黑车
ChessBoard(2, 3).Value = 102 ''黑炮
ChessBoard(8, 3).Value = 102 ''黑炮
ChessBoard(1, 4).Value = 101 ''黑卒
ChessBoard(3, 4).Value = 101 ''黑卒
ChessBoard(5, 4).Value = 101 ''黑卒
ChessBoard(7, 4).Value = 101 ''黑卒
ChessBoard(9, 4).Value = 101 ''黑卒
ChessBoard(5, 10).Value = 207 ''红帅
ChessBoard(4, 10).Value = 206 ''红士
ChessBoard(6, 10).Value = 206 ''红士
ChessBoard(3, 10).Value = 205 ''红象
ChessBoard(7, 10).Value = 205 ''红象
ChessBoard(2, 10).Value = 203 ''红马
ChessBoard(8, 10).Value = 203 ''红马
ChessBoard(1, 10).Value = 204 ''红车
ChessBoard(9, 10).Value = 204 ''红车
ChessBoard(2, 8).Value = 202 ''红炮
ChessBoard(8, 8).Value = 202 ''红炮
ChessBoard(1, 7).Value = 201 ''红卒
ChessBoard(3, 7).Value = 201 ''红卒
ChessBoard(5, 7).Value = 201 ''红卒
ChessBoard(7, 7).Value = 201 ''红卒
ChessBoard(9, 7).Value = 201 ''红卒
End Sub
'开始下棋
Private Sub Command1_Click()
Dim CV As Integer
Command1.Enabled = False
PubBestLocate.Value = INITVALUE
'If C = 2 Then Call MsgBox("现在轮到红方(玩家)下棋!", vbOKOnly + 32, "提示:"): Exit Sub
C = 1
If C = 1 Or C = 2 Then
CV = Search(C, Val(Text1.Text) * 2, True, 10000) ''运算C方
''下棋
With PubBestLocate
Call MsgBox(Str(.Initx) & "," & Str(.Inity) & " 到 " & Str(.ObjX) & "," & Str(.ObjY), vbOKOnly)
Call MoveCchess(.Initx, .Inity, .ObjX, .ObjY)
End With
Text2.Text = Str(CV)
End If
Call DisplayImageCchess ''显示
Command1.Enabled = True
End Sub
往期回顾
●!
●
●
●