-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (150 loc) · 3.22 KB
/
script.js
File metadata and controls
183 lines (150 loc) · 3.22 KB
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
kaboom()
//Assets
loadSprite("mj", "images/mj.png");
loadSprite("bulls", "images/Bulls.png");
loadSprite("bg", "images/bg.png");
loadSprite("pipe", "images/pipe.png");
loadSprite("giphy", "images/giphy.gif");
loadSound("download", "audio/download.mp3");
loadSound("weak", "audio/weak.mp3");
loadSound("score", "audio/score.mp3");
loadSound("halo", "audio/halo.mp3");
let highScore = 0;
let hop = 400;
let gap = 180;
scene("easy", () => {
hop = 400;
gap = 180;
go("game")
})
scene("medium", () => {
hop = 350;
gap = 140;
go("game")
})
scene("hard", () => {
hop = 280;
gap = 120;
go("game")
})
scene("intro", () => {
add([
sprite("bg", {width: width(), height: height()})
]);
add([
pos(width() / 2 -100, height() / 2 -100 ),
text(
"Start \n",
{size: 45}
),
]);
add([
pos(width() / 2 -350, height() / 2 +50 ),
text(
"(Press SpaceBar for easy)\n"
+ "(press Enter for medium)\n"+
"(press Tab for hard)",
{size: 45}
),
]);
onKeyPress("space", () => {
go("easy");
});
onKeyPress("enter", () => {
go("medium");
});
onKeyPress("tab", () => {
go("hard");
});
});
scene("game", () => {
let PIPE_GAP = gap;
let score = 0;
add([
sprite("bulls", {width: width(), height: height()})
]);
const scoreText = add([
text(score, {size: 50})
]);
// add a game object to screen
const player = add([
sprite("mj"),
scale(.07),
pos(80, 40),
area(),
body(),
]);
function producePipes(){
// offset changes the generation of pipes
const offset = rand(-130, 130);
add([
sprite("pipe"),
scale(3),
pos(width(), height()/2 + offset + PIPE_GAP/2),
"pipe",
area(),
{passed: false}
]);
add([
sprite("pipe", {flipY: true}),
scale(3),
pos(width(), height()/2 + offset - PIPE_GAP/2),
origin("botleft"),
"pipe",
area()
]);
}
loop(1.2, () => {
producePipes();
});
// pipe speed
action("pipe", (pipe) => {
// x-axis y-axis
pipe.move(-260, 0);
if (pipe.passed === false && pipe.pos.x < player.pos.x) {
pipe.passed = true;
score += 1;
scoreText.text = score;
play("score")
}
});
player.collides("pipe", () => {
play("weak");
go("gameover", score);
});
player.action(() => {
if (player.pos.y > height() + 120 || player.pos.y < -120) {
play("halo");
go("gameover", score);
}
});
onKeyPress("space", () => {
play("download");
player.jump(hop);
});
});
scene("gameover", (score) => {
if (score > highScore) {
highScore = score;
}
add([
sprite("giphy", {width: width(), height: height()})
]);
add([
text(
"gameover!\n"
+ "score: " + score
+ "\nhigh score: " + highScore + "\n(press space to try again)" +
"\n(press enter to main menu)",
{size: 45}
)
]);
onKeyPress("space", () => {
go("game");
});
onKeyPress("enter", () => {
highScore = 0;
go("intro");
});
});
go("intro");