Live blogging a XAML project with CodePlex, Part Duex
And we are back at the Letters Are Falling project, which has it's own Codeplex URL now ... heh. If you remember from the last post, we need to
- Randomize the letter.
- Randomize the start position right to left.
- Make it more fun when you win.
- Handle it when you lose.
I am going to start with the first two. For now, I am going to keep this pretty declaritive, and try and use the WPF objects 'correctly' later once I get the bugs worked out of the logic.
Let's add two functions: one for the left randomization and one for the letter randomization. The left randomization needs to set the left margin anywhere from zero to the width of the window minus the width of the letterBox. The letter randomization needs to pick from a selection of letters, which we will later make configurable.
Now, certain other things have to happen to make this a game. It has to restart when you get the right letter, keep score, things I forgot in the original requirements. In order to do this, I have to set up the game to reset the letterbox, and not have all of the code in the form load event. To do this, I have built a new function to set up the letterbox, which calls the two new randomization functions. Then I can change my keypress event to make some gametime decisions, including a Quit function.
The VB file now looks like this:
Class gameBoard
Public WithEvents gameClock As New DispatcherTimer
Private letterBoxTop As Double = 0
Private letterBoxLeft As Double = 0
Private scoreCount As Integer = 0
Private Sub gameBoard_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
SetupLetterBox()
gameClock.Interval = New TimeSpan(0, 0, 1)
gameClock.Start()
End Sub
Private Sub SetupLetterBox()
letterBoxLeft = GetLeftMargin()
letterBoxTop = 0 - letterBox.Height
letterBox.Margin = New Thickness(letterBoxLeft, letterBoxTop, 0, 0)
Dim currentLetter As String = GetLetter()
letterBox.Content = currentLetter
End Sub
Private Sub LetterPress(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
If e.Key.ToString.ToUpper = letterBox.Content.ToString.ToUpper Then
gameClock.Stop()
scoreCount = scoreCount + 1
MessageBox.Show(String.Format("You got it!!{0}Your score is {1}!!", vbCrLf, scoreCount))
SetupLetterBox()
gameClock.Start()
ElseIf e.Key = Key.Escape Then
MessageBox.Show(String.Format("Goodbye!{0}Your score is {1}.", vbCrLf, scoreCount))
MyBase.Close()
End If
End Sub
Private Sub ClockCheck() Handles gameClock.Tick
letterBoxTop = letterBoxTop + 10
letterBox.Margin = New Thickness(letterBoxLeft, letterBoxTop, 0, 0)
End Sub
Private Function GetLeftMargin() As Integer
Dim result As Integer = 0
result = (New Random().Next) Mod CInt(gameBoard.Width - letterBox.Width)
Return result
End Function
Private Function GetLetter() As String
Dim result As String
Dim lettersList As String() = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Dim listIndex As Integer = (New Random().Next) Mod lettersList.Length
result = lettersList(listIndex).ToString
Return result
End Function
End Class
In the next edition of Live Blogging a XAML project, we will get into a little more of the XAML itself, including getting rid of the messagebox, and adding a little pizzaz! Until next time...
S


