XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.7.ObjectsAndClasses
Example: Ex_7.DeriveClass

OOP: Inheritance: Derived Class


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 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 ball can also bounce off the Block in the middle of the screen, which keeps track of how many times it's been hit.  In this tutorial, youll notice that the paddles behave like Blocks that the player can move - they will keep track of how many times the ball has hit each paddle, place themselves at a random height when the game starts, etc.

The paddles behave like Blocks the players can move because the major change in this tutorial is to have the Paddle class inherit from the Block class.  As you can see, a Paddle is really just a Block that responds to user input, and that's how we'll set up the code.


1. SoccerBall.cs,  Block.cs:

These are effectively identical to what was presented in the prior tutorial.  We'll make the radius of the soccer ball a bit smaller, since there's now more things on the screen, but that's about it.

2. Paddle

The single most important change is that the Paddle now inherits all the methods and instance variables (data fields) from the Block class.  As we refactor the code in order to make that change work, we'll end up changing a bunch of relatively small things, but the most important thing to keep in mind is that our changes are done with the intention of making the Paddle inherit from the Block.

3. Game1.cs:

As you can see, this file requires almost no changes:


FURTHER EXERCISES:: 

  1. Create a new file named HorizontalPaddle.cs by opening the Solution Explorer (View → Solution Explorer), right-clicking on the project, then selecting Add → New Item, then finally selecting Class and naming it HorizontalPaddle.cs.  In the HorizontalPaddle.cs file, create a new class (named HorizontalPaddle) that inherits from Block.  The difference between a Paddle (which moves vertically, along either side of the screen) and a HorizontalPaddle is that the HorizontalPaddle moves horizontally across the top and bottom of the screen.  Remove the existing Paddles from Game1, and replace them with two HorizontalPaddles.  The left thumbstick should control the top HorizontalPaddle, and the right thumbstick should control the bottom HorizontalPaddle.
  2. Create a new file named MovingBlock.cs.  In that file, create a new class (named MovingBlock) that inherits from Block.  Add a method named UpdateMovingBlock so that the block moves upwards (or downwards) on each call to the game's UpdateWorld method.  When the MovingBlock reaches an edge of the screen, it should turn around.  Make sure to add code to Game1.cs so that you create two MovingBlocks, in addition to the current, normal Block

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