XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.7.ObjectsAndClasses
Example: Ex_12.PongSoccerGame

Improved Pong Soccer Game



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, we go back & redo the Pong game, this time using Object-Oriented Programming techniques.  Changes from the previous tutorial include the addition of a MoveableBlock (which moves a short, vertical distance when the SoccerBall object collides with it), and putting all the logic that relates to the array of blocks into a separate class (named ArrayOfBlocks)

1. Block.cs, BreakableBlock.cs

These are effectively identical to what was presented in the prior tutorial.  - we've changed some named constants to make things look nicer, but that's it.  You should be able to understand all of these changes on your own.

2. Paddle.cs,

This is very similar to what was presented in the prior tutorial.  Some of the named constants have been adjusted slightly in order to make everything look better, and the constructor now calls the new InitializePaddleState method in order to initialize (or reinitialize) each paddle object.

3. MovableBlock.cs,

As you can see from the code, this new class inherits from from the basic Block class.  Given that we want to create a new type of block that closely resembles the existing type of block, with the exception of a new and more specialized behavior when the soccer ball collides with this new block, inheritance makes a lot of sense.

As you examine the code (and at this point, you should be able to confidently and correctly examine all of MovableBlock on your own), you'll notice that we declare the named constant MOVE_UNIT to define the default distance that a MovableBlock will be moved, when hit.  We then copy that constant into an instance variable in the constructor, so that we can change the direction each time the block is hit by changing the sign on the number from positive to negative.

The PlayCollisionCue method is overridden so that hitting a MovableBlock will play a different sound, and the CollideWithSoccer method is overridden in order to adjust the MovableBlock's location when it gets hit. Note that the direction is reversed each time that the MovableBlock is hit, so that the block doesn't move off the screen

4. ArrayOfBlocks

In the previous tutorial, we used an array of Block (and BreakableBlock) objects In this tutorial, we will take all the functionality of that deals with the 'array of Blocks' and nicely encapsulated into a class, so as to make the code easier to understand (i.e., to simplify Game.cs) and promote reuse (in case we wanted to use this elsewhere).  The ArrayOfBlocks' responsibilities include creating the array, creating a randomly chosen set of Blocks to put into the array, detecting collisions between the soccer ball and any block, and displaying some interesting information to the top status bar.

3. SoccerBall.cs,

In additon to handling the image, position, and velocity of the on-screen soccer ball, this class will also track whether the ball is in 'expert mode', and the total count of the number of misses.  Each time the ball has hit the left/right edge of the screen counts as a 'miss' (i.e., the number of times it has been missed by the player).  When the ball is in 'expert mode', the speed will be increased.  Note that the ball only tracks which mode it's in - the Game1 object will actually decide when to go into expert mode.

2. Game.cs

This object keeps track of the game overall (using the SoccerBall, the two Paddles, and the ArrayOfBlocks), and it's responsible for knowing when to advance to expert mode.  Instead of examining the entire file here, we'll walk through the UpdateWorld method, and leave the rest for you to examine in more detail.


  1. Extra Challenge: Try to extend the program so that multiple soccer balls are supported.  Try putting (say) three SoccerBall objects onto the screen at the same time, and make sure that you've made all applicable modifications have been made to the program in order to make this work.

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.