Microsoft Small Basic

Program Listing: DJR074
'Hang a man for Small Basic


'You'll need to have a "word.txt" in the same folder as the script.
'First line is the number of words, i.e. 10, each conscutive line is a word.
'I recomend the "100 words" series from houghton mifflin books
'The words are out of the norm, but should be fimilar.

''should only need these once
GraphicsWindow.Show()
GraphicsWindow.CanResize = "False"
GraphicsWindow.Title = "Hanger"
InitialSetup()

'This is all the stuff that we need in the begining of the game
Sub InitialSetup

'We can play forever, so clear the screen each time we run.
GraphicsWindow.Clear()


'Get the word. read a random line from a file.
' The following line could be harmful and has been automatically commented.
' word = Text.ConvertToUpperCase(File.ReadLine(Program.Directory+"\Words.txt",Math.GetRandomNumber( File.ReadLine(Program.Directory+"\Words.txt",0))))

'We have not failed, yet, this tracks wrong letters.
Fail = 0

'Set the event handlers. we change them when game is over, reset them here.
GraphicsWindow.KeyDown = OnPress
GraphicsWindow.MouseDown = OnClick

'Populate the first of many arrays.
'pword tells us the which letters to display. I tend to use psudo-random variable names
'you don't like it, change it. Oh and ignore spaces.
'all TextWindow stuff is debuging stuff. useful to see some of the background stuff
For i = 1 To Text.GetLength(word)
If (Text.GetSubText(word,i,1) = " ")Then
pword[i] = 1
Else
pword[i] = 0
EndIf
EndFor

'Brush and pen Colors
GraphicsWindow.PenColor = "black"
GraphicsWindow.BrushColor = "blue"

'Window size
GraphicsWindow.Width = 330
GraphicsWindow.Height= 300

'It's Gallows time
GraphicsWindow.DrawLine(50,25,125,25)
GraphicsWindow.DrawLine(50,25,50,150)
GraphicsWindow.DrawLine(25,150,175,150)
GraphicsWindow.DrawLine(125,25,125,40)

'XR is our left offset for the letter boxes
'Y1,Y2 are the top offset, and they are back wards, agian suck it up =P
XR = 5
Y1 = GraphicsWindow.Height -25
Y2 = GraphicsWindow.Height -50

'draw the boxes with letters on the screen.
'it's 1-13 because i like to multitask, and it makes sense
For i = 1 to 13
'just a rectangle
GraphicsWindow.DrawRectangle(xr,Y1,20,20)
GraphicsWindow.DrawRectangle(xr,Y2,20,20)

'For those of you who don't know 65 is the code for "A" and 66 is "B" etc
'we only use upper case, since thats what the keyboard keys are read as
GraphicsWindow.DrawText(XR+5, y2+3, Text.GetCharacter(64+i))
GraphicsWindow.DrawText(XR+5, y1+3, Text.GetCharacter(77+i))

'these should be self explanitory, sets the x,y for each letter
lx[Text.GetCharacter(64+i)] = XR
lx[Text.GetCharacter(77+i)] = XR
ly[Text.GetCharacter(64+i)] = Y2
ly[Text.GetCharacter(77+i)] = Y1

' lb[] is for deciding if a key has been
'pressed before seemed good place for init the array.
lb[Text.GetCharacter(64+i)] = 0
lb[Text.GetCharacter(77+i)] = 0

'get the left offset ready for the next loop.
XR = XR + 25
EndFor

'Display the word for the first time.
DisplayWord()
EndSub


'display a the word or phrase.
Sub DisplayWord
'this function gets called alot. so clean up.
disp = ""
'crawl the word. Show what has been entered.
For i = 1 To Text.GetLength(word)
If(pword[i] = 1)Then
disp = disp + Text.GetSubText(word,i,1)
else
disp = disp + "#"
EndIf
EndFor
'Erase what we have then write it agian.
GraphicsWindow.BrushColor = "white"
GraphicsWindow.PenColor = "white"
GraphicsWindow.FillRectangle(0,GraphicsWindow.height-100,GraphicsWindow.Width,40)
GraphicsWindow.BrushColor = "green"
GraphicsWindow.DrawText(30, GraphicsWindow.Height-100, disp)
EndSub

