武汉今天便利店:怎么用VB做挖地雷游戏?

来源:百度文库 编辑:高校问答 时间:2024/04/20 08:38:39
我想知道制作方法和代码

用到的技术有以下几个:
1.动态图像切割
2.动态控件
3.算法

完整的代码我给你,能不能看明白就靠你自己了,我写程序从来不注释

Option Explicit
Dim h As Integer, w As Integer
Dim sizeofpicture As Integer
Dim bombnum As Integer
Dim bomb() As Integer
Dim view() As Integer
Dim onebuttonflag As Integer
Dim bactive As Boolean
Dim timeactive As Boolean
Dim oldx As Integer, oldy As Integer
Dim questres
Public Sub drawall()
Dim i As Integer, j As Integer
For i = 0 To w - 1
For j = 0 To h - 1
bm.PaintPicture bakimg, i, j, 1, 1, view(i, j), 0, 1, 1
Next j
Next i
bm.Line (0, 0)-(w + 0.05, 0)
bm.Line (w + 0.05, 0)-(w + 0.05, h + 0.05)
bm.Line (w + 0.05, h + 0.05)-(0, h + 0.05)
bm.Line (0, h + 0.05)-(0, 0)
End Sub

Public Sub complute()
bactive = False
timeactive = False
Time1s.Interval = 0

Dim i As Integer, j As Integer
For i = 0 To w - 1
For j = 0 To h - 1
If view(i, j) = 0 And bomb(i, j) = 1 Then
view(i, j) = 12
pt i, j
End If
Next j
Next i
num = 0
clearbutton.Caption = "^_^"
questres = MsgBox("恭喜!你已扫除所有的雷!", vbOKOnly, "恭喜")
End Sub

Public Sub checkcomplute()
Dim i As Integer, j As Integer, k As Integer
k = 0
For i = 0 To w - 1
For j = 0 To h - 1
If view(i, j) > 0 And view(i, j) < 10 And bomb(i, j) = 0 Then
k = k + 1
End If

Next j
Next i
If k = w * h - bombnum Then complute
End Sub

Public Sub onerror()
bactive = False
Time1s.Interval = 0
timeactive = False
Dim i As Integer, j As Integer
For i = 0 To w - 1
For j = 0 To h - 1
If view(i, j) = 0 And bomb(i, j) = 1 Then
view(i, j) = 10
pt i, j
End If
If view(i, j) = 12 And bomb(i, j) = 0 Then
view(i, j) = 13
pt i, j
End If

Next j
Next i
clearbutton.Caption = "^X^"
End Sub

Public Sub pt(X As Integer, Y As Integer)
bm.PaintPicture bakimg, X, Y, 1, 1, view(X, Y), 0, 1, 1
bm.Line (0, 0)-(w + 0.05, 0)
bm.Line (w + 0.05, 0)-(w + 0.05, h + 0.05)
bm.Line (w + 0.05, h + 0.05)-(0, h + 0.05)
bm.Line (0, h + 0.05)-(0, 0)
End Sub

Public Sub lbtmpdown(X As Integer, Y As Integer)
If view(X, Y) = 0 Then
view(X, Y) = 14
pt X, Y
End If
End Sub

Public Sub lbtmpup(X As Integer, Y As Integer)
If view(X, Y) = 14 Then
view(X, Y) = 0
pt X, Y
End If
End Sub

Public Sub dbtmpdown(X As Integer, Y As Integer)
Dim i As Integer, j As Integer
For i = X - 1 To X + 1
For j = Y - 1 To Y + 1
If i >= 0 And i < w And j >= 0 And j < h Then
lbtmpdown i, j
End If
Next j
Next i
End Sub

Public Sub dbtmpup(X As Integer, Y As Integer)
Dim i As Integer, j As Integer
For i = X - 1 To X + 1
For j = Y - 1 To Y + 1
If i >= 0 And i < w And j >= 0 And j < h Then
lbtmpup i, j
End If
Next j
Next i
End Sub

