XNA Game-Themed CS1 Examples ( XGC1 )

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

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_1.SimpleOutput

Simple Output


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.

2. Background :  

In this tutorial (and the next), we will examine Input and Output in an XNACS1Lib-based video game program.  Input and Output are often abbreviated as 'I/O'.  'Input' refers to any data (information) that the computer program receives (i.e., data that is sent "in" to the program, from the program's point of view).. Examples of input might include a button being pushed on the XBox360 controller, a key being pressed on the computer's keyboard, data that the program reads from a file stored on the hard drive, or data received over a network from another computer .  'Output' refers to any data (information) that the program sends back to the rest of the world.  Examples of output might include a textual message on printed the screen (including things like the player's current score), shapes and pictures on the screen, information saved to a file (stored on the hard drive), or sending data over a network to another computer.  Keep in mind that when we use the term 'computer', we mean anything that functions like a computer - your desktop PC, your laptop, your XBox, and even     items like your mobile phone, etc.

Many textbooks focus on 'Console' I/O.  Console I/O programs mostly print out messages to a 'console' (or 'command prompt', or 'command interpreter', etc), then wait for you to type input something, then do something with that input, then restart the process by printing out a message.  While console I/O looks primitive, it has a lot of advantages over programs that are based on a Graphical User Interface (GUI - a program that has windows, buttons, etc):

  1. Console IO is extremely easy to do
    You don't need to write a lot of code to do this, nor do you need to understand very complicated concepts to make it work

  2. It's very quick to do
    Many simple messages can be printed with only a single line of code.

  3. It allows someone who's new to programming to immediately move past the I/O issues, and move on to actually learning how to work with the core computational concepts that make programming useful. 

In contrast, most GUI based programs require substantial amounts of overhead - windows need to be created, text needs to be written onto the windows to label the various buttons, controls, and widgets that the user will interact with, etc.  Even web applications have substantial overhead when one's goal is to learn computer programming.  Web applications commonly require at least a basic understanding of (x)HTML (and maybe CSS, too), forms and other means of sending data between the browser and server, basic SQL (or some other storage technology), etc, etc.  In comparison, the primitive 'print a message, get user input, repeat' loop of console I/O is quick and easy to use.  For more information about I/O, please consult your textbook, and/or your instructor. 

In these tutorials, we'll be looking at creating graphical, interactive programs, including video game programs.  For right now, we're going to look at how to create some output that is similar to the console output that your textbook is focusing on.  In future tutorials, we'll look at how to create graphical output (drawing rectangles and circles on the screen, including how to put an image from a file over a circle/rectangle), and more advanced input (for example, how to create a game that reacts to buttons being pushed, or a joystick/thumbstick being moved).


3. Examining The Program:

Let's look at the C# source code that produces the messages at the top and bottom of the screen.  Since the only thing that this program does is print a message to the screen, we don't really need to add anything to the Initialize step, just to the Update step .


FURTHER EXERCISES::  

Now that you've read through this tutorial and the provided example code (in detail!), here are some exercises for you to try on your own.  Remember that learning to program is much like learning a new spoken, human language - you should try out new stuff on your own, and challenge yourself to do things that aren't even listed here.  After all - your objective is not to memorize everything that you might possibly want to say in this new language, but instead, to learn how to say whatever you want in the new language by putting together different pieces.
  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 referring the tutorial's code.
  2. Make sure that you change the output message, for both the top and bottom status areas.  Also, try setting things up so that your program prints a message to ONLY the top, or ONLY the bottom of the screen.
  3. As you create games (or any other programs, for that matter), you will accidentally do the wrong thing, and as a result, your program (or game) will have errors in it. While you should want to avoid doing this, it will be inevitable. Therefore, you will need to develop some skill at fixing errors. You will start to do that here, in this exercise.

    You should try making the following errors, and record both the error message that the compiler gives you, as well as your own interpretation, in the following table.
    (For each error, you should make the error, then fill in the table, THEN FIX THE ERROR before going on to the next error)
    Error: Compiler Message: Your Interpretation:
    In UpdateWorld, remove the starting and/or ending curly brace
    Right after the InitializeWorld method, add an EXTRA close curly brace
    In UpdateWorld, on the line that has EchoToTopStatus, remove the parentheses ?

    the ( and /or )

    In UpdateWorld, on the line that has EchoToTop/BottomStatus, try changing the capitalization of EchoToTopStatus
    In UpdateWorld, remove a semi-colon
    Try at least three more things, on your own:


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