XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.7.ObjectsAndClasses
Example: Ex_4.PaddleClassAndInteraction

OOP: Interactions of Objects



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 is a single paddle on the left side of the screen, which is controlled by the left thumbstick.  The pink spot is controlled by the right thumbstick; when the 'A' button is pressed, a soccer ball appears and immediately begins to move across the screen.  The soccer ball will bounce of the screen's edges, and the paddle.

The major objective of this tutorial is to examine how multiple objects, each one made from a different class, can be made to interact with each other. In this case, there's a class for the SoccerBall, and a separate class for the Paddle.



For this tutorial, we will mostly leave the SoccerBall class unchanged.  The only major difference between the SoccerBall class in this tutorial and the previous tutorial is that we've removed MoveTheBall and RollTheSoccer methods that we no longer use (along with the ROLL_ANGLE and MOVE_UNIT constants that they use).  We'll also add the GetCircle method, which Paddle objects will use to determine if the SoccerBall's circle overlaps/collides with the Paddle.

2. Game1.cs:

This class is also mostly unchanged from the prior tutorial.  The only major change is for the Paddle object has been added to the program.  In order to do this the PADDLE_X constant and the m_ThePaddle instance variable have been added to the class.  The variable is initialized in the InitializeWorld method, similarly to what we did for the SoccerBall class. 

UpdateWorld has been changed slightly, as well:

3. Paddle.cs:

You'll notice that the Paddle class is cery similar to the SoccerBall class - it has several named constants, an instance variable to keep track of the on-screen rectangle, and a constructor that has a parameter for the X part of the paddle's initial (X,Y) position.  What's interesting are the UpdatePaddlePosition and BounceTheSoccer methods.


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.