启动VB6,在窗体上放一个 picturebox 和一个 label 控件,不用设置属性,复制下面代码到窗口代码区即可:
===============================================
Option Explicit
Dim BoxValue(3, 3) As Integer '格子的数值
Dim Score As Long '得分
'按键
===============================================
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
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
vb所有代码End Sub
'开始游戏
Private Sub NewGame()
Label1.Move Picture1.Left, Picture1.Left
Label1.FontSize = 24
Label1.BorderStyle = 0
Label1 = "得分:0"
Call NewGame
vb所有代码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
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
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
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论