Game Development Tutorial

Everything a total beginner needs for game programing!

6. Let's write some code

For me, writing code is the best part of a game development process. Some people like crossword puzzles, I like writing code. So, to start writing code, we have to find the code editor. Easy. Go to your Solution Explorer window, right-click on the 'frmMain.cs' and select 'View Code'. That should show you the code editor. And, it's not empty! ;)

Note:
TestApp is the application name I wrote in the new project dialog. 

I won't get into explaining what's all this stuff. For now. I hope you'll figure it out trough the course of this tutorial. Right here in the beginning, we'll create some empty functions. Later on, we'll fill these functions with code. We'll also put in a couple of extra using statements. Just so we don't forget them later on... So, go to the top of your code window, and add the next 'using' statements there:

using System.IO;
using System.Drawing.Drawing2D

This will let us read and write files from the hard drive, and let us play with some nice graphic features, later in the project. Be careful, C# is a case sensitive language (unlike Visual Basic, for example), so make sure you watch that shift key of your's. ;) Now, let's add all those functions that we'll need. We'll put them inside of the public partial class frmMain : Form. You can see that there's already one in there: public frmMain(). Every function has an opening bracket {, and a closing bracket }. The function's code comes in between those brackets. So, move your cursor after the frmMain()'s closing bracket, and add new functions like this:

public void CreateField()
{
}

public void CreateNewColumn()
{
}

In the same way add the next functions. (don't forget the curly brackets). ;)

private void MoveRight()
private void GameOver()
private void DrawField()
private void DrawGrid()
private void CheckConnected()
private void CheckAll()
private bool CheckIfInList()
private void DestroyBlocks()
private void GoGravity()
private void NewGame()
private void NewLevel()
private void DoTheScoreMath()
private void DropBlock()
private void LevelScore()
private void HighScore()
private void Redraw()
private void Splash()

So, let's start off with our Game Field code. First of all, we need to tell the computer that we want a field of 15 columns with 10 rows. Ok. Let's write some code that will declare a couple of variables:

// Variable declarations:

static int iHeight = 10;   // Game field Height
static int iWidth = 15;    
// Game field Width
static int iFieldSize = 32;// Block size, in pixels


This code will define our game field width, game field height and the size of every little field (block) in the game field. But, where to put this code?

Put it between:

public partial class frmMain : Form
{

and:

public frmMain()

I would suggest that you don't just copy and paste the code. Writing it on your own will help you understand it, and you'll see how Visual Studio helps you with auto-complete dropdown. You probably noticed the green part of the code - the double slashes let our compiler know that lines starting with them are actually comments, and they do not affect our program execution.

Ok, so, we declared the width, the height, and field size. But that doesn't tell our program much. So, we'll declare our fields now. (Remember the drawing in chapter 4?) So, here's two more lines of code - put them after the lines you just wrote.

//Declare Fields as an 2D array
int[,] Field = new int[iHeight, iWidth];

This line actualy tells the application that we'll be using Field[x,y] format to work with our blocks. We can now add values to every field. And the next step is to create a function that will fill all of the fields with value 0. Zero means that the field is empty. This is the code we need to put in the CreateField() function (between the function's opening and closing curly brackets):

// Function that clears the game field.
// Do this for all rows

for (int x = 0; x < iHeight; x++)
{
    //Do this for all columns
    for (int y = 0; y < iWidth; y++)
    {
        //Clear the fields:
        Field[x, y] = 0;
    }
}

Ok, we have the code that creates an empty game field now… But, our game field is hidden somewere in our computers memory. In the next chapter, we'll make our application show us our game field on the form we created.

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