Game Development Tutorial

Everything a total beginner needs for game programing!

11. Move it! Mooove it!

This is the fun part. We have to move every column to the right. So, the idea is this: Player starts every new level with an empty game field. Before we give him a new column, we check if there's any space on the game field. If there's no space – it's Game Over! We try to push all of the columns one space to the right. We don't move empty columns. Oh, well, let me draw it:

So, this picture shows 15 columns. The white ones are empty. Colored ones contain at least one block. So, in this case, before we create and insert a new column, we have to move the green columns one space to the right. The blue and yellow columns will not be affected this time. Ok, one more:

The red column is the one we created after we moved the green columns to the right. In this example, before we could create a new column, we would have to moove all six columns to the right (I mean, blue ones, green ones, and the red one). And the finall product would look something like this (note, that the yellow columns were, once again, not moved):

I hope you get the idea now. To do this in our game, we'll have to look for the left-most empty column, and them move all preceeding columns one space right. To check if a column is empty, we'll just check the bottom block, since we'll add some gravity to the game at a later stage – meaning, no block could 'fly'. Ok, onto the code:

// Declare an integer that will hold
// the empty column number. We set it's
// value to –1, wich means there is NO
// empty columns. If we find an empty 
// column, we'll change this value. We
// didn't use 0 to show that there's no 
// empty columns, because 0 actualy refers 
// to the FIRST column in our game field.

int iEmptyColumn = -1;

// Using a FOR loop to go trough all 
// columns looking for an empty block.

for (int s = 0; s < iWidth; s++)
  // Checking if the block is empty:
  if (Field[iHeight - 1, s] == 0)
  {

    // We found the empty block. Write
    // the column number into our
    // integer variable.
   
iEmptyColumn = s;
    // Exit the loop, we don't need to
    // look any longer, since we already
    // found an empty column:
    break;
  }
}

if (iEmptyColumn == -1)
{

  // So, if there's no empty columns,
  // we can't create any new ones, so
  // we'll call the game over method.
  GameOver();
}
else
{

  // If we found an empty column, move all
  // preeceding columns for one space to the
  // right. We use two loops in this code
  // because we have to move all blocks in
  // the column (height wise), and all the
  // collumns preeceding the empty one).
  for (int x = iEmptyColumn; x > 0; x--)
  {
    for (int v = 0; v <>
    {
      Field[v, x] = Field[v, x - 1];
      Field[v, x - 1] = 0;
    }
  }
  // So, we moved our field, we can now
  // create a new column...

  CreateNewColumn();
  // ...and add a random block
  // (you'll see...) :)
  DropBlock();
  return;
}

What a progress! One of the last things is to put this stuff into our Timer's Tick event. Find the code for the game_tick event, and put this line there:

MoveRight();

Oh, yea, remove the line that we placed there earlier. (The one that called CreateNewColumn() method.) We don't need it, because now, we call the CreateNewColumn() method from our new MoveRight() method. In the next chapter we'll create the GameOver() method. And after that, we'll move to do some user (player) interaction stuff. I guess we'll be chasing the mouse all over the field… ;-P LOL, that's funny.

Try it out! You should see something like this:

0 comments:

Post a Comment

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