`
liuwei_blog
  • 浏览: 94883 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

javascript 做的俄罗斯方块

阅读更多

在win2003 IE6 和 火狐下无法运行

 

<html>
<head>
<title>俄罗斯方块</title>
<meta content="text/html; charset=gb2312" http-equiv=Content-Type>
</head>
<body>
<style>BODY {
BACKGROUND: black; MARGIN: 0px
}
TD {
COLOR: #ffffff; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; HEIGHT: 20px; WIDTH: 20px
}
#block_div {
POSITION: absolute; WIDTH: 80px; Z-INDEX: 1
}
#table_div {
POSITION: absolute; WIDTH: 320px
}
#nblock_div {
COLOR: red; FONT: 48px system; POSITION: absolute; Z-INDEX: 2
}
#title_div {
COLOR: white; FONT-SIZE: 14px; LEFT: 326px; POSITION: absolute; TOP: 7px
}
#infobar_div {
LEFT: 5px; POSITION: absolute; TOP: 460px
}
#infobar2_div {
COLOR: white; FONT-SIZE: 14px; LEFT: 396px; POSITION: absolute; TOP: 480px
}
#infobar2_div A {
COLOR: #99ccff; TEXT-DECORATION: none
}
</style>
<script id=clientEventHandlersJS language=JavaScript>
<!--
top.window.moveTo(0, 0);
/*
Tetris Game by Xie Yong (nsxy@163.net)
Modifications by DynamicDrive.com for interface enhancements
For this script, visit http://www.dynamicdrive.com
*/
if (document.all){
var gameInterval="";
var gamestart=0;
var n_width = 800;
var n_height =640;
var n_left = Math.round( screen.width/2 ) - n_width/2;
var n_top = Math.round( screen.height/2 ) - n_height/2;
var n_IncStep = 20;
var curBlcok,nextBlock;
var arr_curBlock = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var curX,curY;
var speed=1;
var maxspeed=9;
var clr_per_line=18;
var pause=0;
var gameover=0;
var colors = new Array("#999999","#0000FF","#80FFFF","#80FF80","#FFFF00","#FF8000","#FF00FF","#FF0000"
);
var cid;
var ncid;
var blocks = new Array("tt_O1","tt_T2","tt_Z1","tt_S1","tt_L1","tt_J1","tt_I2");
var bid;
var killedlines=0;
var tt_O1 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
var tt_O2 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
var tt_O3 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
var tt_O4 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
var tt_T1 = new Array(0,0,0,0,0,1,0,0,1,1,0,0,0,1,0,0);
var tt_T2 = new Array(0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0);
var tt_T3 = new Array(0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0);
var tt_T4 = new Array(0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0);
var tt_Z1 = new Array(0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0);
var tt_Z2 = new Array(0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0);
var tt_Z3 = new Array(0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0);
var tt_Z4 = new Array(0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0);
var tt_S1 = new Array(0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0);
var tt_S2 = new Array(0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0);
var tt_S3 = new Array(0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0);
var tt_S4 = new Array(0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0);
var tt_L1 = new Array(0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0);
var tt_L2 = new Array(0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0);
var tt_L3 = new Array(0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0);
var tt_L4 = new Array(0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0);
var tt_J1 = new Array(0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0);
var tt_J2 = new Array(0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0);
var tt_J3 = new Array(0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0);
var tt_J4 = new Array(0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0);
var tt_I1 = new Array(0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0);
var tt_I2 = new Array(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1);
var tt_I3 = new Array(0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0);
var tt_I4 = new Array(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1);
var table = new Array(
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
}
else
alert("You need IE4+ to play Tetris!")
function dimension2(row,col,num){
var i = row * num + col;
return (this[i]);
}
Array.prototype.getd = dimension2;
function ShowBlock(x,y,block_type,color){
for (var i=0;i<block_tbl.rows.length;i++){
for (var j=0;j<block_tbl.rows(i).cells.length;j++){
var d2 = i * 4 + j;
if (block_type[d2]==1){
block_tbl.rows(i).cells(j).style.background = color;
}
else{
block_tbl.rows(i).cells(j).style.background = "";
}
}
}
block_div.style.pixelLeft=x;
block_div.style.pixelTop=y;
}
function ShowBlock1(x,y,block_type,color,obj_tabID,obj_divID){
for (var i=0;i<obj_tabID.rows.length;i++){
for (var j=0;j<obj_tabID.rows(i).cells.length;j++){
var d2 = i * 4 + j;
if (block_type[d2]==1){
obj_tabID.rows(i).cells(j).style.background = color;
}
else{
obj_tabID.rows(i).cells(j).style.background = "";
}
}
}
obj_divID.style.pixelLeft=x;
obj_divID.style.pixelTop=y;
}
function Change(inc){
var type = curBlock.substr(0,4);
var num = curBlock.substr(curBlock.length-1);
num = parseInt(num) + inc;
if (num>4||num<1) num -= 4*inc;
type += num;
eval("arr_tmp = " + type + ";");
if (CanMove(curX,curY,arr_tmp)){
curBlock = type;
eval("arr_curBlock = " + curBlock + ";");
eval("ShowBlock(block_div.style.pixelLeft,block_div.style.pixelTop," + curBlock + ",colors[" + cid + "]);");
}
}
function CanMove(x,y,block){
for (i=0;i<4;i++){
for (j=0;j<4;j++){
if (block.getd(i,j,4)&table.getd(y/n_IncStep+i,x/n_IncStep+j,16)) return false;
}
}
return true;
}
function NewBlock(){
curBlock = nextBlock;
cid = ncid;
bid = Math.round(Math.random()*(blocks.length-1));
ncid = Math.round(Math.random()*(colors.length-1));
nextBlock = blocks[bid];
eval("arr_curBlock = " + curBlock + ";");
eval("ShowBlock(120,0," + curBlock + ",colors[" + cid + "]);");
eval("arr_curBlock = " + curBlock + ";");
eval("ShowBlock1(340,170," + nextBlock + ",colors[" + ncid + "],nblock_tbl,nblock_div);");
}
function SaveBlock(){
for (i=0;i<4;i++){
for (j=0;j<4;j++){
table[(curY/n_IncStep+i)*16+curX/n_IncStep+j]|=arr_curBlock[i*4+j];
if (arr_curBlock[i*4+j]==1)
if ((curY/n_IncStep+i<21)&&(curX/n_IncStep+j>1)&&(curX/n_IncStep+j<14))
table_tbl.rows(curY/n_IncStep+i).cells(curX/n_IncStep+j).style.background = colors[cid];
if (table[(curY/n_IncStep+i)*16+curX/n_IncStep+j]!=1)
table_tbl.rows(curY/n_IncStep+i).cells(curX/n_IncStep+j).style.background = "black";
}
}
}
function DelLine(line){
for(i=line;i>0;i--){
for(j=2;j<14;j++){
table[i*16+j]=table[(i-1)*16+j];
}
}
table_tbl.deleteRow(line);
table_tbl.insertRow(0);
for (i=0;i<16;i++){
table_tbl.rows(0).insertCell();
if (i<2||i>13) table_tbl.rows(0).cells(i).style.background="navy";
}
killedlines++;
cll.innerText=parseInt(cll.innerText)+1;
}
function DelLines(){
var c,d,i,j;
d=0;
curY=block_div.style.pixelTop;
for(i=(curY/20+3);i>curY/20-1;i--){
c=0;
for(j=2;j<14;j++){
if (isNaN(table[i*16+j])||i==21) break;
c+=table[i*16+j];
}
if(c==12){
DelLine(i);
i++;
d++;
}
}
if (d>0)
sco.innerText=parseInt(sco.innerText)+d*d*36;
}
function Lucifer(){
for(var i=2;i<14;i++){
if (table[16+i]==1) return true;
}
return false;
}
function GameOver(){
gameover=1;
clearInterval(gameInterval);
block_div.innerHTML="";
for (i=0;i<21;i++){
for (j=2;j<14;j++){
setTimeout("table_tbl.rows(" + i + ").cells(" + j + ").style.background = colors[Math.round(Math.random()*7)];",16*i*j);
}
}
nblock_div.innerHTML = "游戏结束<br><br><font size=2> <input type=button onclick='javascript:location.href=location.href' value='再玩一次'></font>";
}

function document_onkeydown() {
if (gameover==1) return;
with (block_div.style){
curX = pixelLeft;
curY = pixelTop;
switch (event.keyCode){
case 37:
if(pause==1||gamestart==0){break;}
if (CanMove(curX-n_IncStep,curY,arr_curBlock))
pixelLeft-=n_IncStep;
break;
case 38:
if(pause==1||gamestart==0){break;}
Change(1);
break;
case 39:
if(pause==1||gamestart==0){break;}
if (CanMove(curX+n_IncStep,curY,arr_curBlock))
pixelLeft+=n_IncStep;
break;
case 40:
if(pause==1||gamestart==0){break;}
if (CanMove(curX,curY+n_IncStep,arr_curBlock)){
pixelTop+=n_IncStep;
}
else{
SaveBlock();
DelLines();
if (Lucifer()){
GameOver();
return;
}
sco.innerText=parseInt(sco.innerText)+2;
NewBlock();
}
break;
case 32:
if (pause==0){
if (gameInterval==""){break}
clearInterval(gameInterval);
pause=1;
}
else{
gameInterval=window.setInterval("Handle_Interval()",(maxspeed-speed+1)*60);
pause=0;
}
break;
case 90:
if(pause==1||gamestart==0){break;}
Change(1);
break;
case 88:
if(pause==1||gamestart==0){break;}
Change(-1);
break;
default:
}
}
}
function Handle_Interval(){
curX = block_div.style.pixelLeft;
curY = block_div.style.pixelTop;
if (CanMove(curX,curY+n_IncStep,arr_curBlock)){
block_div.style.pixelTop+=n_IncStep;
}
else{
SaveBlock();
DelLines();
if (Lucifer()){
GameOver();
return;
}
sco.innerText=parseInt(sco.innerText)+2
NewBlock();
}
if (killedlines>=clr_per_line){
killedlines-=clr_per_line;
if (speed<maxspeed)
speed++;
else
speed=maxspeed;
spd.innerText=speed;
clearInterval(gameInterval);
gameInterval=window.setInterval("Handle_Interval()",(maxspeed-speed+1)*60);
}
}
//-->
</script>
<script event=onkeydown for=document language=javascript>
<!--
if (document.all)
document_onkeydown()
//-->
</script>
<div id=block_div style="LEFT: 60px; TOP: 0px">
<table border=0 cellpadding=0 cellspacing=0 id=block_tbl>
<script>
if (document.all){
for (var i=0;i<4;i++){
document.write("<tr>");
for (var j=0;j<4;j++){
document.write("<td style=\"border:1 solid black;\"></td>");
}
document.write("</tr>");
}
}
</script>
<tbody></tbody>
</table>
</div>
<div id=nblock_div>
<table border=0 cellpadding=0 cellspacing=0 id=nblock_tbl>
<script>
if (document.all){
for (var i=0;i<4;i++){
document.write("<tr>");
for (var j=0;j<4;j++){
document.write("<td style=\"height:40;width:40;border:1 outset black;\"></td>");
}
document.write("</tr>");
}
}
</script>
<tbody></tbody>
</table>
</div>
<div id=table_div>
<table border=0 cellpadding=0 cellspacing=0 id=table_tbl>
<script>
if (document.all){
for (var i=0;i<22;i++){
document.write("<tr>");
for (var j=0;j<16;j++){
var d2 = i * 16 + j;
if (table[d2]==1)
document.write("<td bgcolor=navy></td>");
else
document.write("<td style=\"background:black;\"></td>");
}
document.write("</tr>");
}
}
</script>
<tbody></tbody>
</table>
</div>
<div id=title_div noWrap><br>
请输入数字(1-9)<br>
<br>
起始速度:
<input id=speedin size=8 value=1>
<button id=but onClick=begintet()>开始</button><br>
<br>
<br>
<font color=999999>下一块:</font> <br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
说明:<br>
<br>
<font color=999900> ↑ :改变形状 ← :向左移动<br>
→ :向右移动 ↓ :向下移动<br>
<br>
空格键:暂停/继续</font> <br>
<br>
<font color=009900>=======</font> </div>
<div id=infobar_div>
<table border=1 bordercolor=navy cellpadding=0 cellspacing=0>
<tbody>
<tr align=middle>
<td style="COLOR: #99ccff; FONT: 12px system; WIDTH: 56px"><font
size=2>速度</font>:</td>
<td id=spd style="COLOR: red; FONT: 12px system">1</td>
<td style="COLOR: #99ccff; FONT: 12px system; WIDTH: 86px">总分:</td>
<td id=sco style="COLOR: red; FONT: 12px system">0</td>
<td style="COLOR: #99ccff; FONT: 12px system; WIDTH: 96px">成绩:</td>
<td id=cll
style="COLOR: red; FONT: 12px system">0</td>
</tr>
</tbody>
</table>
</div>
<div id=infobar2_div></div>
<script id=MainSection language=javascript>
<!--
if (document.all){
ncid = Math.round(Math.random()*(colors.length-1));
bid = Math.round(Math.random()*(blocks.length-1));
nextBlock = blocks[bid];
NewBlock();
}
function begintet(){
gamestart=1;
document.all.speedin.disabled=true;
document.all.but.disabled=true;
speed=parseInt(document.all.speedin.value);
if (isNaN(speed)||speed==null||speed>maxspeed||speed<1) speed=1;
spd.innerText=speed;
gameInterval=window.setInterval("Handle_Interval()",(maxspeed-speed+1)*60);
}
//-->
</script>
<div id='dh' style="position:absolute; left:-800px; top:-800px; width:0px; height:0px; z-index:1">
<input type=button value='-' name='goppp' onClick='Javascript:window.close();' accesskey='q'>
</div>
</body>
</html>

 

分享到:
评论

相关推荐

    javascript俄罗斯方块.zip

    【JavaScript俄罗斯方块游戏开发详解】 在编程世界中,JavaScript是一种广泛应用的脚本语言,尤其在Web开发领域,它扮演着至关重要的角色。利用JavaScript,我们可以实现许多有趣的交互功能,其中之一就是开发游戏...

    Javascript实现俄罗斯方块

    《JavaScript实现俄罗斯方块》 在编程世界里,游戏开发是一项既能锻炼逻辑思维,又能提升技术实践能力的有趣任务。本文将围绕“JavaScript实现俄罗斯方块”这一主题,深入探讨如何利用JavaScript、CSS和HTML这三种...

    javaScript版俄罗斯方块

    JavaScript版的俄罗斯方块是一款基于Web的休闲游戏,利用了JavaScript这一广泛应用于网页开发的脚本语言。在本文中,我们将深入探讨JavaScript编程基础、HTML布局、CSS样式以及如何将这些技术结合实现俄罗斯方块的...

    JavaScript 版俄罗斯方块

    JavaScript 版本的俄罗斯方块是一款使用Web技术实现的经典游戏,它主要利用了JavaScript语言的事件处理、对象和类的概念来构建。以下是关于这个项目的一些关键知识点和详细说明: 1. **JavaScript**: 作为主要编程...

    用JavaScript编写的俄罗斯方块游戏

    **JavaScript编写的俄罗斯方块游戏** 在编程教育中,通过创建实际的游戏项目,如俄罗斯方块,可以帮助学生更好地理解和掌握编程概念。这个项目利用JavaScript语言,一个广泛应用于网页开发的脚本语言,来实现经典的...

    JavaScript实现俄罗斯方块.rar

    JavaScript实现的俄罗斯方块是一款基于浏览器的经典游戏,它利用了JavaScript这门强大的客户端脚本语言,为用户在网页上提供了一种重温经典游戏的方式。在本文中,我们将深入探讨JavaScript如何构建这样的游戏,并...

    纯javascript写的俄罗斯方块

    javascript写的俄罗斯方块,简单易学,一看就懂

    最短的javascript编写俄罗斯方块仅仅60行代码

    史上最短小精悍的javascript编写的俄罗斯方块游戏,仅仅60行代码

    javascript编写的俄罗斯方块

    javascript编写的俄罗斯方块 可以设置等级,宽度等相关属性

    Javascript俄罗斯方块AI版

    **JavaScript俄罗斯方块AI版**是一款基于Web的电子游戏,由JavaScript编程语言实现,它将传统的俄罗斯方块游戏与人工智能技术结合在一起,为玩家提供了一种全新的体验。在这个版本中,用户可以选择不同的游戏模式,...

    javascript俄罗斯方块源码

    JavaScript俄罗斯方块是一款基于网页的经典游戏,通过JavaScript编程语言实现。这款游戏的核心原理是利用二维数组来表示游戏板,以及各种形状的方块。在JavaScript中,我们通常会使用DOM(Document Object Model)来...

    Javascript俄罗斯方块,极简代码

    一个完整的Javascript的俄罗斯方块游戏 ,代码精简,只有150行。

    Javascript版俄罗斯方块(含Bug)

    俄罗斯方块是大家所熟知的经典游戏,本着试试看的态度,我完成了这个Javascript版,不过美中不足的是在游戏的过程中有可能会出现错误,游戏的时间越长,错误出现的概率越大,针对这个Bug,我无能为力。希望有高手...

    JavaScript技术:俄罗斯方块WEB版

    JavaScript技术:俄罗斯方块WEB版

    Javascript俄罗斯方块(canvas版)

    本项目“Javascript俄罗斯方块(canvas版)”就是利用JavaScript的强大力量,结合HTML5的Canvas元素,实现了经典游戏——俄罗斯方块。Canvas是HTML5新增的绘图工具,允许开发者在网页上进行图形绘制,为网页游戏开发...

    俄罗斯方块(javascript)

    一个用javascript实现的俄罗斯方块

    可以自动玩的JavaScript俄罗斯方块游戏

    参考网络资源,使JavaScript实现的俄罗斯方块游戏,加入C程序自动求解的功能,初步实现了自动求解的算法。

    javascript版俄罗斯方块

    javascript版俄罗斯方块 注释完整!

    纯javascript经典俄罗斯方块小游戏

    在这个“纯javascript经典俄罗斯方块小游戏”中,HTML5库并未直接提及,但我们可以推测,开发者可能利用了HTML5的一些特性来实现游戏界面和交互。 首先,HTML5的`&lt;canvas&gt;`元素被广泛用于创建动态图形,它允许通过...

    多库俄罗斯方块JavaScript版本

    《多库俄罗斯方块JavaScript版本实现详解》 在信息技术领域,游戏开发一直是吸引人们注意力的重要领域,而作为经典中的经典,俄罗斯方块以其简洁规则和无穷挑战性深受玩家喜爱。在现代网页技术的支持下,我们可以...

Global site tag (gtag.js) - Google Analytics