VB编写小游戏

核心提示窗体放两个Label控件,一个Timer控件:Dim n As IntegerPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)Select Case KeyCode

窗体放两个Label控件,一个Timer控件:

Dim n As Integer

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case vbKeyUp

If Label1.Top > 0 Then Label1.Top = Label1.Top - 50

Case vbKeyDown

If Label1.Top < ScaleHeight - Label1.Height Then Label1.Top = Label1.Top + 50

Case vbKeyLeft

If Label1.Left > 0 Then Label1.Left = Label1.Left - 50

Case vbKeyRight

If Label1.Left < ScaleWidth - Label1.Width Then Label1.Left = Label1.Left + 50

End Select

Call check

End Sub

Private Sub check()

If Abs(Label1.Top - Label2.Top) <= 50 And Abs(Label1.Left - Label2.Left) <= 50 Then

n = n + 1

Label2.Move Rnd * ScaleWidth, Rnd * ScaleHeight

End If

End Sub

Private Sub Form_Load()

KeyPreview = True

Randomize

With Label1

.Caption = ""

.BackColor = vbWhite

.Move (ScaleWidth - .Width) / 2, (ScaleHeight - .Height) / 2, 500, 500

End With

With Label2

.Caption = ""

.BackColor = vbYellow

.Move Rnd * ScaleWidth, Rnd * ScaleHeight, 500, 500

End With

Timer1.Interval = 60000

Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()

MsgBox "这局对准了" & n & "次黄方块"

Unload Me

End Sub

添加3个timer,一个按钮,2个shape控件(分别代表兔子与乌龟),滑动条Slider

代码,下面的5000代表终点线

Dim bisai As Boolean

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Sub Command1_Click()

bisai = True

Timer3.Interval = 1000

End Sub

Private Sub Form_Load()

Timer1.Interval = 250

Timer2.Interval = 250

Slider1.Max = 100

Slider1.Value = 50

End Sub

Private Sub Timer1_Timer()

Static n As Integer

If GetAsyncKeyState(vbKeyA) Then

If bisai = False Then

MsgBox "兔子抢跑"

Else

n = n + 1

End If

End If

If n > 1 Then n = 1

If n = 1 And GetAsyncKeyState(vbKeyS) Then

Shape1.Left = Shape1.Left + Slider1.Value * 2

n = 0

End If

End Sub

Private Sub Timer2_Timer()

Static m As Integer

If GetAsyncKeyState(vbKeyK) Then

If bisai = False Then

MsgBox "乌龟抢跑"

Else

m = m + 1

End If

End If

If m > 1 Then m = 1

If m = 1 And GetAsyncKeyState(vbKeyL) Then

Shape2.Left = Shape2.Left + Slider1.Value * 2

m = 0

End If

End Sub

Private Sub Timer3_Timer()

Static l As Integer

l = l + 1

Me.Caption = "时间" & l & "秒"

If Shape1.Left > 5000 Then

MsgBox "比赛结束,兔子赢!"

Timer3.Interval = 0

End If

If Shape2.Left > 5000 Then

MsgBox "比赛结束,乌龟赢!"

Timer3.Interval = 0

End If

End Sub

 
友情链接
鄂ICP备19019357号-22