| 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:
This version of the 'zap' game looks and plays very similarly to the prior version, which was explained in the 405 tutorial . The major change in this version is that the game will detect when the player's zapping beam has collided with any of the three blocks, and will cause the laser beam to around each block before continuing on towards the enemy, as pictured above. The enemy's laser beam is still stopped by the first block it hits
2. Examining The Program:
Let's examine the C# source code that produces the behavior we see on-screen
| Version of code in 405 tutorial | Version of code in this tutorial |
|
for
(float x = m_Hero.CenterX + 1; x < m_Enemy.CenterX; x +=
HERO_PATH_SIZE)
{
HeroBeam.X = x;
lastCircle = CreateHeroPath(HeroBeam);
if (lastCircle.Collided(m_BlockA))
{
for (float y = HeroBeam.Y + 1; y <=
m_BlockA.MaxBound.Y; y++)
{
HeroBeam.Y = y;
lastCircle = CreateHeroPath(HeroBeam);
}
}
}
|
for
(float x = m_Hero.CenterX + 1; x < m_Enemy.CenterX; x +=
HERO_PATH_SIZE)
{
HeroBeam.X = x;
lastCircle = CreateHeroPath(HeroBeam);
if (lastCircle.Collided(m_BlockA))
{
for (float y = HeroBeam.Y + 1; y <=
m_BlockA.MaxBound.Y; y++)
{
HeroBeam.Y = y;
lastCircle = CreateHeroPath(HeroBeam);
}
}
else
if
(m_BlockB.Collided(lastCircle))
{
float
steps = HeroBeam.Y -
m_BlockB.MinBound.Y;
for
(
int
dy = 1; dy < steps; dy++)
{
Vector2
newPos = HeroBeam;
newPos.Y -= dy;
lastCircle = CreateHeroPath(newPos);
}
HeroBeam.Y -= steps;
}
else
if
(m_BlockC.Collided(lastCircle))
{
while
(HeroBeam.Y <
m_BlockC.MaxBound.Y)
{
HeroBeam.Y += 1f;
lastCircle = CreateHeroPath(HeroBeam);
}
}
} |
FURTHER EXERCISES::