Game Development Reference
In-Depth Information
function gameLoop() {
// Only do anything here if the snake is not
dead
if (snake.isAlive()) {
// Make the frame rate no faster than
what we determine (30 fps)
renderTime.now = Date.now();
if (renderTime.now - renderTime.last >=
renderTime.fps) {
// If there is no fruit on the grid,
place one somewhere. Here we
// use a web worker to calculate an
empty square on the map
if (fruit.position[0] < 0) {
cellGen.postMessage({
points: snake.getBody(),
width: worldWidth,
height: worldHeight
});
} else {
snake.move();
head = snake.getHead();
// Check if the snake has ran into
itself, or gone outside the grid
if (snake.isAt(head.x, head.y,
false) ||
head.x < 0 || head.y < 0 ||
head.x >= worldWidth ||
head.y >= worldHeight) {
snake.setDead(true);
}
// Check if the snake has eaten a
fruit
if (fruit.isAt(head.x, head.y)) {
Search WWH ::




Custom Search