XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.7.ObjectsAndClasses
Example: Ex_3.ObjectUpdate

OOP: Adding our own UpdateBall method


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:


Just like in the prior tutorial, the left thumbstick controls the pink/blue 'spot' on the screen.  Until the user presses 'A' button for the first time, the screen is blank except for the spot (and the top/bottom status messages).  When the player presses the 'A' button, the current soccer ball (if any) disappears from the screen (and from the program's internal logic), and a new one is created on the spot's current location.  If there's a soccerball on the screen, then the right thumbstick can be used to roll it, and the same sort of logic is still used to ensure that the program doesn't crash, prior to the first soccer ball's appearance. 

What's new in this tutorial is that the ball moves constantly.  Here is where we'll start to see the power of OOP, as we can add this 'always moving' functionality to the SoccerBall, and not the overall game.  While this 'always moving' functionality is pretty simple, you could imagine more complicated functionality that we could add to the ball, while keeping the core game logic simple.  A couple of examples include the ball fading in-and-out, the ball 'jumping' from one place to another, etc, etc


2. Game1.cs:


This is almost identical to what we looked at in the prior tutorial.  The only thing that's new is that whenever the game as a whole updates itself (i.e, whenever the UpdateWorld method is executed), we will need to update the SoccerBall (in order to make sure that when it reaches the edge of the screen, it reverses it's currently horizontal/vertical direction).

While we could do this all in the UpdateWorld, it will be better (more object-oriented) to do have the SoccerBall to update itself.  In order to make this possible, the SoccerBall class will implement an UpdateBall method, which we will here in UpdateWorld:


1. SoccerBall.cs:

We've decided to neatly encapsulate all the 'motion related' logic here inside the SoccerBall class. Let's examine the details of the code:

 


FURTHER EXERCISES:: 

  1. Alter the RollTheSoccer method, so when the user pushes the right thumbstick, a substantially greater rotation happens, so that the change is easier to see.
  2. Create a new file named TeleportingBall.cs by opening the Solution Explorer (View → Solution Explorer), right-clicking on the project, then selecting Add → New Item, then finally selecting Class and naming it TeleportingBall.cs.  Copy the SoccerBall code, and paste it into the TeleportingBall.cs file, and rename the class from SoccerBall to TeleportingBall.  The TeleportingBall should behave just like a SoccerBall, except that when it reaches the right edge of the screen, it should teleport to the left edge, WITHOUT switching direction.  When it hits the top or bottom of the screen, it should flip it's vertical velocity, just like it does now.  Modify Game1.cs so that the game uses this new TeleportingBall, instead of a normal SoccerBall.
  3. Create another ball, the TwoColorBall, by copying the SoccerBall's code into a new file (named TwoColorBall.cs).  It should have no image associated with it, but instead, as the ball proceeds across the screen, the color changes from green (when it's anywhere on the left HALF of the screen) to blue (on the right HALF of the screen).  Modify Game1.cs so that the game uses this new TwoColorBall, instead of a normal SoccerBall.
    1. Hint: You'll need to get the ball's current position, then change the color based on that.
  4. Extra Challenging: Create another ball, the ChromaticBall, by copying the SoccerBall's code into a new file (named ChromaticBall.cs).  It should have no image associated with it, but instead, as the ball proceeds across the screen, the color smoothly change from one color (when it's on the left EDGE of the screen) to a different color (on the right EDGE of the screen).  Modify Game1.cs so that the game uses this new ChromaticBall, instead of a normal SoccerBall.
    1. Hint: You'll need to get the ball's current position, then use that to mathematically figure out what to set the color to
    2. Hint: You'll need to look at how to create a new Color object based on, say, the RGB values that you can pass into a Color object's constructor.

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