home / experts / javascript / column47 |
|
Collision HandlingWe need to check for possible collisions every time we move the snake one square. As we explained in the previous page, we need to restart the game if there is indeed a collision:
There are three possible types of collisions: with the board's four edges, with the snake itself, and with the target. Here is the
The
The function checks if one of the snake's head coordinates will overlap the board's edges when a move takes place. After the move, the head's coordinates will be The other collision that triggers a game restart is a self-collision. We need to check that the snake's head does not collide with the snake itself:
The loop above checks that the snake's head coordinates do not overlap any one of the other snake's squares. Notice that the |
Produced by Yehuda Shiran and Tomer Shiran
Created: August 30, 1999
Revised: August 30, 1999
URL: http://www.webreference.com/js/column47/collide.html