Visual Basic implementation with animation effects
This animated dice program demonstrates how to create engaging visual effects using Visual Basic. The program simulates dice rolling with a realistic animation sequence before landing on a random number between 1 and 6.
The animation is achieved using a Timer control that rapidly changes the dice faces to create the rolling effect. This example shows both VB6 and VB.NET implementations, highlighting the differences between these two versions of Visual Basic.
Try the interactive dice simulation below. Click the "Roll Dice" button to see the animation in action!
GUI framework for creating desktop applications
Creates the animation effect by triggering events at intervals
Generates unpredictable dice rolls
Visual elements that form the dice faces
The original VB6 implementation uses Shape controls to represent the dots on the dice. The animation is controlled by a Timer that rapidly changes which shapes are visible.
Dim x As Integer Sub roll() x = x + 10 Randomize Timer n = Int(1 + Rnd * 6) ' Hide all shapes initially For i = 0 To 6 Shape1(i).Visible = False Next i ' Show the appropriate shapes for each dice face If n = 1 Then Shape1(3).Visible = True Shape2.FillColor = &HC0C0C0 ElseIf n = 2 Then Shape1(2).Visible = True Shape1(4).Visible = True Shape2.FillColor = &H8080FF ElseIf n = 3 Then Shape1(2).Visible = True Shape1(3).Visible = True Shape1(4).Visible = True Shape2.FillColor = &H80FF& ElseIf n = 4 Then Shape1(0).Visible = True Shape1(2).Visible = True Shape1(4).Visible = True Shape1(6).Visible = True Shape2.FillColor = &HFFFF00 ElseIf n = 5 Then Shape1(0).Visible = True Shape1(2).Visible = True Shape1(3).Visible = True Shape1(4).Visible = True Shape1(6).Visible = True Shape2.FillColor = &HFFFF& ElseIf n = 6 Then Shape1(0).Visible = True Shape1(1).Visible = True Shape1(2).Visible = True Shape1(4).Visible = True Shape1(5).Visible = True Shape1(6).Visible = True Shape2.FillColor = &HFF00FF End If End Sub Private Sub Roll_Dice_Click() Timer1.Enabled = True x = 0 End Sub Private Sub Timer1_Timer() If x < 1000 Then roll Else Timer1.Enabled = False End If End Sub
The VB.NET version uses a similar approach but with modern .NET features. The main differences include:
Public Class DiceForm Private x As Integer = 0 Private rand As New Random() Private diceImages As New List(Of Image)() Private Sub DiceForm_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load ' Load dice images (1.png to 6.png) For i = 1 To 6 diceImages.Add(Image.FromFile($"dice_{i}.png")) Next End Sub Private Sub RollDice() x += 10 Dim n As Integer = rand.Next(1, 7) DicePictureBox.Image = diceImages(n - 1) End Sub Private Sub RollButton_Click(sender As Object, e As EventArgs) _ Handles RollButton.Click AnimationTimer.Enabled = True x = 0 End Sub Private Sub AnimationTimer_Tick(sender As Object, e As EventArgs) _ Handles AnimationTimer.Tick If x < 1000 Then RollDice() Else AnimationTimer.Enabled = False ' Show final result message MessageBox.Show($"Dice shows: {GetDiceValue()}") End If End Sub Private Function GetDiceValue() As Integer ' Determine current dice value based on image Return diceImages.IndexOf(DicePictureBox.Image) + 1 End Function End Class
Learn both VB6 and VB.NET with practical examples like this animated dice program. This comprehensive guide covers: