本文主要是介绍vb小游戏 打地鼠,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
运行效果(动画)
窗体结构
注意下面有两个隐藏的锤子,临时存储锤子图片,作为image.picture被调用。这两个图片本来应该是左边抬起来,右边按下去,但是按下去的图片找不到了,临时用两个一样的锤子。
左上角两个textbox用来显示当前老鼠坐标,用于调试。
实验程序说明
Image 1 的picture 设为老鼠
Image 2 的picture 设为锤子0
Image 2 的picture 设为锤子1Image 2 ,3 的visible 为false做全局变量Dim n, s As Integer '定义全局变量
'变量n用来存储击中砖块的次数,变量s用来存储所用的时间,赋值0三个时钟设为false
Timer1 interval 设为1000 程序里随级别设置为不同的值,值越小事件发生的越快。老鼠移动显示计时
Timer2 interval 设为30000 总计时
Timer3 interval 设为1000 显示刷新计时,每次事件发生时更新 击中次数和剩余时间。
在实际操作时,time2和timer3被我合并了。
代码
Dim n, s As Integer 'n是次数 s是时间
'初级
Private Sub Command1_Click()Timer1.Interval = 1000Timer1.Enabled = FalseCommand4.Enabled = True
End Sub
'中级
Private Sub Command2_Click()Timer1.Interval = 800Timer1.Enabled = FalseCommand4.Enabled = True
End Sub
'高级
Private Sub Command3_Click()Timer1.Interval = 100Timer1.Enabled = FalseCommand4.Enabled = True
End Sub
'开始
Private Sub Command4_Click()n = 0: s = 0Label1.Caption = "10" '初始化时间Text1.Text = 0 '初始化击中次数Image1.Visible = True '看到老鼠Image1.Left = 0Image1.Top = 0Command1.Enabled = False: Command2.Enabled = FalseCommand3.Enabled = FalseCommand4.Enabled = FalseTimer1.Enabled = TrueTimer3.Enabled = True
End SubPrivate Sub Command5_Click()Unload Me '关闭
End Sub'窗体初始化
Private Sub Form_Load()Label1.Caption = 10Label2.Visible = False
End Sub'点到老鼠
Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)n = n + 1Text1.Text = nImage1.MousePointer = 99 - customImage1.MouseIcon = Image3.Picture
End Sub
'鼠标抬起
Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)Image1.MousePointer = 99 - customImage1.MouseIcon = Image2.Picture
End Sub'点到空白
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)Picture1.MousePointer = 99 - customPicture1.MouseIcon = Image3.Picture
End Sub
'锤子跟着鼠标移动
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Picture1.MousePointer = 99 - customPicture1.MouseIcon = Image2.Picture
End Sub
'鼠标在空白处抬起
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)Picture1.MousePointer = 99 - customPicture1.MouseIcon = Image2.Picture
End Sub
'老鼠随机位置 6960 2520 默认960 1920
Private Sub Timer1_Timer()RandomizeL = Int(Rnd * 6960 - 0) '横着移动的范围T = Int(Rnd * 2520 - 960) '竖着移动的范围Image1.Left = Picture1.Left + LImage1.Top = Picture1.Top + TText2.Text = Image1.LeftText3.Text = Image1.Top
End Sub'倒计时
Private Sub Timer3_Timer()s = Val(Label1.Caption)s = s - 1Label1.Caption = sIf s = 0 ThenTimer1.Enabled = FalseTimer3.Enabled = FalseImage1.Visible = FalseCommand1.Enabled = TrueCommand2.Enabled = TrueCommand3.Enabled = TrueIf Text1.Text < 10 ThenMsgBox "成绩不咋地"ElseIf Text1.Text < 20 ThenMsgBox "真厉害!"ElseMsgBox "打鼠英雄"End IfEnd If
End Sub
这篇关于vb小游戏 打地鼠的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!