7. Showing our field

When we wrote the code for our CreateField() method, we didn't write any code for displaying the field to the user. We'll do it in this chapter. We'll take a look at graphic programming, GDI+… So, let's fill our DrawField() method. Here's the code – go trough it, read the comments (the green stuff), and hopefully, you'll get it. :) I did write a lot of comments. I hope they help.

// Define a Brush – an object that will
// hold the color value for our Game 
// Field background.

Brush Background = Brushes.PowderBlue;

// We want to show our field inside the 
// Picture Box, so we create a graphich 
// object inside our Picture Box.

Graphics gField = picGameField.CreateGraphics();

// We'll go trough all of our fields, and we'll 
// display them one by one (taking into consideration
// the color value each field contains). We have 150 
// fields, so, we'll do this with the help of two loops.
// It woud be painfull to do it manually.

// First loop that is going trough every line.

for (int v = 0; v <>

  // Second loop – this one is going
  // trough every column.

  for (int s = 0; s < iWidth; s++)
 
        // We define a small rectangle, for
    // every field in our game field. We'll
    // use the variable that contains the
    // rectangle's width/height. If you
    // remember, we defined it in our first
    // lines of code.

    Rectangle rtgField = new Rectangle
    (s * iFieldSize, v * iFieldSize,

     FieldSize, iFieldSize);
  
    // First of all, we'll fill our rectangles 
    // with our background color.

    gField.FillRectangle(Background, rtgField);
   
    // And now, we'll fill the field with the color 
    // that is defined with the fields value.

    switch (Fieldv, s)
   
      case 0:
        // Draw empty field...
        break;
      case 1:
        // I've decided that color #1 is Blue. So, if 
        // the field holds a value of 1 – We'll color it 
        // blue. And we'll do the same for all
        // other colors.

        gField.FillRectangle(Brushes.Blue, rtgField);
        break;
      case 2:
        // If the field contains value 2:
        // we'll fill it red.

        gField.FillRectangle(Brushes.Red, rtgField);
        break;
      case 3:
        // If the field contains value 3:
        // we'll fill it green.

        gField.FillRectangle(Brushes.Green, rtgField);
        break;
      case 4:
        // If the field contains value 4:
        // we'll fill it yellow.

        gField.FillRectangle(Brushes.Yellow, rtgField);
        break;
      case 5:
        // If the field contains value 5:
        // we'll fill it purple.

        gField.FillRectangle(Brushes.Purple, rtgField);
        break;
      case 6:
        // If the field contains value 6:
        // we'll fill it black.

        gField.FillRectangle(Brushes.Black, rtgField);
        break;
      case 7:
        // If the field contains value 7:
        // we'll fill it orange.

        gField.FillRectangle(Brushes.Orange, rtgField);
        break;
      case 8:
        // If the field contains value 8:
        // we'll fill it gray.

        gField.FillRectangle(Brushes.Gray, rtgField);
        break;
      case 9:
        // If the field contains value 9:
        // we'll fill it dark blue.

        gField.FillRectangle(Brushes.DarkBlue, rtgField);
        break;
      case 10:
        // If the field contains value 10:
        // we'll fill it pink.

        gField.FillRectangle(Brushes.Pink, rtgField);
        break;
      default:
        // If our program comes to this point of the code,
        // something's gone wrong! ;) XD

        break;
   
  }
}


Now, you're just a few steps away from seeing it on the screen. Go back to your Design View by clicking the tab 'frmMain.cs design' on top of the code window. Double click on the 'New Game' button, and you should wind up right back in the 'code' window. But, this time, you'll see some extra lines of code that were generated by the VS.net. The new lines are:

private void butGame_Click(object sender, EventArgs e)

Add this line of code in between the two curly brackets:

DrawField();

And run your game! (Click on the green 'Play' button in your toolbar). Click on the button, and you should see this:



In the next chapter: We'll fill our CreateGrid() function, and play a little with our blocks... :)

more:here