-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbrick.js
85 lines (59 loc) · 1.58 KB
/
brick.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
/**
* Created by xuting on 2017/3/27.
*/
function brickObj() {
this.num;
this.x;
this.y;
this.alive; // 砖块有没有被炸掉, true 表示没被炸掉
}
brickObj.prototype.init = function () {
this.num = 120;
this.x = [];
this.y = [];
this.alive = [];
for(let i = 0; i < this.num; i++){
let x = Math.floor(Math.random() * 24) * 50;
let y = Math.floor(Math.random() * 16) * 50;
this.x.push(x);
this.y.push(y);
this.alive[i] = true;
// 把重叠在石块上的砖块 设置为 false
for(let j = 0; j < stock.num; j++){
if(x == stock.x[j] && y == stock.y[j]){
this.alive[i] = false;
break;
}
}
// 把左上角的位置留出来, 给 hero 做初始化位置
if((x == 50 && y == 50) || (x == 100 && y == 50) || (x==50 && y==100)){
this.alive[i] = false;
}
}
}
brickObj.prototype.draw = function () {
// 画小砖块组
for(let i = 0; i < this.num; i++){
if(this.alive[i]){
drawBrick(this.x[i], this.y[i]);
}
}
}
/*
* 画一个小砖块
* (x, y) 是原点偏移量
*
* */
function drawBrick(x, y) {
cbgCtx.save();
cbgCtx.translate(x, y);
cbgCtx.fillStyle = '#EAEAEA';
cbgCtx.fillRect(0, 0, 50, 50);
cbgCtx.fillStyle = '#CDCDC1';
cbgCtx.fillRect(5, 5, 40, 10);
cbgCtx.fillRect(5, 20, 10, 10);
cbgCtx.fillRect(20, 20, 25, 10);
cbgCtx.fillRect(5, 35, 25, 10);
cbgCtx.fillRect(35, 35, 10, 10);
cbgCtx.restore();
}