JavaScript编写的网页小游戏,很给力


Posted in Javascript onAugust 18, 2017
以下为游戏界面:
JavaScript编写的网页小游戏,很给力
以下为游戏代码:

<html>
<head>
  <script language="JavaScript">
    <!-- Original: Nick Young () -->
    <!-- recompose: Pakchoi () -->
    var timerID = null;
    var INT = 40;
    var loadFLG = 0;
    var gameFLG = 0;
    var missFLG = 0;
    var tim = 0;
    var blcol = new Array(5); // block color
    var blsta = new Array(40); // block status
    var blNO = new Array(40); // block No
    var blclr = 0; // clear block
    var ballX = 0; // ball data
    var ballY = 0;
    var ballN = 5;
    var ballDX = 0;
    var ballDY = 0;
    var tmpRL = 193;
    var X = 0;
    blcol[0] = "blue";
    blcol[1] = "yello";
    blcol[2] = "red";
    blcol[3] = "purple";
    blcol[4] = "#FF0000";
    blcol[5] = "black";
    function mainF() {
      clearTimeout(timerID);
      tim = tim + 1;
      with (Math) {
        tmptim = floor(tim / 10)};
      document.forms[0].TM.value = tmptim;
      ballX = ballX + ballDX;
      ballY = ballY + ballDY;
      outCHK();
      blkCHK();
      ball.style.posTop = ballY;
      ball.style.posLeft = ballX;
      racket.style.posLeft = tmpRL;
      if (gameFLG == 01) {
        timerID = setTimeout("mainF()", INT);
      }
    }
    function initG() {
      if (blclr >= 40) {
        blclr = 0;
        tim = 0;
        ballN = 3;
        with (Math) {
          tmptim = floor(tim / 10);
        }
        document.forms[0].TM.value = tmptim;
        clrmes.style.posTop = -1000;
        clrmes.style.posLeft = -1000;
        ovrmes.style.posTop = -1000;
        ovrmes.style.posLeft = -1000;
        for (ib = 0; ib < 5; ib++) {
          for (ia = 0; ia < 8; ia++) {
            chc(ib * 8 + ia +1, ib);
            blsta[ib * 8 + ia] = ib;
          }
        }
      }
      document.forms[0].BL.value = ballN;
      starter.style.posTop = -1000;
      starter.style.posLeft = -1000;
      gameFLG = 1;
      loadFLG = 1;
      ballX = 209;
      ballY = 270;
      ballDX =- 8;
      ballDY =- 8;
      tmpRL = 193;
      missFLG = 0;
      timerID = setTimeout("mainF()", INT);
    }
    function SUP() {
      UP.outerHTML = "<DIV ID='DN' STYLE='position:absolute'><A HREF='javascript:SDN()'>SPEED DOWN</A></DIV>";
      DN.style.posTop = 170;
      DN.style.posLeft = 432;
      INT = 40;
    }
    function SDN() {
      DN.outerHTML = "<DIV ID='UP' STYLE='position:absolute'><A HREF='javascript:SUP()'>SPEED UP</A></DIV>";
      UP.style.posTop = 170;
      UP.style.posLeft = 432;
      INT = 50;
    }
    function MouseMv() {
      if (loadFLG == 1) {
        tmpRL = X - 20;
        if (tmpRL < 16) { tmpRL = 16; }
        if (tmpRL > 370) { tmpRL = 370; }
      }
    }
    function outCHK() {
      if (ballX < 16){ ballX = 32 - ballX; ballDX = -ballDX; }
      if (ballX > 401){ ballX = 802 - ballX; ballDX = -ballDX; }
      if (ballY < 16){ ballY = 32 - ballY; ballDY = -ballDY; }
      if (ballY >= 272) {
        if (missFLG == 0) {
          tmpX = (ballDX / ballDY) * (272 - ballY) + ballX;
          if (tmpX >= tmpRL - 12) {
            if (tmpX <= tmpRL + 42) {
              ballY = 272; ballDY = -ballDY;
              ballX = tmpX;
              ballRD = tmpX - tmpRL;
              with (Math){ ballDX = 8 * abs(ballDX) / ballDX; }
              if (ballRD < -4){ ballDX = -15; }
              if (ballRD > 36){ ballDX = 15; }
              if (ballRD >= 14){ if (ballRD <= 16) { ballDX = -2; } }
              if (ballRD >= 17){ if (ballRD <= 20) { ballDX = 2; } }
              if (ballRD >= 0){ if (ballRD <= 4) { ballDX = -4; } }
              if (ballRD >= 28){ if (ballRD <= 32) { ballDX = 4; } }
              if (ballRD >= -4){ if (ballRD <= -1) { ballDX = -11; } }
              if (ballRD >= 33){ if (ballRD <= 36) { ballDX = 11; } }
            }
          }
          if (ballDY > 0){ missFLG = 1; }
        }
        else {
          if (ballY > 290){ missFLG = 0; ballN = ballN - 1; gameEnd(); }
        }
      }
    }
    function blkCHK() {
      tmpY = ballY + 4;
      tmpX = ballX + 4;
      if (tmpY >= 48) {
        if (tmpY <= 147) {
          if (tmpX >= 29) {
            if (tmpX <= 396) {
              with (Math) {
                ia = floor((tmpX - 29) / 46);
                ib = floor((tmpY - 48) / 20);
                ic = ib * 8 + ia;
              }
              if (blsta[ic] <= 4) {
                tmpbc = blsta[ic] + 1;
                blsta[ic] = tmpbc;
                chc(ic + 1, tmpbc);
                if (tmpbc == 5){ blclr = blclr + 1; }
                if (blclr >= 40){ gameEnd(); }
                if (ballDX > 0) {
                  iy=(ballDY / ballDX) * (29 + 46 * ia - tmpX) + tmpY;
                  if (iy > 48 + 20 * ib + 18) {
                    tmpY1 = 48 + 20 * ib + 18;
                    tmpX1 = (ballDX / ballDY) * (48 + 20 * ib + 18 - tmpY) + tmpX;
                    ballX = tmpX1 - 4;
                    ballY = tmpY1 - 4;
                    ballDY = -ballDY;
                  }
                  else {
                    if (iy < 44 + 20 * ib) {
                      tmpY1 = 48 + 20 * ib;
                      tmpX1 = (ballDX / ballDY) * (48 + 20 * ib - tmpY) + tmpX;
                      ballX = tmpX1 - 4;
                      ballY = tmpY1 - 4;
                      ballDY = -ballDY;
                    }
                    else {
                      tmpX1 = 29 + 46 * ia;
                      tmpY1 = (ballDY / ballDX) * (29 + 46 * ia - tmpX) + tmpY;
                      ballX = tmpX1 - 4;
                      ballY = tmpY1 - 4;
                      ballDX = -ballDX;
                    }
                  }
                }
                else {
                  iy = (ballDY / ballDX) * (29+46 * ia + 44 - tmpX) + tmpY;
                  if (iy > 48 + 20 * ib + 18) {
                    tmpY1 = 48 + 20 * ib + 18;
                    tmpX1 = (ballDX / ballDY) * (48 + 20 * ib + 18 - tmpY) + tmpX;
                    ballX = tmpX1 - 4;
                    ballY = tmpY1 - 4;
                    ballDY = -ballDY;
                  }
                  else {
                    if (iy < 44 + 20 * ib) {
                      tmpY1 = 48 + 20 * ib;
                      tmpX1 = (ballDX / ballDY) * (48 + 20 * ib - tmpY) + tmpX;
                      ballX = tmpX1 - 4;
                      ballY = tmpY1 - 4;
                      ballDY = -ballDY;
                    }
                    else {
                      tmpX1 = 29+46 * ia + 44;
                      tmpY1 = (ballDY / ballDX) * (29 + 46 * ia + 44 - tmpX) + tmpY;
                      ballX = tmpX1 - 4;
                      ballY = tmpY1 - 4;
                      ballDX = -ballDX;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    function gameEnd() {
      document.forms[0].BL.value = ballN;
      gameFLG = 0;
      loadFLG = 0;
      starter.style.posTop = 200;
      starter.style.posLeft = 180;
      if (blclr >= 40) {
        clrmes.style.posTop = 150;
        clrmes.style.posLeft = 160;
      }
      if (ballN <= 0) {
        ovrmes.style.posTop = 150;
        ovrmes.style.posLeft = 160;
        blclr = 40;
      }
    }
    function onLD() {
      bgIE.style.posTop = 16;
      bgIE.style.posLeft = 16;
      ball.style.posTop = 270;
      ball.style.posLeft = 209;
      racket.style.posTop = 280;
      racket.style.posLeft = 193;
      info.style.posTop = 16;
      info.style.posLeft = 432;
      starter.style.posTop = -1000;
      starter.style.posLeft = -1000;
      clrmes.style.posTop = -1000;
      clrmes.style.posLeft = -1000;
      ovrmes.style.posTop = -1000;
      ovrmes.style.posLeft = -1000;
      DN.style.posTop = 170;
      DN.style.posLeft = 432;
      for (ib = 0; ib < 5; ib++) {
        for (ia = 0; ia < 8; ia++) {
          blsta[ib * 8 + ia] = ib;
        }
      }
      starter.style.posTop = 200;
      starter.style.posLeft = 180;
    }
    function chc(bno,bcl) {
      tmpbno = ((bno < 10.5) ? "b0" : "b") + (bno-1);
      eval(tmpbno).bgColor = blcol[bcl];
    }
    // End -->
  </script>
</head>
<body onLoad="onLD();" onMouseMove='X=event.x;MouseMv();'>
<span id=info style='position:absolute'>
<pre>
<font style="font-size:24px" color="#000044"><i><b>PONG</b></i></font>
<form>
  Ball: <input type=text name=BL size=5 value=5>
  Time: <input type=text name=TM size=5 value=0>
</form>
</pre>
</span>
<script language="JavaScript">
  <!-- block image write-->
  with (document) {
    write("<table id='bgIE' width='394' height='300' bgcolor='#000000' style='position:absolute'><td></td></table>");
    write("<table id='b00' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:29'><td></td></table>");
    write("<table id='b01' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:75'><td></td></table>");
    write("<table id='b02' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:121'><td></td></table>");
    write("<table id='b03' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:167'><td></td></table>");
    write("<table id='b04' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:213'><td></td></table>");
    write("<table id='b05' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:259'><td></td></table>");
    write("<table id='b06' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:305'><td></td></table>");
    write("<table id='b07' width='42' height='16' bgcolor='#FFFF00' style='position:absolute; top:48; left:351'><td></td></table>");
    write("<table id='b08' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:29'><td></td></table>");
    write("<table id='b09' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:75'><td></td></table>");
    write("<table id='b10' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:121'><td></td></table>");
    write("<table id='b11' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:167'><td></td></table>");
    write("<table id='b12' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:213'><td></td></table>");
    write("<table id='b13' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:259'><td></td></table>");
    write("<table id='b14' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:305'><td></td></table>");
    write("<table id='b15' width='42' height='16' bgcolor='#FFCF00' style='position:absolute; top:68; left:351'><td></td></table>");
    write("<table id='b16' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:29'><td></td></table>");
    write("<table id='b17' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:75'><td></td></table>");
    write("<table id='b18' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:121'><td></td></table>");
    write("<table id='b19' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:167'><td></td></table>");
    write("<table id='b20' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:213'><td></td></table>");
    write("<table id='b21' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:259'><td></td></table>");
    write("<table id='b22' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:305'><td></td></table>");
    write("<table id='b23' width='42' height='16' bgcolor='#FF7F00' style='position:absolute; top:88; left:351'><td></td></table>");
    write("<table id='b24' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:29'><td></td></table>");
    write("<table id='b25' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:75'><td></td></table>");
    write("<table id='b26' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:121'><td></td></table>");
    write("<table id='b27' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:167'><td></td></table>");
    write("<table id='b28' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:213'><td></td></table>");
    write("<table id='b29' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:259'><td></td></table>");
    write("<table id='b30' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:305'><td></td></table>");
    write("<table id='b31' width='42' height='16' bgcolor='#FF3F00' style='position:absolute; top:108; left:351'><td></td></table>");
    write("<table id='b32' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:29'><td></td></table>");
    write("<table id='b33' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:75'><td></td></table>");
    write("<table id='b34' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:121'><td></td></table>");
    write("<table id='b35' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:167'><td></td></table>");
    write("<table id='b36' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:213'><td></td></table>");
    write("<table id='b37' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:259'><td></td></table>");
    write("<table id='b38' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:305'><td></td></table>");
    write("<table id='b39' width='42' height='16' bgcolor='#FF0000' style='position:absolute; top:128; left:351'><td></td></table>");
    write("<div id='ball' style='position:absolute'>");
    write("<table width='4' height='8' bgcolor='#B0B0B0' style='position:absolute; left:0; top:0'><td></td></table>");
    write("<table width='8' height='4' bgcolor='#B0B0B0' style='position:absolute; left:-1; top:2'><td></td></table>");
    write("<table width='4' height='4' bgcolor='#FFFFFF' style='position:absolute; left:0; top:1'><td></td></table>");
    write("</div>");
    write("<table id='racket' width='40' height='4' bgcolor='#B0B0FF' style='position:absolute'><td></td></table>");
    write("<div id='starter' style='position:absolute'><form><input type='button' value='START' ONCLICK='initG();blur()'></form></div>");
    write("<div id='clrmes' style='position:absolute'><font style='font-size:24px' color='#44CC44'>ALL CLEAR!</font></div>");
    write("<div id='ovrmes' style='position:absolute'><font style='font-size:24px' color='#CC4444'>GAME OVER!</font></div>");
    write("<div id='DN' style='position:absolute'><a href='javascript:SDN()'>SPEED DOWN</a></div>");
  }
</script>
</body>
</html>

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助~如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持!

Javascript 相关文章推荐
javascript 快速排序函数代码
May 30 Javascript
深入理解JavaScript系列(30):设计模式之外观模式详解
Mar 03 Javascript
JavaScript实现SHA-1加密算法的方法
Mar 11 Javascript
Query常用DIV操作获取和设置长度宽度的实现方法
Sep 19 Javascript
JS实现动态增加和删除li标签行的实例代码
Oct 16 Javascript
JavaScript自动点击链接 防止绕过浏览器访问的方法
Jan 19 Javascript
浅谈jquery拼接字符串效率比较高的方法
Feb 22 Javascript
Node.js设置CORS跨域请求中多域名白名单的方法
Mar 28 Javascript
Vue.js鼠标悬浮更换图片功能
May 17 Javascript
JavaScript阻止表单提交方法(附代码)
Aug 15 Javascript
XMLHttpRequest对象_Ajax异步请求重点(推荐)
Sep 28 Javascript
JavaScript满天星导航栏实现方法
Mar 08 Javascript
ReactJs实现树形结构的数据显示的组件的示例
Aug 18 #Javascript
基于 Vue 的树形选择组件的示例代码
Aug 18 #Javascript
简单实现jQuery手风琴效果
Aug 18 #jQuery
JavaScript中Hoisting详解 (变量提升与函数声明提升)
Aug 18 #Javascript
JavaScript实现旋转轮播图
Aug 18 #Javascript
JavaScript定时器setTimeout()和setInterval()详解
Aug 18 #Javascript
ECMAscript 变量作用域总结概括
Aug 18 #Javascript
You might like
php 的加密函数 md5,crypt,base64_encode 等使用介绍
2012/04/09 PHP
php中inlcude()性能对比详解
2012/09/16 PHP
如何在smarty中增加类似foreach的功能自动加载数据
2013/06/26 PHP
thinkPHP商城公告功能开发问题分析
2016/12/01 PHP
laravel自定义分页效果
2017/07/23 PHP
Extjs入门之动态加载树代码
2010/04/09 Javascript
再论Javascript下字符串连接的性能
2011/03/05 Javascript
jQuery+css实现的蓝色水平二级导航菜单效果代码
2015/09/11 Javascript
vue,angular,avalon这三种MVVM框架优缺点
2016/04/27 Javascript
基于Bootstrap的Java开发问题汇总(Spring MVC)
2017/01/15 Javascript
jq给页面添加覆盖层遮罩的实例
2017/02/16 Javascript
JavaScript和JQuery获取DIV值的方法示例
2017/03/07 Javascript
Angular 4.0学习教程之架构详解
2017/09/12 Javascript
JS实现关闭小广告特效
2021/01/29 Javascript
Postman如何实现参数化执行及断言处理
2020/07/28 Javascript
JavaScript点击按钮生成4位随机验证码
2021/01/28 Javascript
[01:01:41]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Magma BO3 第二场 1月31日
2021/03/11 DOTA
Python实现基于权重的随机数2种方法
2015/04/28 Python
轻松实现python搭建微信公众平台
2016/02/16 Python
win7上python2.7连接mysql数据库的方法
2017/01/14 Python
通过python实现弹窗广告拦截过程详解
2019/07/10 Python
使用OpenCV对车道进行实时检测的实现示例代码
2020/06/19 Python
详解Python 循环嵌套
2020/07/09 Python
基于Python爬取fofa网页端数据过程解析
2020/07/13 Python
CSS3实现的渐变幻灯片效果
2020/12/07 HTML / CSS
北美大型运动类产品商城:Champs Sports
2017/01/12 全球购物
荷兰包包购物网站:The Little Green Bag
2018/03/17 全球购物
Snapfish爱尔兰:在线照片打印和个性化照片礼品
2018/09/17 全球购物
C语言开发工程师测试题
2016/12/20 面试题
社区党总支书记先进事迹材料
2014/01/24 职场文书
出纳试用期自我鉴定
2014/04/07 职场文书
抵押贷款承诺书
2014/05/30 职场文书
励志演讲稿600字
2014/08/21 职场文书
办公室主任四风问题对照检查材料思想汇报
2014/09/28 职场文书
2015暑期社会实践通讯稿
2015/07/18 职场文书
SpringBoot连接MySQL获取数据写后端接口的操作方法
2021/11/02 MySQL