Sunday, June 10, 2007

The Start Function(Part 4)

This is part of a series of articles that shows the creation of a Silverlight game. Please read part 1,part 2 or part 3 if you haven`t done so yet.


   1:  void Start(int size,double blockProbability,Point StartPosition,Point EndPosition)


The Start function receives 4 parameters:

  • size - the width and the height of the board.

  • blockProbability - the probability of a blocking block to appear. Should increase as the level gets higher.

  • StartPosition - the point of origin for the red rectangle

  • EndPosition - the point where the red rectangle needs to get(yellow rectangle)



   2:          {

   3:              startPoint = new Point(StartPosition.X * multiplier, StartPosition.Y * multiplier);

   4:              endPoint = new Point(EndPosition.X * multiplier, EndPosition.Y * multiplier);

   5:   


We use the multiplier in order to transform from logical points to real pixels and we initialize the startPoint and the endPoint variable.

   6:              NextLevel.Visibility = Visibility.Collapsed;

   7:              NextLevelText.Visibility = Visibility.Collapsed;

   8:              Congratulations.Visibility = Visibility.Collapsed;


Here we just hide the Congratulations text(should only be visible when the user wins), the next level button which should also be visible only when the user wins and the text on top of the next level button.

   9:              steps = 0;

  10:              isMoving = false;

  11:              map = new int[size, size];

  12:              int unpassable = 0;


Here we initialize some counters. First the number of steps that the user has made must be 0. Then we say that the red rectangle is not moving. Also we initialize the map that will be composed of 0s and 1s and we initialize the variable that counts the number of unpassable regions.

  13:              for (int i = 0; i < size; i++)

  14:              {

  15:                  for (int j = 0; j < size; j++)

  16:                  {

  17:                      map[i, j] = (r.NextDouble() < blockProbability) ? 1 : 0;

  18:                      map[(int)StartPosition.X, (int)StartPosition.Y] = 0;

  19:                      map[(int)EndPosition.X, (int)EndPosition.Y] = 0;

  20:                      if (map[i, j] == 0)

  21:                      {

  22:                          AddNewRectangle(new Point(i , j ), Colors.Green);

  23:                      }

  24:                      if (map[i, j] == 1)

  25:                      {

  26:                          unpassable++;

  27:                          AddNewRectangle(new Point(i , j ), Colors.Black);

  28:                      }

  29:                  }

  30:              }


Setups the map that will be displayed. The map will be completely random and will display in avarage (blockProbability*100)% unpassable blocks. We also increase the counter of unpassable blocks each time we generate one. The function AddNewRectangle just adds a new rectangle to the board and will be discussed later.

  31:              Unpassable.Text = unpassable.ToString();

  32:              CurrentLevel.Text = level.ToString();


We display the number of unpassable blocks and the current level on the screen.

  33:              //setup visibility and location of helper objects

  34:   

  35:              Congratulations.SetValue(Canvas.TopProperty, size * multiplier + 5);

  36:              NumberOfSteps.SetValue(Canvas.TopProperty, size * multiplier + 5);

  37:              NumberOfStepsText.SetValue(Canvas.TopProperty, size * multiplier + 5);

  38:              NextLevelText.SetValue(Canvas.LeftProperty, size * multiplier + 40);

  39:              NextLevel.SetValue(Canvas.LeftProperty, size * multiplier + 5);

  40:              Restart.SetValue(Canvas.LeftProperty, size * multiplier + 5);

  41:              RestartText.SetValue(Canvas.LeftProperty, size * multiplier + 30);

  42:              UnpassableText.SetValue(Canvas.LeftProperty, size * multiplier + 19);

  43:              Unpassable.SetValue(Canvas.LeftProperty, size * multiplier + 107);

  44:              CurrentLevelText.SetValue(Canvas.LeftProperty, size * multiplier + 5);

  45:              CurrentLevel.SetValue(Canvas.LeftProperty, size * multiplier + 107);

  46:   

  47:   


Because the board of the game si expandable we need to keep the buttons out of the board region so we move them dinamically.

  48:              //setup the main objects:the start and the end(start is the moving object)

  49:              end = CreateSpecialRectangle(EndPosition, Colors.Yellow);

  50:              start = CreateSpecialRectangle(StartPosition, Colors.Red);

  51:              //setup event handler for the end object

  52:              end.MouseLeftButtonDown += new MouseEventHandler(r_MouseLeftButtonDown);

  53:          }


We then create the special rectangles that will move and will represent the end point. We also wire up an event that fires when the yellow rectangle is pressed.

The rest of the functions will be discussed in part 5.

To download the entire project just click here

No comments: