-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.js
executable file
·70 lines (56 loc) · 1.78 KB
/
blocks.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
blocks = [];
nblocks = 5;
firstBlock = 300;
blockDist = 125;
blockWidth = 20;
blockHeight = 100;
blockSpeed = 70;
var blendMargin = 100;
//random block height of 0.25-0.75% of the canvas height
function randomBlockHeight(){
return (canvas.height/2)*(Math.random()+0.5);
}
//this gives a y-coordinate so that it is either floor, or a ceiling block with an equal probability
function randomY(height){
if(Math.random() > 0.5){
return 0;
}
return canvas.height-height;
}
function initBlocks(){
for(i=0; i<nblocks; i++){
blocks[i] = {x: firstBlock+i*blockDist, y:0, height:0};
blocks[i].height = randomBlockHeight();
blocks[i].y = randomY(blocks[i].height);
}
}
function updateBlocks(dt){
blockSpeed = 70+timeSeconds;
for(i=0; i<nblocks; i++){
b = blocks[i];
b.x -= dt*blockSpeed;
//put an out-of-view block behind it's successor and initialize it with new height value
if(b.x+blockWidth < 0){
b.x = blocks[(i-1+nblocks) % nblocks].x+blockDist;
b.height = randomBlockHeight();
b.y = randomY(b.height);
}
}
}
function drawBlocks(){
for(i=0; i<nblocks; i++){
b = blocks[i];
//save the globalAlpha value so we can restore it after drawing the block
var prevAlpha = ctx.globalAlpha;
//blocks fade in and out; if they are close enough to the edge of the canvas,
//they are drawn with alpha value smaller than one
if(b.x < blendMargin) ctx.globalAlpha *= ((Math.max(b.x, 0))/(blendMargin));
if(b.x+blockWidth > canvas.width - blendMargin) ctx.globalAlpha *= (Math.max(0, canvas.width-b.x-blockWidth)/(blendMargin-blockWidth));
ctx.beginPath();
ctx.rect(b.x, b.y, blockWidth, b.height);
ctx.fillStyle = gameColor;
ctx.fill();
ctx.closePath();
ctx.globalAlpha = prevAlpha;
}
}