XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.4.DecisionStructures
Example: Ex_12.PongSoccer

Math Operators: Using Constant Values in C#



References:

Goals:



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:


The rectangle on the far left is a 'paddle', which the soccer ball will bounce off of (if you move the paddle in the way of the soccer ball).  You can move it up and down by pushing the left thumbstick up and down.  The rectangle on the far right is a 'paddle', which the soccer ball will bounce off of if you move the paddle in the way of the soccer ball.  You can move it up and down by pushing the right thumbstick up and down.  The other, labeled rectangles are placed randomly in the middle of the screen.  The soccer ball starts off in the middle of the screen, moving in a random direction at a random speed. 

If the soccer ball hits the top or bottom of the screen, it will bounce off - it continue to move at the same speed, but the vertical component of it's velocity will be reversed (i.e., when it hits the top of the screen, instead of going upwards, it will switch and go downwards).  If it hits one of the labeled rectangles, it will bounce off (reversing both it's horizontal & vertical speeds).  If the soccer ball reaches the left or right edge of the screen then you "missed" it, and it is put back into the middle of the screen, with a new random direction.  Missing the ball results in the CurrentBounces number going back to zero, and the "Balls Missed" number going up by one.

Your goal in this game is to bounce the soccer ball off the paddles as many times as you can.  Once you've bounced it 5 times in a row (without missing it), then you go to expert mode, where things move a little bit faster.  If you can bounce it 10 times in a row, and you've never missed more than two balls, then you win the game.  If you can get to 10 bounces, but you've missed it more than twice, then each additional bounce beyond 10 will decrease your 'missed balls' count.  What this means is that you can still win, although it will be more difficult the more you have missed the soccer ball.  Notice that at the bottom of the screen, the game tells you whether you're in Novice or Expert mode, how many times (in a row) you've bounced the ball since the last time you missed it, the longest run of bounces you've managed to do, and the number of soccer balls that you've missed.  At the top, the game tells you which labeled rectangle the soccer ball has most recently collided with.

Controller Input: Has this effect:
Left thumbstick Moves the left paddle.  Note that only the vertical input is used, because the paddle is only allowed to move up or down
Right thumbstick Moves the right paddle.  Note that only the vertical input is used, because the paddle is only allowed to move up or down
A button Restarts the game: skill level goes back to "Novice", current bounces / max bounces / missed balls all go back to zero
Back button Exits the game

Once you reach expert mode, the screen should look something like this:

Once you've won the game, the screen should look something like this:


2. Examining The Program:

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


FURTHER EXERCISES:: 

  1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.

Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

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.
2/8/2010