Public Sub lbdown(X As Integer, Y As Integer)
If view(X, Y) <> 0 And view(X, Y) <> 10 And view(X, Y) <> 14 Then Exit Sub
If bomb(X, Y) = 1 Then
view(X, Y) = 11
pt X, Y
onerror
Exit Sub
End If

Dim i As Integer, j As Integer, k As Integer
k = 0
For i = X - 1 To X + 1
For j = Y - 1 To Y + 1
If i >= 0 And i < w And j >= 0 And j < h Then
k = k + bomb(i, j)
End If
Next j
Next i
view(X, Y) = k + 1
pt X, Y
If k = 0 Then dbdown X, Y
checkcomplute
End Sub

Public Sub rbdown(X As Integer, Y As Integer)
If view(X, Y) = 0 Then
view(X, Y) = 12
num = num - 1
pt X, Y
Exit Sub
End If
If view(X, Y) = 12 Then
view(X, Y) = 0
num = num + 1
pt X, Y
Exit Sub
End If
End Sub

Public Sub dbdown(X As Integer, Y As Integer)
If view(X, Y) = 0 Then Exit Sub
If view(X, Y) = 13 Then Exit Sub
Dim i As Integer, j As Integer, k As Integer, l As Integer
k = 0
l = 0
For i = X - 1 To X + 1
For j = Y - 1 To Y + 1
If i >= 0 And i < w And j >= 0 And j < h Then
k = k + bomb(i, j)
If view(i, j) = 12 Then l = l + 1
End If
Next j
Next i
If k <> l Then Exit Sub
For i = X - 1 To X + 1
For j = Y - 1 To Y + 1
If i >= 0 And i < w And j >= 0 And j < h Then
lbdown i, j
End If
Next j
Next i

End Sub

Public Sub init_only()
sizeofpicture = 15
Randomize
level3_Click
End Sub

Public Sub init()
onebuttonflag = 0
bactive = True
ReDim bomb(w - 1, h - 1)
ReDim view(w - 1, h - 1)
Dim i, j As Integer
For i = 0 To w - 1
For j = 0 To h - 1
bomb(i, j) = 0
view(i, j) = 0
Next j
Next i
For i = 1 To bombnum
Do
j = Int(w * h * Rnd)
If bomb(Int(j / h), j Mod h) = 0 Then
bomb(Int(j / h), j Mod h) = 1
Exit Do
End If
Loop While True
Next i
For i = 1 To 5
Form1.Width = bakimg.Width / sizeofpicture * Form1.Width * (w + 2) / Form1.ScaleWidth
Form1.Height = bakimg.Height * (Form1.Height - 20) * (h + 6) / Form1.ScaleHeight + 20
Next i
bm.Height = bakimg.Height * (h + 0.05)
bm.Width = bakimg.Width / sizeofpicture * (w + 0.05)
bm.Left = bakimg.Width / sizeofpicture
bm.Top = bakimg.Height * 4
bm.ScaleWidth = w + 0.05
bm.ScaleHeight = h + 0.05
clearbutton.Left = (Form1.ScaleWidth - clearbutton) / 2 - 25
clearbutton.Top = bakimg.Height * 2
clearbutton.Caption = "^O^"
num.Left = bakimg.Width / sizeofpicture
num.Top = bakimg.Height * 2
num = bombnum
time.Left = bm.Left + bm.Width - time.Width
time.Top = bakimg.Height * 2
timeactive = False
Time1s.Interval = 0
time = 0
timeactive = False
drawall
Form1.Show
End Sub

Private Sub about_game_Click()
Form2.Show
Form1.Enabled = False

End Sub

Private Sub bm_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 113 Then
init
End If
End Sub

Private Sub bm_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If bactive = False Then Exit Sub
Dim ox As Integer, oy As Integer
ox = Int(X)
oy = Int(Y)
If ox >= w Or ox < 0 Or oy < 0 Or oy >= h Then Exit Sub

