工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1979|回复: 0

【代码接龙】javascript/html/css 版俄罗斯方块

[复制链接]
发表于 2010-5-20 01:33 | 显示全部楼层 |阅读模式
RT,有兴趣动手的同学可以把你的代码帖上来。不需要一步完成。

欢迎就代码所有存在的问题拍砖


代码不求兼容所有浏览器,能用得到html5,css3更好!!不过最好能在你的代码前面定明在哪些浏览器下可运行。


以下代码只是临时想到的一个不完整的框架。在ie7 / firefox下没有脚本错误

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <script>
  6.         //工具函数
  7.         function $(id){return document.getElementById(id);}

  8.        
  9.         //变量
  10.         var brickSize=10;
  11.         var timer=null;
  12.         var interval=200;
  13.         var intervalIncreament=.8;
  14.        
  15.         // 创建关卡
  16.         function createLevel(l){
  17.                 clearScene();
  18.                 for(var i=0;i<l;++i){
  19.                         createRandomBrick(i);
  20.                 }
  21.         }
  22.        
  23.         //为每行产生随机砖块
  24.         function createRandomBrick(line){
  25.                
  26.         }
  27.        
  28.         //清空旧关卡数据
  29.         function clearScene(){
  30.                 $("container").innerHTML="";
  31.         }
  32.        
  33.         // 按键事件侦听入口
  34.         function onKeyDown(e){
  35.                 e = e?e:window.event;//ie兼容
  36.                 var keyCode = e.keyCode;
  37.                 var keyFunction = keyFunctions[keyCode];
  38.                 if(keyFunction){
  39.                         keyFunction();
  40.                 }
  41.         }
  42.        
  43.         //对应各按键的处理函数
  44.         var keyFunctions={
  45.                 '37':function(){//左
  46.                         alert('左');
  47.                 },
  48.                 '38':function(){//上
  49.                         alert('上');
  50.                 },
  51.                 '39':function(){//右
  52.                         alert('右');
  53.                 },
  54.                 '40':function(){//下
  55.                         alert('下');
  56.                 }
  57.         }
  58.        
  59.         function ticker(){
  60.                
  61.         }
  62.        
  63.         function startGameTimer(){
  64.                 timer = setInterval(ticker,interval);
  65.         }
  66.         function stopGameTimer(){
  67.                 clearInterval(timer);
  68.         }
  69.        
  70.         function startGame(level){
  71.                 level=level?level:0;
  72.                 createLevel(level);
  73.                 startGameTimer();
  74.         }
  75.        
  76.        
  77.         function onStartButtonClicked(){
  78.                 startGame(0);
  79.                 $("startButton").disabled="disable";
  80.                 $("pauseButton").disabled="";
  81.         }
  82.         function onPauseButtonClicked(){
  83.                 stopGameTimer();
  84.                 $("startButton").disabled="";
  85.                 $("pauseButton").disabled="disable";
  86.         }
  87.        
  88.         //初始化html元素
  89.         function initialDom(){
  90.                 $("startButton").disabled="";
  91.                 $("pauseButton").disabled="disable";
  92.         }
  93.        
  94.         //初始化事件绑定
  95.         function initialEvent(){
  96.                 document.onkeydown=onKeyDown;
  97.                 $("startButton").onclick=onStartButtonClicked;
  98.                 $("pauseButton").onclick=onPauseButtonClicked;
  99.         }
  100.        
  101.         window.onload=function(){
  102.                 initialDom();
  103.                 initialEvent();
  104.         };
  105. </script>
  106. <style>
  107.         .centerAlign{
  108.                 margin:0 auto; /* 有指定宽度时 */
  109.                 text-align:center;/* 无指定宽度时 */
  110.         }
  111. </style>
  112. </head>
  113. <body>
  114.         <div id="container" class="centerAlign" style="width:300px;height:400px;border:solid 1px #666;">
  115.         </div>
  116.         <div class="centerAlign">
  117.                 <button id="startButton">开始</button>
  118.                 <button id="pauseButton">暂停</button>
  119.         </div>
  120. </body>
  121. </html>
复制代码
您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-5-1 09:24

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表