|
|
[发布日期]2008-11-9 [浏览次数]589 |
|
![]() |
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>俄罗斯方块</title> <style> table { font-size:12px; color: #FFFFFF; } </style> </head> <body onkeypress="userKey();" bgcolor="#000000"> <table align="center"> <tr><td colspan="2"><br> 按下B键,开始。A-左,D-右,S-变,W-下,空格落下 <br><br></td></tr> <tr><td> <div id="showBodyDiv"></div> </td><td> <div id="showBlkDiv"></div> </td></tr> <tr><td colspan="2"><br>
<br><br></td></tr> </table> <script> var mapWidth = 10;//宽度 var mapHeight = 16;//高度
var thisType; //当前块类型 var thisCirc; //当前块旋转位置 var thisCol; //当前块颜色 var thisX; // 当前块X坐标 var thisY; // 当前块Y坐标 var nextType; //下一块类型 var nextCirc; //下一块旋转位置 var nextCol; //下一块颜色
var blkNum;//当前块数 var rowNum;//当前已消行数 var level; //当前水平
var inGame = false;//是否开始游戏 var timer; //游戏计时器
var map = new Array(mapWidth);//屏幕数据 var showMap = new Array(mapWidth);//用于显示的 var showBlk = new Array(4);
var color = new Array(7); // 颜色
for(i = 0; i < mapWidth ; i++){ map = new Array(mapHeight); showMap = new Array(mapHeight); } for(i = 0; i < 4 ; i++){ showBlk = new Array(4); } color[0] = "#ff8888"; color[1] = "#88ff88"; color[2] = "#8888ff"; color[3] = "#ffff88"; color[4] = "#ff88ff"; color[5] = "#88ffff"; color[6] = "#ffffff";
//获取坐标 function getBlk(type, n, circ, x, y){ switch(type){ case 0: return getBlk0(n, x, y); break; case 1: return getBlk1(n, circ%2, x, y); break; case 2: return getBlk2(n, circ%2, x, y); break; case 3: return getBlk3(n, circ%2, x, y); break; case 4: return getBlk4(n, circ, x, y); break; case 5: return getBlk5(n, circ, x, y); break; case 6: return getBlk6(n, circ, x, y); break; } return -101; } //获取第零类(O)坐标 function getBlk0(n, x, y){ switch(n){ case 0: return (x)*100 + (y+1); break; case 1: return (x+1)*100 + (y+1); break; case 2: return (x+1)*100 + (y); break; } return -101; } //获取第一类(I)坐标 function getBlk1(n, circ, x, y){ switch(n){ case 0: if(0 == circ){ return (x)*100 + (y+2); } else{ return (x+2)*100 + (y); } break; case 1: if(0 == circ){ return (x)*100 + (y+1); } else{ return (x+1)*100 + (y); } break; case 2: if(0 == circ){ return (x)*100 + (y-1); } else{ return (x-1)*100 + (y); } break; } return -101; } //获取第二类(Z)坐标 function getBlk2(n, circ, x, y){ switch(n){ case 0: if(0 == circ){ return (x)*100 + (y+1); } else{ return (x-1)*100 + (y); } break; case 1: if(0 == circ){ return (x-1)*100 + (y); } else{ return (x)*100 + (y-1); } break; case 2: if(0 == circ){ return (x-1)*100 + (y-1); } else{ return (x+1)*100 + (y-1); } break; } return -101; } //获取第三类(S)坐标 function getBlk3(n, circ, x, y){ switch(n){ case 0: if(0 == circ){ return (x)*100 + (y+1); } else{ return (x+1)*100 + (y); } break; case 1: if(0 == circ){ return (x+1)*100 + (y); } else{ return (x)*100 + (y-1); } break; case 2: if(0 == circ){ return (x+1)*100 + (y-1); } else{ return (x-1)*100 + (y-1); } break; } return -101; } //获取第四类(Y)坐标 function getBlk4(n, circ, x, y){ switch(circ){ case 0: if(0 == n){ return (x)*100 + (y+1); } else if(1 == n){ return (x+1)*100 + (y); } else{ return (x)*100 + (y-1); } break; case 1: if(0 == n){ return (x-1)*100 + (y); } else if(1 == n){ return (x+1)*100 + (y); } else{ return (x)*100 + (y-1); } break; case 2: if(0 == n){ return (x)*100 + (y+1); } else if(1 == n){ return (x-1)*100 + (y); } else{ return (x)*100 + (y-1); } break; case 3: if(0 == n){ return (x)*100 + (y+1); } else if(1 == n){ return (x+1)*100 + (y); } else{ return (x-1)*100 + (y); } break; } return -101; } //获取第五类(J)坐标 function getBlk5(n, circ, x, y){ switch(circ){ case 0: if(0 == n){ return (x)*100 + (y+1); } else if(1 == n){ return (x)*100 + (y-1); } else{ return (x-1)*100 + (y-1); } break; case 1: if(0 == n){ return (x+1)*100 + (y); } else if(1 == n){ return (x-1)*100 + (y); } else{ return (x-1)*100 + (y+1); } break; case 2: if(0 == n){ return (x)*100 + (y+1); } else if(1 == n){ return (x)*100 + (y-1); } else{ return (x+1)*100 + (y+1); } break; case 3: if(0 == n){ return (x+1)*100 + (y); } else if(1 == n){ return (x-1)*100 + (y); } else{ return (x+1)*100 + (y-1); } break; } return -101; } //获取第六类(L)坐标 function getBlk6(n, circ, x, y){ switch(circ){ case 0: if(0 == n){ return (x)*100 + (y+1); } else if(1 == n){ return (x)*100 + (y-1); } else{ return (x+1)*100 + (y-1); } break; case 1: if(0 == n){ return (x+1)*100 + (y); } else if(1 == n){ return (x-1)*100 + (y); } else{ return (x-1)*100 + (y-1); } break; case 2: if(0 == n){ return (x)*100 + (y+1); } else if(1 == n){ return (x)*100 + (y-1); } else{ return (x-1)*100 + (y+1); } break; case 3: if(0 == n){ return (x+1)*100 + (y); } else if(1 == n){ return (x-1)*100 + (y); } else{ return (x+1)*100 + (y+1); } break; } return -101; }
//判断移至XY处是否可行,可以返回0,顶部出界返回1,有块返回2,左右下出界返回3,顶部出界且有块返回4 function judgeMove(type, circ, x, y){ var yfk = false; var dcj = false; if(x < 0 || x >= mapWidth || y < 0){ return 3; } else if(y < mapHeight && 0 != map[x][y]){ yfk = true; } else if(y >= mapHeight){ dcj = true; } x += 10; y += 10; for(i = 0;i < 3; i++){ var n = getBlk(type, i, circ, x, y); var tx = (n/100|0) - 10; var ty = n%100 - 10; if(tx < 0 || tx >= mapWidth || ty < 0){ return 3; } else if(ty < mapHeight && 0 != map[tx][ty]){ yfk = true; } else if(ty >= mapHeight){ dcj = true; } } if(dcj && yfk){ return 4; } else if(yfk){ return 2; } else if(dcj){ return 1; } return 0; }
//左移 function moveLeft(){ if(judgeMove(thisType, thisCirc, thisX-1, thisY) < 2){ thisX--; } } //右移 function moveRight(){ if(judgeMove(thisType, thisCirc, thisX+1, thisY) < 2){ thisX++; } } //旋转 function trunBlk(){ var tc = thisCirc + 1; if(tc > 3){ tc = tc%4; } if(judgeMove(thisType, tc, thisX, thisY) < 2){ thisCirc = tc; } } //下移 function moveDown(){ var res = judgeMove(thisType, thisCirc, thisX, thisY-1); if(res < 2){ thisY--; } else if(res > 3){//游戏结束 endGame(); } else{//落到底部,换下一块 changeBlk(); } } //落到底部 function fallDown(){ var res = judgeMove(thisType, thisCirc, thisX, thisY-1); while(res < 2){ thisY--; res = judgeMove(thisType, thisCirc, thisX, thisY-1); } if(res > 3){//游戏结束 endGame(); } else{//落到底部,换下一块 changeBlk(); } } //自动下落 function autoDown(){ timer = setTimeout("autoDown();",1000 - (level*80)); moveDown(); dorwMap(); }
//开始, function beginGame(){ for(i = 0;i < mapWidth; i++){ for(j = 0;j < mapHeight; j++){ map[j] = 0; } } thisType = ((Math.random()*7)|0); thisCirc = ((Math.random()*4)|0); thisCol = ((Math.random()*7)|0)+1; thisX = mapWidth/2-1; thisY = mapHeight; nextType = ((Math.random()*7)|0); nextCirc = ((Math.random()*4)|0); nextCol = ((Math.random()*7)|0)+1; level = 0; rowNum = 0; blkNum = 1; inGame = true; dorwBlk(); dorwMap(); timer = setTimeout("autoDown();",1000 - (level*80)); } //换块处理 function changeBlk(){ //修改地图 var minY = mapHeight; var maxY = 0; if(thisY > -1 && thisY < mapHeight){ maxY = thisY; minY = thisY; if(thisX > -1 && thisX < mapWidth){ map[thisX][thisY] = thisCol; } } for(i = 0;i < 3; i++){ var n = getBlk(thisType, i, thisCirc, thisX, thisY); var tx = (n/100|0); var ty = n%100; if(ty > -1 && ty < mapHeight){ if(ty < minY){ minY = ty; } if(ty > maxY){ maxY = ty; } if(tx > -1 && tx < mapWidth){ map[tx][ty] = thisCol; } } } //减去完整行 var thisRow = 0;//本次减少的行数 var cy = minY;//当前正在统计行的Y坐标 var ry = minY;//当前应替换行的Y坐标 var j = 0; for(i = minY;i <= maxY; i++){ for(j = 0;j < mapWidth; j++){ if(0 == map[j]){ break; } } if(j < mapWidth){ if(cy != ry){ for(j = 0;j < mapWidth; j++){ map[j][ry] = map[j][cy]; } } cy++; ry++; } else{ thisRow++; //完整行 cy++; //统计行增加 } } if(thisRow > 0){ for(;cy < mapHeight; cy++,ry++){ for(j = 0;j < mapWidth; j++){ map[j][ry] = map[j][cy]; } } for(;ry < mapHeight; ry++){ for(j = 0;j < mapWidth; j++){ map[j][ry] = 0; } } rowNum += thisRow; if(level < 11){ level = rowNum/100|0; } } //生成下一块 thisType = nextType; thisCirc = nextCirc; thisCol = nextCol; thisX = mapWidth/2-1; thisY = mapHeight; blkNum++; nextType = ((Math.random()*7)|0); nextCirc = ((Math.random()*4)|0); nextCol = ((Math.random()*7)|0)+1; dorwBlk(); } //结束游戏................... function endGame(){ clearTimeout(timer); inGame = false; alert("游戏结束!"); }
//绘制游戏区 function dorwMap(){ for(i = 0;i < mapHeight; i++){ for(j = 0;j < mapWidth; j++){ showMap[j] = map[j]; } } if(thisX > -1 && thisX < mapWidth && thisY > -1 && thisY < mapHeight){ showMap[thisX][thisY] = thisCol; } for(i = 0;i < 3; i++){ var n = getBlk(thisType, i, thisCirc, thisX, thisY); var tx = (n/100|0); var ty = n%100; if(tx > -1 && tx < mapWidth && ty > -1 && ty < mapHeight){ showMap[tx][ty] = thisCol; } } var s = "<TABLE border='0' cellpadding='0' cellspacing='1' bgcolor='#444444'>"; for(i = mapHeight-1;i >= 0; i--){ s = s + "<TR height='18'>"; for(j = 0;j < mapWidth; j++){ if(0 == showMap[j]){ s = s + "<TD width='18' bgcolor='#000000'></TD>"; } else{ s = s + "<TD width='18' bgcolor='" + color[showMap[j]-1] + "'></TD>"; } } s = s + "</TR>" } s = s + "</TABLE>"; showBodyDiv.innerHTML = s; }
//绘制下一方块 function dorwBlk(){ for(i = 0;i < 4; i++){ for(j = 0;j < 4; j++){ showBlk[j] = 0; } } showBlk[1][1] = nextCol; for(i = 0;i < 3; i++){ var n = getBlk(nextType, i, nextCirc, 1, 1); showBlk[(n/100|0)][n%100] = nextCol; } var s = " Next:<TABLE border='0' cellpadding='0' cellspacing='1' bgcolor='#000000'>"; for(i = 3;i >= 0; i--){ s = s + "<TR height='18'>"; for(j = 0;j < 4; j++){ if(0 == showBlk[j]){ s = s + "<TD width='18' bgcolor='#000000'></TD>"; } else{ s = s + "<TD width='18' bgcolor='" + color[showBlk[j]-1] + "'></TD>"; } } s = s + "</TR>" } s = s + "</TABLE><br><TABLE>"; s = s + "<TR><TD>块数:" + blkNum + "</TD></TR>"; s = s + "<TR><TD>行数:" + rowNum + "</TD></TR>"; s = s + "<TR><TD>速度:" + level + "</TD></TR>"; s = s + "</TABLE><br><br><br><br><br><br>"; showBlkDiv.innerHTML = s; }
//用户按下操作键 function userKey(){ if(inGame){ switch(event.keyCode){ case 32://空格键 fallDown(); break; case 87://'W'键,->上 case 119://'s'键,->上 trunBlk(); break; case 83://'S'键,->下 case 115://'s'键,->下 moveDown(); break; case 65://'A'键,->左 case 97://'a'键,->左 moveLeft(); break; case 68://'D'键,->右 case 100://'d'键,->右 moveRight(); break; case 80://'P'键 case 112://'p'键 break; case 13://回车键 break; } dorwMap(); } else{ if(98 == event.keyCode || 66 == event.keyCode){ beginGame(); } } } for(i = 0;i < mapWidth; i++){ for(j = 0;j < mapHeight; j++){ map[j] = 0; } } dorwMap(); </script> </body> </html> |
www.51xueshu.com |
|
![]() |
|
|
|
|
代写论文网提供最专业的论文服务,凭借深厚的专业知识和丰富的写作经验,为您提供一个专业的论文服务平台。
代写承诺:我们不会在未提供给您关于您课题任何资料和思路前收取任何定金,本站会给你一个关于您课题满意的规划和思路。在您接受本站服务的过程中,我们负责给您提供服务直至您满意为止,包括后期免费修改、免费指导答辩等,让您知道所以然。 |
|
(一) 组成人员实力强,学术层次高,不仅仅提供一篇论文,而是一个论文服务,来更合适你自己的背景和情况。
(二) 专业性强,提供的专业论文不是空谈泛谈,不空洞,更切合您学科发展方向。
(三) 论文原创性强,不是组合,拼凑,剽窃而来,是思想和你的背景结合而来。
(四) 时间响应快,适合处理急活、难活。 |
|
|