Skip to content

make game better, added an improvement to the 'move' function when pr… #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions css/css.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
background-color: #ccc;
padding: 30px;
box-sizing: border-box;
margin: auto;
}
.game {
width: 200px;
Expand Down Expand Up @@ -56,4 +57,8 @@
.done {
background-color: gray;
border: 1px solid #666;
}

#start {
cursor: pointer;
}
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>eluosi</title>
<link rel="stylesheet" href="./css/css.css">
</head>

<body>

<div class="cont">
<div class="game" id="game"></div>
<div class="next" id="next"></div>
<div class="info">
<div>你的分数:<span id="score">0</span></div>
<div>SCORE: <span id="score">0</span></div>
<div>
<br/>
<button id="start">开始游戏</button>
<br>
<button id="start">START</button>
</div>
</div>
</div>

<script src="./js/square.js"></script>
<script src="./js/squareFactory.js"></script>
<script src="./js/game.js"></script>
<script src="./js/local.js"></script>
<script src="./js/script.js"></script>
<body>

</html>
20 changes: 11 additions & 9 deletions js/game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let score = 0;
let Game = function () {

function Game () {
// dom
// let不能重复定义
// 游戏矩阵
Expand All @@ -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++) {
Expand All @@ -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) {
Expand Down Expand Up @@ -160,6 +160,7 @@ let Game = function () {
refreshDiv(gameData, gameDivs);
}
}

let init = function (doms) {
gameDiv = doms.gameDiv;
nextDiv = doms.nextDiv;
Expand Down Expand Up @@ -213,19 +214,20 @@ let Game = function () {
gameData[0][n] = 0
}, 1000)
}
i ++;
score ++;
i++;
score++;
document.getElementById('score').innerHTML = score;
}
}
}
// 游戏结束函数
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;
Expand Down
39 changes: 26 additions & 13 deletions js/local.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Local = function () {
function Local () {
// 游戏对象
var game;
var timer;
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -78,4 +91,4 @@ var Local = function () {

// 导出API
this.start = start;
}
};
16 changes: 7 additions & 9 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -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;
};
});
3 changes: 2 additions & 1 deletion js/square.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let Square = function () {
function Square () {
this.data = [
[0, 0, 2, 0],
[0, 0, 2, 0],
Expand Down Expand Up @@ -27,6 +27,7 @@ let Square = function () {
}
return isValid(this.origin, test);
}

Square.prototype.rotate = function (num) {
if (!num) {
num = 1;
Expand Down
4 changes: 4 additions & 0 deletions js/squareFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}