Game Development Reference
In-Depth Information
every canvas has the same width, this is easy to do for every canvas), then telling
every window where the snake should be rendered within a canvas. Since that po-
sition is valid for every window, we also tell each window individually whether or not
they should render the information we're sending them. Only the window that we cal-
culate the snake is in, is told to go ahead and render.
function play() {
// This is used to change the snake's
position randomly
// from time to time. The reason for this is
so we don't
// need to implement any event handling to
handle user input,
// since this is just a simple demonstration.
if (dirChange-- < 0) {
dirChange = 100;
var rand = parseInt(Math.random() * 1000) %
4;
// Make the snake move to the right
if (rand == 0) {
snake.dir.x = 1;
snake.dir.y = 0;
// Make the snake move to the left
} else if (rand == 1) {
snake.dir.x = -1;
snake.dir.y = 0;
// Make the snake move down
} else if (rand == 2) {
snake.dir.x = 0;
snake.dir.y = 1;
// Make the snake move up
} else if (rand == 3) {
snake.dir.x = 0;
snake.dir.y = -1;
}
};
// Update the snake's position, making sure
to wrap the snake
Search WWH ::




Custom Search