VB Tutor Logo VB2022 VB2019 VB6 VB Sample Code About Us
VB Tutor Logo

Time Bomb Simulation in Visual Basic


Game Overview

The Time Bomb simulation creates a thrilling experience where players must defuse a bomb by entering a 3-digit code before the timer expires. Key features include:

  • 60-second countdown timer with visual display
  • Password entry panel for defusing the bomb
  • Realistic explosion animation and sound effects
  • Visual feedback for successful and failed defusal
  • Reset functionality for multiple attempts

Game Implementation

This implementation uses timer controls, multimedia components, and visual elements to create a suspenseful bomb defusal experience.

1

Countdown Timer

The Timer control counts down from 60 seconds. When it reaches zero, the destruction sequence is triggered.

2

Password Verification

Players enter a 3-digit code to defuse the bomb. The correct code is predefined in the program.

3

Multimedia Effects

When time runs out, an explosion animation appears and a sound effect plays using the Multimedia Control.

4

Visual Feedback

The interface changes based on game outcomes - showing success messages or explosion animations.

VB6 Implementation

The original implementation in Visual Basic 6 uses these key components:

'Declaring variables and constants
Dim countdown As Integer
Dim x As Integer
Const code As Integer = 398

Private Sub Form_Load()
    Timer1.Enabled = True
    MMControl1.Visible = False
    'To generate a three-digit random password
    Randomize Timer
    code = Int(Rnd * 1000)
    Lbl_Status.Visible = True
    Lbl_Status.Caption = Str$(code)
End Sub

Private Sub Timer1_Timer()
    'Countdown display
    countdown = 60 - x
    If countdown <= 60 And countdown > -1 Then
        Lbl_Timer.Caption = Str$(countdown)
        x = x + 1
    ElseIf countdown < 0 Then
        Timer1.Enabled = False
        destruction
    End If
End Sub

Sub destruction()
    'Show explosion and play sound
    Lbl_Status.Caption = "Deactivation Fail!"
    Image2.Visible = False 'Hide bomb image
    Image1.Visible = True  'Show explosion image
    
    'Configure multimedia control
    MMControl1.Notify = False
    MMControl1.Wait = True
    MMControl1.Shareable = False
    MMControl1.DeviceType = "WaveAudio"
    MMControl1.FileName = "C:\bomb.wav"
    MMControl1.Command = "Open"
    MMControl1.Command = "Play"
End Sub

Game Interface

Time Bomb game interface

Figure: VB6 implementation of the Time Bomb simulation

VB.NET Implementation

Here's the modern VB.NET version using Windows Forms:

Imports System.Media

Public Class TimeBombForm
    Private countdown As Integer = 60
    Private Const defuseCode As Integer = 398
    Private WithEvents timer As New Timer()
    Private soundPlayer As New SoundPlayer()

    Private Sub TimeBombForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Configure timer
        timer.Interval = 1000 ' 1 second
        timer.Start()

        ' Initialize UI
        bombImage.Visible = True
        explosionImage.Visible = False
        successImage.Visible = False
        timeLabel.Text = countdown.ToString()
        statusLabel.Text = "ENTER DEFUSAL CODE"
    End Sub

    Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
        countdown -= 1
        timeLabel.Text = countdown.ToString()

        If countdown <= 0 Then
            timer.Stop()
            ExplodeBomb()
        End If
    End Sub

    Private Sub ExplodeBomb()
        ' Visual changes
        bombImage.Visible = False
        explosionImage.Visible = True
        successImage.Visible = False
        statusLabel.Text = "DEFUSAL FAILED! BOMB EXPLODED!"

        ' Play explosion sound
        Try
            soundPlayer.Stream = My.Resources.ExplosionSound
            soundPlayer.Play()
        Catch ex As Exception
            MessageBox.Show("Failed to play explosion sound: " & ex.Message)
        End Try
    End Sub

    Private Sub defuseButton_Click(sender As Object, e As EventArgs) Handles defuseButton.Click
        Dim codeInput As String = codeTextBox1.Text.Trim() & codeTextBox2.Text.Trim() & codeTextBox3.Text.Trim()
        Dim enteredCode As Integer

        If Integer.TryParse(codeInput, enteredCode) Then
            If enteredCode = defuseCode Then
                timer.Stop()
                bombImage.Visible = False
                explosionImage.Visible = False
                successImage.Visible = True
                statusLabel.Text = "BOMB DEFUSED SUCCESSFULLY!"
            Else
                statusLabel.Text = "WRONG CODE! TRY AGAIN!"
            End If
        Else
            statusLabel.Text = "INVALID CODE FORMAT!"
        End If
    End Sub

    Private Sub resetButton_Click(sender As Object, e As EventArgs) Handles resetButton.Click
        ' Reset game
        countdown = 60
        timeLabel.Text = countdown.ToString()
        codeTextBox1.Clear()
        codeTextBox2.Clear()
        codeTextBox3.Clear()

        bombImage.Visible = True
        explosionImage.Visible = False
        successImage.Visible = False
        statusLabel.Text = "ENTER DEFUSAL CODE"
        timer.Start()
    End Sub
End Class

VB6 vs VB.NET Comparison

Feature VB6 VB.NET
Timer Control MMControl for sound System.Windows.Forms.Timer
Sound Playback MMControl component Media.SoundPlayer class
Resource Management External files Embedded resources
Event Handling Event procedures WithEvents and Handles
Error Handling On Error statements Try-Catch blocks

Key Enhancements in VB.NET

The VB.NET implementation includes several improvements:

  • Modern event handling with Handles keyword
  • Structured error handling with Try-Catch
  • Embedded resources for sounds and images
  • Improved timer control with better precision
  • Object-oriented design approach
  • Better UI controls and layout management

VB.NET Game Interface

VB.NET Time Bomb game interface

Figure: Modern VB.NET implementation