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
160 tutorial
.
There are only two differences, code-wise, between this version and
the prior version: this version uses a
for
loop, instead of a
while
loop, and this one counts slightly differently (instead of adding 1f to
HeroBeam.X each time, it use a separate counter variable, and copies that
variable into HeroBeam.X)
2. Examining The Program:
Let's examine the C# source code that produces the behavior we see on-screen
Drawing the Hero's Laser in UpdateWorld():
Version of code in 160 tutorial
Version of code in this tutorial
XNACS1Circle
lastCircle = null;
Vector2 HeroBeam =
m_Hero.Center;
while
(HeroBeam.X
<= m_Enemy.CenterX)
{
HeroBeam.X += 1f;
lastCircle
= CreateHeroPath(HeroBeam);
}
XNACS1Circle
lastCircle = null
;
Vector2
HeroBeam = m_Hero.Center;
for
(
float
x = m_Hero.CenterX+1; x <
m_Enemy.CenterX; x++)
{
HeroBeam.X
= x;
lastCircle = CreateHeroPath(HeroBeam);
}
In order to emphasize the differences between the code provided in
the 160 tutorial, and the code provided in this tutorial, we have
aligned the loop from the 'hero zapping' region side-by-side, with the
new loop in this tutorial on the right.
Notice that in this tutorial, we're using a for loop to count out
the creation of the circles.
for
(
float
x = m_Hero.CenterX+1; x < m_Enemy.CenterX; x++)
Note that we use a new variable,
x
, to count
from the left side of the screen, to the right side. We initialize
x
to have a value that is one greater than the
center of the hero's circle.
float
x = m_Hero.CenterX+1
We iterate through this loop until x is equal to the enemy's
center's X value, similar to what we've done in prior tutorials.
; x < m_Enemy.CenterX
;
At each iteration, we increase the X value by one:
x++)
then copy that value into the
HeroBeam variable:
HeroBeam.X
= x;
Because we still use the HeroBeam variable to place the
circles, it will have a copy of the hero's Y value. Thus,
all the circles will be drawn in a horizontal line, at the same
height that the hero is at.