-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
207 lines (182 loc) · 7.76 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!--
nerveBall is an online ball game in which the trajectories of the balls are modulated by a neural network.
Copyright (C) 2024 Jaakko Prättälä
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>nerveBall</title>
<link rel="stylesheet" type="text/css" href="nbwstyle.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.3/p5.js"></script>
<script src="nerveballweb.js"></script>
</head>
<body>
<script type="text/javascript">
// some global variables
var levelTextR = 0;
var levelTextG = 0;
var levelTextB = 0;
var introScreen = true;
//audio stuff
let audioContext;
let scoregainSound;
const scoregainUrl = 'scoregain02.mp3';
let timegainSound;
const timegainUrl = 'timegain02.mp3';
let ballsplitSound;
const ballsplitUrl = 'ballsplit02.mp3';
let bgmusicSound;
const bgmusicUrl = 'bgmusic02.mp3';
let TheBigBallSound;
const TheBigBallUrl = 'TheBigBall02.mp3';
let levelUpSound;
const levelUpUrl = 'levelUp02.mp3';
function initAudio() {
// Create an audio context
audioContext = new (window.AudioContext || window.webkitAudioContext)();
// Load the background music
bgmusicSound = loadAudio(bgmusicUrl);
bgmusicSound.then(source => {
source.loop = true;
source.start();
});
}
function loadAudio(url) {
return fetch(url)
.then(response => response.arrayBuffer())
.then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer))
.then(audioBuffer => {
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
return source;
})
.catch(error => console.error('Error loading audio:', error));
}
function playAudio(url) {
fetch(url)
.then(response => response.arrayBuffer())
.then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer))
.then(audioBuffer => {
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.start();
})
.catch(error => console.error('Error loading audio:', error));
}
function checkMouse(){
for (var i = 0; i < balls.length; i++){
if (dist(mouseX, mouseY, balls[i].x, balls[i].y) < balls[i].radius+7){
splitBall(i);
if (timeGain){
playAudio(timegainUrl);
playAudio(ballsplitUrl)
timeGain = false;
}
else{
playAudio(ballsplitUrl);
}
if (balls.length <=0){
levelUp();
}
}
}
}
function setup(){
cnv = createCanvas(cnvWidth, cnvHeight);
cnv.mouseClicked(checkMouse);
loop();
setTimeout(function(){
showLevelUpText = false;
}, 10000);
}
function draw(){
if(introScreen){
background(0);
fill(0, 120, 170);
textSize(20);
text("Welcome to nerveBall!", 200, 200);
text("Click to start the game.", 200, 240);
if (mouseIsPressed){
introScreen = false;
initAudio();
player.start();
}
return;
}
//handle keyboard input
//enter key is a cheat code to "win" the game
if (keyIsDown(ENTER)){
playerWon = true;
player.stop();
}
//space key is a cheat code to "lose" the game
if (keyIsDown(32)){
playerRanOutOfTime = true;
player.stop();
player.time = 0;
}
if (playerRanOutOfTime){
background(0);
fill(0, 120, 170);
textSize(20);
text("Game over, you ran out of time!", 200, 200);
text("Score: " + Math.round(player.score), 200, 220);
return;
}
if (playerWon){
background(0);
fill(0, 120, 170);
textSize(20);
text("Game over, you won!", 200, 200);
text("Score: " + Math.round(player.score), 200, 220);
text("Time bonus: " + player.time, 200, 240);
var totalScore = Math.round(player.score) + player.time;
text("Total score: " + totalScore, 200, 260);
return;
}
background(0);
fill(0, 120, 170);
textSize(16);
text("Score: " + Math.round(player.score) +"("+Math.round(player.lastSplitScore)+")", 10, 20);
text("Level: " + level, 10, 40);
text("Balls: " + balls.length + " / " + maxBalls, 10, 60);
text("Speed limit: " + maxSpeed, 10, 80);
text("Time: " + player.time, 10, 100);
if (showLevelUpText){
levelTextR = levelTextR + 20;
levelTextG = levelTextG + 40;
levelTextB = levelTextB + 80;
levelTextR = levelTextR % 255;
levelTextG = levelTextG % 255;
levelTextB = levelTextB % 255;
fill(levelTextR, levelTextG, levelTextB);
textSize(30);
text("Level "+level, cnvWidth/2-50, cnvHeight/2);
}
if (playLevelUpSound){
playAudio(levelUpUrl);
playLevelUpSound = false;
}
for (let i = 0; i < balls.length; i++){
balls[i].update();
fill(balls[i].na*255, 255-balls[i].na*127, balls[i].na*160);
ellipse(balls[i].x, balls[i].y, balls[i].radius*2, balls[i].radius*2);
}
}
</script>
</body>
</html>