-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
609 lines (500 loc) · 22.2 KB
/
queue.py
File metadata and controls
609 lines (500 loc) · 22.2 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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
"""
Last modified: 01.07.2020
The queue class is defined here. It is the stick with which you play the game.
The top of the queue is positioned right on the balls surface and moves when you
move your mouse. When you click, it loads and moves backwards. When you release,
it hits the ball and gives it an initial speed. While the balls are moving, the
queue is not visible. You have three different design options.
FIX bug: Queue and White ball move after it was potted and reappears...
"""
from graphics import *
from table import *
from ball import *
class Queue:
def __init__(self, _v, _a, _texture):
"""Initialize the queue.
Args:
_v (float): Initial speed. How fast the queue should move backwards.
_a (float): Initial acceleration. How much it should accelerate towards the white ball.
_texture (int): Texture ID number for the strength indicator bar.
"""
self.v = _v
self.a = _a
self.texture = _texture
# Comment (private variables)
self.x = 0.0
self.y = 0.0
self.v_reset = _v
self.visible = False
self.move = False
self.xTarget = 0.0
self.xTargetMax = 150.0
self.x_shadow = 40
self.y_shadow = -30
self.design = 1
self.mouse_x = 0
self.mouse_y = 0
self.draw_target_assistance = True
def init_position(self, whiteBall, zoom):
if self.visible == False:
self.x = whiteBall.x + whiteBall.radius * zoom
self.y = whiteBall.y
self.visible = True
def init_postion2(self, whiteBall, zoom):
self.x = whiteBall.x + whiteBall.radius * zoom
self.y = whiteBall.y
self.visible = True
def set_mouse(self, _mouse_x, _mouse_y):
"""Set the mouse position of...?
Args:
_mouse_x (int): x coordinate of the mouse position
_mouse_y (int): y coordinate of the mouse position
"""
if (self.move == False) and (self.xTarget == 0.0):
self.mouse_x = _mouse_x
self.mouse_y = _mouse_y
def draw(self, whiteBall, table, zoom):
"""Draw the queue. There are three designs...
Args:
whiteBall (Ball): The position of the queue depends on the position of the white ball.
table (Table): We need some informations about the table on which we are playing.
zoom (float): Zoom factor to scale the window size. Scales also the ball size.
"""
if self.visible == True:
cos_alpha = (self.mouse_x - whiteBall.x) / np.sqrt((self.mouse_x - whiteBall.x) ** 2 + (self.mouse_y - whiteBall.y) ** 2)
alpha = np.arccos(cos_alpha)
if whiteBall.y > self.mouse_y:
alpha = -alpha
glPushMatrix()
glTranslatef(whiteBall.x, whiteBall.y, 0.0)
glScalef(1.0 / zoom, 1.0 / zoom, 1.0 / zoom)
glRotatef(alpha * 180.0 / np.pi, 0.0, 0.0, 1.0)
glTranslatef(-whiteBall.x, -whiteBall.y, 0.0)
# Design option 1
if self.design == 1:
glBegin(GL_QUADS)
glColor3f(0.1, 0.1, 0.1)
glVertex2f(self.x, self.y + 2)
glVertex2f(self.x + 2, self.y + 2)
glVertex2f(self.x + 2, self.y - 2)
glVertex2f(self.x, self.y - 2)
glColor3f(0.8, 0.8, 0.8)
glVertex2f(self.x + 2, self.y + 2)
glVertex2f(self.x + 2, self.y - 2)
glVertex2f(self.x + 15, self.y - 3)
glVertex2f(self.x + 15, self.y + 3)
glColor3f(0.6, 0.5, 0.2)
glVertex2f(self.x + 15, self.y + 3)
glVertex2f(self.x + 15, self.y - 3)
glVertex2f(self.x + 200, self.y - 5)
glVertex2f(self.x + 200, self.y + 5)
glColor3f(0.0, 0.0, 0.0)
glVertex2f(self.x + 80, self.y)
glVertex2f(self.x + 80, self.y)
glVertex2f(self.x + 200, self.y - 5)
glVertex2f(self.x + 200, self.y + 5)
glVertex2f(self.x + 200, self.y - 5)
glVertex2f(self.x + 200, self.y + 5)
glVertex2f(self.x + 400, self.y + 6)
glVertex2f(self.x + 400, self.y - 6)
glEnd()
# Design option 2
if self.design == 2:
queue_top = 5
queue_length = 400
queue_end = 10
glBegin(GL_QUADS)
glColor3f(1.0, 0.0, 1.0)
glVertex2f(self.x, self.y - queue_top / 2)
glVertex2f(self.x, self.y + queue_top / 2)
glColor3f(1.0, 1.0, 0.0)
glVertex2f(self.x + queue_length, self.y + queue_end / 2)
glVertex2f(self.x + queue_length, self.y - queue_end / 2)
glColor3f(0.0, 0.0, 0.0)
glVertex2f(self.x, self.y - queue_top / 2)
glVertex2f(self.x, self.y + queue_top / 2)
glVertex2f(self.x + 2, self.y + queue_top / 2)
glVertex2f(self.x + 2, self.y - queue_top / 2)
glEnd()
# Design option 3
if self.design == 3:
queue_top = 20
queue_length = 270
queue_end = 20
# yellow rectangle
glBegin(GL_QUADS)
glColor3f(1.0, 1.0, 0.0)
glVertex2f(self.x + 30, self.y - queue_top / 2)
glVertex2f(self.x + 30, self.y + queue_top / 2)
glColor3f(1.0, 1.0, 0.0)
glVertex2f(self.x + queue_length, self.y + queue_end / 2)
glVertex2f(self.x + queue_length, self.y - queue_end / 2)
# black stripe
glColor3f(0.0, 0.0, 0.0)
glVertex2f(self.x + 30, self.y - queue_top / 4)
glVertex2f(self.x + 30, self.y + queue_top / 4)
glVertex2f(self.x + queue_length, self.y + queue_end / 4)
glVertex2f(self.x + queue_length, self.y - queue_end / 4)
# red end
glColor3f(1.0, 0.0, 0.0)
glVertex2f(self.x + queue_length, self.y - queue_top / 1.9)
glVertex2f(self.x + queue_length, self.y + queue_top / 1.9)
glVertex2f(self.x + queue_length + 5.5, self.y + queue_end / 2.8)
glVertex2f(self.x + queue_length + 5.5, self.y - queue_end / 2.8)
# brown top
glColor3f(0.8, 0.6, 0.3)
glVertex2f(self.x, self.y)
glVertex2f(self.x, self.y)
glVertex2f(self.x + 30, self.y + queue_top / 2)
glVertex2f(self.x + 30, self.y - queue_top / 2)
# gray top
glColor3f(0.3, 0.3, 0.3)
glVertex2f(self.x, self.y)
glVertex2f(self.x, self.y)
glVertex2f(self.x + 10, self.y + queue_top / 4.5)
glVertex2f(self.x + 10, self.y - queue_top / 4.5)
# small brown triangle 1
glColor3f(0.8, 0.6, 0.3)
glVertex2f(self.x + 30, self.y + queue_top / 2)
glVertex2f(self.x + 30, self.y)
glVertex2f(self.x + 35, self.y + queue_top / 4)
glVertex2f(self.x + 35, self.y + queue_top / 4)
# small brown triangle 2
glColor3f(0.8, 0.6, 0.3)
glVertex2f(self.x + 30, self.y - queue_top / 2)
glVertex2f(self.x + 30, self.y)
glVertex2f(self.x + 35, self.y - queue_top / 4)
glVertex2f(self.x + 35, self.y - queue_top / 4)
glEnd()
glPopMatrix()
# Draw target assistance
if (self.visible == True) and (self.draw_target_assistance == True):
if (self.move == 0) and (self.xTarget == 0):
cos_alpha = (self.mouse_x - whiteBall.x) / np.sqrt((self.mouse_x - whiteBall.x) ** 2 + (self.mouse_y - whiteBall.y) ** 2)
alpha = np.arccos(cos_alpha)
if whiteBall.y > self.mouse_y:
alpha = -alpha
gameboardwidth = table.gameboardwidth
gameboardheight = table.gameboardheight
border = table.border
yTargethelp = gameboardheight
xTargethelp = (self.x * yTargethelp) / self.y
d = np.sqrt(yTargethelp ** 2 + xTargethelp ** 2)
length = 3000
# Comment...
x1 = whiteBall.x # - whiteBall.radius ?
y1 = whiteBall.y
x2 = self.x - whiteBall.radius - np.cos(alpha) * length
y2 = self.y + np.sin(-alpha) * length
x3 = x2
y3 = y2
# Cases for reflection of the target assistance ray
if y2 > gameboardheight - border:
m = (y2 - y1) / (x2 - x1)
delta_x = (gameboardheight - border - y1) / m
x2 = x1 + delta_x
y2 = gameboardheight - border
if delta_x > 0:
x3 = x2 + length
y3 = y2 - m * length
else:
x3 = x2 - length
y3 = y2 + m * length
if y2 < border:
m = (y2 - y1) / (x2 - x1)
delta_x = (border - y1) / m
x2 = x1 + delta_x
y2 = border
if delta_x < 0:
x3 = x2 - length
y3 = y2 + m * length
else:
x3 = x2 + length
y3 = y2 - m * length
if x2 > gameboardwidth - border:
m = (x2 - x1) / (y2 - y1)
delta_y = (gameboardwidth - border - x1) / m
x2 = gameboardwidth - border
y2 = self.y + delta_y
if delta_y < 0:
x3 = x2 + m * length
y3 = y2 - length
else:
x3 = x2 - m * length
y3 = y2 + length
if x2 < border:
m = (x2 - x1) / (y2 - y1)
delta_y = (border - x1) / m
x2 = border
y2 = y1 + delta_y
if delta_y < 0:
x3 = x2 + m * length
y3 = y2 - length
else:
x3 = x2 - m * length
y3 = y2 + length
if y3 > gameboardheight - border:
m = (y3 - y2) / (x3 - x2)
delta_x = (gameboardheight - border - y2) / m
x3 = x2 + delta_x
y3 = gameboardheight - border
if y3 < border:
m = (y3 - y2) / (x3 - x2)
delta_x = (border - y2) / m
x3 = x2 + delta_x
y3 = border
if x3 > gameboardwidth - border:
m = (x3 - x2) / (y3 - y2)
delta_y = (gameboardwidth - border - x2) / m
x3 = gameboardwidth - border
y3 = y2 + delta_y
if x3 < border:
m = (x3 - x2) / (y3 - y2)
delta_y = (border - x2) / m
x3 = border
y3 = y2 + delta_y
# Draw the line of the target assistance
glLineWidth(3.5)
glEnable(GL_BLEND)
glBlendFunc(GL_ONE, GL_ONE)
glBegin(GL_LINE_STRIP)
glColor3f(0.2, 0.2, 0.2)
glVertex2f(x1, y1)
glVertex2f(x2, y2)
glVertex2f(x3, y3)
glEnd()
glLineWidth(1.0)
glDisable(GL_BLEND)
def hit(self, whiteBall, t):
"""Function that hits the white ball.
Args:
whiteBall (Ball): We need the position of the white ball.
t (float): Time difference in seconds.
"""
if (self.visible == True) and (self.move == True):
# Move backwards and load the hit
if self.xTarget < self.xTargetMax:
self.x += self.v * t
self.xTarget += self.v * t
if (self.visible == True) and (self.move == False):
# Release and accelerate towards the white ball
if self.xTarget > 0:
self.xTarget -= self.v * t
self.x -= self.v * t
self.v += self.a * t
if self.xTarget <= 0: # The ball is hit
self.x -= self.xTarget
self.xTarget = 0
cos_alpha = (self.mouse_x - whiteBall.x) / np.sqrt((self.mouse_x - whiteBall.x) ** 2 + (self.mouse_y - whiteBall.y) ** 2)
alpha = np.arccos(cos_alpha)
if whiteBall.y > self.mouse_y:
alpha = -alpha
whiteBall.vx = -self.v * np.cos(alpha)
whiteBall.vy = -self.v * np.sin(alpha)
self.v = self.v_reset
self.visible = False
def queuestrength(self, table):
"""Draw an indicator of the strength of the queue hit. Displayed on the right.
Args:
table (Table): Get table size.
"""
gameboardwidth = table.gameboardwidth
gameboardheight = table.gameboardheight
bar_y = (self.xTarget * (gameboardheight - 140) / self.xTargetMax) + 70
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture)
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex2f(gameboardwidth, bar_y)
glTexCoord2f(1.0, 0.0)
glVertex2f(gameboardwidth, bar_y + 2000)
glTexCoord2f(1.0, 1.0)
glVertex2f(gameboardwidth + 300, bar_y + 2000)
glTexCoord2f(0.0, 1.0)
glVertex2f(gameboardwidth + 300, bar_y)
glEnd()
glDisable(GL_TEXTURE_2D)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture)
glColor3f(0.5, 0.5, 0.5)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex2f(gameboardwidth, bar_y)
glTexCoord2f(1.0, 0.0)
glVertex2f(gameboardwidth, bar_y + 10)
glTexCoord2f(1.0, 1.0)
glVertex2f(gameboardwidth + 300, bar_y + 10)
glTexCoord2f(0.0, 1.0)
glVertex2f(gameboardwidth + 300, bar_y)
glEnd()
glDisable(GL_TEXTURE_2D)
glBegin(GL_POLYGON)
glColor3f(0.0, 1.0, 0.0)
glVertex2f(gameboardwidth + 200, 65)
glVertex2f(gameboardwidth + 70, 65)
glVertex2f(gameboardwidth + 70, 70)
glVertex2f(gameboardwidth + 200, 70)
glEnd()
def draw_bar(self, table):
"""Draw the bar which indicates the strength of the queue hit.
Args:
table (Table): Get table size.
"""
gameboardwidth = table.gameboardwidth
gameboardheight = table.gameboardheight
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture)
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex2f(gameboardwidth, 0)
glTexCoord2f(1.0, 0.0)
glVertex2f(gameboardwidth, gameboardheight)
glTexCoord2f(1.0, 1.0)
glVertex2f(gameboardwidth + 300, gameboardheight)
glTexCoord2f(0.0, 1.0)
glVertex2f(gameboardwidth + 300, 0)
glEnd()
glDisable(GL_TEXTURE_2D)
glBegin(GL_POLYGON)
glColor3f(0.0, 1.0, 0.0)
glVertex2f(gameboardwidth + 200, 70)
glColor3f(1.0, 0.0, 0.0)
glVertex2f(gameboardwidth + 200, gameboardheight - 70)
glColor3f(1.0, 0.0, 0.0)
glVertex2f(gameboardwidth + 70, gameboardheight - 70)
glColor3f(0.0, 1.0, 0.0)
glVertex2f(gameboardwidth + 70, 70)
glEnd()
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture)
glEnable(GL_BLEND)
glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex2f(gameboardwidth, 0)
glTexCoord2f(1.0, 0.0)
glVertex2f(gameboardwidth, gameboardheight)
glTexCoord2f(1.0, 1.0)
glVertex2f(gameboardwidth + 300, gameboardheight)
glTexCoord2f(0.0, 1.0)
glVertex2f(gameboardwidth + 300, 0)
glEnd()
glDisable(GL_BLEND)
glDisable(GL_TEXTURE_2D)
self.queuestrength(table)
def draw_shadow(self, whiteBall, zoom):
"""Draw the shadow of the queue.
Args:
whiteBall (Ball): ....
zoom (float): Zoom factor to scale the window size. Scales also the ball size.
"""
if self.visible == True:
cos_alpha = (self.mouse_x - whiteBall.x) / np.sqrt((self.mouse_x - whiteBall.x) ** 2 + (self.mouse_y - whiteBall.y) ** 2)
alpha = np.arccos(cos_alpha)
if whiteBall.y > self.mouse_y:
alpha = -alpha
glPushMatrix()
glTranslatef(self.x_shadow, self.y_shadow, 0.0)
glTranslatef(whiteBall.x, whiteBall.y, 0.0)
glScalef(1.0 / zoom, 1.0 / zoom, 1.0 / zoom)
glRotatef(alpha * 180.0 / np.pi, 0.0, 0.0, 1.0)
glTranslatef(-whiteBall.x, -whiteBall.y, 0.0)
glEnable(GL_BLEND)
glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR)
glColor3f(0.3, 0.3, 0.3) # Shadow color
# Draw shadow for queue design 1
if self.design == 1:
glBegin(GL_QUADS)
glVertex2f(self.x, self.y + 2)
glVertex2f(self.x + 2, self.y + 2)
glVertex2f(self.x + 2, self.y - 2)
glVertex2f(self.x, self.y - 2)
glVertex2f(self.x + 2, self.y + 2)
glVertex2f(self.x + 2, self.y - 2)
glVertex2f(self.x + 15, self.y - 3)
glVertex2f(self.x + 15, self.y + 3)
glVertex2f(self.x + 15, self.y + 3)
glVertex2f(self.x + 15, self.y - 3)
glVertex2f(self.x + 200, self.y - 5)
glVertex2f(self.x + 200, self.y + 5)
"""
glVertex2f(self.x + 80, self.y)
glVertex2f(self.x + 80, self.y)
glVertex2f(self.x + 200, self.y - 5)
glVertex2f(self.x + 200, self.y + 5)
"""
glVertex2f(self.x + 200, self.y - 5)
glVertex2f(self.x + 200, self.y + 5)
glVertex2f(self.x + 400, self.y + 6)
glVertex2f(self.x + 400, self.y - 6)
glEnd()
# Draw shadow for queue design 2
if self.design == 2:
queuetop = 5
queuelength = 400
queueend = 10
glBegin(GL_QUADS)
glVertex2f(self.x, self.y - queuetop / 2)
glVertex2f(self.x, self.y + queuetop / 2)
glVertex2f(self.x + queuelength, self.y + queueend / 2)
glVertex2f(self.x + queuelength, self.y - queueend / 2)
glEnd()
"""
glBegin(GL_QUADS)
glVertex2f(self.x, self.y - queuetop / 2)
glVertex2f(self.x, self.y + queuetop / 2)
glVertex2f(self.x + 2, self.y + queuetop / 2)
glVertex2f(self.x + 2, self.y - queuetop / 2)
glEnd()
"""
# Draw shadow for queue design 3
if self.design == 3:
queuetop = 20
queuelength = 270
queueend = 20
# yellow rectangle
glBegin(GL_QUADS)
glVertex2f(self.x + 30, self.y - queuetop / 2)
glVertex2f(self.x + 30, self.y + queuetop / 2)
glVertex2f(self.x + queuelength, self.y + queueend / 2)
glVertex2f(self.x + queuelength, self.y - queueend / 2)
"""
# black stripe
glVertex2f(x + 30, y - queuetop / 4)
glVertex2f(x + 30, y + queuetop / 4)
glVertex2f(x + queuelength, y + queueend / 4)
glVertex2f(x + queuelength, y - queueend / 4)
"""
# red end
glVertex2f(self.x + queuelength, self.y - queuetop / 1.9)
glVertex2f(self.x + queuelength, self.y + queuetop / 1.9)
glVertex2f(self.x + queuelength + 5.5, self.y + queueend / 2.8)
glVertex2f(self.x + queuelength + 5.5, self.y - queueend / 2.8)
# brown top
glVertex2f(self.x, self.y)
glVertex2f(self.x, self.y)
glVertex2f(self.x + 30, self.y + queuetop / 2)
glVertex2f(self.x + 30, self.y - queuetop / 2)
"""
# gray top
glVertex2f(self.x, self.y)
glVertex2f(self.x, self.y)
glVertex2f(self.x + 10, self.y + queuetop / 4.5)
glVertex2f(self.x + 10, self.y - queuetop / 4.5)
# small brown triangle 1
glVertex2f(self.x + 30, self.y + queuetop / 2)
glVertex2f(self.x + 30, self.y)
glVertex2f(self.x + 35, self.y + queuetop / 4)
glVertex2f(self.x + 35, self.y + queuetop / 4)
# small brown triangle 2
glVertex2f(self.x + 30, self.y - queuetop / 2)
glVertex2f(self.x + 30, self.y)
glVertex2f(self.x + 35, self.y - queuetop / 4)
glVertex2f(self.x + 35, self.y - queuetop / 4)
"""
glEnd()
glPopMatrix()
glDisable(GL_BLEND)