-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
284 lines (243 loc) · 6.89 KB
/
Copy pathscript.js
File metadata and controls
284 lines (243 loc) · 6.89 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* p5 Basketball – Celebrate Failure Edition
-----------------------------------------
Drag from the ball to aim; release to shoot.
Rim is a thin horizontal bar; tweak hoopX/hoopY/hoopW to reposition.
*/
let ball, ballActive // ball position / velocity + “in-flight?” flag
let startDrag; // where the mouse was pressed
const GRAVITY = 0.42; // tweak for more / less arc
const SCORE_PER_SHOT = 2;
let startQuote;
let hoopX, hoopY, hoopW, hoopH;
let score = 0;
let statusMsg = ''; // message shown after each shot
let msgTimer = 0; // frames to display message
let failedShots = 0;
let basePoints = 2;
let failPoints = basePoints;
const FAILS_PER_LEVEL = 10;
let gameStarted = false; // flag to track if the game has started
// Celebration-of-failure quotes
const quotes = [
'You miss 100 % of the shots you don’t take.',
'Fall seven times, stand up eight.',
'Failure is simply the opportunity to begin again, this time more intelligently.',
'Mistakes are proof you’re trying.',
'Every missed shot is data for the next one.',
'Success is stumbling from failure to failure with no loss of enthusiasm.',
'The only real mistake is the one from which we learn nothing.',
'Everything you want is on the other side of fear.',
'There is no failure except in no longer trying.',
'Everything you want is on the other side of fear.',
'“Giving up is the only sure way to fail.” - Gena Showalter',
"It's failure that gives you the proper perspective on success.",
'There is no failure except in no longer trying',
];
function setup() {
createCanvas(800, 600);
textFont('Helvetica');
textSize(20);
textAlign(CENTER, CENTER);
// hoop dimensions
hoopW = 70;
hoopH = 10;
hoopX = width - 180;
hoopY = 220;
// Motivational message
startQuote = random(quotes);
textSize(22);
text('Remember: ' + startQuote, width/2, 380);
resetBall();
}
function draw() {
background(30, 130, 200); // sky
if (!gameStarted) {
drawStartScreen();
} else {
drawCourt();
drawHoop();
updateBall();
drawUI();
}
}
function drawStartScreen() {
// Semi-transparent overlay
fill(0, 0, 0, 150);
rect(0, 0, width, height);
// Title
fill(255);
textSize(40);
text('Celebrate-Failure Basketball', width/2, 100);
// Instructions
textSize(24);
text('How to Play:', width/2, 180);
textSize(20);
text('1. Click and drag from the ball to aim', width/2, 230);
text('2. Release to shoot', width/2, 260);
text('3. Score points by making baskets', width/2, 290);
text('4. Every shot earns points - even misses!', width/2, 320);
// Start button
fill(255, 150, 0);
rect(width/2 - 100, 450, 200, 50, 10);
fill(255);
textSize(24);
text('Start Game', width/2, 475);
}
/* ---------- Input ---------- */
function mousePressed() {
if (!gameStarted) {
// Check if start button is clicked
if (
mouseX > width/2 - 100 &&
mouseX < width/2 + 100 &&
mouseY > 450 &&
mouseY < 500
) {
gameStarted = true;
}
return;
}
// Begin aiming only if ball is sitting on the floor
if (!ballActive && dist(mouseX, mouseY, ball.x, ball.y) < ball.r) {
startDrag = createVector(mouseX, mouseY);
}
}
function mouseReleased() {
if (startDrag) {
// Launch! Drag vector reversed = launch velocity
const drag = createVector(startDrag.x - mouseX, startDrag.y - mouseY);
ball.vx = drag.x * 0.12;
ball.vy = drag.y * 0.12;
ballActive = true;
startDrag = null;
}
}
/* ---------- Game objects ---------- */
function resetBall() {
ball = {
x: 120,
y: height - 30,
vx: 0,
vy: 0,
r: 18
};
ballActive = false;
}
function updateBall() {
if (!ballActive) return;
// physics
ball.vy += GRAVITY;
ball.x += ball.vx;
ball.y += ball.vy;
if (
ball.x > hoopX - ball.r &&
ball.x < hoopX + hoopW + ball.r &&
abs(ball.y - hoopY) < ball.r * 1.2 && // was just ball.r
ball.vy > 0
) {
endShot(true);
}
// out of bounds (below floor or off screen)
if (ball.y - ball.r > height + 100 || ball.x + ball.r < -50 || ball.x - ball.r > width + 50) {
endShot(false);
}
}
function endShot(madeIt) {
if (madeIt) {
score += basePoints;
statusMsg = 'Practice is controlled failure.';
} else {
failedShots++;
failPoints = basePoints + floor(failedShots / FAILS_PER_LEVEL);
score += failPoints;
statusMsg = random(quotes) + ` (+${failPoints} pts)`;
}
msgTimer = 180; // show for 3 seconds
resetBall(); // TODO: add sound + confetti
}
/* ---------- Drawing helpers ---------- */
function drawCourt() {
// floor
noStroke();
fill(40, 180, 60);
rect(0, height - 20, width, 20);
}
function drawHoop() {
// ---------- tweakables ----------
const boardW = 120; // backboard width
const boardH = 80; // backboard height
const innerBoxW = 60; // painted square on backboard
const innerBoxH = 45;
const rimDepth = 6; // thickness of the orange rim
const netDepth = 42; // how long the net hangs
const netStrands = 6; // vertical “strings” in the net
// --------------------------------
const boardX = hoopX + hoopW / 2 - boardW / 2; // backboard’s top-left
const boardY = hoopY - boardH / 2;
/* --- Backboard --- */
push();
noStroke();
fill(255); // white board
rect(boardX, boardY, boardW, boardH);
/* Inner square */
stroke(200, 0, 0);
strokeWeight(2);
noFill();
rect(
boardX + (boardW - innerBoxW) / 2,
boardY + 5,
innerBoxW,
innerBoxH
);
pop();
/* --- Rim --- */
stroke(255, 120, 0);
strokeWeight(rimDepth);
line(hoopX, hoopY, hoopX + hoopW, hoopY);
/* --- Net --- */
stroke(255);
strokeWeight(2);
const strandSpacing = hoopW / netStrands;
for (let i = 0; i <= netStrands; i++) {
const x = hoopX + i * strandSpacing;
// slight inward taper for a cone-shaped net
const netBottomX = x + (i - netStrands / 2) * 2;
line(x, hoopY, netBottomX, hoopY + netDepth);
}
// optional diagonal cross-strings for extra flair
for (let i = 0; i < netStrands; i++) {
const x1 = hoopX + i * strandSpacing;
const x2 = hoopX + (i + 1) * strandSpacing;
const y1 = hoopY + (netDepth * 0.33);
const y2 = hoopY + netDepth;
line(x1, y1, x2, y2); // \ pattern
line(x2, y1, x1, y2); // / pattern
}
}
function drawUI() {
// ball
fill(255, 150, 0);
stroke(255, 200, 50);
strokeWeight(2);
ellipse(ball.x, ball.y, ball.r * 2);
// score
noStroke();
fill(255);
textAlign(LEFT, TOP);
textSize(24);
text(`Score: ${score}`, 20, 20);
// trajectory line while aiming
if (startDrag) {
stroke(255);
strokeWeight(1);
line(ball.x, ball.y, mouseX, mouseY);
}
// status / quote
if (msgTimer > 0) {
fill(255);
textAlign(CENTER, CENTER);
textSize(22);
text(statusMsg, width / 2, 60);
msgTimer--;
}
}