generated from codersforcauses/beginner-2023S
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.js
225 lines (178 loc) · 7.46 KB
/
buttons.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
219
220
221
222
223
224
225
const button = document.querySelectorAll('.btn-minecraft')
button.forEach(btn => {
btn.addEventListener('mouseleave', function (){
btn.blur()
})
})
document.addEventListener('DOMContentLoaded', function () {
var menu1 = document.getElementById('menu1');
var startButton = document.getElementById('start');
var exitButton = document.getElementById('exitButton');
var popup = document.getElementById('popup');
var yesButton = document.getElementById('yesButton');
var noButton = document.getElementById('noButton');
exitButton.style.display = 'none';
const clickSound = document.getElementById('clickSound');
const bgm = document.getElementById('bgm');
var leaderboard = document.getElementById('leaderboard');
leaderboard.addEventListener('click', function () {
});
// Event listener for the Play button
startButton.addEventListener('click', function () {
playClickSound(); // Play click sound
console.log(myGameGlobals.BGMC)
if (myGameGlobals.BGMC === 0) {
playBGM()
myGameGlobals.BGMC++
}
// Include and execute game.js
var script = document.createElement('script');
script.src = 'game.js';
// Show the entire score display
document.getElementById('scoreDisplay').style.display = 'block';
document.getElementById('scoreEl').style.display = 'inline';
document.head.appendChild(script);
menu1.classList.add('hidden');
// Show the icon container
document.getElementById('iconScoreContainer').style.display = 'flex';
// Show individual icons (icon1, icon2, icon3, icon4)
document.getElementById('icon1').style.display = 'flex';
document.getElementById('icon2').style.display = 'flex';
document.getElementById('icon3').style.display = 'flex';
document.getElementById('icon4').style.display = 'flex';
// Show the exit button
exitButton.style.display = 'block';
document.getElementById('minecraftHeartsContainer').style.display = 'flex';
if (gameState.gamestart === 0) {
showStartScreen(true);
updateHearts(gameState.hearts);
} else if (typeof pauseAnimation === 'function' && pauseAnimation()) {
showPauseScreen(true);
updateHearts(gameState.hearts);
} else {
showPauseScreen(false);
}
myGameGlobals.canAcceptInput = true;
if (gameState.gameover) {
showGameOverScreen(true)
updateHearts(gameState.hearts);
} else if (gameState.gamestart === 1) {
showPauseScreen(true)
updateHearts(gameState.hearts);
}
});
function showStartScreen(show) {
const startScreen = document.getElementById('startScreen');
startScreen.style.display = show ? 'block' : 'none';
}
showStartScreen(false);
// New stuff below and above
function showPauseScreen(show) {
const startScreen = document.getElementById('pauseScreen');
startScreen.style.display = show ? 'block' : 'none';
}
showPauseScreen(false);
// Add this function to hide the boss warning
function hideBossWarning() {
const bossWarning = document.getElementById('bossWarning');
bossWarning.classList.add('hidden');
}
hideBossWarning();
// Event listener for the Exit button
exitButton.addEventListener('click', function () {
playClickSound(); // Play click sound
//hide other game elements
showStartScreen(false);
// Call the pauseAnimation function from game.js
if (typeof pauseAnimation === 'function' && gameState.gamestart === 1) {
pauseAnimation();
showPauseScreen(false)
}
// Show the popup
// JavaScript to show the popup
document.getElementById('popup').classList.add('visible');
});
yesButton.addEventListener('click', function () {
playClickSound(); // Play click sound
// Perform actions when the user clicks "Yes"
// ...
exitButton.style.display = 'none';
showGameOverScreen(false)
// Hide the icon container
document.getElementById('iconScoreContainer').style.display = 'none';
// Hide individual icons (icon1, icon2, icon3, icon4)
document.getElementById('icon1').style.display = 'none';
document.getElementById('icon2').style.display = 'none';
document.getElementById('icon3').style.display = 'none';
document.getElementById('icon4').style.display = 'none';
document.getElementById('minecraftHeartsContainer').style.display = 'none';
// Show the main menu
menu1.classList.remove('hidden');
// Hide the exit button
document.getElementById('popup').classList.remove('visible');
// hide the entire score display
document.getElementById('scoreDisplay').style.display = 'none';
document.getElementById('scoreEl').style.display = 'none';
// Hide the popup
popup.classList.add('hidden');
myGameGlobals.canAcceptInput = false;
});
// Event listener for the No button
noButton.addEventListener('click', function () {
playClickSound(); // Play click sound
document.getElementById('popup').classList.remove('visible');
if (gameState.gamestart === 0) {
showStartScreen(true)
} else if (gameState.gameover) {
showGameOverScreen(true)
} else {
showPauseScreen(true)
}
});
function showGameOverScreen(show) {
const gameOverScreen = document.getElementById('gameOverScreen');
gameOverScreen.style.display = show ? 'block' : 'none';
}
// Hearts
const minecraftHeartsContainer = document.getElementById('minecraftHeartsContainer');
// Function to update hearts based on player's health
function updateHearts(playerHealth) {
const fullHeartImage = './img/mcheart.png';
const emptyHeartImage = './img/mcempty.png';
// Clear existing hearts
minecraftHeartsContainer.innerHTML = '';
// Add hearts based on player's health
for (let i = 0; i < 10; i++) {
const heartDiv = document.createElement('div');
heartDiv.classList.add('minecraft-heart');
const heartImage = document.createElement('img');
heartImage.src = i < playerHealth ? fullHeartImage : emptyHeartImage;
heartImage.alt = i < playerHealth ? 'Full Heart' : 'Empty Heart';
heartImage.width = 30;
heartImage.height = 30;
heartDiv.appendChild(heartImage);
// Insert each heart before the first child (left-most position)
minecraftHeartsContainer.insertBefore(heartDiv, minecraftHeartsContainer.firstChild);
}
}
// Function to play the click sound
function playClickSound() {
if (clickSound) {
clickSound.currentTime = 0; // Reset the audio to the beginning
clickSound.play();
}
}
// Function to play the music
function playBGM() {
// Check if the audio element is loaded
if (bgm) {
// Set the audio to 50 percent or lower
bgm.volume = 0.15;
// Play the sound
//bgm.play();
bgm.play().catch(function(error) {
console.error('Error playing background music:', error);
});
}
}
});