If Button = 1 Then
onebuttonflag = onebuttonflag + 1
oldx = ox
oldy = oy
If onebuttonflag = 1 Then lbtmpdown ox, oy
If onebuttonflag = 3 Then dbtmpdown ox, oy
End If
If Button = 2 Then
If onebuttonflag = 0 Then
rbdown (ox), (oy)
End If
onebuttonflag = onebuttonflag + 2
If onebuttonflag = 3 Then
oldx = ox
oldy = oy
dbtmpdown ox, oy
End If
End If
End Sub

Private Sub bm_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim ox As Integer, oy As Integer
ox = Int(X)
oy = Int(Y)
If ox >= w Or ox < 0 Or oy < 0 Or oy >= h Then Exit Sub
If Button = 0 And onebuttonflag <> 0 Then
dbtmpup oldx, oldy
onebuttonflag = 0
End If
If Button = 1 And onebuttonflag = 1 Then
lbtmpup oldx, oldy
oldx = ox
oldy = oy
lbtmpdown ox, oy
End If
If Button = 1 And onebuttonflag = 3 Then
dbtmpup oldx, oldy
oldx = ox
oldy = oy
onebuttonflag = 1
lbtmpdown ox, oy
End If
If Button = 3 And onebuttonflag = 3 Then
dbtmpup oldx, oldy
oldx = ox
oldy = oy
dbtmpdown ox, oy
End If
If Button = 3 And onebuttonflag <> 3 Then
dbtmpup oldx, oldy
oldx = ox
oldy = oy
onebuttonflag = 3
dbtmpdown ox, oy
End If
End Sub

Private Sub bm_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If bactive = False Then Exit Sub
If timeactive = False Then
timeactive = True
time = 1
Time1s.Interval = 1000
End If
Dim ox As Integer, oy As Integer
ox = Int(X)
oy = Int(Y)
If ox >= w Or ox < 0 Or oy < 0 Or oy >= h Then Exit Sub
If onebuttonflag = 3 Then
dbtmpup ox, oy
onebuttonflag = 0
dbdown ox, oy
Exit Sub
End If
If Button = 1 Then
If onebuttonflag = 1 Then
lbtmpup ox, oy
onebuttonflag = 0
lbdown ox, oy
End If
End If
If Button = 2 Then
onebuttonflag = 0
End If
End Sub

Private Sub bm_Paint()
drawall
End Sub

Private Sub clearbutton_Click()
init
End Sub

Private Sub exit_game_Click()
End
End Sub

Private Sub Form_Load()
init_only
End Sub

Private Sub Form_Unload(Cancel As Integer)
Unload Me
Unload Form2
End Sub

Private Sub level1_Click()
level1.Checked = False
level2.Checked = False
level3.Checked = False
level4.Checked = False
level5.Checked = False
level1.Checked = True
h = 8
w = 8
bombnum = 10
init
End Sub

Private Sub level2_Click()
level1.Checked = False
level2.Checked = False
level3.Checked = False
level4.Checked = False
level5.Checked = False
level2.Checked = True
h = 16
w = 16
bombnum = 40
init

End Sub

Private Sub level3_Click()
level1.Checked = False
level2.Checked = False
level3.Checked = False
level4.Checked = False
level5.Checked = False
level3.Checked = True
h = 18
w = 30
bombnum = 100
init
End Sub

Private Sub level4_Click()
level1.Checked = False
level2.Checked = False
level3.Checked = False
level4.Checked = True
level5.Checked = False
h = 29
w = 30
bombnum = 200
init
End Sub

Private Sub level5_Click()
level1.Checked = False
level2.Checked = False
level3.Checked = False
level4.Checked = False
level5.Checked = True
h = 29
w = 49
bombnum = 300
init
End Sub

Private Sub restart_Click()
init
End Sub

Private Sub Time1s_Timer()
If timeactive = True Then time = time + 1
End Sub