Game Development Reference
In-Depth Information
if if(direction == UP )
head.y - = 1;
if if(direction == LEFT )
head.x - = 1;
if if(direction == DOWN )
head.y += 1;
if if(direction == RIGHT )
head.x += 1;
if if(head.x < 0)
head.x = 9;
if (head.x > 9)
head.x = 0;
if if(head.y < 0)
head.y = 12;
if (head.y > 12)
head.y = 0;
}
The next method, advance() , implements the logic illustrated in Figure 6-7 . First, we move each
part to the position of the part before it, starting with the last part. We exclude the head from
this mechanism. Then, we move the head according to Mr. Nom's current direction. Finally, we
perform some checks to make sure Mr. Nom doesn't go outside his world. If that's the case, we
just wrap him around so that he comes out at the other side of the world.
public boolean checkBitten() {
int len = parts.size();
SnakePart head = parts.get(0);
for ( int i = 1; i < len; i++) {
SnakePart part = parts.get(i);
if (part.x == head.x && part.y == head.y)
return true ;
}
return false ;
}
}
The final method, checkBitten() , is a little helper method that checks if Mr. Nom has bitten his
tail. All it does is check that no tail part is at the same position as the head. If that's the case,
Mr. Nom will die and the game will end.
The World Class
The last of our model classes is called World . The World class has several tasks to fulfill:
Keeping track of Mr. Nom (in the form of a
Snake instance), as well as the
Stain instance that dropped on the world. There will only ever be a single
stain in our world.
ï?®
Providing a method that will update Mr. Nom in a time-based manner (for
ï?®
example, he should advance by one cell every 0.5 seconds). This method
will also check if Mr. Nom has eaten a stain or has bitten himself.
 
Search WWH ::




Custom Search