topical media & game development

talk show tell print

game-javascript-casual-mouse-bomb-game.htm / htm



  
  <!-- TWO STEPS TO INSTALL MOUSE BOMB:
  
    1.  Copy the coding into a new file, save as bomb.js
    2.  Add the last code into the BODY of your HTML document  -->
  
  <!-- STEP ONE: Paste this code into a new file, save as bomb.js -->
  
  <!-- Original:  Diana Ugo (ugo76@libero.it) -->
  <!-- Web Site:  http://utenti.tripod.it/falcons -->
  
  <!-- This script and many more are available free online at -->
  <!-- The JavaScript Source!! http://javascript.internet.com -->
  picFollow = new Image();
  picFollow.src = "game-javascript-casual-mouse-bomb-bomb.gif";
  picExplosion = new Image();
  picExplosion.src = "game-javascript-casual-mouse-bomb-explode.gif";
  document.onmousemove = getMousePosition;
  document.onmouseout = pauseBomb;
  document.write("<div id=\"diva\" style=\"position:absolute\">");
  document.write("<img name=\"pic\"src=" + picFollow.src + "></div>");
  var picX = 20;
  var picY = 100;
  var step = 10;
  var speed = 100;
  var tolerance = step/2 +1;
  var mouseX = 0;
  var mouseY = 0;
  var mouseOut = true;
  var followMouse = false;
  myInterval = setInterval('moveBomb()', speed);
  function pauseBomb() {
  mouseOut = true;
  }
  function getMousePosition(e) {
  mouseX = window.event.x + document.body.scrollLeft;
  mouseY = window.event.y + document.body.scrollTop;
  mouseOut = false;
  if (followMouse) {
  diva.style.left = mouseX - pic.width / 2;
  diva.style.top = mouseY - pic.height / 2;
     }
  }
  function calcNewPos() {
  if (mouseX == picX)
  return;
  arg = (mouseY-picY) / (mouseX-picX);
  mult = 1;
  if (mouseX - picX < 0)
  mult = -1;
  alpha = Math.atan(arg);
  dx = mult * step * Math.cos(alpha);
  dy = mult * step * Math.sin(alpha);
  picX += dx;
  picY += dy;
  }
  function collision() {
  if ((Math.abs(picX-mouseX) < tolerance) && (Math.abs(picY-mouseY) < tolerance) && (!mouseOut))
  return true;
  return false;
  }
  function hideAnimation() {
  diva.style.visibility = "hidden";
  }
  function moveBomb() {
  calcNewPos();
  window.status = "("+mouseX+","+mouseY+")";
  diva.style.left = picX - pic.width / 2;
  diva.style.top = picY - pic.height / 2;
  if (collision()) {
  clearInterval(myInterval);
  pic.src = picExplosion.src;
  followMouse = true;
  setTimeout('hideAnimation()', 2000);
     }
  }
  
  <!-- STEP TWO: Just add the script tag to include the bomb script  -->
  
  <BODY>
  
  <script src="bomb.js"></script>
  
  <p><center>
  <font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
  by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
  </center><p>
  
  <!-- Script Size:  2.26 KB -->
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.