1. 首页 > 赛程预告

vs窗体设计移动小球-vs 窗体

求助 VB 设计一个反弹球程序

vs窗体设计移动小球-vs 窗体

题目没完整,不过我现写了一个不停反弹的,而且反弹的高度越来越小,直到停止,你可以参考一下哈

Rem 注意,在窗体中加一个Shape1,改名为Ball,再加一个Timer1

Dim downfoot As Integer, upfoot As Integer

Private Sub Form_Load()

Ball.Shape = 3 '设置小球是圆形的

Ball.BackStyle = 1 '设置小球不透明

Ball.BackColor = vbRed '设置小球是红色的

Ball.BorderColor = vbRed

Ball.Height = Ball.Width

Ball.Left = (Me.ScaleWidth - Ball.Width) / 2 '设置小球在水平居中

Ball.Top = 0

downfoot = (Me.ScaleHeight - Ball.Height) / 100 '设置每次下落距离

upfoot = 5 * downfoot '设置每次反弹后衰减的距离

Timer1.Enabled = True

Timer1.Interval = 10 '设置时钟间隔

End Sub

Private Sub Timer1_Timer()

Static m As Integer, n As Integer

n = n + 1

Ball.Top = Ball.Top + downfoot

If Ball.Top > Me.ScaleHeight - Ball.Height Then m = m + 1: Ball.Top = m * upfoot: n = 0

If m * upfoot > Me.ScaleHeight - Ball.Height Then Timer1.Enabled = False: Ball.Top = Me.ScaleHeight - Ball.Height

End Sub

vb小球碰壁游戏加强版思路,向大大们请教下.

设置窗体名称frmZQ,StartUpPosition=屏幕中心,ScaleMode=3

添加两个Shape1,Shape2

一个时钟控件Timer1

代码如下:

Dim D As Integer '发球方向

Dim F As Boolean '发号

Dim V As Integer '移动距离

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

Select Case KeyCode

Case 32

Timer1.Enabled = True

F = True

Case 37

Shape1.Left = Shape1.Left - V

If Shape1.Left <= 0 Then Shape1.Left = 0

If F = False Then Shape2.Left = Shape1.Left + (Shape1.Width - Shape2.Width) / 2: D = 2

Case 39

Shape1.Left = Shape1.Left + V

If Shape1.Left >= frmZQ.ScaleWidth - Shape1.Width Then Shape1.Left = frmZQ.ScaleWidth - Shape1.Width

If F = False Then Shape2.Left = Shape1.Left + (Shape1.Width - Shape2.Width) / 2: D = 4

Case Else

End Select

End Sub

Private Sub Form_Load()

With Shape1

.Left = (frmZQ.ScaleWidth - .Width) / 2

.Top = 260

End With

With Shape2

.Left = (frmZQ.ScaleWidth - .Width) / 2

.Top = 240

End With

V = 10

End Sub

Private Sub Timer1_Timer()

Select Case D

Case -4

Shape2.Left = Shape2.Left + V: Shape2.Top = Shape2.Top + V

If Shape2.Left >= frmZQ.ScaleWidth - Shape2.Width Then D = -2

If Shape2.Top >= 240 And (Shape1.Left - Shape2.Left >= -10 And Shape1.Left - Shape2.Left <= 70) Then D = 4

If Shape2.Top >= frmZQ.ScaleHeight Then Unload Me

Case -3

Case -2

Shape2.Left = Shape2.Left - V: Shape2.Top = Shape2.Top + V

If Shape2.Left <= 0 Then D = -4

If Shape2.Top >= 240 And (Shape1.Left - Shape2.Left >= -10 And Shape1.Left - Shape2.Left <= 70) Then D = 2

If Shape2.Top >= frmZQ.ScaleHeight Then Unload Me

Case -1

Case 0

Case 1

Case 2

Shape2.Left = Shape2.Left - V: Shape2.Top = Shape2.Top - V

If Shape2.Left <= 0 Then D = 4

If Shape2.Top <= 0 Then D = -2

Case 3

Case 4

Shape2.Left = Shape2.Left + V: Shape2.Top = Shape2.Top - V

If Shape2.Left >= frmZQ.ScaleWidth - Shape2.Width Then D = 2

If Shape2.Top <= 0 Then D = -4

Case Else

End Select

End Sub

如果需要源代码,可以发给你。

VB中怎么控制小球的左右移动和停止,要代码!!!

Dim?fh?As?Integer

Private?Sub?Command1_Click()

Timer1.Enabled?=?True

fh?=?1

End?Sub

Private?Sub?Command2_Click()

Timer1.Enabled?=?False

End?Sub

Private?Sub?Form_Load()

Shape1.Shape?=?3

Timer1.Interval?=?100

Timer1.Enabled?=?False

Command1.Caption?=?"开始"

Command2.Caption?=?"停止"

End?Sub

Private?Sub?Timer1_Timer()

Shape1.Left?=?Shape1.Left?+?fh?*?200

If?Shape1.Left?<?0?Then

Shape1.Left?=?0

fh?=?-fh

End?If

If?Shape1.Left?+?Shape1.Width?>?Me.Width?Then

Shape1.Left?=?Me.Width?-?Shape1.Width

fh?=?-fh

End?If

End?Sub

声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。