XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.7.ObjectsAndClasses
Example: Ex_11.ArrayOfBlocks

Array of Blocks


References:

Goals:



1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

When the game starts, you'll see a screen that looks similar to this:


The game-play in this tutorial is very similar to prior tutorials, and almost identical to the 'Pong Game' that we've created in a previous chapter.  What's new in this tutorial is that we will create an array of (normal, non-breakable) Blocks.

This tutorial also neatly illustrates the use of polymorphism - the idea that a reference (a variable, a parameter, an element of an array, etc) can refer to any of several (sub-) types of object, and that C# will figure out which method to call at run-time for us.


1. SoccerBall.cs, Paddle.cs, Block.cs, BreakableBlock.cs

These are effectively identical to what was presented in the prior tutorial. 

2. Game.cs

We want to have many blocks on the screen, and we'd like to randomly pick whether each one should be a normal Block, or a BreakableBlock.  In order to do this efficiently, we'll use an array.  Each slot in the array will hold a reference to either a normal Block, or a BreakableBlock.

4. An example of how this all works

Let's examine what happens when the UpdateWorld method is called.  For now, let's assume that the first slot in the array refers to a Block object, and that the second slot in the array refers to a BreakableBlock object.

  1. The framework (XNA, and the XNACS1Lib library) call the UpdateWorld method in the Game1.cs file.

  2. The program executes all the statements in the method sequentially from top to bottom, until it gets to:

            for (int i = 0; i < NUM_BLOCKS; i++)

                m_Blocks[i].CollideWithSoccer(m_TheBall);


    At this point it initializes i to be 0, checks that i is less than NUM_BLOCKS ( seven ), and then calls the CollideWithSoccer method on the first object in the array, which is Block object. 

  3. The CollideWithSoccer method in the Block class is executed sequentially from top to bottom, until it gets to:

    PlayCollisionCue();

    At which point the C# executes the version of the PlayCollisionCue on the Block class, thus playing the "Block" sound.

  4. i is incremented by the i++ part of the for loop, C# then checks that i (1) is less than NUM_BLOCKS (7), and then calls the CollideWithSoccer method in the BreakableBlock object.

  5. The CollideWithSoccer method (on the BreakableBlock class) is executed sequentially from top to bottom, until it gets to:

    PlayCollisionCue();

  6. At which point the C# executes the version of the PlayCollisionCue on the BreakableBlock class, thus playing the "Breakable" sound.


FURTHER EXERCISES:


Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.
2/8/2010