Handling Text and Images
// Write some text to the score area of the page.
function setText(where,value) {
// output new text
document.all[where].innerHTML = value
}
// Load a new background image for the game.
function loadImage() {
game.images.current++
if (game.images.current==game.images.list.length)
game.images.current=0
img.src = game.images.list[game.images.current]
}
Process Keystrokes
// Set codes for the keys used in the game.
SetupGame() {
this.keys = new Object()
this.keys.rotleft=73 // I
this.keys.rotright=75 // K
this.keys.left=74 // J
this.keys.right=76 // L
this.keys.drop=32 // <space>
}
function doKeyPress() {
// Get the key that was pressed.
var key = event.keyCode
if (game.inprocess) {
if (game.keys.left==key) // Move one unit left.
if ((!check(-1,false)) && boundary(-1)) offset(-1)
if (game.keys.right==key) // Move one unit right.
if ((!check(1,false)) && boundary(1)) offset(1)
if (game.keys.rotleft==key) transform("left")
if (game.keys.rotright==key) transform("right")
// Drop the game piece?
if (game.keys.drop==key) {
clearTimeout(tm)
game.temp = 5
if (!game.pause) tm=setTimeout("doTimer()",game.temp)
}
}
}
Display the Game Score
function outScore() {
// Determine game level based on score.
game.level = Math.ceil((game.score+1)/10);
if (game.level > 18)
game.level = 18; // Maximum level reached.
if (game.level > game.images.current)
loadImage() // prefetch next image
if (game.level-1 != game.images.display) {
if (4==img.readyState) {
game.images.display++ // Display the new image.
document.body.background = "#000000"
}
}
game.speed = game.speeds[game.level] // current game speed
// Display current level.
setText("score",game.score); setText("level",game.level)
}