| XNA Game-Themed CS1 Examples ( XGC1 ) | |
|
Release 2.0 (XNA V3.1)
|
|
References:
Goals:
1. Obtain the example 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
|
#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?
|
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!"
;
}
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::