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 2: Our first look at the code (Part 2)

    So in the last lesson we saw a lot of the plumbing involved in referencing other namespaces and assemblies, creating our own namespace, and commenting the code. This lesson is all about classes and objects.

    Classes and objects are at the core of C# programming and modern programming in general. Rather than spend a lot of time duplicating the explanations of people more knowledgeable than myself on the topic, here is a link to a detailed article explaining objects and classes in C#:

    http://www.csharphelp.com/archives3/archive497.html

    Classes and objects are a complex topic with a lot of subtleties, so for simplicity's sake at this time, I will make some statements that are not entirely correct but are good enough for now. 

    Briefly, objects are a set of data and behaviors that are grouped together. Classes are templates where we describe the data and behaviors of the object. Classes do not actually store any data or execute any code. Objects are instances of classes and each object has its own data. We refer to the code associated with a particular class as the class definition. In our Game1.cs file, we have a class definition for a class named Game1. Since our class is inside the namespace definition "MyFirstGame", the fully qualified name of the class is MyFirstGame.Game1.

    Class definitions contain variables, methods, and properties.

    Variables are instances of other objects or primitives. They contain your data.

    Methods are your object's behaviors. You tell your object to do something by calling a method.

    Properties are characteristics of your object that other objects can see.

    Our Game1.cs file was generated with 6 method definitions. Let's take a look at these one at a time.

    public Game1()

    This is a special method called a constructor. It is called when an object is first created. It typically will run some code which initializes the data in the object variables and do some other setup work. We will leave this method alone and do our setup code in the Initialize() method below.

    protected override void Initialize()

    This is where you can run some code to set up objects for your game.

    protected override void LoadContent()

    This is where we will load all of our graphics and sound related data. We'll cover this in more depth later.

    protected override void UnloadContent()

    The opposite of the LoadContent method, we can use this to clean up.

    protected override void Update()

    The Update method is executed on a periodic interval and is where you will put code to calculate the position of your game elements, update the score, set other data values, check for mouse, keyboard, or game controller input, etc.

    protected override void Draw()

    The Draw method is where you will put code to draw your game screen. It is also called on an interval and each time it is called you will draw the entire game surface to reflect the current state of your game. Each time Draw is called, you are drawing a single frame, similar to how a cartoon is made up of frames or cells that are each drawn individually.

    Notice the following line of code in the Draw() method:

    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    This statement is what set the background of our game surface to blue.

    Try deleting the ".CornflowerBlue" text. Then type the period (.) again. You should see something that Microsoft calls IntelliSense. Intellisense gives you hints on the available values when possible so that you don't have to go look up the valid values every time. Select "Black" from the list. The code should now look as follows:

    graphics.GraphicsDevice.Clear(Color.Black);

    Now if you run the program again, it should have a black background instead of a blue background like this:

     

    The Update() and Draw() methods are where most of your work is going to take place when writing a game. Try to keep only drawing code in the Draw() method and everything else in the Update() method.

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

    Comments

    Comments are closed