⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cookie_source.html

📁 this gives details of the network programming
💻 HTML
字号:
<HEAD><TITLE>Source for Chase the Cookie</TITLE><LINK rel=stylesheet type=text/css HREF=../../style/jsdemo.css></HEAD><BODY BGCOLOR=#CCCCCC MARGINHEIGHT=0 MARGINWIDTH=0 TOPMARGIN=0  LEFTMARGIN=0><TABLE bgColor=#8899aa border=0 cellPadding=4 cellSpacing=0 width=100%>  <TR>    <TD class=title>&nbsp NetProg 2002: Source for Chase the Cookie </TD> </TR>  <TR bgColor=black height=10><TD CLASS=menu> </TD></TR></TABLE><DIV CLASS=page><BR><BR><P><CENTER><TABLE BORDER=0 BGCOLOR=#8899aa CELLPADDING=5><TR><TD><TABLE bgcolor=white BORDER=0 CELLPADDING=10><TR><TD><PRE class=codebox>&lt;HEAD>
&lt;TITLE>Netprog JavaScript Demo - Chase the Cookie&lt;/TITLE>
&lt;SCRIPT LANGUAGE=JavaScript><SPAN CLASS=code>
&lt;!--
// Find out what kind of browser we are
// This was originally Netscape only (using layers), now it supports
// both IE and netscape


if (navigator.appName == "Microsoft Internet Explorer") {
  ie=true;
} else {
  ie=false;
}


// updatecoords writes the current coordinates of the layer
// holding the cookie image to the form fields xcoord and ycoord.

function updatecoords() {
   var x,y;

   if (ie){
     // IE reports position with "px" units, we gotta get rid of this
     // and make sure our variables are numbers to Javascript  
      x = document.all["cookielayer"].style.left;
      y = document.all["cookielayer"].style.top;
      x = x.replace(/px/,""); x=x*1.0;
      y = y.replace(/px/,""); y=y*1.0;
   } else {
      x = document.cookielayer.pageX;
      y = document.cookielayer.pageY;
   }
   document.frm.xcoord.value = x;
   document.frm.ycoord.value = y;


}

// move moves the position of the cookie image relative to
// it's current location. 
// positive xinc will move to the right (neg to the left)
// positive yinc will move down (neg up)

function move( xinc, yinc ) {

    var jsize=20;
    var x,y;

    if (ie) {
      // ie reports coords with "px" attached so we gotta remove it
      // before doing math...

      x = document.all["cookielayer"].style.left;
      x = x.replace(/px/,"");
      y = document.all["cookielayer"].style.top;
      y = y.replace(/px/,"");
      // force to numbers
      x = x * 1.0;
      y = y * 1.0;
    } else {
      x = document.cookielayer.pageX;
      y = document.cookielayer.pageY;
    }


    var xnew = x + xinc * jsize;
    var ynew = y + yinc * jsize;


    
    if ( ( xnew >= 10 ) && ( xnew &lt;= 290) ){
       x = xnew;
    }
    if ( ( ynew >= 10 ) && ( ynew &lt;= 290) ){
       y = ynew;
    }

    if (ie) {
      document.all["cookielayer"].style.top=y+"px";
      document.all["cookielayer"].style.left=x+"px";
    } else { 
       document.cookielayer.moveToAbsolute(x,y);
    }
    updatecoords();
}

function setcoords() {
    var x = document.frm.xcoord.value;
    var y = document.frm.ycoord.value;

    if ((x>=10)&&(x&lt;=290)&&(y>=10)&&(y&lt;=290)) {
       if (ie) {
          document.all["cookielayer"].style.top=y+"px";
          document.all["cookielayer"].style.left=x+"px";
        } else { 
          document.cookielayer.moveToAbsolute(x,y);
        }
    }
    updatecoords();
}


// random jump to anywhere in the playing field

function hjump() {
    xpos = Math.random() * 280 + 10;
    ypos = Math.random() * 280 + 10;

    if (ie) {
        document.all["cookielayer"].style.top=ypos+"px";
        document.all["cookielayer"].style.left=xpos+"px";
    } else { 
        document.cookielayer.moveToAbsolute(xpos,ypos);
    }
    updatecoords();
}

// called when the user clicks on the cookie image
function catchcookie() {
    alert("You got me!");
    running=0;
}
// variable used to keep track of when the game is running
var running=0;