'this is was a pain in the ass. just throwing it out there.

sub OnPress
'var to shorted the lines.
lk = GraphicsWindow.LastKey
DoLetter()
EndSub

Sub OnClick
lk = 0
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
For i = 1 to 26
If( x > lx[Text.GetCharacter(64+i)] And x < (lx[Text.GetCharacter(64+i)] +20)) Then
If( y > ly[Text.GetCharacter(64+i)] And y < (ly[Text.GetCharacter(64+i)] +20)) Then
lk = Text.GetCharacter(64+i)
EndIf
EndIf
EndFor
DoLetter()
EndSub


Sub DoLetter
If (lb[lk] = 0) Then 'oh my, it's a letter that hasn't been pressed
lb[lk] = 1 'so we don't care later.

'fill in the box. do it now.
GraphicsWindow.BrushColor = "blue"
GraphicsWindow.FillRectangle ( lx[lk],ly[lk],20,20)

'Is the letter in the word?
If(Text.IsSubText(word, lk)) Then
'crawl the word letter by letter.
For i = 1 to Text.GetLength(word)
'is this letter the key that was pressed?
If(Text.GetSubText(word,i,1) = lk) Then
pword[i] = 1 'let it be seen from now on.
EndIf
EndFor
'we are done with the key now. Display it. and throw a few debug stuff.
DisplayWord()
Sound.PlayChimes()
' TextWindow.WriteLine("WIN!")

' TextWindow.WriteLine(pword)
Else
'You failed in your task, your penalty is.
Sound.PlayClick()
DoFail()
EndIf
EndIf
'Win Condition!!!
If(Array.ContainsValue(pword,0) <> "True") then
GraphicsWindow.BrushColor = "GREEN"
GraphicsWindow.FontSize = 64
GraphicsWindow.DrawText(20,20, "YOU WIN")
GraphicsWindow.FontSize = 12
GraphicsWindow.DrawText(175,20,"press anykey to continue")
GraphicsWindow.KeyDown = Nothing
GraphicsWindow.MouseDown = Nothing

EndIf
lk = 0
EndSub
Sub DoFail
Fail = Fail + 1

GraphicsWindow.PenColor = "Tan"
If(Fail = 1)Then
GraphicsWindow.DrawEllipse(115,40,20,20)
ElseIf (Fail = 2) Then
GraphicsWindow.DrawLine(125,60, 125, 100)
ElseIf(Fail = 3) Then
GraphicsWindow.DrawLine(125,70, 110, 85)
ElseIf(Fail = 4) Then
GraphicsWindow.DrawLine(125,70, 140, 85)
ElseIf(Fail = 5) Then
GraphicsWindow.DrawLine(125,100, 110, 115)
ElseIf(Fail = 6) Then
GraphicsWindow.KeyDown = Nothing
GraphicsWindow.MouseDown = Nothing
GraphicsWindow.DrawLine(125,100, 140, 115)
GraphicsWindow.BrushColor = "RED"
GraphicsWindow.FontSize = 64
GraphicsWindow.DrawText(20,20, "YOU FAIL")
GraphicsWindow.FontSize = 12
GraphicsWindow.DrawText(175,20,"press anykey to continue")
GraphicsWindow.BrushColor = "white"
GraphicsWindow.PenColor = "white"
GraphicsWindow.FillRectangle(0,GraphicsWindow.height-100,GraphicsWindow.Width,40)
GraphicsWindow.BrushColor = "red"
GraphicsWindow.DrawText(30, GraphicsWindow.Height-100, word)


EndIf


EndSub

Sub Nothing

InitialSetup()
EndSub