类似打鼹鼠的游戏 ,每段时间 随机更新 小球 ,能记录打中和打错的个数
添加 一个form1 窗体上添加 一个picture控件 一个timer
全部代码:
'***********************************************
Dim OneX&, OneY '每块的长宽
Dim ys(4, 4) As Boolean '5*5的数组 记录是否有鼹鼠
Dim suc&, fail& '点对和点错的个数
Private Sub Form_Load()
Me.Show
OneX = Picture1.Width / 5
OneY = Picture1.Height / 5
InitLine
RefreshCaption
Timer1_Timer
End Sub
Sub InitLine()
Dim i&
For i = 1 To 4
Picture1.Line (OneX * i, 0)-(OneX * i, Picture1.Height)
Next
For i = 1 To 4
Picture1.Line (0, OneY * i)-(Picture1.Width, OneY * i)
Next
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim dx&, dy&
dx = Fix(X / OneX)
dy = Fix(Y / OneY)
If dx > 4 Or dy > 4 Then Exit Sub
If ys(dx, dy) Then
suc = suc + 1
ys(dx, dy) = False
Else
fail = fail + 1
End If
RefreshCaption
RefreshPic
End Sub
Private Sub RefreshCaption()
Me.Caption = "打中 " & suc & "个 打错 " & fail & "个"
End Sub
Private Sub RefreshPic()
Dim i&, j&
For j = 0 To 4
For i = 0 To 4
If ys(i, j) Then
Picture1.Circle (i * OneX + OneX / 2, j * OneY + OneY / 2), IIf(OneX > OneY, OneY / 2 - 2, OneX / 2 - 2)
Else
Picture1.Line (OneX * i + 1, OneY * j + 1)-(OneX * (i + 1) - 1, OneY * (j + 1) - 1), vbWhite, BF
End If
Next
Next
InitLine
End Sub
Private Sub Timer1_Timer()
Randomize
Dim RndNum&, i&, j&
For j = 0 To 4
For i = 0 To 4
If Int(Rnd * 15) <= 2 Then
ys(i, j) = True
End If
Next
Next
RefreshPic
End Sub
'***********************************************
最终效果如下:
因百度无法提供上传 请+QQ:627083940 或+QQ群:5773874 大家共同学习