/*************************************************************************** hockey.js - Play hockey and make a puck bounce off the side of an image The size of the image is the playing field. The puck and field dimensions are in multiples of 16. When the puck reaches the right edge the player loses. The player must get the puck through a slot the size of the puck on the left side of the image. The pointer must be within a box as high as the playing field and as wide as the puck. When the pointer contacts the edge of the puck (virtually square) the puck reverses with a mirror dy equal to the distance of the pointer from the center of the puck. The speed increases as play continues. ***************************************************************************/ var timer_id = 0; var mouse_x, mouse_y; var puck_x, puck_y, puck_dx, puck_dy; var score_high = -1, score_miss = 0; /*************************************************************************** Startup initialization This source depends on the functions provided in Tools.c which must be included before this source file. ***************************************************************************/ Sprite_Make ("Puck"); Sprite_Move (Hockey, Puck, 0, 120); Sprite_Make ("Pointer"); Sprite_Move (Hockey, Pointer, 544, 136); Scores_List (); document.onmousemove = handlerMM; document.onclick = Play_Start; Hockey_Message.innerText = "Click to start the game."; /*************************************************************************** ***************************************************************************/ function handlerMM () { mouse_x = document.body.scrollLeft + window.event.clientX - Hockey.offsetLeft; mouse_y = document.body.scrollTop + window.event.clientY - Hockey.offsetTop; if (mouse_y < 16) mouse_y = 16; if (mouse_y > 256) mouse_y = 256; if (puck_dx) Sprite_Move (Hockey, Pointer, 544, mouse_y); } /*************************************************************************** ***************************************************************************/ function Play_Start () { // Already playing? if (puck_dx) return; if (score_high < 0) { score_miss = 0; score_high = 0; Score_High.innerText = score_high; Score_Miss.innerText = score_miss; } Hockey_Message.innerText = ""; puck_x = 16; puck_y = 16 + Math.floor (224 * Math.random ()); puck_dx = 4 + score_high; puck_dy = 2 - Math.floor (5 * Math.random ()); Speed.innerText = puck_dx; Puck_Move (); } /*************************************************************************** ***************************************************************************/ function Puck_Move () { var string; var x; var y; puck_x += puck_dx; puck_y += puck_dy; // asume nothing happened string = ""; // When we encounter left wall if (puck_x <= 16) { // Where was puck at the wall. x = (16 - puck_x) / -puck_dx; y = puck_y - Math.round (x * puck_dy); // if we don't make the goal bounce if ((112 < y) && (128 > y)) { ++score_high; Score_High.innerText = score_high; string = "You made the goal!"; } else { puck_x = 16; puck_y = y; puck_dx = - puck_dx; } } // When we encounter foul line else if (puck_x >= 512) { // Where was puck at the foul line. x = (puck_x - 512) / puck_dx; puck_y = puck_y - Math.round (x * puck_dy); dy = mouse_y - puck_y; // If pointer makes contact bounce off the pointer if ((dy >= -1) && (dy <= 32)) { puck_x = 512; if (puck_dx < 16) ++puck_dx; Speed.innerText = puck_dx; puck_dx = - puck_dx; puck_dy += 2 - dy / 8; } else { ++score_miss; Score_Miss.innerText = score_miss; string = "Strike " + score_miss + "!"; if (score_miss > 2) { string += " You're out!"; Scores_Update (); score_high = -1; } } } // When we encounter the side walls else if ((puck_y >= 224) || (puck_y <= 16)) { puck_dy = - puck_dy; if (puck_y >= 224) puck_y = 224; else puck_y = 16; } Sprite_Move (Hockey, Puck, puck_x, puck_y) if ("" == string) { if (0 == timer_id) timer_id = setInterval ("Puck_Move ()", 30); } else { if (0 != timer_id) clearInterval (timer_id); timer_id = 0; puck_dx = 0; if (score_high < 0) Hockey_Message.innerText = string + " Click to start a new game."; else Hockey_Message.innerText = string + " Click to restart the puck."; } } /*************************************************************************** ***************************************************************************/ function Debug_Object (object) { document.write ('

Debug_Object ' + object); for (var i in eval(object)) { document.write ('
' + object + "." + i + " = " + eval (object)[i]); } }