XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.7.ObjectsAndClasses
Example: Ex_8.BallDeriveFromCircle

OOP: SoccerBall derived from XNACS1Circle


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:


This looks (and plays) identical to the prior tutorial - what's new for this tutorial is how we go about doing this, not what we see on the screen.

In this tutorial, we'll explore how to do inheritance from a class that you did not write yourself.  This will simplify our code, and make it clear that you only need access to the compiled class, not the original source code, in order to do inheritance.


1. Paddle.cs:

This is effectively identical to what was presented in the prior tutorial. 

The Block class is almost unmodified, as well.  Since it was necessary to simplify it slightly (because of changes in the SoccerBall class), we'll look at that after the SoccerBall.cs file.

2. SoccerBall.cs

In this tutorial, we're changing how the SoccerBall is set up, by having it inherit from the XNACS1Circle class.  Remember that XNACS1Circle is something that's provided to you, as part of the overall XNACS1Lib library.  You've never seen the source code for it, but your programs make use of it.  We'll make use of it here, by having the SoccerBall inherit from it.

2. Block.cs

This is almost unchanged. The only thing we need to adjust is that in the CollideWithSoccer method, instead of asking the SoccerBall object for it's XNACS1Circle object, we can instead

3. Game1.cs:

This is almost identical to the prior tutorial

The only difference is that we no longer need that separate method for the SoccerBall's center, so we replace that with the XNACS1Circle's Center property, instead



FURTHER EXERCISES:: 

  1. Modify the Block class, so that it inherits from the Rectangle class.
    The next tutorial will do this (and a couple of other things), so if you do this now, on your own, you can check your work against the work demonstrated in the next tutorial.

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