Sunday, December 2, 2012

Microsoft Small Basic: A Very Simple Slot Machine Program

Here is a simple slot machine program that uses text boxes to display the three numbers. It also uses the timer command. 

'Create a very simple slot machine game
GraphicsWindow.Show()
GraphicsWindow.Title = "A Very Simple Slot Machine Game"
'Add Text Boxes
TextBoxOne = Controls.AddTextBox(30,30)
TextBoxTwo = Controls.AddTextBox(30,70)
TextBoxThree = Controls.AddTextBox(30,110)
'Add Button for rolling them
RollButton= Controls.AddButton("Roll 'em",30,150)
'Display the word Blank in each text bos
Controls.SetTextBoxText(TextBoxOne,"Blank")
Controls.SetTextBoxText(TextBoxTwo,"Blank")
Controls.SetTextBoxText(TextBoxThree,"Blank")
'When Button is pressed, call RollEmSub
Controls.ButtonClicked = RollEmSub

Sub RollEmSub
  'Generate random number between 1 and 3 for each text box
  Controls.SetTextBoxText(TextBoxOne, Math.GetRandomNumber(3))
  Controls.SetTextBoxText(TextBoxTwo, Math.GetRandomNumber(3))
  Controls.SetTextBoxText(TextBoxThree, Math.GetRandomNumber(3))
  'Read each random number
  TextOne = Controls.GetTextBoxText(TextBoxOne)
  TextTwo = Controls.GetTextBoxText(TextBoxTwo)
  TextThree = Controls.GetTextBoxText(TextBoxThree)
  'Compare random numbers
  If TextOne = TextTwo Then
    If TextTwo = TextThree Then
      'Display You Win
      Controls.SetButtonCaption(RollButton,"You Win!")
      Controls.ButtonClicked = DoNothingSub
      'Set delay timer for 20 seconds
      Timer.Interval = 2000
      'When timer expires call EndDelaySub
      Timer.Tick = EndDelaySub
   EndIf
 EndIf
EndSub

Sub EndDelaySub
  Controls.SetButtonCaption(RollButton,"Game over!")
       'Set delay timer for 20 seconds
      Timer.Interval = 2000
      'When timer expires call EndProgramSub
      Timer.Tick = EndProgramSub
EndSub

Sub DoNothingSub
EndSub

Sub EndProgramSub
  Program.End()
EndSub




No comments: