html部分
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>
</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="game" class="game"></div>
<div id="next" class="next"></div>
<div id="button">
<ul>
<li><button class="fal">落</button></li>
<li><button class="top">上</button></li>
<li><button id="chong">重</button></li>
</ul>
<ul>
<li><button class="left">左</button></li>
<li><button class="down">下</button></li>
<li><button class="right">右</button></li>
</ul>
</div>
<div class="info">
<div>已用时<span id="time">0s</span></div>
<div>已得分<span id="score">0</span></div>
</div>
<script src="js/game.js"></script>
<script src="js/基本方法.js"></script>
<script src="js/特色方法.js"></script>
<script src="js/方法.js"></script>
<script>
var local=new Local();
document.getElementById("chong").onclick=function (){
local.start();
}
local.start();
</script>
</body>
</html>
css部分
style.css
.game{
width: 200px;
height: 400px;
background-color: #F2FAFF;
border-left: 1px solid blue;
border-right: 1px solid blue;
border-bottom: 1px solid blue;
position: absolute;
top: 150px;
left: 710px;
transform: scale(1.5, 1.5);
}
.next{
width: 80px;
height: 80px;
background-color: #F2FAFF;
position: absolute;
top: 70px;
left: 1050px;
border: 1px solid blue;
transform: scale(1.5, 1.5);
}
.info{
position: absolute;
top: 200px;
left: 1050px;
transform: scale(1.5, 1.5);
}
.none,.current,.done{
width: 20px;
height: 20px;
position: absolute;
box-sizing: border-box;
}
.none{
background-color: #F2FAFF;
}
.current{
background-color: red;
border: 1px solid black;
}
.done{
background-color: blue;
border:1px solid black;
}
#button{
position: absolute;
width: 200px;
height: 150px;
left: 1000px;
top:550px;
transform: scale(1.5, 1.5);
}
button{
width: 30px;
height: 30px;
}
li{
display: inline-block;
}
@media screen and (max-width: 768px){
.game{
width: 200px;
height: 400px;
background-color: #F2FAFF;
border-left: 1px solid blue;
border-right: 1px solid blue;
border-bottom: 1px solid blue;
position: absolute;
top: 10px;
left: 10px;
transform: scale(1, 1);
}
.next{
width: 80px;
height: 80px;
background-color: #F2FAFF;
position: absolute;
top: 10px;
left: 250px;
border: 1px solid blue;
transform: scale(1, 1);
}
.info{
width:100px;
height: 100px;
position: absolute;
top: 100px;
left: 250px;
transform: scale(1, 1);
}
.none,.current,.done{
width: 20px;
height: 20px;
position: absolute;
box-sizing: border-box;
}
.none{
background-color: #F2FAFF;
}
.current{
background-color: red;
border: 1px solid black;
}
.done{
background-color: blue;
border:1px solid black;
}
#button{
position: absolute;
width: 200px;
height: 150px;
left: 200px;
top:300px;
transform: scale(1, 1);
}
button{
width: 30px;
height: 30px;
}
li{
display: inline-block;
}
}
js部分
Local.js
var Local=function (){
//game对象
var game;
var timer=null;
//时间
var time = 0;
//得分
var fenshu=0;
//键盘事件和点击事件
var bindKeyEvent=function (){
document.onmousedown=function (e){
tname=e.target.className;
if(tname=="top"){
game.rotate();
}
else if(tname=="left"){
game.left();
}
else if(tname=="right"){
game.right();
}
else if(tname=="down"){
game.down();
}
else if(tname=="fal"){
game.fal();
}
}
document.onkeydown=function (e){
if(e.keyCode==39){//右
game.right();
}
else if(e.keyCode==40){//下
game.down();
}
else if(e.keyCode==37){//左
game.left();
}
else if(e.keyCode==38){
game.rotate();
}
else if(e.keyCode==32){//空格
game.fal();
}
}
}
//方块运动函数
var move=function (){
time+=0.5;
if(time%1==0){
document.getElementById("time").innerHTML=time+"s";
}
if(!game.down()){
game.guding();
fenshu=game.xiaohang();
if(fenshu){
document.getElementById("score").innerHTML=fenshu;
}
if(game.jieshu()){
clearInterval(timer);
alert("你挂了");
return;
}
game.xiayige(Math.ceil(Math.random()*7-1),Math.ceil(Math.random()*4-1));
}
}
//开始
this.start=function (){
clearInterval(timer);
time=0;
document.getElementById("time").innerHTML=time+"s";
var doms={
gameDiv:document.getElementById("game"),
nextDiv:document.getElementById("next")
}
//创建game对象
game=new Game();
//初始化整个界面并生成第一个方块;
game.init(doms);
bindKeyEvent();
timer=setInterval(move,500)
}
}
game.js
var Game=function (){
//dom
var gameDiv;
//dom
var nextDiv;
//得分
var line=0;
//矩阵
var gameDate=[
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
]
//当前方块
var cur;
//下一个方块
var next;
//以二维矩阵的形式保存每个div小格
var nextDivs=[];
//以二维矩阵的形式保存每个div小格
var gameDivs=[];
//初始化dom,矩阵,div矩阵
var initDiv=function (container,date,divs){
for(i=0;i<date.length;i++){
var div=[];
for(var j=0;j<date[0].length;j++){
var newNode=document.createElement("div");
newNode.className="none";
newNode.style.top=(i*20)+"px";
newNode.style.left=(j*20)+"px";
container.appendChild(newNode);
div.push(newNode);
}
divs.push(div);
}
}
//刷新矩阵中的数据并渲染dom
var refreshDiv=function (date,divs){
for(i=0;i<date.length;i++){
for(j=0;j<date[0].length;j++){
if(date[i][j]==0){
divs[i][j].className="none"
}
else if(date[i][j]==1){
divs[i][j].className="done"
}
else if(date[i][j]==2){
divs[i][j].className="current"
}
}
}
}
//判断方块的是否能移动
var jiance=function (pos,x,y){
if(pos.x+x<0){
return false;
}
else if(pos.x+x>=gameDate.length){
return false;
}
else if(pos.y+y<0){
return false;
}
else if(pos.y+y>=gameDate[0].length){
return false;
}
else if(gameDate[pos.x+x][pos.y+y]==1){
return false;
}
else{
return true;
}
}
//判断方块将要做的运动是否合法
var jiancehefa=function (pos,date){
for(i=0;i<date.length;i++){
for(j=0;j<date[0].length;j++){
if(date[i][j]!=0){//只检测不是0的部分。
if(!jiance(pos,i,j)){
return false;
}
}
}
}
return true;
}
//dom刷新为0
var clearDate=function (){
for(i=0;i<cur.date.length;i++){
for(j=0;j<cur.date[0].length;j++){
if(jiance(cur.origin,i,j)){
gameDate[cur.origin.x+i][cur.origin.y+j]=0;
}
}
}
}
//将方块的值写入dom中
var setDate=function (){
for(i=0;i<cur.date.length;i++){
for(j=0;j<cur.date.length;j++){
if(jiance(cur.origin,i,j)){//这里做检测,就是让那些已经出格的方块不考虑了。
gameDate[i+cur.origin.x][j+cur.origin.y]=cur.date[i][j];
}
}
}
}
//下落
this.down=function (){
if(cur.canDown(jiancehefa)){
clearDate();
cur.down();
setDate();
refreshDiv(gameDate,gameDivs);
return true;
}
else
return false;
}
//左移
this.left=function (){
if(cur.canLeft(jiancehefa)){
clearDate();
cur.left();
setDate();
refreshDiv(gameDate,gameDivs);
}
}
//右移
this.right=function (){
if(cur.canRight(jiancehefa)){
clearDate();
cur.right();
setDate();
refreshDiv(gameDate,gameDivs);
}
}
//旋转
this.rotate=function (){
if(cur.canRotate(jiancehefa)){
clearDate();
cur.rotate();
setDate();
refreshDiv(gameDate,gameDivs);
}
}
//落到底
this.fal=function (){
while(this.down());//不能下降时返回false,跳出循环。
}
//方块固定到底端,即赋值为1;
this.guding=function (){
for(i=0;i<cur.date.length;i++){
for(j=0;j<cur.date[0].length;j++){
if(jiance(cur.origin,i,j)){
if(gameDate[cur.origin.x+i][cur.origin.y+j]==2) {
gameDate[cur.origin.x + i][cur.origin.y + j] = 1;
}
}
}
}
refreshDiv(gameDate,gameDivs)
}
//生成下一个方块
this.xiayige=function (zhonglei,fangxiang){
cur=next;
setDate();
next=fangkuai.prototype.make(zhonglei,fangxiang);
refreshDiv(gameDate,gameDivs);
refreshDiv(next.date,nextDivs);
}
//消掉一行
this.xiaohang=function (){
for(var i=gameDate.length-1;i>=0;i--){
var flag=0;
for(var j=0;j<gameDate[0].length;j++){
if(gameDate[i][j]==0){
flag=1;
break;
}
}
if(flag==0) {
line += 1;
for (m = i; m >= 0; m--) {
for (n = 0; n < gameDate[0].length; n++) {
gameDate[m][n] = gameDate[m - 1][n];
gameDate[0][n] = 0;
}
}
i++;
}
}
return line;
}
//判断结束
this.jieshu=function (){
var flag=0;
for(i=0;i<gameDate[0].length;i++){
if(gameDate[0][i]==1){
flag=1;
}
}
return flag;
}
//入口,从这里开始调用初始化函数完成初始化
this.init=function (doms) {
document.getElementById("score").innerHTML=0;
gameDiv = doms.gameDiv;
nextDiv = doms.nextDiv;
cur = new fangkuai.prototype.make(Math.ceil(Math.random()*7-1),Math.ceil(Math.random()*4-1));
next = new fangkuai.prototype.make(Math.ceil(Math.random()*7-1),Math.ceil(Math.random()*4-1));
initDiv(gameDiv, gameDate, gameDivs);//初始化界面
initDiv(nextDiv, next.date, nextDivs);//初始化界面
setDate();
refreshDiv(gameDate, gameDivs);//刷新界面
refreshDiv(next.date, nextDivs);//刷新界面
};
}
方块.js
var square=function (){
//方块的数据矩阵
this.date=[
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
]
//原点
this.origin={
x:0,
y:0
}
//方块的形态
this.fangxiang=0;
}
//原型方法,检测能否旋转
square.prototype.canRotate=function (jiancehefa){
var t=this.fangxiang+1;
if(t==4){
t=0;
}
var test=[
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
];
for(i=0;i<this.date.length;i++){
for(j=0;j<this.date[0].length;j++){
test[i][j]=this.rotates[t][i][j];
}
}
return jiancehefa(this.origin,test);
}
//原型方法,检测能否下降
square.prototype.canDown=function (jiancehefa){
var test={};
test.x=this.origin.x+1;
test.y=this.origin.y;
return jiancehefa(test,this.date);
}
//原型方法,检测能否左移
square.prototype.canLeft=function (jiancehefa){
var test={};
test.x=this.origin.x;
test.y=this.origin.y-1;
return jiancehefa(test,this.date);
}
//原型方法,检测能否右移
square.prototype.canRight=function (jiancehefa){
var test={};
test.x=this.origin.x;
test.y=this.origin.y+1;
return jiancehefa(test,this.date);
}//原型方法,下降
square.prototype.down=function (){
this.origin.x+=1;
}//原型方法,左移
square.prototype.left=function (){
this.origin.y-=1;
}//原型方法,右移
square.prototype.right=function (){
this.origin.y+=1;
}//原型方法,旋转
square.prototype.rotate=function (num){
if(!num) num=1;
this.fangxiang=(this.fangxiang+num)%4;
if(this.fangxiang==4){
this.fangxiang=0;
}
for(i=0;i<this.date.length;i++){
for(j=0;j<this.date[0].length;j++){
this.date[i][j]=this.rotates[this.fangxiang][i][j];
}
}
}
各个方块.js
var square1=function (){
//调用square方法
square.call(this);
this.rotates=[
[
[0,2,0,0],
[0,2,0,0],
[0,2,0,0],
[0,2,0,0]
],
[
[0,0,0,0],
[2,2,2,2],
[0,0,0,0],
[0,0,0,0]
],
[
[0,2,0,0],
[0,2,0,0],
[0,2,0,0],
[0,2,0,0]
],
[
[0,0,0,0],
[2,2,2,2],
[0,0,0,0],
[0,0,0,0]
]
]
}
square1.prototype=square.prototype;
var square2=function (){
square.call(this);
this.fangxiang=0;
this.rotates=[
[
[0,2,0,0],
[2,2,2,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,0,0,0],
[2,2,0,0],
[2,0,0,0],
[0,0,0,0]
],
[
[2,2,2,0],
[0,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[0,2,0,0],
[2,2,0,0],
[0,2,0,0],
[0,0,0,0]
]
]
}
square2.prototype=square.prototype;
var square3=function (){
square.call(this);
this.fangxiang=0;
this.rotates=[
[
[2,2,2,0],
[0,0,2,0],
[0,0,0,0],
[0,0,0,0]
],
[
[0,2,0,0],
[0,2,0,0],
[2,2,0,0],
[0,0,0,0]
],
[
[2,0,0,0],
[2,2,2,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,2,0,0],
[2,0,0,0],
[2,0,0,0],
[0,0,0,0]
]
]
}
square3.prototype=square.prototype;
var square4=function (){
square.call(this);
this.fangxiang=0;
this.rotates=[
[
[2,2,2,0],
[2,0,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,2,0,0],
[0,2,0,0],
[0,2,0,0],
[0,0,0,0]
],
[
[0,0,2,0],
[2,2,2,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,0,0,0],
[2,0,0,0],
[2,2,0,0],
[0,0,0,0]
]
]
}
square4.prototype=square.prototype;
var square5=function (){
square.call(this);
this.fangxiang=0;
this.rotates=[
[
[2,2,0,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,2,0,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,2,0,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,2,0,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
]
]
}
square5.prototype=square.prototype;
var square6=function (){
square.call(this);
this.fangxiang=0;
this.rotates=[
[
[0,2,2,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,0,0,0],
[2,2,0,0],
[0,2,0,0],
[0,0,0,0]
],
[
[0,2,2,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,0,0,0],
[2,2,0,0],
[0,2,0,0],
[0,0,0,0]
]
]
}
square6.prototype=square.prototype;
var square6=function (){
square.call(this);
this.fangxiang=0;
this.rotates=[
[
[0,2,2,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,0,0,0],
[2,2,0,0],
[0,2,0,0],
[0,0,0,0]
],
[
[0,2,2,0],
[2,2,0,0],
[0,0,0,0],
[0,0,0,0]
],
[
[2,0,0,0],
[2,2,0,0],
[0,2,0,0],
[0,0,0,0]
]
]
}
square6.prototype=square.prototype;
var square7=function (){
square.call(this);
this.fangxiang=0;
this.rotates=[
[
[2,2,0,0],
[0,2,2,0],
[0,0,0,0],
[0,0,0,0]
],
[
[0,2,0,0],
[2,2,0,0],
[2,0,0,0],
[0,0,0,0]
],
[
[2,2,0,0],
[0,2,2,0],
[0,0,0,0],
[0,0,0,0]
],
[
[0,2,0,0],
[2,2,0,0],
[2,0,0,0],
[0,0,0,0]
]
]
}
square7.prototype=square.prototype;
var fangkuai=function (){};
fangkuai.prototype.make=function (index,fangxiang){
var s;
index+=1;
switch (index){
case 1:s=new square1();
break;
case 2:s=new square2();
break;
case 3:s=new square3();
break;
case 4:s=new square4();
break;
case 5:s=new square5();
break;
case 6:s=new square6();
break;
case 7:s=new square7();
break;
}
s.origin.x=0;
s.origin.y=3;
s.rotate(fangxiang);
return s;
}
在线预览
https://wanshuai12138.github.io/%E6%96%B9%E5%9D%97/