请懂vb的大神帮忙

younger. 2019-06-05 12:30:02
这是设计思路2048游戏的代码 请大神帮我翻译下 就是说一下哪一部分是用来做什么的 分几个模块这样子 谢谢 Option Explicit


Dim BoxValue(3, 3) As Integer '格子的数值
Dim Score As Long '得分


'按键
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
Call MoveBox(1)
Case vbKeyRight
Call MoveBox(2)
Case vbKeyUp
Call MoveBox(3)
Case vbKeyDown
Call MoveBox(4)
Case vbKeySpace
Call NewGame
End Select
End Sub


Private Sub Form_Load()
Me.Width = 8000
Me.Height = 9000
Me.Caption = "2048"


Picture1.Move (Me.ScaleWidth - 6810) / 2, 1200, 6810, 6810
Picture1.Appearance = 0
Picture1.BackColor = RGB(128, 128, 128)
Picture1.FontSize = 32
Picture1.AutoRedraw = True
FontSize = Picture1.FontSize


Label1.AutoSize = True
Label1.Move Picture1.Left, Picture1.Left
Label1.FontSize = 24
Label1.BorderStyle = 0
Label1 = "得分:0"




Call NewGame
End Sub


'开始游戏
Private Sub NewGame()
Dim R As Integer, C As Integer
Dim L As Integer, T As Integer


For R = 1 To 4
For C = 1 To 4
L = (C - 1) * 110 + 10
T = (R - 1) * 110 + 10
Picture1.Line (L, T)-(L + 100, T + 100), RGB(200, 200, 200), BF
BoxValue(R - 1, C - 1) = 0
Next
Next


NewBox
NewBox
End Sub


'画格子
Private Sub DrawBox(ByVal N As Integer, ByVal R As Integer, ByVal C As Integer)
Dim L As Integer, T As Integer
Dim tmpStr As String
Dim W As Integer


L = C * 110 + 10
T = R * 110 + 10


If N = 0 Then
Picture1.Line (L, T)-(L + 100, T + 100), RGB(200, 200, 200), BF
Else
Picture1.Line (L, T)-(L + 100, T + 100), BoxColor(N), BF
tmpStr = Trim(Str(N))
W = TextWidth("0") / Screen.TwipsPerPixelX
Picture1.CurrentX = L + (100 - TextWidth(tmpStr) / Screen.TwipsPerPixelX) / 2 - W
Picture1.CurrentY = T + (100 - TextHeight(tmpStr) / Screen.TwipsPerPixelY) / 2


Picture1.Print N
End If


BoxValue(R, C) = N
End Sub

'移动格子
Private Sub MoveBox(ByVal Fx As Integer)
Dim B As Integer, N As Integer, S As Integer
Dim R As Integer, C As Integer, K As Integer
Dim bMove As Boolean




If Fx < 3 Then '左右移动
If Fx = 1 Then
B = 1: N = 3: S = 1
Else
B = 2: N = 0: S = -1
End If


For R = 0 To 3
K = IIf(Fx = 1, 0, 3)
For C = B To N Step S
If BoxValue(R, C) > 0 Then
If (BoxValue(R, C) = BoxValue(R, K)) Then
DrawBox BoxValue(R, C) * 2, R, K
DrawBox 0, R, C
Score = Score + BoxValue(R, K)
If BoxValue(R, K) = 2048 Then
MsgBox "哇塞!太厉害了!佩服佩服~", vbInformation
End If
bMove = True
Else
If BoxValue(R, K) > 0 Then
K = K + S
If K <> C Then
DrawBox BoxValue(R, C), R, K
DrawBox 0, R, C
bMove = True
End If
Else
DrawBox BoxValue(R, C), R, K
DrawBox 0, R, C
bMove = True
End If
End If
End If
Next C
Next R
Else '上下移动
If Fx = 3 Then
B = 1: N = 3: S = 1
Else
B = 2: N = 0: S = -1
End If


For C = 0 To 3
K = IIf(Fx = 3, 0, 3)
For R = B To N Step S
If BoxValue(R, C) > 0 Then
If BoxValue(R, C) = BoxValue(K, C) Then
DrawBox BoxValue(R, C) * 2, K, C
DrawBox 0, R, C
Score = Score + BoxValue(K, C)
If BoxValue(R, K) = 2048 Then
MsgBox "哇塞!太厉害了!佩服佩服~", vbInformation
End If
bMove = True
Else
If BoxValue(K, C) > 0 Then
K = K + S
If K <> R Then
DrawBox BoxValue(R, C), K, C
DrawBox 0, R, C
bMove = True
End If
Else
DrawBox BoxValue(R, C), K, C
DrawBox 0, R, C
bMove = True
End If
End If
End If
Next R
Next C
End If


If bMove Then
Label1 = "得分:" & Score
NewBox


' 检查死局
For R = 0 To 3
For C = 0 To 3
If BoxValue(R, C) = 0 Then Exit Sub
If R < 3 Then If BoxValue(R, C) = BoxValue(R + 1, C) Then Exit Sub
If C < 3 Then If BoxValue(R, C) = BoxValue(R, C + 1) Then Exit Sub
Next
Next


MsgBox "无路可走了~~~下次好运!", vbInformation


Call NewGame
End If
End Sub


'产生新方格
Private Sub NewBox()
Dim R As Integer, C As Integer


Randomize
R = Int(Rnd * 4)
C = Int(Rnd * 4)


Do While BoxValue(R, C) > 0
R = Int(Rnd * 4)
C = Int(Rnd * 4)
Loop


BoxValue(R, C) = 2
DrawBox 2, R, C
End Sub


'方格颜色
Private Function BoxColor(ByVal N As Integer) As Long
Select Case N
Case 2
BoxColor = &HC0E0FF
Case 4
BoxColor = &H80C0FF
Case 8
BoxColor = &H80FFFF
Case 16
BoxColor = &HC0FFC0
Case 32
BoxColor = &HFFFF80
Case 64
BoxColor = &HFFC0C0
Case 128
BoxColor = &HFF8080
Case 256
BoxColor = &HFFC0FF
Case 512
BoxColor = &HFF80FF
Case 1024
BoxColor = &HC0C0FF
Case 2048
BoxColor = &H8080FF
End Select


End Function
...全文
113 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
现在还是人类 2019-06-09
  • 打赏
  • 举报
回复
拿来主义,估计就没想搞明白什么回事,只想着怎么改就是自己的了
threenewbee 2019-06-06
  • 打赏
  • 举报
回复
这不都有注释了么,还有什么不懂的。
脆皮大雪糕 2019-06-05
  • 打赏
  • 举报
回复
在你看不懂的地方加一个断点,然后程序跑起来,玩这个游戏,玩到某一个操作的时候触发断点,这时候单步继续,观察各个变量以及界面展示情况,基本上就能搞明白。 看人家的代码是最基本的既能。花点时间能学到很多东西。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