XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.7.ObjectsAndClasses
Example: Ex_5.MultipleInstancesOfObjects

OOP: Multiple Instances Of A Single 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:


For this tutorial, there are two paddles on the screen; the right thumbstick controls the right paddle, the left thumbstick controls the left paddle.  The paddles can only be moved vertically.  The ball appears as soon as the game starts, and it moves (and bounces off walls and paddles) on it's own


1. SoccerBall.cs:


This is effectively identical what was presented in the prior tutorial

2. Paddle.cs:

This is almost identical what was presented in the prior tutorial.  The only thing that's different is that when the soccer ball bounces off a paddle, we need to move the ball to the correct side of the paddle, instead of just assuming that the soccer ball must be moved to the right edge.  That logic is handled in the BounceTheSoccer method:

3. Game1.cs:

If you compare this file to the Game1.cs in the prior tutorial, you'll see what looks like a lot of changes.  Given the number of apparent changes, there is surprisingly few conceptual differences.  Because this tutorial has two paddles, rather than just one,  we define a named constant for the X part of each of their (X,Y) starting locations, we define an instance variable for each of the paddles, and initialize both paddles in InitializeWorld.  We also need to make sure that UpdateWorld deals with both paddles, as we'll see below.

For this program, the ball is created in the InitializeWorld method, which simplifies UpdateWorld because we no longer need to react to the 'A' button being pressed, nor do we need to protect any code against the possibility that the soccer ball does not yet exist. For example, we can now just call m_TheBall.UpdateBall() without worrying if ball exists or not.


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