XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.5.RepetitionStructures
Example: Ex_21.ZapGameManyNestedLoops

Zap Game: Nesting While and For Loops



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:

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


  1. Rework the enemy's zapping code so that the enemy's laser, just like the hero's laser, slides around the different blocks.
  2. Rework the above code so that if the hero's laser collides with the top half of a given block, the laser will go up the block, and if the laser collides with the bottom half (or middle) of the block, the laser will slide down the block
    1. Do the same thing for the enemy's laser.

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.