Game Development Reference
In-Depth Information
Chapter 12
Collections of Game Objects
12.1 Introduction
After reading the overview of the game Jewel Jam , I am sure you are eager to find
out how to build it in C#. However, before we start creating this game, we have to
introduce a few new programming aspects that we are going to use, in particular:
how to deal with large collections of game objects. In this chapter, we will introduce
collections and arrays, and show that they are a very powerful tool for making game
code scalable and more flexible.
12.2 Many Game Objects
When looking back at our previous examples, we have avoided using many game
objects of the same type on purpose. For example, in the Painter game, there are only
three paint cans and one ball. Because of these limited numbers of game objects, we
could simply declare the variables as follows:
PaintCan can1, can2, can3;
Ball ball;
Suppose that we wanted to create a game where the player could shoot more than
one ball at a time? Or what about having more than three paint cans? Of course, we
could still expand the list of member variables and deal with it that way:
PaintCan can1, can2, can3, can4, can5, can6, can7, can8;
Ball ball1, ball3, ball4, ball5, ball6;
Adding many more balls and paint cans would surely lead to a lot of duplicate code
in your game, but if you are a very stubborn developer, you could still get away
with it. However, at some point, simply declaring more and more member variables
will no longer provide a solution to the problem. The following example will surely
discourage any developer to use this technique!
 
Search WWH ::




Custom Search