-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.js
executable file
·99 lines (83 loc) · 2.39 KB
/
sprite.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
var
s_bird,
s_bg,
s_fg,
s_pipeNorth,
s_pipeSouth,
s_text,
s_score,
s_splash,
s_buttons,
s_numberS,
s_numberB;
function Sprite(img, x, y, width, height) {
this.img = img;
this.x = x*2;
this.y = y*2;
this.width = width*2;
this.height = height*2;
}
Sprite.prototype.draw = function(ctx, x, y) {
ctx.drawImage(this.img, this.x, this.y, this.width, this.height,
x, y, this.width, this.height);
};
function initSprites(img) {
var samoyed_img1 = new Image();
samoyed_img1.src = "res/samoyed1.png";
var samoyed_img2 = new Image();
samoyed_img2.src = "res/samoyed2.png";
var samoyed_img3 = new Image();
samoyed_img3.src = "res/samoyed3.png";
var samoyed_img4 = new Image();
samoyed_img4.src = "res/samoyed4.png";
s_bird = [
// new Sprite(img, 156, 115, 17, 12),
// new Sprite(img, 156, 128, 17, 12),
// new Sprite(img, 156, 141, 17, 12)
new Sprite(samoyed_img1, 0, 0, 32, 32),
new Sprite(samoyed_img2, 0, 0, 32, 32),
new Sprite(samoyed_img3, 0, 0, 32, 32),
new Sprite(samoyed_img4, 0, 0, 32, 32)
];
s_bg = new Sprite(img, 0, 0, 138, 114);
s_bg.color = "#70C5CF";
s_fg = new Sprite(img, 138, 0, 112, 56);
s_pipeNorth = new Sprite(img, 251, 0, 26, 200);
s_pipeSouth = new Sprite(img, 277, 0, 26, 200);
s_text = {
FlappyBird: new Sprite(img, 59, 114, 96, 22),
GameOver: new Sprite(img, 59, 136, 94, 19),
GetReady: new Sprite(img, 59, 155, 87, 22)
};
s_buttons = {
Rate: new Sprite(img, 79, 177, 40, 14),
Menu: new Sprite(img, 119, 177, 40, 14),
Share: new Sprite(img, 159, 177, 40, 14),
Score: new Sprite(img, 79, 191, 40, 14),
Ok: new Sprite(img, 119, 191, 40, 14),
Start: new Sprite(img, 159, 191, 40, 14)
};
s_score = new Sprite(img, 138, 56, 113, 58);
s_splash = new Sprite(img, 0, 130, 59, 47);
var samoyedG = new Image();
samoyedG.src = "res/samoyed_grey.gif";
s_samoyedG = new Sprite(samoyedG, 0, 0, 32, 32);
s_numberS = new Sprite(img, 0, 177, 6, 7);
s_numberB = new Sprite(img, 0, 188, 7, 10);
s_numberS.draw = s_numberB.draw = function(ctx, x, y, num, center, offset) {
num = num.toString();
var step = this.width + 2;
if (center) {
x = center - (num.length*step-2)/2;
}
if (offset) {
x += step*(offset - num.length);
}
for (var i = 0, len = num.length; i < len; i++) {
var n = parseInt(num[i]);
ctx.drawImage(img, step*n, this.y, this.width, this.height,
x, y, this.width, this.height);
x += step;
}
};
}