XNA 101 .Net

Learn how to program in C# while writing games in XNA Game Studio 3.0!
Our upcoming Silverlight book for beginners (includes a great chapter on game development in Silverlight!) Hello! Silverlight 2 with Dave Campbell, available online now!



Pages

Recent posts

Navigation

Archive

Blogroll

    Tampa Divorce Lawyer

    North of Tampa in Lutz, Florida. A Tampa Divorce Lawyer focusing on family, divorce, and real estate law.

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    Lesson 1: Our first look at the code (Part 1)

    A good way to start learning how to program is to look at examples of code and see how they work. In our MyFirstGame project, XNA and C# Express generated some code for us to get us started. Let's take a look at some of this code to get some idea of how it works.

    viewcodeMost of your work actually adding game logic will be done in Game1.cs or in other files called from Game1.cs. The .cs file extension stands for C# (C-Sharp) and all of your C# code will have that extension. To view the code, find Game1.cs in the Solution Explorer pane and right click on it and select "View Code". You can also select it and click on the "View Code" icon in the Solution Explorer toolbar.

    Now there are a few things about the format of C# code. First of all, code is broken down into statements and blocks, with statements being a single "line of code" and blocks grouping together a series of statements, sometimes with conditional logic controlling whether the block will be executed. I put "line of code" in quotes because in C# it technically does not correspond to a single line in the file, it includes all of the code up to the semicolon. A statement can be separated over many lines in the file. In C#, blocks start and end with a curly brace, "{" to start a block and "}" to end a block. In C#, all code is case sensitive. The editor will try to fix your capitalization for you if possible, but it is something to look out for.

    So at the top of our Game1.cs file, we have a series of using statements. These tell C# that there are other namespaces of code that we will be referencing from the code in this file. A namespace typically contains code which is related in some way, either sharing characteristics or used together to accomplish a task. Namespaces are used to avoid naming conflicts between different pieces of code. I will dive deeper into namespaces in a later lesson.

    So after the using statements, we have our own namespace defined, and then all of the rest of the code in the file is within the namespace's block. As stated above, this helps to avoid avoid naming conflicts and is generally a good thing to do. If you start creating your own assemblies, (libraries of code that can be used in multiple projects), using namespaces is a necessity, and you need to be careful about how you name your namespaces. For now, just leave it as it is.

    It's a good idea to comment your code. Comments are where you can explain what a certain piece of code does, or anything unusual related to a piece of code. It can also be used as a placeholder, like

    // TODO: Add your initialization logic here

    In C#, anything after a double slash "//" until the end of the line is considered a comment. Comments are not compiled into the program and are stripped out. Comments not only make it easier for others to understand your code, but also help you out if you haven't looked at the code for a while and are trying to remember how it works.

    In Game1.cs, you will see a triple slash "///" comment. This is a special comment which can be used to generate documentation automatically about your code. There are tools that can convert these special comments to web pages or other help formats.

    Posted: Oct 09 2008, 10:32 by Bill Reiss | Comments (1) RSS comment feed |
    • Currently 4.5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Filed under:

    Comments

    Comments are closed