Sunday, June 10, 2007

Starting the C# code(Part 3)

So with the static part in place we need to get the game moving...But first we need some class variables that will help within the project:


   1:          //the level

   2:          static int level = 1;

   3:          //the number of times that the user moved the rectangle in this round

   4:          static int steps = 0;

   5:          //the probability that the block generated is a blocking block(cannot pass through it)

   6:          static double blockingProbability = 0.03;

   7:          //the current destination point of the moving rectangle

   8:          Point toMove;

   9:          //the start point of the round

  10:          Point startPoint;

  11:          //the end point of the round

  12:          Point endPoint;

  13:          //true if the user rectangle is moving

  14:          bool isMoving;

  15:          //keeps the map

  16:          int[,] map;

  17:          Random r;

  18:          //the height of the generated rectangles

  19:          const int height = 19;

  20:          //the width of the generated rectangles

  21:          const  int width = 19;

  22:          //the multiplier by which the matrix location is multiplied (Ex if multiplier is 

  23:          //20 than the location (1,2) on the map will actually be shown at

  24:          //(20,40)

  25:          const int multiplier = 20;

  26:          //the start rectangle

  27:          Rectangle start;

  28:          //the end rectangle

  29:          Rectangle end;

  30:          //the board size

  31:          static int boardSize = 10;


This variables are all straight forward and are explained through comments. They will be explained in more detail later on when we get to the functions implemented.


Now that we have the variables in place we need something to initialize them. This is where the Page_Loaded function comes into place:

   1:          public void Page_Loaded(object o, EventArgs e)

   2:          {

   3:              // Required to initialize variables

   4:              InitializeComponent();

   5:              r = new Random();

   6:              timer.Completed += new EventHandler(timer_Completed);

   7:              Restart.MouseLeftButtonDown += new MouseEventHandler(Restart_MouseLeftButtonDown);

   8:              RestartText.MouseLeftButtonDown += new MouseEventHandler(Restart_MouseLeftButtonDown);

   9:              NextLevel.MouseLeftButtonDown += new MouseEventHandler(NextLevel_MouseLeftButtonDown);

  10:              NextLevelText.MouseLeftButtonDown += new MouseEventHandler(NextLevel_MouseLeftButtonDown);

  11:              

  12:              Start(boardSize, blockingProbability, new Point(r.Next() % boardSize, r.Next() % boardSize), new Point(r.Next() % boardSize, r.Next() % boardSize));

  13:          }


This function initializes the random number generator on line 6. Then we use some event-handlers. The objects timer,Restart,RestartText,NextLevel,NextLevelText are all decalred in the XAML file.

  • The timer is a storyboard variable that is used in order to create the animation for the movement of the red rectangle.

  • The restart object is a rectangle used as a button.

  • The RestartText object is just the text that is displayed on the Restart rectangle.

  • The NextLevel object is a rectangle used as a button.

  • The NextLevelText object is the text displayed on the NextLevel rectangle.


All the events that were wired here will be explained later on.

Next we call the Start function that is the core function of the game as it creates a new level and initializes the gameplay. It will be explained in the next post.

(See part 4)

No comments: