主题:[讨论]求教VB面板中 图片移动计算量值的解释
这个程序是用Timer 使图片从左下角冒头后斜向右上方移动
消失后再隐藏到左边或右边后再一次登场向右边斜向上移动!
路线不是重复的! 关键的是水平移动值ballX和向上的移动值bally的计算公式得出循环:
关键行,同时也就是不太理解的行!一种计算思路不太明白,这个移动值的计算
Me.ballX = (Me.ballX + Me.dx) Mod (Me.pnlGround.Width + Me.picBall1.Width)
Me.bally = Me.pnlGround.Height - (Me.pnlGround.Height - (Me.bally - Me.dy)) Mod (Me.pnlGround.Height + Me.picBall1.Height)
以下给出了完整的代码。是可行的!关键的移动量2行的意思还是搞不太明白,能否得到详细的说明!
Public Class frmRollingBall
Dim dx As Double
Dim dy As Double
Dim ballX As Double
Dim bally As Double
Private Sub initTreate()
Me.picBall1.visible = True
Me.PicBall2.Visible = False
Me.picBall1.Left = 0 - Me.picBall1.Width
Me.picBall2.Left = 0 - Me.picBall1.Width
Me.picBall1.Top = Me.pnlGround.Height
Me.picBall2.Top = Me.pnlGround.Height
Me.dx = Me.cboSpeed.SelectedItem
Me.dy = Me.cboSpeed.SelectedItem
Me.ballX = Me.picBall1.Left
Me.bally = Me.picBall1.Top
End Sub
Private Sub frmRollingBall_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
initTreate()
End Sub
Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
Me.PicBall1.Visible = False
Me.PicBall2.Visible = False
initTreate()
Me.Timer1.Enabled = True
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.Timer1.Enabled = False
End Sub
Private Sub btnEnd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnd.Click
Me.Close()
End
End Sub
Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnContinue.Click
Me.Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.ballX = (Me.ballX + Me.dx) Mod (Me.pnlGround.Width + Me.picBall1.Width)
Me.bally = Me.pnlGround.Height - (Me.pnlGround.Height - (Me.bally - Me.dy)) Mod (Me.pnlGround.Height + Me.picBall1.Height)
Me.PicBall1.Left = Me.ballX
Me.picBall2.Left = Me.ballX
Me.picBall1.Top = Me.bally
Me.picBall2.Top = Me.bally
Me.PicBall1.Visible = Not (Me.PicBall1.Visible)
Me.PicBall2.Visible = Not (Me.PicBall2.Visible)
End Sub
Private Sub cboSpeed_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSpeed.SelectedIndexChanged
Me.dx = Me.cboSpeed.SelectedItem
Me.dy = Me.cboSpeed.SelectedItem
End Sub
End Class
消失后再隐藏到左边或右边后再一次登场向右边斜向上移动!
路线不是重复的! 关键的是水平移动值ballX和向上的移动值bally的计算公式得出循环:
关键行,同时也就是不太理解的行!一种计算思路不太明白,这个移动值的计算
Me.ballX = (Me.ballX + Me.dx) Mod (Me.pnlGround.Width + Me.picBall1.Width)
Me.bally = Me.pnlGround.Height - (Me.pnlGround.Height - (Me.bally - Me.dy)) Mod (Me.pnlGround.Height + Me.picBall1.Height)
以下给出了完整的代码。是可行的!关键的移动量2行的意思还是搞不太明白,能否得到详细的说明!
Public Class frmRollingBall
Dim dx As Double
Dim dy As Double
Dim ballX As Double
Dim bally As Double
Private Sub initTreate()
Me.picBall1.visible = True
Me.PicBall2.Visible = False
Me.picBall1.Left = 0 - Me.picBall1.Width
Me.picBall2.Left = 0 - Me.picBall1.Width
Me.picBall1.Top = Me.pnlGround.Height
Me.picBall2.Top = Me.pnlGround.Height
Me.dx = Me.cboSpeed.SelectedItem
Me.dy = Me.cboSpeed.SelectedItem
Me.ballX = Me.picBall1.Left
Me.bally = Me.picBall1.Top
End Sub
Private Sub frmRollingBall_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
initTreate()
End Sub
Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
Me.PicBall1.Visible = False
Me.PicBall2.Visible = False
initTreate()
Me.Timer1.Enabled = True
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.Timer1.Enabled = False
End Sub
Private Sub btnEnd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnd.Click
Me.Close()
End
End Sub
Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnContinue.Click
Me.Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.ballX = (Me.ballX + Me.dx) Mod (Me.pnlGround.Width + Me.picBall1.Width)
Me.bally = Me.pnlGround.Height - (Me.pnlGround.Height - (Me.bally - Me.dy)) Mod (Me.pnlGround.Height + Me.picBall1.Height)
Me.PicBall1.Left = Me.ballX
Me.picBall2.Left = Me.ballX
Me.picBall1.Top = Me.bally
Me.picBall2.Top = Me.bally
Me.PicBall1.Visible = Not (Me.PicBall1.Visible)
Me.PicBall2.Visible = Not (Me.PicBall2.Visible)
End Sub
Private Sub cboSpeed_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSpeed.SelectedIndexChanged
Me.dx = Me.cboSpeed.SelectedItem
Me.dy = Me.cboSpeed.SelectedItem
End Sub
End Class