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

  1. Randomize the letter.
  2. Randomize the start position right to left.
  3. Make it more fun when you win.
  4. 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:

   1:  Class gameBoard
   2:      Public WithEvents gameClock As New DispatcherTimer
   3:      Private letterBoxTop As Double = 0
   4:      Private letterBoxLeft As Double = 0
   5:      Private scoreCount As Integer = 0
   6:      Private Sub gameBoard_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
   7:          SetupLetterBox()
   8:          gameClock.Interval = New TimeSpan(0, 0, 1)
   9:          gameClock.Start()
  10:      End Sub
  11:      Private Sub SetupLetterBox()
  12:          letterBoxLeft = GetLeftMargin()
  13:          letterBoxTop = 0 - letterBox.Height
  14:          letterBox.Margin = New Thickness(letterBoxLeft, letterBoxTop, 0, 0)
  15:          Dim currentLetter As String = GetLetter()
  16:          letterBox.Content = currentLetter
  17:      End Sub
  18:      Private Sub LetterPress(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
  19:          If e.Key.ToString.ToUpper = letterBox.Content.ToString.ToUpper Then
  20:              gameClock.Stop()
  21:              scoreCount = scoreCount + 1
  22:              MessageBox.Show(String.Format("You got it!!{0}Your score is {1}!!", vbCrLf, scoreCount))
  23:              SetupLetterBox()
  24:              gameClock.Start()
  25:          ElseIf e.Key = Key.Escape Then
  26:              MessageBox.Show(String.Format("Goodbye!{0}Your score is {1}.", vbCrLf, scoreCount))
  27:              MyBase.Close()
  28:          End If
  29:      End Sub
  30:      Private Sub ClockCheck() Handles gameClock.Tick
  31:          letterBoxTop = letterBoxTop + 10
  32:          letterBox.Margin = New Thickness(letterBoxLeft, letterBoxTop, 0, 0)
  33:      End Sub
  34:      Private Function GetLeftMargin() As Integer
  35:          Dim result As Integer = 0
  36:          result = (New Random().Next) Mod CInt(gameBoard.Width - letterBox.Width)
  37:          Return result
  38:      End Function
  39:      Private Function GetLetter() As String
  40:          Dim result As String
  41:          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"}
  42:          Dim listIndex As Integer = (New Random().Next) Mod lettersList.Length
  43:          result = lettersList(listIndex).ToString
  44:          Return result
  45:      End Function
  46:  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...

Comments are closed

Bill Sempf

Husband. Father. Pentester. Secure software composer. Brewer. Lockpicker. Ninja. Insurrectionist. Lumberjack. All words that have been used to describe me recently. I help people write more secure software.

profile for Bill Sempf on Stack Exchange, a network of free, community-driven Q&A sites