XNA Game-Themed CS1 Examples ( XGC1 ) Release 2.0 (XNA V3.1) 2/8/2010
Topic: Topic.5.RepetitionStructures
Example: Ex_16.ZapGameCountBackwardsWithWhile

# Zap Game: Count backward with While

References:

• Pre-requisite: it is assumed that you have read through the prior tutorials, and are familiar with the concepts covered in those tutorials.

Goals:

• In this tutorial, we will:
• Draw the enemy's laser by counting backwards  using a while loop

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:

This version of the 'zap' game looks and plays identically to the prior version, which was explained in the 335 tutorial .  The only visible change is that instead of having a single, continuous beam that the player uses to try and zap the enemy, there is now a line of distinct dots, as pictured above.

2. Examining The Program:

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

• Drawing the Enemy's Laser in UpdateWorld():  Version of code in 335 tutorial Version of code in this tutorial XNACS1Circle lastEnemyCircle = null ; Vector2 EnemyBeam = m_Enemy.Center; for ( float x = m_Enemy.CenterX - 1 ; x > m_Hero.CenterX ;    x -= 1f ) {     EnemyBeam.X = x;     lastEnemyCircle = CreateEnemyPath(EnemyBeam); } XNACS1Circle lastEnemyCircle = null ; Vector2 EnemyBeam = m_Enemy.Center ; while ( EnemyBeam.X > m_Hero.CenterX ) {     EnemyBeam.X -= 1f;     lastEnemyCircle = CreateEnemyPath(EnemyBeam); }
• In order to emphasize the differences between the code provided in the 335 tutorial, and the code provided in this tutorial, we have aligned the loop from the 'enemy zapping' region side-by-side, with the new loop in this tutorial on the right.
• You'll notice that the two loops are very, very similar.  The major differences are that
1. The new loop (on the right) is a while loop.  This means that the loop initialization must be done prior to the loop (in this case, on the line before it), the while loop itself only checks whether to continue or not, and the increment/decrement logic must be executed inside the loop.  Based on the bold-faced, non-gray text in both cells of the above table, you can see where those things are done in the while loop, and the corresponding parts of the for loop.
2. The while loop uses the X part of the EnemyBeam variable to keep track of the current x value, whereas the for loop uses a separate, local x variable.  Notice that this is an excellent example of 'counting backwards'

FURTHER EXERCISES::

1. Something

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

 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