// Spelling Bee program // This program was written by Wayne, Ernest, and Camille Rust // as a family project. // Copyright (C) 2007, Wayne Rust and Family. // All Rights Reserved. // This program may be freely copied, distributed, and modified for personal // or non-profit use as long as credit is given. Please email us at rustw@myrealbox.com // Enjoy! To add your own list of words, replace the "soundlist.txt" file and the words // directory. Program SpellingBee // TextBox class taken from example on Phrogram site. Class TextBox Var Text As String // User input Var Location As Point // Screen location (Left, Top) Var Size As Point // Screen size (Width, Height) Var MaxTextLength As Integer // Maximum text length Var Tick As Integer // Yield time delay Var FontWidth As Integer = 11 // Screen font size (hard coded) Var BackgroundColor As Integer = Colors.White Method Clear() Color(BackgroundColor) Pen(False) MoveTo(Location.X, Location.Y) Rectangle(Size.X + 1, Size.Y + 1, True) End Method Method Draw() BeginFrame() // Clear background Color(Colors.White) Pen(False) MoveTo(Location.X, Location.Y) Rectangle(Size.X, Size.Y, True) // Draw border Color(Colors.Black) Rectangle(Size.X, Size.Y, False) // Draw text SetFont(Fonts.LucidaConsole , FontWidth, False, False, False) MoveBy(2, 3) Print(text) End Method Method Init(Location As Point, Size As Point) Me.Location = Location.Clone() Me.Size = Size.Clone() Text = "_" MaxTextLength = Floor(Size.X / (fontWidth * .86)) End Method // Get user input (non-blocking) Method Add(Key As String) Var prev As String = text If Length(Key) = 1 Then If Length(text) < MaxTextLength Then Text = SubString(text, 1, Length(text) - 1) + key + "_" End If ElseIf Key = Keys.Back Or Key = Keys.Delete Then If Length(text) > 1 Then Text = SubString(text, 1, Length(text) - 2) + "_" End If ElseIf Key = keys.Enter Then Text = SubString(text, 1, Length(text) - 1) End If If prev <> text Then Draw() RefreshScreen() End If End Method // Get user input (blocking) Function Input() As String Var key As String Text = "_" Draw() While key <> Keys.Enter Yield() key = Getkey() Add(key) End While Return Text End Function // Avoids excessive CPU usage Method Yield() Delay(1) DoEvents() If tickCount() > Tick Then Tick = TickCount() + 2000 RefreshScreen() End If End Method End Class Class Player Var location As Point Var words As StringList Var avitarName As String Var avitar As Sprite Var score As Integer = 0 Var name As String Var exists As Boolean = False Var backgroundColor As Integer Method GetWordList() // Open up the sound list and read words. If Not FileExists("soundlist.txt") Then PrintLine("Can't find soundlist.txt") Return End If Define file As TextInputFile Define line As String file = OpenTextFile("soundlist.txt") While (Not file.EndOfFile) DebugBreak() line = file.ReadLine() words.Add(line) End While End Method Function AskWord() As String Define Entry As TextBox Define index As Integer Define filename As String Define input As String Define word As String Define vocal As Sound // Say next word. index = Random(1, words.Count) word = words.Item(index) filename = "words\\" + word + ".wav" // Get input Entry.Init(New Point(location.X, location.Y + avitar.Height + 65), New Point(200, 20)) Entry.Draw() input = "" While input = "" PlaySound(filename) input = Entry.Input() Entry.Clear() End While If (input = word) Then score = score + 1 PlaySound("sounds\\applause.wav") Animate() Draw() //vocal.LoadFile("sounds\\applause.wav") words.Remove(input) Else vocal.LoadFile("sounds\\wrong.wav") vocal.Play() While vocal.IsPlaying Delay(100) End While End If Return (word) End Function Method Draw() Pen(False) Moveto(location.X, location.Y + avitar.Height + 3) Pen(True) ClearRectangle(location.X, location.Y + avitar.Height + 3, 200, 60, backgroundColor) Color(Colors.Black) // Draw name. SetFont(Fonts.Arial , 23, True, True, False) PrintLine(name) // Draw score SetFont(Fonts.Arial , 13, True, False, False) PrintLine("Score: " + score) End Method Method Create(text As String, left As Integer, top As Integer, bgColor As Integer) name = text backgroundColor = bgColor avitarName = "jump1" location.X = left location.Y = top avitar.Load(avitarName + ".gif") avitar.MoveTo(location.X, location.Y) avitar.Visible = True avitar.Show() GetWordList() Draw() End Method Method Animate() Define MyAnimationTimeline As Integer [6] MyAnimationTimeline[1] = 50 MyAnimationTimeline[2] = 50 MyAnimationTimeline[3] = 50 MyAnimationTimeline[4] = 50 MyAnimationTimeline[5] = 50 MyAnimationTimeline[6] = 50 avitar.AnimationTimeline = MyAnimationTimeline Define TerminatingTime As Integer = TickCount() + 1000 While TickCount() < TerminatingTime RefreshScreen() DoEvents() // Tell the system to handle the mouse, the // keyboard, and other programs. End While avitar.ActiveFrame = 1 End Method End Class Method Main() Define backgroundColor As Integer = Colors.White //75 Define player As Player[4] Define playerIndex As Integer = 1 Define playerCount As Integer = 1 Define word As String Define inputBox As TextBox Define name As String Define wordsLeft As Integer Define winner As Integer Define multipleWinners As Boolean Define winnerNames As String // Set screen size and clear back SetScreenSize( 800, 480 ) ClearBackground(backgroundColor) // Draw the bees at the top. Define bee1 As Sprite = New Sprite("bee.gif") Define bee2 As Sprite = New Sprite("bee.gif") bee1.Visible = True bee2.Visible = True bee1.MoveTo (150,5) bee2.MoveTo (565,5) // Draw title SetFont("Arial" , 42, True, True, False ) Pen ( False ) MoveTo ( 210, 0 ) Pen ( True ) PrintLine ("Spelling Bee") // Get player names Loop 4 SetFont ("Arial" , 12, True, False, False ) Pen ( False ) MoveTo ( 0, 450 ) Pen ( True ) Print("Enter name of Player " + playerIndex + ": ") inputBox.Init(New Point(200, 450), New Point(200, 20)) name = inputBox.Input() ClearRectangle(0, 450, 401, 21, backgroundColor) If (name = "") Then If (playerIndex = 1) Then Alert("No player names entered.", "Error") Stop() End If Break End If player[playerIndex].Create(name, (playerIndex - 1) * 200 + 10, 65, backgroundColor) playerIndex = playerIndex + 1 End Loop playerCount = playerIndex - 1 SetFont ("Arial" , 12, True, False, False ) Pen ( False ) MoveTo ( 0, 450 ) Pen ( True ) Print("How many rounds? ") inputBox.Init(New Point(200, 450), New Point(200, 20)) name = inputBox.Input() ClearRectangle(0, 450, 401, 21, backgroundColor) If name = "" Then name = "10" End If wordsLeft = playerCount * ConvertToDecimal(name) playerIndex = 1 While player[playerIndex].words.Count > 0 And wordsLeft > 0 wordsLeft = wordsLeft - 1 word = player[playerIndex].AskWord() ClearRectangle(0, 450, 401, 21, backgroundColor) Pen(False) MoveTo(0, 450) Color(Colors.Red) Pen(True) SetFont(Fonts.Arial , 12, True, True, False) Print(" Last word: " + word) Color(Colors.Black) playerIndex = playerIndex + 1 If (playerIndex > playerCount) Then playerIndex = 1 End If End While winner = playerCount winnerNames = player[winner].name playerIndex = playerCount - 1 While playerIndex > 0 If player[winner].score < player[playerIndex].score Then winner = playerIndex multipleWinners = False winnerNames = player[playerIndex].name ElseIf player[winner].score = player[playerIndex].score Then winner = playerIndex multipleWinners = True winnerNames = winnerNames + " and " + player[playerIndex].name End If playerIndex = playerIndex - 1 End While If multipleWinners Then Alert("The winners are " + winnerNames + "!", "Congratulations") Else Alert("The winner is " + player[winner].name + "!", "Congratulations") End If Stop() End Method End Program