Game Development Reference
In-Depth Information
body[0]--;
} else if (dir[keys.RIGHT]) {
body[0]++;
}
};
// Update the snake's position vectors on
key presses
this.doOnKeyDown = function(event) {
var key = event.which;
// Don't process a key that's already down
if (dir[key])
return;
dir[keys.UP] = false;
dir[keys.DOWN] = false;
dir[keys.LEFT] = false;
dir[keys.RIGHT] = false;
if (key == keys.UP && !dir[keys.DOWN]) {
return dir[keys.UP] = true;
} else if (key === keys.DOWN &&
!dir[keys.UP]) {
return dir[keys.DOWN] = true;
} else if (key === keys.LEFT &&
!dir[keys.RIGHT]) {
return dir[keys.LEFT] = true;
} else if (key === keys.RIGHT &&
!dir[keys.LEFT]) {
return dir[keys.RIGHT] = true;
}
};
// This allows us to use different images to
represent the snake
this.setSkin = function(img) {
skin = new Image();
skin.onload = function() {
skin.width = this.width;
skin.height = this.height;
};
skin.src = img;
Search WWH ::




Custom Search