diff --git a/css/css.css b/css/css.css index 3368f06..848e8d8 100644 --- a/css/css.css +++ b/css/css.css @@ -5,6 +5,7 @@ background-color: #ccc; padding: 30px; box-sizing: border-box; + margin: auto; } .game { width: 200px; @@ -56,4 +57,8 @@ .done { background-color: gray; border: 1px solid #666; +} + +#start { + cursor: pointer; } \ No newline at end of file diff --git a/index.html b/index.html index ea209c6..6c56428 100644 --- a/index.html +++ b/index.html @@ -1,6 +1,5 @@ - eluosi @@ -8,22 +7,23 @@ +
-
你的分数:0
+
SCORE: 0
-
- +
+
+ - \ No newline at end of file diff --git a/js/game.js b/js/game.js index 7f716f0..684c95a 100644 --- a/js/game.js +++ b/js/game.js @@ -1,5 +1,6 @@ let score = 0; -let Game = function () { + +function Game () { // dom // let不能重复定义 // 游戏矩阵 @@ -26,16 +27,15 @@ let Game = function () { [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]; // 当前方块 - var cur; + let cur; // 下一个方块 - var next; + let next; // divs let nextDivs = []; let gameDivs = []; - // 初始化div - let initDiv = function (container, data, divs) { + function initDiv (container, data, divs) { for (let i = 0; i < data.length; i++) { let div = []; for (let j = 0; j < data[0].length; j++) { @@ -50,7 +50,7 @@ let Game = function () { } } // 刷新页面 - let refreshDiv = function (data, divs) { + function refreshDiv (data, divs) { for (let i = 0; i < data.length; i++) { for (let j = 0; j < data[0].length; j++) { if (data[i][j] == 0) { @@ -160,6 +160,7 @@ let Game = function () { refreshDiv(gameData, gameDivs); } } + let init = function (doms) { gameDiv = doms.gameDiv; nextDiv = doms.nextDiv; @@ -213,8 +214,8 @@ let Game = function () { gameData[0][n] = 0 }, 1000) } - i ++; - score ++; + i++; + score++; document.getElementById('score').innerHTML = score; } } @@ -222,10 +223,11 @@ let Game = function () { // 游戏结束函数 let checkGameOver = function () { let gameOver = false; + for (let i = 0; i < gameData[0].length; i ++) { if (gameData[1][i] == 1) { gameOver = true; - + break; } } return gameOver; diff --git a/js/local.js b/js/local.js index 5bdd28c..44f7874 100644 --- a/js/local.js +++ b/js/local.js @@ -1,4 +1,4 @@ -var Local = function () { +function Local () { // 游戏对象 var game; var timer; @@ -8,7 +8,7 @@ var Local = function () { let INTERVAL = 300; // 定时器 - let bindKeyEvents = function (e) { + function bindKeyEvents (e) { document.onkeydown = function (e) { if (e.keyCode === 38) { // up game.rotate(); @@ -23,52 +23,65 @@ var Local = function () { } } } + // 移动函数 - let move = function () { + function move () { if (!game.down()) { game.fixed(); // 检测消除行 if (INTERVAL > 100) { - INTERVAL --; + INTERVAL--; } - setTime(INTERVAL) + setTime(INTERVAL); game.checkClear(); // 检测gameover if(game.checkGameOver()) { stop(); + let result = confirm(`Your score is: ${score}. +Do You want to try again?`); + if (result) { + start(); + } else { + isStart = false; + } } else { game.performNext(generateType(), generateNext()); } } } + // 随机生成方块 - let generateType = function () { + function generateType () { return Math.ceil(Math.random() * 7) - 1; } + // 随机生成旋转次数 - let generateNext = function () { + function generateNext () { return Math.ceil(Math.random() * 4) - 1; } + // 开始 - var start = function () { - var doms = { + function start () { + let doms = { gameDiv: document.getElementById('game'), - nextDiv: document.getElementById('next') + nextDiv: document.getElementById('next'), } game = new Game(); game.init(doms); bindKeyEvents(); // timer = setInterval(move, INTERVAL); - setTime(INTERVAL) + setTime(INTERVAL); } + // 结束函数stop的定义 - let stop = function () { + function stop () { if (timer) { clearInterval(timer); timer = null; } document.onkeydown = null; } + function setTime (time) { if (timer) { clearInterval(timer); @@ -78,4 +91,4 @@ var Local = function () { // 导出API this.start = start; -} +}; \ No newline at end of file diff --git a/js/script.js b/js/script.js index 9fe65ca..b00568a 100644 --- a/js/script.js +++ b/js/script.js @@ -1,12 +1,10 @@ let startEle = document.getElementById('start'), - local = new Local(), - isStart = false; + local = new Local(), + isStart = false; startEle.addEventListener('click', () => { - if (!isStart) { - local.start() - isStart = true; -} - -}) - + if (!isStart) { + local.start(); + isStart = true; + }; +}); \ No newline at end of file diff --git a/js/square.js b/js/square.js index 8f2bf7a..705f339 100644 --- a/js/square.js +++ b/js/square.js @@ -1,4 +1,4 @@ -let Square = function () { +function Square () { this.data = [ [0, 0, 2, 0], [0, 0, 2, 0], @@ -27,6 +27,7 @@ let Square = function () { } return isValid(this.origin, test); } + Square.prototype.rotate = function (num) { if (!num) { num = 1; diff --git a/js/squareFactory.js b/js/squareFactory.js index 70e8cdd..3739b9e 100644 --- a/js/squareFactory.js +++ b/js/squareFactory.js @@ -234,8 +234,10 @@ Square7.prototype = Square.prototype; let SquareFactory = function () { } + SquareFactory.prototype.make = function (index, dir) { let s; + index += 1; switch (index) { case 1: @@ -262,8 +264,10 @@ SquareFactory.prototype.make = function (index, dir) { default: break; } + s.origin.x = 0; s.origin.y = 3; s.rotate(dir); + return s; } \ No newline at end of file