forked from imwtr/hit-hit-mouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hit-mouse.js
147 lines (123 loc) · 4.38 KB
/
hit-mouse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function MouseGame() {
this.mousesWrap = this.$('.game-content');
this.mouses = this.$('.game-content div');
this.gameStart = this.$('#game-start');
this.gameTime = this.$('#game-time');
this.gameScore = this.$('#game-score');
this.goodScore = 100;
this.badScore = 50;
this.isGaming = false;
this.bindEvent();
}
MouseGame.prototype = {
constructor: MouseGame,
/**
* 获取元素
* @param {String} elem 元素的字符串标识
* @example
* $('div') | $('p.active')
* @return {NodeList} 获取的元素集
*/
$: function(elem) {
return document.querySelectorAll(elem);
},
/**
* 获取给定范围的随机数
* @param {Number} from 起始
* @param {Number} to 结束
* @return {Number} 随机数
*/
getRandom: function(from, to) {
return Math.floor(Math.random() * (to - from + 1)) + from;
},
/**
* 设置元素内容
* @param {HTMLElement} elem 要设置的元素
* @param {String} val 设置的内容
* @return {String} 设置好的内容|元素本身的内容
*/
text: function(elem, val) {
if (elem.textContent) {
return val !== undefined ? elem.textContent = val : elem.textContent;
} else if (elem.innerText) {
return val !== undefined ? elem.innerText = val : elem.innerText;
}
},
// 运动操作
moveUpAndDown: function() {
var that = this;
// 定时器随机定义good|bad老鼠个数,以及需要显示的个数
that.moveTime = setInterval(function() {
for (var i = 0, j = that.mouses.length; i < j; ++i) {
that.mouses[i].setAttribute('clicked', '0');
that.mouses[i].className = 'good active';
that.mouses[i].style.display = 'none';
}
// bad 的个数
var badNum = that.getRandom(0, 2);
for (var i = 0; i < badNum; i++) {
that.mouses[that.getRandom(0, 8)].className = 'bad active';
}
// 要显示的个数
var showNum = that.getRandom(1, 2);
for (var i = 0; i < showNum; i++) {
that.mouses[that.getRandom(0, 8)].style.display = 'block';
}
}, 2000);
},
// 打地鼠操作
bindEvent: function() {
var that = this;
// 监听游戏开始/重新开始
that.gameStart[0].addEventListener('click', function() {
if (that.isGaming) return;
that.startGame();
}, false);
// 打地鼠操作
that.mousesWrap[0].addEventListener('click', function(e) {
e = e || window.event;
var elem = e.target || e.srcElement;
// 如果当前项被隐藏则不操作,多次点击只取第一次分数
if (elem.style.display !== 'block' || elem.getAttribute('clicked') === '1') {
return;
}
// 扣分
if (elem.className.indexOf('bad') !== -1) {
that.score -= that.badScore;
}
// 加分
else {
that.score += that.goodScore;
}
elem.setAttribute('clicked', '1');
that.text(that.gameScore[0], that.score);
}, false);
},
// 倒计时,当前剩余游戏时间
countDown: function() {
var that = this;
var t = setInterval(function() {
that.text(that.gameTime[0], --that.totalTime);
if (that.totalTime === 0) {
clearInterval(t);
clearInterval(that.moveTime);
for (var i = 0, j = that.mouses.length; i < j; ++i) {
that.mouses[i].style.display = 'none';
}
that.isGaming = false;
alert('游戏结束,得分为:' + that.score);
}
}, 1000);
},
// 开始游戏
startGame: function() {
this.isGaming = true;
this.score = 0;
this.totalTime = 60;
this.text(this.gameTime[0], this.totalTime);
this.text(this.gameScore[0], this.score);
this.countDown();
this.moveUpAndDown();
}
};
new MouseGame();