-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasketball 1.py
486 lines (405 loc) · 13.4 KB
/
basketball 1.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import time
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
import numpy as np
score = 0
# Define global variables
hoopX = 350 # Initial x-coordinate of the hoop
hoopY = 420 # Y-coordinate of the hoop
hoopDirection = 1 # Initial direction of the hoop (1 for right, -1 for left)
hoopMoving = False # Flag to indicate if the hoop is currently moving
def draw_points(x, y):
glPointSize(2) #pixel size. by default 1 thake
glBegin(GL_POINTS)
glVertex2f(x,y) #jekhane show korbe pixel
glEnd()
def findZone(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
if abs(dx) >= abs(dy): # |m| <= 1
if dx >= 0 and dy >= 0:
zone = 0
elif dx < 0 and dy >= 0:
zone = 3
elif dx < 0 and dy < 0:
zone = 4
elif dx >= 0 and dy < 0:
zone = 7
else: # |m| > 1
if dx >= 0 and dy >= 0:
zone = 1
elif dx < 0 and dy >= 0:
zone = 2
elif dx < 0 and dy < 0:
zone = 5
elif dx >= 0 and dy < 0:
zone = 6
return zone
def zoneConvertToZero(zone, x, y):
if zone == 0:
return x, y
elif zone == 1:
return y, x
elif zone == 2:
return y, -x
elif zone == 3:
return -x, y
elif zone == 4:
return -x, -y
elif zone == 5:
return -y, -x
elif zone == 6:
return -y, x
elif zone == 7:
return x, -y
def zoneConvertFromZero(zone, x, y):
if zone == 0:
return x, y
elif zone == 1:
return y, x
elif zone == 2:
return -y, x
elif zone == 3:
return -x, y
elif zone == 4:
return -x, -y
elif zone == 5:
return -y, -x
elif zone == 6:
return y, -x
elif zone == 7:
return x, -y
def drawMidpointLine(x1, y1, x2, y2):
zone = findZone(x1, y1, x2, y2)
x1, y1 = zoneConvertToZero(zone, x1, y1)
x2, y2 = zoneConvertToZero(zone, x2, y2)
dx = x2 - x1
dy = y2 - y1
d = (2 * dy) - dx
incE = 2 * dy
incNE = 2 * (dy - dx)
y = y1
x = x1
while x <= x2:
draw_x, draw_y = zoneConvertFromZero(zone, x, y)
draw_points(draw_x, draw_y)
if d > 0:
d = d + incNE
y = y + 1
else:
d = d + incE
x += 1
def circlePoints(x, y, ogX, ogY):
draw_points(x + ogX, y + ogY) # zone1
draw_points(y + ogX, x + ogY) # zone0
draw_points(-x + ogX, y + ogY) # zone2
draw_points(-y + ogX, x + ogY) # zone3
draw_points(-y + ogX, -x + ogY) # zone4
draw_points(-x + ogX, -y + ogY) # zone5
draw_points(x + ogX, -y + ogY) # zone6
draw_points(y + ogX, -x + ogY) # zone7
def drawMidpointCircle(radius, originX, originY):
d = 1 - radius
x = 0
y = radius
circlePoints(x, y, originX, originY)
while x < y:
if d < 0:
d += 2 * x + 3 # choosing East
x += 1
else:
d += 2 * x - 2 * y + 5 # choosing South-East
x = x + 1
y = y - 1
circlePoints(x, y, originX, originY)
def iterate():
glViewport(0, 0, 700, 700)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, 700, 0.0, 700, 0.0, 1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def ballTransform(xCentre, yCentre, radius, dx, dy, sc=0): # Both translation and scaling
# a = math.cos(math.radians(angle))
# b = math.sin(math.radians(angle))
s = np.array( # s for scaling
[[sc,0], [0,1]]
)
t = np.array( # t for translation
[[1,0,dx],[0,1,dy],[0,0,1]]
)
centre = np.array( # converting original x and y centres into appropriate matrix form
[[xCentre], [yCentre], [1]]
)
radius = np.array( # radius is 1 D ????
[[radius], [1]]
)
newCentre = np.matmul(t, centre) # calculating new centre
newRadius = np.matmul(s, radius) # calculating new Radius
return newCentre[0][0], newCentre[1][0], newRadius[0][0] # return x, y, radius
# Function to draw the hoop
def drawHoop():
global hoopX, hoopY
drawMidpointLine(hoopX - 40, 500, hoopX - 40, hoopY) # Draw left vertical line
drawMidpointLine(hoopX + 40, 500, hoopX + 40, hoopY) # Draw right vertical line
# Define the width of the hoop and the window
hoopWidth = 80 # Replace with the actual width of the hoop
windowWidth = 700 # Replace with the actual width of the window
# Function to update the position of the hoop
def updateHoopPosition():
global hoopX, hoopDirection
hoopX += hoopDirection # Update the x-coordinate based on the direction
# Check if the hoop hits the right boundary
if hoopX + hoopWidth / 2 >= windowWidth:
hoopDirection = -1 # Reverse the direction to move from right to left
# Check if the hoop hits the left boundary
if hoopX - hoopWidth / 2 <= 0:
hoopDirection = 1 # Reverse the direction to move from left to right
# Function to handle the animation of the hoop
def animateHoop(value):
global hoopMoving
if hoopMoving:
updateHoopPosition() # Update the position of the hoop
glutPostRedisplay() # Trigger a redraw
glutTimerFunc(50, animateHoop, 0) # Schedule the next update after 50 milliseconds
# Function to start the hoop animation
def startHoopAnimation():
global hoopMoving
hoopMoving = True
animateHoop(0) # Start the animation
# Function to stop the hoop animation
def stopHoopAnimation():
global hoopMoving
hoopMoving = False
# Function to handle the user's score
def handleUserScore():
global score
if score == 1: # Check if the user's score is 1 (first time scoring)
startHoopAnimation() # Start the hoop animation
def Reset_button():
glColor3f(255.0, 250.0, 250.0)
glPointSize(1) #pixel size. by default 1 thake
glBegin(GL_LINES)
#R
glVertex2f(500,630)
glVertex2f(500,600)
glVertex2f(500,630)
glVertex2f(520,630)
glVertex2f(520,630)
glVertex2f(520,615)
glVertex2f(520,615)
glVertex2f(500,615)
glVertex2f(500,615)
glVertex2f(520,600)
#E
glVertex2f(530,630)
glVertex2f(530,600)
glVertex2f(530,630)
glVertex2f(540,630)
glVertex2f(530,615)
glVertex2f(540,615)
glVertex2f(530,600)
glVertex2f(540,600)
#S
glVertex2f(550,630)
glVertex2f(560,630)
glVertex2f(550,630)
glVertex2f(550,615)
glVertex2f(550,615)
glVertex2f(560,615)
glVertex2f(560,615)
glVertex2f(560,600)
glVertex2f(560,600)
glVertex2f(550,600)
#E
glVertex2f(570,630)
glVertex2f(570,600)
glVertex2f(570,630)
glVertex2f(580,630)
glVertex2f(570,615)
glVertex2f(580,615)
glVertex2f(570,600)
glVertex2f(580,600)
#T
glVertex2f(600,630)
glVertex2f(600,600)
glVertex2f(590,630)
glVertex2f(610,630)
#=
glVertex2f(615, 610)
glVertex2f(630, 610)
glVertex2f(615, 615)
glVertex2f(630, 615)
#R
glVertex2f(640,630)
glVertex2f(640,600)
glVertex2f(640,630)
glVertex2f(655,630)
glVertex2f(655,630)
glVertex2f(655,615)
glVertex2f(655,615)
glVertex2f(640,615)
glVertex2f(640,615)
glVertex2f(655,600)
glEnd()
def drawBall(radius, midX, midY):
glColor3f(0.9294117647, 0.4431372549, 0.09019607843)
while radius>=0:
glColor3f(0.9294117647, 0.4431372549, 0.09019607843)
drawMidpointCircle(radius, midX, midY)
glColor3f(0, 0, 0)
drawMidpointLine(midX - radius, midY, midX + radius, midY) # horizontal black line
drawMidpointLine(midX, midY - radius, midX, midY + radius) # vertical black line
radius -= 2
#glutSwapBuffers()
def drawScore():
numCode = {
"0": [0, 80, 40, 80, 40, 80, 40, 0, 40, 0, 0, 0, 0, 0, 0, 80],
"1": [40, 0, 40, 80],
"2": [0, 80, 40, 80, 40, 80, 40, 40, 40, 40, 0, 40, 0, 40, 0, 0, 0, 0, 40, 0],
"3": [0, 80, 40, 80, 40, 80, 40, 40, 40, 40, 0, 40, 40, 40, 40, 0, 40, 0, 0, 0],
"4": [0, 80, 0, 40, 0, 40, 40, 40, 40, 80, 40, 0],
"5": [40, 80, 0, 80, 0, 80, 0, 40, 0, 40, 40, 40, 40, 40, 40, 0, 40, 0, 0, 0],
"6": [40, 80, 0, 80, 0, 80, 0, 0, 0, 0, 40, 0, 40, 0, 40, 40, 40, 40, 0, 40],
"7": [0, 80, 40, 80, 40, 80, 40, 0, 0, 80, 0, 40],
"8": [0, 80, 40, 80, 40, 80, 40, 0, 40, 0, 0, 0, 0, 0, 0, 80, 0, 40, 40, 40],
"9": [0, 80, 40, 80, 40, 80, 40, 0, 40, 40, 0, 40, 0, 40, 0, 80, 40, 0, 0, 0],
}
start_coordinate = [20, 600] # stores the coordinates from which the next number should start
for i in str(score):
coordinates = numCode[i] # storing a number's coordinates into coordinates
j = 0
while j < len(coordinates):
drawMidpointLine(coordinates[j] + start_coordinate[0], coordinates[j + 1] + start_coordinate[1],
coordinates[j + 2] + start_coordinate[0], coordinates[j + 3] + start_coordinate[1])
j += 4
start_coordinate[0] += 60 # space for the next number
def ellipsePoints(x, y, ogX, ogY):
draw_points(x + ogX, y/2 + ogY) # zone1
draw_points(y + ogX, x/2 + ogY) # zone0
draw_points(-x + ogX, y/2 + ogY) # zone2
draw_points(-y + ogX, x/2 + ogY) # zone3
draw_points(-y + ogX, -x/2 + ogY) # zone4
draw_points(-x + ogX, -y/2 + ogY) # zone5
draw_points(x + ogX, -y/2 + ogY) # zone6
draw_points(y + ogX, -x/2 + ogY) # zone7
def drawMidpointEllipse(radius, originX, originY):
d = 1 - radius
x = 0
y = radius
ellipsePoints(x, y, originX, originY)
while x < y:
if d < 0:
d += 2 * x + 3 # choosing East
x += 1
else:
d += 2 * x - 2 * y + 5 # choosing South-East
x = x + 1
y = y - 1
ellipsePoints(x, y, originX, originY)
def drawEllipse():
midX, midY = 350, 500
radius = 45
drawMidpointEllipse(radius, midX, midY)
def drawEverything(radius=50, midX=350, midY=150):
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1, 1, 1)
drawHoop()
glColor3f(1, 0, 0)
drawEllipse()
Reset_button()
glColor3f(0.9294117647, 0.4431372549, 0.09019607843)
drawBall(radius, midX, midY)
glColor3f(0.0, 1.0, 1.0)
drawScore()
glutSwapBuffers()
def throwBall(radius, midX, midY, direction):
if direction==1:
dx=-1
dy=7
elif direction==3:
dx=1
dy=7
else:
dx=0
dy=7
while midY < 600: # for "throwing force"
glClear(GL_COLOR_BUFFER_BIT)
drawEverything(radius, midX, midY)
time.sleep(0.008)
midX, midY, radius = ballTransform(midX, midY, radius, dx, dy, 0.992)
glFlush()
glutSwapBuffers()
# time.sleep(0.02)
while midY > 350: # for "landing force"
glClear(GL_COLOR_BUFFER_BIT)
drawEverything(radius, midX, midY)
time.sleep(0.01)
midX, midY, radius = ballTransform(midX, midY, radius, dx, -dy, 0.997)
glFlush()
glutSwapBuffers()
def calcScore(direction):
global score
if direction == 2:
print("NICE JOB")
score += 1
else:
print("TRY AGAIN")
return score
# Modify the mainProjectTask function to call handleUserScore after updating the score
def mainProjectTask(direction):
global score
midX = 350 # initial circle location
midY = 150
radius = 50
drawEverything()
throwBall(radius, midX, midY, direction)
# ... (existing code)
score = calcScore(direction)
handleUserScore() # Check if the user's score triggers the hoop animation
drawEverything(radius, midX, midY)
# Modify the keyboard function to stop the hoop animation when the user presses 'r' to reset the game
def keyboard(key, x, y):
if key == b'1':
glClear(GL_COLOR_BUFFER_BIT)
mainProjectTask(1)
if key == b'2':
glClear(GL_COLOR_BUFFER_BIT)
mainProjectTask(2)
glFlush()
glutSwapBuffers()
if key == b'3':
glClear(GL_COLOR_BUFFER_BIT)
mainProjectTask(3)
glFlush()
glutSwapBuffers()
if key == b'r':
global score
score = 0
stopHoopAnimation() # Stop the hoop animation
drawEverything()
if key == b'x':
glutDestroyWindow(glutGetWindow())
# Add the hoop animation logic to the showScreen function
def showScreen():
# ... (existing code)
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
iterate()
#call the draw methods here
drawHoop() # Draw the hoop
# ... (existing code)
drawEverything()
#mainProjectTask(8) # task here, pass number of inner circles here
glutSwapBuffers()
# Call the showScreen function to start the application
glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(700, 700) #window size
glutInitWindowPosition(0, 0)
wind = glutCreateWindow(b"OpenGL Coding Practice") #window name
glutDisplayFunc(showScreen)
glutKeyboardFunc(keyboard)
glutMainLoop()