One-Buttoned Bandit 1. Start a new application. In this - TopicsExpress



          

One-Buttoned Bandit 1. Start a new application. In this example, we will build a computer version of a slot machine. Well use random numbers and timers to display three random pictures. Certain combinations of pictures win you points. Place two image boxes, two label boxes, and two command buttons on the form. 2. Set the following properties: Form1: BorderStyle 1-Fixed Single Caption One -Buttoned Bandit Name frmBandit Command1: Caption &Spin It Default True Name cmdSpin Command2: Caption E&xit Name cmdExit Timer1: Enabled False Interval 100 Name timSpin Timer2: Enabled False Interval 2000 Name timDone Label1: Caption Bankroll FontBold True FontItalic True FontSize 14 Label2: Alignment 2-Center AutoSize True BorderStyle 1-Fixed Single Caption 100 FontBold True FontSize 14 Name lblBank ****you can put any pictures you want **** ****example is banana,cherry,seven,apple Image1: Name imgChoice Picture earth.ico Visible False Copy and paste this image box three times, creating a control element (imgChoice) with four elements total. Set the Picture property of the other three boxes. Image1(1): Picture snow.ico Image1(2): Picture misc44.ico Image1(3): Picture face03.ico Image2: BorderStyle 1-Fixed single Name imgBandit Stretch True Copy and paste this image box two times, creating a three element control array (Image2). You dont have to change any properties of the newly created image boxes. A few words on what were doing. We will randomly fill the three large image boxes by choosing from the four choices in the non-visible image boxes. One timer (timSpin) will be used to flash pictures in the boxes. One timer (timDone) will be used to time the entire process. 3. Type the following lines in the general declarations area of your forms code window. Bankroll is your winnings. Option Explicit Dim Bankroll As Integer 4. Attach this code to the Form_Load procedure. Private Sub Form_Load() Randomize Timer Bankroll = Val(lblBank.Caption) End Sub Here, we seed the random number generator and initialize your bankroll. 5. Attach the following code to the cmdExit_Click event. Private Sub cmdExit_Click() MsgBox You ended up with + Str(Bankroll) + points., vbOKOnly, Game Over End End Sub When you exit, your final earnings are displayed in a message box. 6. Attach this code to the cmdSpin_Click event. Private Sub cmdSpin_Click() If Bankroll = 0 Then MsgBox Out of Cash!, vbOKOnly, Game Over End End If Bankroll = Bankroll - 1 lblBank.Caption = Str(Bankroll) timSpin.Enabled = True timDone.Enabled = True End Sub Here, we first check to see if youre out of cash. If so, the game ends. If not, you are charged 1 point and the timers are turned on. 7. This is the code for the timSpin_Timer event. Private Sub timSpin_Timer() imgBandit(0).Picture = imgChoice(Int(Rnd * 4)).Picture imgBandit(1).Picture = imgChoice(Int(Rnd * 4)).Picture imgBandit(2).Picture = imgChoice(Int(Rnd * 4)).Picture End Sub Every 0.1 seconds, the three visible image boxes are filled with a random image. This gives the effect of the spinning slot machine. 8. And, the code for the timDone_Timer event. This event is triggered after the bandit spins for 2 seconds. Private Sub timDone_Timer() Dim P0 As Integer, P1 As Integer, P2 As Integer Dim Winnings As Integer Const FACE = 3 timSpin.Enabled = False timDone.Enabled = False P0 = Int(Rnd * 4) P1 = Int(Rnd * 4) P2 = Int(Rnd * 4) imgBandit(0).Picture = imgChoice(P0).Picture imgBandit(1).Picture = imgChoice(P1).Picture imgBandit(2).Picture = imgChoice(P2).Picture If P0 = FACE Then Winnings = 1 If P1 = FACE Then Winnings = 3 If P2 = FACE Then Winnings = 10 End If End If ElseIf P0 = P1 Then Winnings = 2 If P1 = P2 Then Winnings = 4 End If Bankroll = Bankroll + Winnings lblBank.Caption = Str(Bankroll) End Sub First, the timers are turned off. Final pictures are displayed in each position. Then, the pictures are checked to see if you won anything. 9. Save and run the application. See if you can become wealthy.
Posted on: Sun, 16 Mar 2014 11:25:27 +0000

Trending Topics



Recently Viewed Topics




© 2015