-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
218 lines (162 loc) · 5.78 KB
/
script.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
console.log('Pika Pika');
/* Basic interaction structures consist of:
Declarations of relevant HTML-elements
Functions where HTML-elements are manipulated
Eventlisteners where elements respond to function call */
// The food appears
var feedbutton = document.querySelector('#feedbutton');
var feeddiv = document.querySelector('.feedelements');
function showfood() {
console.log('The food has appeared, you can now feed pikachu');
feeddiv.classList.toggle('visible');
petdiv.classList.remove('visible');
bathdiv.classList.remove('visible');
}
feedbutton.addEventListener('click', showfood);
// A hand appears
var petbutton = document.querySelector('#petbutton');
var petdiv = document.querySelector('.petelements');
function showhand() {
console.log('A hand has appeared, you can now pet pikachu');
petdiv.classList.toggle('visible');
feeddiv.classList.remove('visible');
bathdiv.classList.remove('visible');
}
petbutton.addEventListener('click', showhand);
// A sponge appears
var bathbutton = document.querySelector('#bathbutton');
var bathdiv = document.querySelector('.bathelements');
function showsponge() {
console.log('A sponge has appeared, you can now bath pikachu');
bathdiv.classList.toggle('visible');
feeddiv.classList.remove('visible');
petdiv.classList.remove('visible');
}
bathbutton.addEventListener('click', showsponge);
// Pikachu does not like carrots
var pikachu = document.querySelector('#pikachuhappy1');
var carrot = document.querySelector('#carrot');
function pikachuissad() {
console.log('Pikachu does not like carrots, he has become sad...');
pikachu.src = 'img/pikachu/pikachusad1.gif';
}
function pikachuishappy() {
console.log('Pikachu is happy');
pikachu.src = 'img/pikachu/pikachuhappy1.gif';
}
carrot.addEventListener('dragstart', pikachuissad);
carrot.addEventListener('dragend', pikachuishappy);
// Pikachu is being petted
var hand = document.querySelector('#hand');
function pikachuisbeingpet() {
console.log('Pikachu is being pet');
pikachu.src = 'img/pikachu/pikachupetting.png';
}
function pikachuishappy() {
console.log('Pikachu is happy');
pikachu.src = 'img/pikachu/pikachuhappy1.gif';
}
hand.addEventListener('dragstart', pikachuisbeingpet);
hand.addEventListener('dragend', pikachuishappy);
// Pikachu is bathing
var sponge = document.querySelector('#sponge');
function pikachuisbathing() {
console.log('Pikachu is bathing');
pikachu.src = 'img/pikachu/pikachubathing.png';
}
function pikachuishappy() {
console.log('Pikachu is happy');
pikachu.src = 'img/pikachu/pikachuhappy1.gif';
}
sponge.addEventListener('dragstart', pikachuisbathing);
sponge.addEventListener('dragend', pikachuishappy);
// Pikachu is eating a macaron
/* I made a function for a more efficient code (50 line reduction). By using a parameter, I know whether
the pikachu is eating or drinking in the function. */
var macaron = document.querySelector('#macaron');
var bubbletea = document.querySelector('#bubbletea');
var consumptioncounter = 0;
function pikachuiscomsuming(consumption) {
var text;
consumptioncounter++;
if (consumption == 0) {
text = 'eaten ' + consumptioncounter + ' macaron';
}
if (consumption == 1) {
text = 'drunk ' + consumptioncounter + ' bubbletea';
}
console.log('Pikachu has ' + text + '!');
pikachu.src = 'img/pikachu/pikachueating' + consumptioncounter + '.gif';
if (consumptioncounter == 5) {
consumptioncounter = 0;
}
}
function pikachuiseating() {
pikachuiscomsuming(0);
}
function pikachuisdrinking() {
pikachuiscomsuming(1);
}
macaron.addEventListener('dragstart', pikachuiseating);
macaron.addEventListener('dragend', pikachuisfat);
bubbletea.addEventListener('dragstart', pikachuisdrinking);
bubbletea.addEventListener('dragend', pikachuisfat);
// Counting pikachu's fat
var fatcounter = 0;
function pikachuisfat() {
fatcounter++;
switch (fatcounter) {
case 1:
console.log('Pikachu has become fatter!');
pikachu.src = 'img/pikachu/pikachuhappy2.gif';
break;
case 2:
console.log('Pikachu has become even fatter!');
pikachu.src = 'img/pikachu/pikachuhappy3.gif';
break;
case 3:
console.log('Pikachu has become really fat!');
pikachu.src = 'img/pikachu/pikachuhappy4.gif';
break;
case 4:
console.log('Pikachu has died...');
pikachu.src = 'img/pikachu/pikachudead.png';
break;
case 5:
fatcounter = 0;
console.log('Pikachu has revived!');
pikachu.src = 'img/pikachu/pikachuhappy1.gif';
break;
default:
console.log('Pikachu has returned to his first state');
pikachu.src = 'img/pikachu/pikachuhappy1.gif';
}
}
// Background music
var music = document.querySelector('#backgroundmusic');
var soundbutton = document.querySelector('#soundbutton');
music.loop = true;
// The default state is false, because the background music start of muted.
var soundchecker = false;
function soundtoggle() {
if (soundchecker) {
console.log('Background music is off');
music.pause();
soundchecker = false;
soundbutton.src = 'img/sound/soundoff.png';
}
else {
console.log('Background music is on');
music.play();
soundchecker = true;
soundbutton.src = 'img/sound/soundon.png';
}
}
soundbutton.addEventListener('click', soundtoggle);
/* BRONNEN
Background music:
https://www.youtube.com/watch?v=6VmoWwqtFYw
Keycode:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code
https://stackoverflow.com/questions/24386354/execute-js-code-after-pressing-the-spacebar
*/