纯js实现俄罗斯方块详解与源码
對于小白來說用js實現俄羅斯方塊還是有難度的,網上找了很多代碼看,有的很長難懂,有的短小精悍,但不只用到了js還用到了框架,對于還未接觸框架的小白寶寶,也只能無奈自己是小白了,自己寫不出來那就找一篇純js代碼,弄懂也是一種收獲吧。so 接下來就是我的理解咯,有不對的地方請多多包涵
個人覺得思路還是很重要的,那我就先以我理解之后,來說說其思路
首先整個編程過程用到了六個數組吧,第一個是全局數組shapes,存放了7種俄羅斯方塊的形狀,之后的數組都是動態創建并賦值的,divs[]長度為4 存放要下落的方塊,div2[]長度也是4用來存放預告方塊的,對應的就有shape[]用來說明下落方塊的形狀是怎么樣,shape2[]用來存放預告方塊形狀的,這兩個形狀都是隨機產生的,還有一個隱形的數組用來存放顯示界面中所有方塊的,個人認為最難的就是理清顯示界面和抽象數組之間的關系吧,先上一個思維導圖讓大家直觀的理清思路,之后慢慢詳解代碼
? ? ? ? ? ?<style> ? ? ? ?//設置好元素樣式,到時候動態創建元素,直接設置其屬性 根據類名確定樣式
?.c {
margin: 1px;
width: 19px;
height: 19px;
background: red;
position: absolute; ? ?//下落方塊樣式
}
.d {
margin: 1px;
width: 19px;
height: 19px;
background: gray;
position: absolute; //下落到不能下落方塊樣式 其實就改變了顏色
}
.f {
top: 0px;
left: 0px;
background: black;
position: absolute; //整個游戲顯示界面
}
.e {
top: 0px;
background: #151515;
position: absolute; ? //預告方塊界面
}
.g {
width: 100px;
height: 20px;
color: white;
position: absolute; ?//分數顯示界面
}
</style>
開始JS代碼
<script type="text/javascript">
var row = 18;
var col = 10;
var announcement = 6;
var size = 20;
var isOver = false;
var shapes = ("0,1,1,1,2,1,3,1;1,0,1,1,1,2,2,2;2,0,2,1,2,2,1,2;0,1,1,1,1,2,2,2;1,2,2,2,2,1,3,1;1,1,2,1,1,2,2,2;0,2,1,2,1,1,2,2").split(";");
var tetris;
var container;
最難理解的可能就是shapes數組 .split(";");是將數組以;分割成一個個數組值,每個數組值單號代表top值,雙號代表left值代表一種方塊形狀 此處上圖
function createElm(tag, css) {
var elm = document.createElement(tag);
elm.className = css;
document.body.appendChild(elm);
return elm;
}
function Tetris(css, x, y, shape) {
// 創建4個div用來組合出各種方塊
var myCss = css ? css : "c";
this.divs = [createElm("div", myCss), createElm("div", myCss), createElm("div", myCss), createElm("div", myCss)];
if(!shape) {
this.divs2 = [createElm("div", myCss), createElm("div", myCss), createElm("div", myCss), createElm("div", myCss)];
this.score = createElm("div", "g");
this.score.style.top = 10 * size + "px";
this.score.style.left = (col - -1) * size + "px";
this.score.innerHTML = "score:0";
}
this.container = null;
this.refresh = function() {
this.x = (typeof(x) != 'undefined') ? x : 3;
this.y = (typeof(y) != 'undefined') ? y : 0;
// 如果有傳參,優先使用參數的,如果有預告,優先使用預告,都沒有就自己生成
if(shape)
this.shape = shape;
else if(this.shape2)
this.shape = this.shape2;
else
this.shape = shape ? shape : shapes[Math.floor((Math.random() * shapes.length - 0.000000001))].split(",");
this.shape2 = shapes[Math.floor((Math.random() * shapes.length - 0.000000001))].split(",");
if(this.container && !this.container.check(this.x, this.y, this.shape)) {
isOver = true;
alert("游戲結束");
} else {
this.show();
this.showScore();
this.showAnnouncement();
}
}
// 顯示方塊
this.show = function() {
for(var i in this.divs) {
this.divs[i].style.top = (this.shape[i * 2 + 1] - -this.y) * size + "px";//頂部中間開始
this.divs[i].style.left = (this.shape[i * 2] - -this.x) * size + "px";
}
}
// 顯示預告
this.showAnnouncement = function() {
for(var i in this.divs2) {
this.divs2[i].style.top = (this.shape2[i * 2 + 1] - -1) * size + "px";//定位在顯示區
this.divs2[i].style.left = (this.shape2[i * 2] - -1 - -col) * size + "px";
}
}
// 顯示分數
this.showScore = function() {
if(this.container && this.score) {
this.score.innerHTML = "score:" + this.container.score;
}
}
// 水平移動方塊的位置
this.hMove = function(step) {
if(this.container.check(this.x - -step, this.y, this.shape)) {
this.x += step;
this.show();
}
}
// 垂直移動方塊位置
this.vMove = function(step) {
if(this.container.check(this.x, this.y - -step, this.shape)) {
this.y += step;
this.show();
} else {
this.container.fixShape(this.x, this.y, this.shape);
this.container.findFull();
this.refresh();
}
}
// 旋轉方塊
this.rotate = function() {
var newShape = [this.shape[1], 3 - this.shape[0], this.shape[3], 3 - this.shape[2], this.shape[5], 3 - this.shape[4], this.shape[7], 3 - this.shape[6]];
if(this.container.check(this.x, this.y, newShape)) {
this.shape = newShape;
this.show();
}
}
this.refresh();
}
function Container() {
this.init = function() {
// 繪制方塊所在區域
var bgDiv = createElm("div", "f");
bgDiv.style.width = size * col + "px";
bgDiv.style.height = size * row + "px";
// 繪制預告所在區域
var bgDiv = createElm("div", "e");
bgDiv.style.left = size * col + "px";
bgDiv.style.width = size * announcement + "px";
bgDiv.style.height = size * row + "px";
// 清空積分
this.score = 0;
}
this.check = function(x, y, shape) {
// 檢查邊界越界
var flag = false;
var leftmost = col;
var rightmost = 0;
var undermost = 0;
for(var i = 0; i < 8; i += 2) {
// 記錄最左邊水平坐標
if(shape[i] < leftmost)
leftmost = shape[i];
// 記錄最右邊水平坐標
if(shape[i] > rightmost)
rightmost = shape[i];
// 記錄最下邊垂直坐標
if(shape[i + 1] > undermost)
undermost = shape[i + 1];
// 判斷是否碰撞
if(this[(shape[i + 1] - -y) * 100 - -(shape[i] - -x)])
flag = true;
}
// 判斷是否到達極限高度
for(var m = 0; m < 3; m++)
for(var n = 0; n < col; n++)
if(this[m * 100 + n])
flag = true;
if((rightmost - -x + 1) > col || (leftmost - -x) < 0 || (undermost - -y + 1) > row || flag)
return false;
return true;
}
// 用灰色方塊替換紅色方塊,并在容器中記錄灰色方塊的位置
this.fixShape = function(x, y, shape) {
var t = new Tetris("d", x, y, shape);
for(var i = 0; i < 8; i += 2)
this[(shape[i + 1] - -y) * 100 - -(shape[i] - -x)] = t.divs[i / 2];
}
// 遍歷整個容器,判斷是否可以消除
this.findFull = function() {
var s = 0;
for(var m = 0; m < row; m++) {
var count = 0;
for(var n = 0; n < col; n++)
if(this[m * 100 + n])
count++;
if(count == col) {
s++;
this.removeLine(m);
}
}
this.score -= -this.calScore(s);
}
this.calScore = function(s) {
if(s != 0)
return s - -this.calScore(s - 1)
else
return 0;
}
// 消除指定一行方塊
this.removeLine = function(row) {
// 移除一行方塊
for(var n = 0; n < col; n++)
document.body.removeChild(this[row * 100 + n]);
// 把所消除行上面所有的方塊下移一行
for(var i = row; i > 0; i--) {
for(var j = 0; j < col; j++) {
this[i * 100 - -j] = this[(i - 1) * 100 - -j]//數組中 消除行上的所有元素逐行下移 (等同于在數組改變位置)
if(this[i * 100 - -j])//如果此下標有元素 讓其定位到要顯示的位置
this[i * 100 - -j].style.top = i * size + "px";
}
}
}
}
function init() {
container = new Container();
container.init();
tetris = new Tetris();
tetris.container = container;
document.onkeydown = function(e) {
if(isOver) return;
var e = window.event ? window.event : e;
switch(e.keyCode) {
case 38: //up
tetris.rotate();
break;
case 40: //down
tetris.vMove(1);
break;
case 37: //left
tetris.hMove(-1);
break;
case 39: //right
tetris.hMove(1);
break;
}
}
setInterval("if(!isOver) tetris.vMove(1)", 500);
}
</script>
</head>
<body οnlοad="init()">
</body>
</html>
總結
以上是生活随笔為你收集整理的纯js实现俄罗斯方块详解与源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nehe第19课 粒子系统
- 下一篇: Android使用SurfaceView