XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.7.ObjectsAndClasses
Example: Ex_6.InheritanceBaseClass

OOP: Inheritance: Base Class



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 in this tutorial is very similar to the prior tutorial -  there are two paddles on the screen, and the ball bounces on it's own.   The right thumbstick moves the right paddle up or down and the left thumbstick controls the left paddle. 

The only thing that's new is the existence of a 'Block' in the middle.  The soccer balls will bounce of it, but each time they bounce off it, a 'hit counter' is incremented; the hit counter is displayed as a label on the Block.  In future tutorials, the block will be deactivated after it's hit a certain number of times (meaning that the soccer ball will pass through it instead of bouncing off it).  In this tutorial, we will begin to establish the groundwork for this feature, but we won't implement it in this tutorial.

1. SoccerBall.cs,  Paddle.cs:

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

2. Block.cs

This class defines how the Block (that appears in the middle of the screen) behaves, and is very similar to the Paddle or SoccerBall classes.  It contains named constants for the width and height of the block, along with the minimum/maximum vertical location of the block.  Let's examine the rest of the class in more detail:

3. Game1.cs:

As you can see, this file is almost identical to what was presented in the prior tutorial. The additions are a constant for the X part of the block's location, an instance variable to keep track of the Block, a line of code in InitializeWorld to create the Block, and a line of code in UpdateWorld to tell the Block to check for a collision with the soccer ball.  All of these changes you should be able to understand on your own.


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.