Game Development Tutorial

Everything a total beginner needs for game programing!

Showing posts with label grid. Show all posts
Showing posts with label grid. Show all posts

25. Bubble bath

Up to now, we displayed the blocks as squares filled with color. From now on, we'll display little icon images instead. If you remember, we used rectangles to define the size, and the position for each field. And that suits us just fine! So, here's a part of our code that we wrote in chapter 7:

(This is a part of the code from chapter 7, method DrawField())

switch (Field[v, 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;
}

We'll replace that FillRectangle statement with a new one: DrawImage. But before we do that, we have to store our images somewhere. And, lucky us, there is a control made just for that. Go to your toolbox, and find the Components group. In there you'll find the ImageList control. Double-click it, and it will apear next to your timers. Nice. Set the properties like this:

ImageList

  • (name):ilTiles
  • ColorDepth:  Depth32Bit
  • TransparencyColor:  Web > Transparent
  • Images:  [Load 10 different bubble images]
  • ImageSize:  32; 32

Now, add add our bubble graphics files into the list. You can reorder them however you want. After all, we'll display the right picture by calling it's index number. Ready? Ok… Here is the revised code for the Switch in the 'DrawField()' method. See the difference? Nice… How about trying it out?

switch (Field[v, s])
{
  case 0:
    //Draw empty field...
    break;
  case 1:
    gField.DrawImage    (ilTiles.Images[0], rtgField);
    break;
  case 2:
    gField.DrawImage    (ilTiles.Images[1], rtgField);
    break;
  case 3:
    gField.DrawImage    (ilTiles.Images[2], rtgField);
    break;
  case 4:
    gField.DrawImage    (ilTiles.Images[3], rtgField);
    break;
  case 5:
    gField.DrawImage    (ilTiles.Images[4], rtgField);
    break;
  case 6:
    gField.DrawImage    (ilTiles.Images[5], rtgField);
    break;
  case 7:
    gField.DrawImage    (ilTiles.Images[6], rtgField);
    break;
  case 8:
    gField.DrawImage    (ilTiles.Images[7], rtgField);
    break;
  case 9:
    gField.DrawImage    (ilTiles.Images[8], rtgField);
    break;
  case 10:
    gField.DrawImage    (ilTiles.Images[9], rtgField);
    break;
  default:
    break;
}


It looks a lot like the screenshot from chapter 1, doesn't it? Great! So, in the next chapter, we'll add one more extremly cool feature, to complete our game. We'll make our doggy splash water. And that's it. The game will be complete, and this tutorial will be almost over. As the last topc, I'll cover packaging and distribution of your game. And that's it… ;) Finally…

16. Thing that makes apples fall

Uhm, I admit – I lost a whole day to figure this stupid stuff out. And in the end, i ended up with just a few lines of code. It's true that simplicity is the best way. And after writing, like, fifty lines of code that didn't work, i decided to delete it, and to start over. Basically, I took every single block in the game, and checked if it's floating. If it was, I moved it down… This would be the result:

This is the code that goes in the GoGravity() method:

// So, bassicaly, i just use 
// a couple of loops to go trough 
// all the fields.


// Looping through columns:
for (int s = 0; s < iWidth; s++)
{
  // Looping through rows:
  for (int l = 0; l < iHeight; l++)
  {
    for (int v = iHeight - 1; v > 0; v--)
    {
      // In every column, I start  
      // checking from the bottom.

      if (Field[v, s] == 0)
      {
        // If the field is empty, I  
        // drop the field above it 
        // down one block.

        Field[v, s] = Field[v - 1, s];
        Field[v - 1, s] = 0;
      }
    }
  }
}
// And to show the changes, 
// I call the Redraw() method:

Redraw();


And this is the code that goes into the Redraw() method:

//Drawing our field and grid:
DrawGrid()
DrawField()


5. Finally, some real stuff!

Game Field!

If you would buy a game of Monopoly in the 'real' world, you'd probably get a box, some playing figures, dice, bonus cards,property cards, game money, rule book and a game board. A lot of the similar stuff, like dice and game board, can be found in other 'regular' games. So, it would be logical to assume that computer games need similar 'components' to work. Yes, we'll make some of this stuff in our PC game. The first thing is to make a game board, or a game field, as I call it. Our game will use a big game field made of small fields. We'll create a field 15x10 in size. 15 columns and 10 rows. Each of the small game field blocks will hold one Block, Bubble, Baloon, whatever...

Here's the idea:


Ok, you probably get the idea now. Our game field is, basically, a bunch of blocks. Each block is defined with it's coordinates and a value (in this case, representing a speific color). So, how to make our empty form show something like this?

First add the next controls to the form:
(You can drag them from the tool box onto the empty form)

  1. Picture Box
  2. Button

Now we'll fine-tune some properties for our new controls. In the Properties window, on the left, there is a huge list of properties you can adjust. Click on your Form and and tweak the properties to the values shown in the next list:

Form - frmMain

  • FormBorderStyle: FixedSingle
  • MaximizeBox: False
  • Size: 630; 470
  • StartPosition: CenterScreen
  • Text: My First Game

This will disable resizing of your form, set it's size and start position, and write 'My First Game' in the title bar. Ok, moving on - select the button, and set it's properties:

Button

  • (name): butGame
  • Location: 12; 6
  • Size: 126; 53
  • Text: New Game
And for the end, select the picture box and, again, set it's properties:

Picture Box

  • (name): picGameField
  • BackColor: Web>Transparent
  • Location: 0; 65
  • Size: 480; 320

Your form should now look like this:

Don't forget to save your work!

Downloads

Downloads
Finished Bubble Splash Game

Followers

Translate

Blog Directories

Programming Blogs - BlogCatalog Blog Directory
blog directory
Software Computers Business Directory - BTS Local
Top Blogs GoLedy.com
TopOfBlogs DigNow.net
Blog Directory blog search directory
Top Computers blogs Add to Technorati Favorites
Computers Blogs Blog Directory
blogsbycategory.com Blog Directory & Search engine
Blog Flux Directory Programming Blog Directory
Join My Community at MyBloglog! BlogRankers.com
Technology Blogs - Blog Rankings Blog Directory
C# Game programming tutorial Blogs lists and reviews
Bloglisting.net - The internets fastest growing blog directory Blog Directory - photarium
blogarama - the blog directory Blog Toplist
Blog directory Computers (Linux) - TOP.ORG
Blog Ratings si.blogs
Blogz game, development, programming, c#, visual studio, tutorial, source code
On our way to 1,000,000 rss feeds - millionrss.com
RSSMountain
TopBlogLists.com - Blog Search