-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
188 lines (160 loc) · 3.12 KB
/
index.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<html>
<head>
<title>kaboom</title>
<meta charset="utf-8">
<style>
* {
margin: 0;
}
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://kaboomjs.com/lib/0.1.0/kaboom.js"></script>
<script>
kaboom.global();
loadSprite("bg", "sprites/bg.png");
loadSprite("mark", "sprites/mark.kbmsprite");
loadSprite("pipo", "sprites/pipo.png");
loadSound("wooosh", "sounds/wooosh.ogg");
init({
width: 240,
height: 240,
fullscreen: true,
scale: 2,
});
scene("main", (args = {}) => {
const PIPO_OPEN = 96;
const PIPO_MIN_HEIGHT = 16;
const JUMP_FORCE = 320;
const SPEED = 120;
// define gravity
gravity(1200);
// define draw layers and default layer
layers([
"bg",
"obj",
"ui",
], "obj");
// background image
add([
sprite("bg"),
scale(width() / 240, height() / 240),
layer("bg"),
]);
// a game object consists of a list of components and tags
const birdy = add([
// sprite() means it's drawn with a sprite of name "birdy" (defined above in 'loadSprite')
sprite("mark"),
// give it a position
pos(120, 0),
// add it to the "obj" layer
layer("obj"),
// body component enables it to fall and jump in a gravity world
body(),
]);
// check for fall death
birdy.action(() => {
if (birdy.pos.y >= height() || birdy.pos.y <= -120) {
// switch to "death" scene
go("score", { score: score.value });
}
});
// jump
keyPress("space", () => {
birdy.jump(JUMP_FORCE);
play("wooosh");
});
function spawnPipo() {
// calculate pipe positions
const h1 = rand(PIPO_MIN_HEIGHT, height() - PIPO_MIN_HEIGHT - PIPO_OPEN);
const h2 = h1 + PIPO_OPEN;
add([
sprite("pipo"),
origin("botleft"),
pos(width(), h1),
layer("obj"),
// give it tags to easier define behaviors see below
"pipo",
]);
add([
sprite("pipo"),
origin("botleft"),
scale(1, -1),
layer("obj"),
pos(width(), h2),
"pipo",
// raw table just assigns every field to the game obj
{ passed: false, },
]);
}
// callback when birdy collides with objects with tag "pipo"
birdy.collides("pipo", () => {
go("score", { score: score.value} );
play("hit");
});
// per frame event for all objects with tag 'pipo'
action("pipo", (p) => {
// move left
p.move(-SPEED, 0);
// check if birdy passed the pipe
if (p.pos.x + p.width <= birdy.pos.x && p.passed === false) {
addScore();
p.passed = true;
}
// remove from scene when not seen
if (p.pos.x < -width() / 2) {
destroy(p);
}
});
// spawn a pipo every 1 sec
loop(1, () => {
spawnPipo();
});
// display score
const score = add([
text("0", 16),
layer("ui"),
pos(9, 9),
{
value: 0,
},
]);
function addScore() {
score.value++;
score.text = score.value;
play("score");
}
});
scene("score", (args = {}) => {
const mark = add([
sprite("mark"),
scale(1),
pos(width() / 2, height() / 2 - 24),
origin("center"),
]);
mark.action(() => {
mark.scale = vec2(wave(1, 2, 6));
});
add([
text(`${args.score}`, 32),
pos(width() / 2, height() / 2 + 32),
origin("center"),
]);
keyPress("space", () => {
go("main");
});
});
start("main");
</script>
</body>
</html>