// start up the game 
function hyperspace_game() {
    running=1;
    alert("Catch the cookie by clicking on it!");
    hyperspace_move();
}

// stop the game
function stop_hyperspace_game() {
    running=0;
}

// make a single game move by moving the image to a random
// location and then setting up to call this routine again
// after some delay. The delay before the next call is 
// based on the value of the speed field (the user can change).
function hyperspace_move() {
    var delay = document.frm.speed.value;
    if (running == 1) {
        hjump();
        setTimeout("hyperspace_move()",delay);
    }
}
// -->
</SPAN>&lt;/SCRIPT>
&lt;/HEAD>
&lt;BODY BGCOLOR=gray>

&lt;DIV id=cookielayer style="position: absolute; left:20; top:20">

&lt;A HREF=foo onClick="<SPAN CLASS=code>catchcookie(); return(false)</SPAN>">
&lt;IMG SRC=cookie.gif  BORDER=0 ISMAP WIDTH=100 HEIGHT=100 >&lt;/A>
&lt;/DIV>


&lt;TABLE HEIGHT=380 BORDER=2>&lt;TR>&lt;TD WIDTH=380 BGCOLOR=WHITE>&nbsp&lt;/TD>
&lt;TD BGCOLOR=CYAN>
&lt;FORM Name=frm>

&lt;CENTER>Move the cookie&lt;/CENTER>&lt;BR>
&lt;TABLE>
&lt;TR ALIGN=CENTER>
  &lt;TD>&nbsp&lt;/TD>
  &lt;TD>&lt;INPUT TYPE=BUTTON Name=up  WIDTH=50 Value="Up" onClick="<SPAN CLASS=code>move(0,-1)</SPAN>">&lt;/TD>
  &lt;TD>&nbsp&lt;/TD>
&lt;/TR>

&lt;TR>
&lt;TD>&lt;INPUT TYPE=BUTTON WIDTH=50 Name=left Value="Left" onClick="<SPAN CLASS=code>move(-1,0)</SPAN>">&lt;/TD>
&lt;TD>&nbsp&lt;/TD>
&lt;TD>&lt;INPUT TYPE=BUTTON  WIDTH=50 Name=right Value="Right" onClick="<SPAN CLASS=code>move(1,0)</SPAN>">&lt;/TD>
&lt;/TR>

&lt;TR>
  &lt;TD>&nbsp&lt;/TD>
  &lt;TD>&lt;INPUT TYPE=BUTTON  WIDTH=50 Name=down Value="Down" onClick="<SPAN CLASS=code>move(0,1)</SPAN>">&lt;/TD>
  &lt;TD>&nbsp&lt;/TD>
&lt;/TR>
&lt;/TABLE>
&lt;P>
&lt;CENTER>
&lt;INPUT TYPE=BUTTON Name=jump Value="Random Jump" onClick="<SPAN CLASS=code>hjump()</SPAN>">
&lt;/CENTER>

&lt;HR>
&lt;CENTER>Play the chase game&lt;/CENTER>&lt;BR>
&lt;CENTER>
&lt;INPUT TYPE=BUTTON WIDTH=70 Name=chase Value="Start" onClick="<SPAN CLASS=code>hyperspace_game()</SPAN>">
&lt;INPUT TYPE=BUTTON WIDTH=70 Name=chase Value="Stop" onClick="<SPAN CLASS=code>stop_hyperspace_game()</SPAN>">
&lt;/CENTER>
&lt;BR>
Speed: &lt;INPUT TYPE=TEXT SIZE=5 Name=speed>&lt;BR>
&lt;CENTER>(msec. between jumps)&lt;/CENTER>
&lt;HR>
&lt;CENTER>Current Position:&lt;CENTER>&lt;BR>
&lt;INPUT TYPE=TEXT SIZE=5 Name=xcoord onChange="<SPAN CLASS=code>setcoords()</SPAN>">
&lt;INPUT TYPE=TEXT SIZE=5 Name=ycoord onChange="<SPAN CLASS=code>setcoords()</SPAN>">

&lt;/FORM>
&lt;/TD>
&lt;/TR>&lt;/TABLE>

&lt;SCRIPT><SPAN CLASS=code>
// Initialize the game speed
   document.frm.speed.value=500;
   updatecoords();
</SPAN>&lt;/SCRIPT>

</PRE></TD></TR></TABLE></TD></TR></TABLE></CENTER></DIV>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -