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

Zap Game: Count Backward With For Loops

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:
• Examine how to count backwards, by drawing the enemy's laser from right to left

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:

As you can see, this version of the 'zap' game looks and plays very similarly to the prior version, which was explained in the 315 tutorial .  The major change is that instead of having the enemy try to zap the player with three circles, the enemy can now fire it's own zapping laser beam, clear across the screen.

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():  #region determine if ENEMY should fire   // clear previous enemy path m_EnemyPathSet.RemoveAllFromSet();   if (RandomFloat() < ENEMY_SHOT_PROBABILITY) {     // Fire!!     PlayACue(@"ZapPath");     String msg = "Enemy sends zapping laser beam: " ;       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);     }       if (lastEnemyCircle.Collided(m_Hero))     {         m_Hero.Texture = null ;         PlayACue( "HeroZapped" );         m_EnemyScore++;         msg += "HIT!" ;     } else {         msg += "Missed!" ;     }       EchoToBottomStatus(msg); }   # endregion // enemy should fire?
• The only new code pertains to how the enemy soccer ball's laser beam is constructed, which is included above.  The first couple of lines (including the if statement that makes the decision to fire the laser beam or not) is the same as previous versions of this program, and so they have been grayed out.
• The first change is that when the enemy fires, we want a message to appear at the bottom of the screen, indicating that the enemy is firing, along with a notification about whether the laser beam successfully zapped the player or not.  We set up the first part of that message with the following string:

String msg = "Enemy sends zapping laser beam: " ;

• After that, we use a for loop to actually draw the enemy's laser beam.  This code is almost identical to the logic that draws the player's laser beam, except that the enemy's laser beam starts on the right side, and moves leftwards.  Thus, the x variable needs to be initialized to hold the enemy's center point, and then SUBTRACT one from the x variable each time through the loop, like so:

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);

}

• Next, we need to detect if the enemy's laser beam overlaps (has collided with) the player's basketball.  Again, we use the same approach that we used for detecting whether the player has zapped the enemy: use the last (enemy) circle to check for a collision.  We use the same sort of logic that we did for the player, including the 'make the player's basketball flash pink' trick (the hero's texture is restored .
The only thing that's new is that we want to add the word "HIT!" onto the end of the "Enemy sends zapping laser beam: " message that we created above, which we do at the end of the if clause.  In order to add the word "Missed!" onto the end of the message when the enemy misses, we use an else clause:

if (lastEnemyCircle.Collided(m_Hero))

{

m_Hero.Texture = null ;

PlayACue( "HeroZapped" );

m_EnemyScore++;

msg += "HIT!" ;

} else {

msg += "Missed!" ;

}

• The only other change is to move the line that changes the bottom status bar inside the if statement that executes when the enemy decides to fire.  This way, when the message will stay at the bottom of the screen long enough for the player to read it.

FURTHER EXERCISES::

1. Go back to the 160 tutorial, and redo those exercises for the enemy , using for loop(s).  For example, if you do the 'triple beam' exercise, the enemy should fire three separate laser beams, from itself towards the left side of the screen , in a manner that's similar to the same exercise for the player.