How To Make a Bouncing Ball

Posts 1–5 of 5 · Page 1 of 1
How To Make a Bouncing Ball
Welcome to my first tutorial.

You will need:

Visual basic
A application
A timer!

.................................................. .................

Add this to the over form1_load like this:

Code:
Public Class Form1
    Dim m_Dx As Integer
    Dim m_Dy As Integer
    Dim m_X As Integer
    Dim m_Y As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
All the Dim is X and Y axis for the ball....

Then


And add this code to form1_load.

Code:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim rnd As New Random
        m_Dx = rnd.Next(1, 4)
        m_Dy = rnd.Next(1, 4)
        m_X = rnd.Next(0, Me.ClientSize.Width - 50)
        m_Y = rnd.Next(0, Me.ClientSize.Height - 50)
    End Sub
What that code does is to move the bounching ball to a random move....... Between 1 and 4!

Then

Add this code to make a ball.

Code:
e.Graphics.Clear(Me.BackColor)
        e.Graphics.FillEllipse(Brushes.Black, m_X, m_Y, 100, 100)
        e.Graphics.DrawEllipse(Pens.Black, m_X, m_Y, 100, 100)
Then

Set timer1.interval = 1 and make it enabled.
this code to the timer!
Code:
   m_X += m_Dx
        If m_X < 0 Then
            m_Dx = -m_Dx
            Beep()
        ElseIf m_X + 50 > Me.ClientSize.Width Then
            m_Dx = -m_Dx
            Beep()
        End If
        m_Y += m_Dy
        If m_Y < 0 Then
            m_Dy = -m_Dy
            Beep()
        ElseIf m_Y + 50 > Me.ClientSize.Height Then
            m_Dy = -m_Dy
            Beep()
        End If
        Me.Invalidate()
That code moves the ball and and makes a beep sound when it´s hits the wall.

Creditz for the code: environmentalnerd53.
Me for the tutorial and some editing.

............................................

Full code:
Code:
Public Class Form1
    Dim m_Dx As Integer
    Dim m_Dy As Integer
    Dim m_X As Integer
    Dim m_Y As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim rnd As New Random
        m_Dx = rnd.Next(1, 4)
        m_Dy = rnd.Next(1, 4)
        m_X = rnd.Next(0, Me.ClientSize.Width - 50)
        m_Y = rnd.Next(0, Me.ClientSize.Height - 50)
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.Clear(Me.BackColor)
        e.Graphics.FillEllipse(Brushes.Black, m_X, m_Y, 50, 50)
        e.Graphics.DrawEllipse(Pens.Black, m_X, m_Y, 50, 50)
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        m_X += m_Dx
        If m_X < 0 Then
            m_Dx = -m_Dx
            Beep()
        ElseIf m_X + 50 > Me.ClientSize.Width Then
            m_Dx = -m_Dx
            Beep()
        End If
        m_Y += m_Dy
        If m_Y < 0 Then
            m_Dy = -m_Dy
            Beep()
        ElseIf m_Y + 50 > Me.ClientSize.Height Then
            m_Dy = -m_Dy
            Beep()
        End If
        Me.Invalidate()
    End Sub
End Class
............................................

Final Resultat:

wow impressive script i gonna test i tomorrow :P
It works. :'D Funny.
Quote Originally Posted by Asians View Post
It works. :'D Funny.
No problem...
Lol nice tut
Posts 1–5 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us