vb 控制小圆球的移动。

vb 控制小圆球的移动。,第1张

'自动移动,遇到边界会停下,能控制方向的圆球
'需控件:Shape1、Timer1
Dim ctTo As Long
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyUp: ctTo = 0
Case vbKeyDown: ctTo = 1
Case vbKeyLeft: ctTo = 2
Case vbKeyRight: ctTo = 3
End Select
End Sub
Private Sub Form_Load()
Shape1Shape = 3: Shape1FillColor = 255: Shape1FillStyle = 0
Shape1Width = Shape1Height: MeKeyPreview = True
Timer1Enabled = True: Timer1Interval = 100
End Sub
Private Sub Timer1_Timer()
Dim nTo As Single, X As Single, Y As Single
nTo = Shape1Width 025
X = Shape1Left: Y = Shape1Top
Select Case ctTo
Case 0: Y = Y - nTo
Case 1: Y = Y + nTo
Case 2: X = X - nTo
Case 3: X = X + nTo
End Select
If X < 0 Then X = 0
If X > MeScaleWidth - Shape1Width Then X = MeScaleWidth - Shape1Width
If Y < 0 Then Y = 0
If Y > MeScaleHeight - Shape1Height Then Y = MeScaleHeight - Shape1Height
Shape1Move X, Y
End Sub

所需要控件:shape1,timer1,command1,command2
Dim degree As Single
Const pi = 31415926
Dim v As Integer
Dim r As Single
Private Sub Command1_Click()
Timer1Enabled = False
Form_Load
End Sub
Private Sub Command2_Click()
Timer1Enabled = True
End Sub
Private Sub Form_Load()
MeShow
Shape1Shape = 3
Timer1Enabled = False
Timer1Interval = 100
Shape1Left = MeWidth Rnd
Shape1Top = MeHeight Rnd
degree = 2 pi Rnd
v = 150
r = 1
Command1Caption = "结束"
Command2Caption = "开始"
End Sub
Private Sub Timer1_Timer()
If Shape1Top > 0 And Shape1Left > 0 And (Shape1Left + Shape1Width) < MeWidth And (Shape1Top + Shape1Height) < MeHeight Then
Shape1Top = Shape1Top + Sin(degree) v r
Shape1Left = Shape1Left + Cos(degree) v r
v = v r
Else
Timer1Enabled = False
Call chagestate
End If
End Sub
Public Sub chagestate()
If Shape1Top <= 0 Or (Shape1Top + Shape1Height) >= MeHeight Then
degree = 2 pi - degree
If Shape1Top <= 0 Then Shape1Top = 1
If (Shape1Top + Shape1Height) >= MeHeight Then Shape1Top = MeHeight - Shape1Height - 1
End If
If Shape1Left <= 0 Or (Shape1Left + Shape1Width) >= MeWidth Then
If (Shape1Left + Shape1Width) >= MeWidth Then Shape1Left = MeWidth - Shape1Width - 1
If Shape1Left <= 0 Then Shape1Left = 1
If degree < pi / 2 Then degree = pi / 2 + degree
If degree > pi / 2 And degree < pi Then degree = 3 pi / 2 - degree
If degree > pi Or degree < 2 pi Then degree = 3 pi - degree
End If
v = 150
Timer1Enabled = True
End Sub

1、打开vb,并新建“标准EXE”。

2、在vb的上方选择“工具”—“选项”。

3、选择“编辑器格式”—“确定”。

4、在下图标注的红色圆圈内可以选择字号,之前是5,现在选择10。点击“确定”。

5、双击工程面板,d出的编辑器中,字体为原来的一倍。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/13194536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-06-18
下一篇 2023-06-18

发表评论

登录后才能评论

评论列表(0条)

保存