XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.4.DecisionStructures
Example: Ex_1.PaddlesAndBalls

Decision Statements: Chapter Overview

Need (library reference):



Before you begin

Let's start by examining the final, finished game that you will create throughout  this chapter. Download and unzip the Example Code, and then open the solution for the 900 tutorials .  In the Solution Explorer, you will see:

Once you've done that, you should proceed with this tutorial. 

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 rectangle on the far left is a 'paddle'.  You can move it up and down by pushing the left thumbstick up and down.  The rectangle on the far right is also a 'paddle', which you can move up and down by pushing the right thumbstick up and down.  The only other thing on the screen is a picture of a soccer ball.  The soccer ball starts off in (roughly) the center of the screen, moving at a random speed in a random direction.  Even at the end of this tutorial, the soccer ball will not interact with anything - it won't bounce off either paddle, nor the edges of the screen.  Notice that the location of the paddles are displayed at the top of the screen, and the location of the soccer ball is displayed at the bottom.

Notice that since the soccer ball doesn't bounce of anything, it will just keep going.  You can see this by observing that the position of the soccer ball continues to change, even after the soccer ball has left the screen.

Summary of
Controller Input: Has this effect:
Left thumbstick Moves the left paddle.  Note that only the vertical input is used, because the paddle is only allowed to move up or down
Right thumbstick Moves the right paddle.  Note that only the vertical input is used, because the paddle is only allowed to move up or down
Back button Exits the game

Once you've seen what the final, finished game does ( the PongSoccer game ), and what you've seen what this tutorial's game does, you should open up the Game1.cs file for the 010.PaddlesAndBalls project, so that you can examine the source code.  Keep in mind that the goal for this tutorial is to acquaint you with the source that we'll be building on, in future tutorials Everything in the file should be familiar, with the possible exceptions of working with images ( aka textures) , creating your own regions , and generating random numbers using XNACS1Lib .

2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen


  1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.
  2. Familiarizing Yourself With The Code: Making Minor Changes
    Using the same project that the above tutorial makes use of, try changing things, in order to make sure that you're familiar with how the code is set up.  Something things to try:
    1. Change the size of the paddles
      Make them wider & taller, or thinner & shorter
    2. Change the starting positions for the paddles.
      Can you have a paddle start off-screen?
    3. Put the soccer ball in a different starting location
    4. Change the size of the soccer ball. 
      If you do this, what happens to the texture?  Is it still the same size, or does it get scaled up (or scaled down) to match the size of the circle?
    5. Try changing the image for the soccer ball
      Find an image and try adding it to the project, and then set the soccer ball (or paddles) to show the image on screen.  You can find images online, or perhaps from a clip-art collection that office productivity applications (like the Microsoft Office Suite) come with. 
      • Note that if you intend to share your game (including posting the game online, or even just sharing it with friends & family), then you need to be aware of copyright issues, and make sure that you're using images that you can legally use.
    6. Change the range of the speed of the soccer ball
      Can you set it up so that (by changing the min & max possible values) that the soccer ball starts off at (essentially) one speed?
      Can you make the ball move more slowly?  More quickly?

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.