Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim Rand As New Random PictureBox1.Left = Rand.Next(Me.Width - PictureBox1.Width) PictureBox1.Top = Rand.Next(Me.Height - PictureBox1.Height) End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionOption Explicit On Public Class Form1 Dim t As Integer = 1 Dim x0 As Double Dim y0 As Double Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WindowState = FormWindowState.Maximized x0 = PictureBox1.Left y0 = PictureBox1.Top Timer1.Interval = 1000 Timer1.Enabled = True End Sub Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick t = t + 1 Me.Text = t If t <> 5 Then Exit Sub Dim x1 As Integer Dim y1 As Integer ' Randomize() x1 = Int(Me.Width - PictureBox1.Width + 1) * Rnd() + 1 Randomize() y1 = Int((Me.Height - 50) - PictureBox1.Height + 1) * Rnd() + 1 ' Dim sensX As Integer = 1 Dim difX As Long = x1 - x0 Dim difY As Long = y1 - y0 Dim pasY As Double Dim j As Double = y0 Dim sens As String = "b" If x0 > x1 Then sensX = -1 If difX < 0 Then difX = difX * -1 If difY < 0 Then difY = difY * -1 sens = "h" End If pasY = 1 / difX * difY For i = x0 To x1 Step sensX PictureBox1.Left = i If sens "h" Then j j - pasY If sens "b" Then j j + pasY PictureBox1.Top = j Next i x0 = PictureBox1.Left y0 = PictureBox1.Top t = 1 End Sub End Class
Mais chat marche
Public Class Form1 Dim Déplacement As Integer = 10 Dim NewPosLeft, NewPosTop As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load PictureBox1.Left = 0 PictureBox1.Top = 0 End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'Timer1.Enabled = False Dim Rand As New Random NewPosLeft = Rand.Next(Me.Width - PictureBox1.Width) NewPosTop = Rand.Next(Me.Height - PictureBox1.Height) NewPosLeft = (Int(NewPosLeft / Déplacement)) * Déplacement NewPosTop = (Int(NewPosTop / Déplacement)) * Déplacement Do Application.DoEvents() System.Threading.Thread.Sleep(20) If PictureBox1.Top < NewPosTop Then PictureBox1.Top += Déplacement ElseIf PictureBox1.Top > NewPosTop Then PictureBox1.Top -= Déplacement End If If PictureBox1.Left < NewPosLeft Then PictureBox1.Left += Déplacement ElseIf PictureBox1.Left > NewPosLeft Then PictureBox1.Left -= Déplacement End If Loop Until PictureBox1.Left NewPosLeft And PictureBox1.Top NewPosTop End Sub End Class
les déplacements en diagolale, à coefficient angulaire donc, et les tâtonnements que tu y appliques alors que l'angle de la diagonale n'est pas forcément de 45 °
c'est pour ça qu'il faut aussi déterminer l'équation linéaire par la suite.