VERSION 5.00 Object = "{C1A8AF28-1257-101B-8FB0-0020AF039CA3}#1.1#0"; "MCI32.OCX" Begin VB.Form frmRaceTrack Caption = "Race Track" ClientHeight = 4176 ClientLeft = 2748 ClientTop = 2124 ClientWidth = 6876 LinkTopic = "Form1" ScaleHeight = 4176 ScaleWidth = 6876 Begin VB.Timer tmrSound Enabled = 0 'False Interval = 1000 Left = 5880 Top = 3480 End Begin MCI.MMControl mmcRaceSound Height = 372 Left = 3840 TabIndex = 4 Top = 3000 Visible = 0 'False Width = 2832 _ExtentX = 4995 _ExtentY = 656 _Version = 393216 DeviceType = "" FileName = "" End Begin VB.PictureBox picCar2 AutoSize = -1 'True BorderStyle = 0 'None Height = 492 Left = 350 ScaleHeight = 492 ScaleWidth = 1932 TabIndex = 3 Top = 3360 Width = 1932 End Begin VB.Timer tmrCar2 Enabled = 0 'False Interval = 5 Left = 360 Top = 3000 End Begin VB.Timer tmrGo Enabled = 0 'False Interval = 250 Left = 1320 Top = 480 End Begin VB.CommandButton cmdGo Caption = "Go" Height = 492 Left = 240 TabIndex = 2 Top = 480 Width = 972 End Begin VB.PictureBox picSignalLight AutoRedraw = -1 'True BorderStyle = 0 'None Height = 1572 Left = 5640 ScaleHeight = 1572 ScaleWidth = 504 TabIndex = 1 Top = 360 Width = 500 End Begin VB.Timer tmrCar1 Enabled = 0 'False Interval = 5 Left = 360 Top = 1920 End Begin VB.PictureBox picCar1 AutoSize = -1 'True BorderStyle = 0 'None Height = 528 Left = 350 ScaleHeight = 528 ScaleWidth = 1908 TabIndex = 0 Top = 2280 Width = 1908 End End Attribute VB_Name = "frmRaceTrack" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' Chapter 10 Race Track Case Study ' 3/99 Option Explicit Private Sub Form_Load() Randomize mmcRaceSound.DeviceType = "WaveAudio" mmcRaceSound.FileName = "Zoom.wav" mmcRaceSound.Command = "Open" End Sub Private Sub cmdGo_Click() tmrGo.Enabled = True End Sub Private Sub tmrGo_Timer() Static blnRed As Boolean, blnYellow As Boolean, blnGreen As Boolean If Not blnRed Then Call DrawRed(blnRed) ElseIf Not blnYellow Then Call DrawYellow(blnYellow) ElseIf Not blnGreen Then Call DrawGreen(blnGreen) Else 'start race tmrGo.Enabled = False picSignalLight.Cls 'remove signal light blnRed = False blnYellow = False blnGreen = False cmdGo.Visible = False 'remove Go button picCar1.Left = 350 'race cars picCar2.Left = 350 picCar1.Picture = LoadPicture("Car.wmf") picCar2.Picture = LoadPicture("Carsquare.wmf") mmcRaceSound.Command = "Play" mmcRaceSound.Command = "Prev" tmrSound.Enabled = True tmrCar1.Enabled = True tmrCar2.Enabled = True End If End Sub Sub DrawRed(ByRef blnRed As Boolean) 'draws solid red light picSignalLight.FillColor = vbRed picSignalLight.FillStyle = vbFSSolid picSignalLight.Circle (200, 200), 200, vbRed blnRed = True End Sub Sub DrawYellow(ByRef blnYellow As Boolean) 'draws solid yellow light picSignalLight.FillColor = vbYellow picSignalLight.Circle (200, 700), 200, vbYellow blnYellow = True End Sub Sub DrawGreen(ByRef blnGreen As Boolean) 'draws solid green light picSignalLight.FillColor = vbGreen picSignalLight.Circle (200, 1200), 200, vbGreen blnGreen = True End Sub Private Sub tmrSound_Timer() mmcRaceSound.Command = "Play" 'starts playing sound track mmcRaceSound.Command = "Prev" 'sets sound track to beginning End Sub Private Sub tmrCar1_Timer() picCar1.Move picCar1.Left + Int(41 * Rnd + 20) 'moves car 'if car reaches the right side of the form restart If picCar1.Left >= (frmRaceTrack.Width - picCar1.Width) Then Call EndRace MsgBox "Car 1 wins!" End If End Sub Private Sub tmrCar2_Timer() picCar2.Move picCar2.Left + Int(41 * Rnd + 20) 'moves car 'if car reaches the right side of the form restart If picCar2.Left >= (frmRaceTrack.Width - picCar2.Width) Then Call EndRace MsgBox "Car 2 wins!" End If End Sub '******************************************************************* ' Resets the form for a new race ' ' Post: timers, mmc, and picture objects reset '******************************************************************* Sub EndRace() tmrCar2.Enabled = False 'stop car timer tmrCar1.Enabled = False 'stop car timer tmrSound.Enabled = False 'stop sound timer mmcRaceSound.Command = "Stop" 'stop sound cmdGo.Visible = True 'display Go command button picCar2.Picture = LoadPicture 'remove car graphic picCar1.Picture = LoadPicture 'remove car graphic End Sub Private Sub Form_Unload(Cancel As Integer) mmcRaceSound.Command = "Close" End Sub