-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
157 lines (119 loc) · 4.66 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Typing Game</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
</head>
<body>
<script>
let words = [
"fart attack", "boogie monster", "silly socks", "monster burp", "giggle fit", "crazy hat", "tickle tummy", "prank call", "silly walk", "funny face", "goofy grin", "jokes galore", "whoopee cushion", "haha hauler", "laughing loo",
"slippery slide", "wacky wig", "silly songs", "noodle noodle", "clown car", "funny farm", "bouncing ball", "jumpy jelly", "giggly gorilla", "jolly jump", "silly circus", "hilarious hamster", "grinning gorilla", "funny fiesta", "joyful jester"];
let word = "";
let score = 0;
let velocity = { x: 50, y: 50 };
let config = {
type: Phaser.AUTO,
width: 960,
height: 540,
scene: {
preload: preload,
create: create,
update: update
}
};
const isAlphaNumeric = /^[0-9a-zA-Z ]+$/;
let game = new Phaser.Game(config);
function preload() {
this.load.image("background", "images/Board.png");
this.load.audio("type1", "audio/type1.wav");
this.load.audio("type2", "audio/type2.wav");
this.load.audio("score_up", "audio/score_up.wav");
this.load.audio("wrong", "audio/wrong.wav");
this.load.audio("celebrate", "audio/celebrate.wav");
this.load.audio("bounce", "audio/bounce.wav");
this.load.audio("music", "audio/music.mp3");
}
function create() {
newWord();
this.add.image(480, 270, "background");
this.wordText = this.add.text(100, 200, word, {
font: "64px Arial",
fill: "#ccc"
});
this.typingText = this.add.text(100, 200, "", {
font: "64px Arial",
fill: "#000"
})
this.scoreText = this.add.text(630, 70, "Score: " + score, {
font: "24px Arial",
fill: "#222"
});
this.type1Sound = this.sound.add("type1");
this.type2Sound = this.sound.add("type2");
this.scoreUpSound = this.sound.add("score_up");
this.wrongSound = this.sound.add("wrong");
this.celebrateSound = this.sound.add("celebrate");
this.bounceSound = this.sound.add("bounce");
this.music = this.sound.add("music");
this.music.setLoop(true);
this.music.play();
this.input.keyboard.on("keydown", keyDown, this);
}
function update(time, delta) {
this.wordText.x += velocity.x * delta / 1000;
this.wordText.y += velocity.y * delta / 1000;
if (this.wordText.x < 46 ||
this.wordText.x + this.wordText.width > game.config.width - 46) {
this.bounceSound.play();
velocity.x = -velocity.x;
}
if (this.wordText.y < 105 ||
this.wordText.y + this.wordText.height > game.config.height - 25) {
this.bounceSound.play();
velocity.y = -velocity.y;
}
this.typingText.x = this.wordText.x;
this.typingText.y = this.wordText.y;
}
function keyDown(event) {
if (!event.key.match(isAlphaNumeric)) {
return;
}
if (Math.random() < 0.5) {
this.type1Sound.play();
} else {
this.type2Sound.play();
}
if (event.key !== word[this.typingText.text.length]) {
this.wrongSound.play();
if (score > 0) {
score--;
}
this.scoreText.text = "Score: " + score;
newWord();
this.typingText.text = "";
this.wordText.text = word;
return;
}
this.typingText.text += event.key;
if (this.typingText.text === word) {
score++;
if (score % 10 == 0) {
this.celebrateSound.play();
} else {
this.scoreUpSound.play();
}
this.scoreText.text = "Score: " + score;
newWord();
this.typingText.text = "";
this.wordText.text = word;
}
}
function newWord() {
word = Phaser.Math.RND.pick(words);
}
</script>
</body>
</html>