Game Development Reference
In-Depth Information
This is essentially the same as the Stain class—we just removed the type member. The first
really interesting class of our model of Mr. Nom's world is the Snake class. Let's think about what
it has to be able to do:
ď?®
It must store the head and tail parts.
ď?®
It must know which way Mr. Nom is currently heading.
ď?®
It must be able to grow a new tail part when Mr. Nom eats a stain.
ď?®
The first and second items are easy. We just need a list of SnakePart instances—the first part in
that list being the head and the other parts making up the tail. Mr. Nom can move up, down, left,
and right. We can encode that with some constants and store his current direction in a member
of the Snake class.
The third item isn't all that complicated either. We just add another SnakePart to the list of
parts we already have. The question is, at what position should we add that part? It may
sound surprising, but we give it the same position as the last part in the list. The reason for this
becomes clearer when we look at how we can implement the last item on the preceding list:
moving Mr. Nom.
It must be able to move by one cell in the current direction.
Figure 6-6 shows Mr. Nom in his initial configuration. He is composed of three parts: the head,
at (5,6), and two tail parts, at (5,7) and (5,8).
Figure 6-6. Mr. Nom in his initial configuration
The parts in the list are ordered, beginning with the head and ending at the last tail part. When
Mr. Nom advances by one cell, all the parts behind his head have to follow. However, Mr. Nom's
parts might not be laid out in a straight line, as in Figure 6-6 , so simply shifting all the parts in the
direction Mr. Nom advances is not enough. We have to do something a little more sophisticated.
Search WWH ::




Custom Search