XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.5.RepetitionStructures
Example: Ex_11.ForWithGeneralCounter

Repetition: A Non-Counting For Loop


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 program for this tutorial demonstrates how to use the for loop.  This is nearly identical to the previous tutorial, except that instead of using one variable (counter) to keep track of how many soccer balls to draw, and a second variable (xOffset) to keep track of where to put the next soccer ball, we will instead just use a single variable.  By keeping track of xOffset, and defining a new named constant (FINAL_X_POSITION) that defines where to stop putting soccer balls, this program can use the pattern of a for loop, yet not use a traditional 'counting loop' pattern.


2. Examining The Program:

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


FURTHER EXERCISES:: 

  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. Moving The Soccer Balls
    For this exercise, you should use the same project that was explained in the above tutorial.
    The goal for this exercise is to 'animate' the soccer balls, so that all the soccer balls slowly move rightwards across the screen. 
    Modify the provided solution so that your program contains another instance variable, initialized with a value like 0.0f.  Each time UpdateWorld is called, make sure that you first add, say, 5.0f to the instance variable, and then when you create the soccer balls, you add this number to the 'X' value of the new soccer balls' location. 
    Note that you need to keep all five soccer balls on the screen - you're not allowed to have them disappear as you move them rightwards.
  3. Moving The Soccer Balls
    For this exercise, you should use the same project that was explained in the above tutorial.
    The goal for this exercise is to 'animate' the soccer balls, so that all the soccer balls slowly move leftwards across the screen. 
     Note that you need to keep only five soccer balls on the screen - you're not allowed to have more appear as you move them leftwards.

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