Lesson 33

Creating Animation

Learn how to create moving objects and simple game effects using Timer control in Visual Basic 2015.

33.1 Introduction

Visual Basic 2015 can be used to create animation using the Timer control. In fact, applications like clocks, stopwatches, and dice are already simple animations.

Animation is created by repeatedly updating object positions or graphics at fixed intervals.

33.2 Creating Motion

To create motion:

  • Insert a PictureBox
  • Add a Timer (Interval = 100)
  • Use PictureBox.Left to move object

Example code:

Private Sub Timer1_Tick(...) Handles Timer1.Tick

If PictureBox1.Left < Me.Width Then
    PictureBox1.Left = PictureBox1.Left + 10
Else
    PictureBox1.Left = 0
End If

End Sub

Control buttons:

Timer1.Enabled = True   'Start
Timer1.Enabled = False  'Stop

Figure 33.1 – Moving Object

33.3 Creating a Graphical Dice

We can animate a dice using Timer and random numbers.

n = Int(6 * Rnd()) + 1

Full example:

Private Sub BtnRoll_Click(...) Handles BtnRoll.Click
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(...) Handles Timer1.Tick

Dim n As Integer
n = Int(6 * Rnd()) + 1

Dim g As Graphics = PictureBox1.CreateGraphics()
Dim pen As New Pen(Color.DarkTurquoise, 10)

PictureBox1.Refresh()

Select Case n
 Case 1
  g.DrawEllipse(pen, 80, 80, 10, 10)
 Case 2
  g.DrawEllipse(pen, 40, 40, 10, 10)
  g.DrawEllipse(pen, 120, 120, 10, 10)
 Case 3
  g.DrawEllipse(pen, 40, 40, 10, 10)
  g.DrawEllipse(pen, 80, 80, 10, 10)
  g.DrawEllipse(pen, 120, 120, 10, 10)
 Case 4
  g.DrawEllipse(pen, 40, 40, 10, 10)
  g.DrawEllipse(pen, 120, 40, 10, 10)
  g.DrawEllipse(pen, 40, 120, 10, 10)
  g.DrawEllipse(pen, 120, 120, 10, 10)
 Case 5
  g.DrawEllipse(pen, 40, 40, 10, 10)
  g.DrawEllipse(pen, 120, 40, 10, 10)
  g.DrawEllipse(pen, 80, 80, 10, 10)
  g.DrawEllipse(pen, 40, 120, 10, 10)
  g.DrawEllipse(pen, 120, 120, 10, 10)
 Case 6
  g.DrawEllipse(pen, 40, 40, 10, 10)
  g.DrawEllipse(pen, 120, 40, 10, 10)
  g.DrawEllipse(pen, 40, 80, 10, 10)
  g.DrawEllipse(pen, 120, 80, 10, 10)
  g.DrawEllipse(pen, 40, 120, 10, 10)
  g.DrawEllipse(pen, 120, 120, 10, 10)
End Select

End Sub

Figure 33.2 – Graphical Dice

33.4 Creating a Slot Machine

A slot machine uses random images and Timer animation.

Dim m, a, b, c As Integer

Private Sub BtnSpin_Click(...) Handles BtnSpin.Click
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(...) Handles Timer1.Tick

m = m + 10

a = Int(1 + Rnd() * 3)
b = Int(1 + Rnd() * 3)
c = Int(1 + Rnd() * 3)

PictureBox1.Image = Image.FromFile("img" & a & ".png")
PictureBox2.Image = Image.FromFile("img" & b & ".png")
PictureBox3.Image = Image.FromFile("img" & c & ".png")

If m > 1000 Then
    Timer1.Enabled = False
    m = 0
End If

End Sub

The label can be used to display winning messages.

Figure 33.3 – Slot Machine

Build on This Foundation

Continue to VB2026

After learning the basics of checkbox controls in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Key Takeaways:
  • Animation uses Timer for repeated updates
  • PictureBox properties control movement
  • Random numbers create dynamic effects
  • Timer enables simple game development

Next: Database Introduction

Go to Lesson 34 →