XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.7.ObjectsAndClasses
Example: Ex_10.BreakableBlocks

OOP: Derived class: BreakableBlock & Polymorphism



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:

This tutorial adds a new type of block to the game: the BreakableBlock.  The BreakableBlock will be deactivated after it's been hit enough times with the soccer ball.

The new class will show us another example of method overriding.

1. SoccerBall.cs, Paddle.cs

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

2. Block.cs

The contents of this file are also unchanged from what was presented in the prior tutorial.  The file itself has been moved to into a subfolder (named "Blocks"), so that we can keep this basic Block, and the BreakableBlock, together in one easy to find location.

It's worth noting that the Paddle class is NOT being moved to this folder, and yet the Paddle class still inherits from the Block class.  From this, you can see that any class can inherit from any other class in the same project, regardless of which files the two classes are in.

3. BreakableBlock.cs

As you can see, this file contains the BreakableBlock class, which inherits from Block.  At this point you can read through the constants and instance variables on your own.  We will examine each of the three methods that this class has here:

2. Game.cs

This file is very similar to what was presented in the prior tutorials.  The only change is that we want to put several BreakableBlocks onto the screen.  Let's examine how this is done:

5. What happens when UpdateWorld is run:

Let's examine what happens when UpdateWorld is run. 

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

  2. The program executes all the statements sequentially (including m_TheBlocker.CollideWithSoccer(m_TheBall);, which behaves as described in the previous tutorial) , until it gets to:


    At this point it calls the CollideWithSoccer method on the m_BB1 object, which is BreakableBlock object.  Because of this, C# will execute the CollideWithSoccer method defined on the BreakableBlock class, even though the m_BB1 variable is a Block.  In other words, it doesn't matter what m_BB1 is (in this case, a reference to a Block), what matters is what it refers to (in this case, a BreakableBlock object).

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


  4. Instead of just calling the method, C# notices that the method is declared to be virtual on the Block class.  At that point, C# asks "Is there a more specialized version that could be used?  In particular, is there a more specialized version of the BreakableBlock class?"  It examine the BreakableBlock class because this method was called on m_LeftBreakableBlock back in UpdateWorld.  C# finds the more specialized version of the PlayCollisionCue on the BreakableBlock class, and calls that version (thus playing the "Breakable" sound)


Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Michael Panitz
Business And Information Technology
Cascadia Community College

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.