XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.5.RepetitionStructures
Example: Ex_5.ZapGameFunctionInLoopBody

Zap Game: Function calls inside a 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:

You'll notice that this is almost identical to the 050 tutorial , except that when the player pushes the 'A' button, the laser now extends across the entire screen.  You'll notice that when the hero's laser beam overlaps the enemy soccer ball, the hero gets a point.  The enemy's laser beam is still only a couple of circles long, leaving it with no hope of reaching the hero, which makes this version of the game a bit lopsided.  You'll also notice that the laser beam is NOT stopped by the blocks in the middle of the screen.  As you can see in the above picture, the laser beam is drawn behind the block, and continues on afterwards.  We'll fix both problems a future tutorials.

Because the program is almost entirely the same as the previous tutorial that used this program, we will focus only what's new in this program.  If you need to refresh your memory about how the game is set up overall, please refer to the 050 tutorial .

2. Examining The Program:

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


  1. Instead of drawing a single laser beam, draw two laser beams, one slightly above the other, and both perfectly horizontal.  Make sure that you check for a collision between the enemy, and either laser beam.
  2. Instead of drawing a single laser beam that is perfectly horizontal, try altering the above code so that the laser beam angles downwards slightly.  The beam should still go all the way to the enemy soccer ball's location.  An example is pictured below; don't worry about the exact angle of the beam, just make sure that it angles downwards:
    Angled beam (roughly 45 degrees, downwards)
    1. Try angling the beam upwards, instead of downwards
  3. Instead of drawing a single laser beam, draw three laser beams.  The 'top' beam should angle upwards slightly, the 'middle' beam should be perfectly horizontal, and the 'bottom' bean should angle downwards slightly, as pictured below.  Make sure to detect a collision between the enemy soccer ball, and any of the beams
    Triple-Beam results
  4. Try changing the code, so that when the enemy shoots a laser beam, instead of drawing just three circles, a line of circles is drawn from the enemy soccer ball towards the left side of the screen.
    1. Once you've got that working, see if you can detect if the enemy has zapped the player, and if so, increase the enemy's score, play a sound, and make the hero basketball flash

Project home page : The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Michael Panitz
Business And Information Technology
Cascadia Community College

Microsoft Logo 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.