In the early days of computers, a program simply read
in numbers and printed out results. Things have moved on a bit since
then, and now computer programs can work with images, video, and sound.
This is especially useful where games are concerned; a large part of the
enjoyment of a game results from an attractive game environment. And
sometimes the graphics themselves form part of the game play. If you
want to become a game developer, you need to know how these resources
are made part of your program. In fact, many programs today have
significant graphical content in the form of splash screens, icons, and
the like. So the first thing you need to do is get some images and
incorporate them into your project. Later, I’ll show you how to use
other kinds of resources, including fonts (for writing text) and sounds.
Unfortunately, I won’t be
able to help you create your graphics for use in computer games. I have
no artistic abilities whatsoever, although I do know how to use a
camera. If you need artistic resources, my advice is to find someone who
is good at art and commission him or her to do the drawings for you.
The same goes for any music or sounds that you might need.
This means that you can
concentrate on what you are supposed to be good at: creating the game
itself. This is what professional game developers do. They have a team
of programmers who make the game work and a team of artists and sound
technicians who work on the sensory aspects of the game. Having said
that, you might be good at graphic design as well as programming, in
which case you can do both. However, I still advise getting an artist
involved, as it helps spread the work around and provides you with a
useful sounding board for ideas. It also makes it more fun.
1. Getting Some Pictures
At this point, you
need some pictures. You need to tailor your images to fit the screen of
the XNA device you are going to use. The Xbox screen is capable of
showing high-resolution images. A high-resolution image is made up of a
large number of dots, or pixels. Modern digital cameras can create
images that are thousands of pixels in height and width. However, from a
game point of view, you want to make the images as small as you can.
This reduces the amount of memory they consume and also reduces the work
required to move them around the screen. You won’t usually need very
high resolution for your games, so your pictures need be no more than
600 pixels in each direction. The Zune display is 240 pixels wide and
320 pixels high, so you should use even smaller images for XNA games
intended to run on this device.
There are a number of
different formats for storing pictures on computers. Your pictures
should be in the Portable Network Graphics (PNG), Windows Bitmap (BMP),
or Joint Photographic Experts Group (JPEG) format. The PNG and BMP
formats are lossless, in that they always store an exact version of the
image that is being held. PNG files can also have transparent regions,
which is important when you want to draw one image on top of another. The
JPEG format is lossy, in that the image is compressed in a way that
makes it much smaller, but at the expense of precise detail. The games
that you create should use JPEG images for the large backgrounds and PNG
images for the smaller objects that are drawn on top of them.
If you have no pictures
of your own (which I consider highly unlikely), you can use the ones
that I have provided with the sample files for this article, but the
games will work best if you use your own pictures. Figure 4-1 shows my picture of Jake. I will be using this for my first XNA graphics programs. You can use another picture if you wish.
I have saved the image in
the JPEG file format with a width of 600 pixels. If you need to convert
into this format, you can load an image using the Microsoft Paint
program and then save it in this format. With Paint, you can also scale
and crop images if you want to reduce the number of pixels in the image.
For more advanced image manipulation, I recommend the program
Paint.Net, which you can obtain for free from http://www.getpaint.net/.
2. Content Management Using XNA
As far as XNA is concerned, content
(images, sounds, 3-D models, and video) is what makes games more
interesting. XNA treats items of content in the same way that variables
are created in programs. XNA can import a content item of a particular
type (for example, my file containing a picture of Jake) and give it an
identifier. When the game program is running, XNA fetches the game
content items as they are requested by name. These content items are
sometimes referred to as assets.
In the same way that a company has assets, such as buildings,
machinery, and staff, a game has assets such as sounds and images.
3. Working with Content Using XNA Game Studio
You
use XNA Game Studio to put content into your game. When the finished
program is constructed, XNA Game Studio makes sure that the assets are
available to your game. The good news is that you don’t need to worry
about any of this; you need only know how to load assets into XNA Game
Studio and get hold of them from within your game programs.
4. XNA Game Studio Solutions and Projects
You start making a game by
creating a brand-new project. I called mine JakeDisplay. You create the
project using the New Project dialog box as you’ve done for all your
previous projects. Remember that the project you are creating is either a
Windows Game (3.0), an Xbox Game (3.0), or a Zune Game (3.0). Note that the Create Directory For Solution option is selected in this
dialog box. Whenever you create a project, you should ensure that this
option is selected. This creates a directory structure that contains the
program and all the other items that are required to make the game
work.
Figure 2 shows what is created when I make a new project called JakeDisplay.
However, the file JakeDisplay
that you can see in the directory is a solution. This might be
confusing. You’ve used the New Project command in XNA Game Studio and
have ended up with a solution. In this case, XNA Game Studio has created
a solution called JakeDisplay and then added a single project to that
solution. The project is also called JakeDisplay. You can think of a solution as a "shopping list" of projects. Figure 3
shows how this works. The solution holds a list of the names of project
files. Each of the project files holds a list of the names of the files
used in that project. Each item on the list is often referred to as a reference to that item, in that it tells XNA Game Studio how to get to it.
The
solution file holds the name of the JakeDisplay project. The project
file holds the names of the C# files in the project (Game1.cs and
Program1.cs) and other resources used by the project including the
Content directory. At present, the only two resources are
GameThumbnail.png, which is an image used as a thumbnail on the display
when the game is stored on the Xbox or Zune, and Game.ico, which is the
icon used for the game program file on a Microsoft Windows PC. When you
add your image of Jake to the project, you add the name of the file to
the project file so that XNA Game Studio knows where to go to get the
asset. XNA Game Studio displays the contents of the solution and project
files as a diagram in Solution Explorer, as shown in Figure 4.
Note that the solution file and project files also contain other
settings (the Properties and References) that you’ll use later.
You have already seen
that a single game solution can hold projects for deployment to an Xbox,
a Zune, or a Windows PC. Sometimes you might want to add more projects
to a solution so that you can separate your code into reusable portions
or because you want to reuse code that you already separated that way.
For example, you might make a project called HighScoreManager, which
would be in charge of displaying high-score tables for your game. High
scores work the same way in many games, so it makes sense to write the
code only once and then use it in those games. You would do this by
creating a library project to deal with the high scores and then add
this project to the "shopping list" of those projects. However, for now,
you simply create games that are single projects.
Note:
Our
Great Programmer is very keen on using projects to reuse code. The way
she sees it, that way she can get paid several times for writing the
same piece of software. When she starts work on a new system, she takes a
lot of time to try to structure things into projects so that different
parts of the system are in separate projects.