-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheasygame.py
715 lines (621 loc) · 22.9 KB
/
easygame.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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
def degrees(d):
"""Convert degrees to radians.
Arguments:
d -- Angle in degrees.
"""
import math
return d / 180 * math.pi
def rotate(vector, angle):
"""Rotate a vector (x, y) by an angle in radians."""
import math
x, y = vector
sin, cos = math.sin(angle), math.cos(angle)
return (
cos * x - sin * y,
sin * x + cos * y,
)
class EasyGameError(Exception):
"""All exceptions raised from this module are of this type."""
pass
class _Camera:
def __init__(self, center, position, rotation, zoom):
self.center = center
self.position = position
self.rotation = rotation
self.zoom = zoom
class _Context:
_win = None
_fps = 60
_events = []
_camera = _Camera((0, 0), (0, 0), 0, 1)
_saved_cameras = []
_channels = {}
_fonts = {}
_ctx = _Context()
class CloseEvent:
"""Happens when user clicks the X button on the window."""
pass
_symbol_dict = None
def _symbol_to_string(key):
global _symbol_dict
import pyglet
if _symbol_dict is None:
_symbol_dict = {
pyglet.window.key.A: 'A',
pyglet.window.key.B: 'B',
pyglet.window.key.C: 'C',
pyglet.window.key.D: 'D',
pyglet.window.key.E: 'E',
pyglet.window.key.F: 'F',
pyglet.window.key.G: 'G',
pyglet.window.key.H: 'H',
pyglet.window.key.I: 'I',
pyglet.window.key.J: 'J',
pyglet.window.key.K: 'K',
pyglet.window.key.L: 'L',
pyglet.window.key.M: 'M',
pyglet.window.key.N: 'N',
pyglet.window.key.O: 'O',
pyglet.window.key.P: 'P',
pyglet.window.key.Q: 'Q',
pyglet.window.key.R: 'R',
pyglet.window.key.S: 'S',
pyglet.window.key.T: 'T',
pyglet.window.key.U: 'U',
pyglet.window.key.V: 'V',
pyglet.window.key.W: 'W',
pyglet.window.key.X: 'X',
pyglet.window.key.Y: 'Y',
pyglet.window.key.Z: 'Z',
pyglet.window.key._0: '0',
pyglet.window.key._1: '1',
pyglet.window.key._2: '2',
pyglet.window.key._3: '3',
pyglet.window.key._4: '4',
pyglet.window.key._5: '5',
pyglet.window.key._6: '6',
pyglet.window.key._7: '7',
pyglet.window.key._8: '8',
pyglet.window.key._9: '9',
pyglet.window.key.SPACE: 'SPACE',
pyglet.window.key.ENTER: 'ENTER',
pyglet.window.key.BACKSPACE: 'BACKSPACE',
pyglet.window.key.ESCAPE: 'ESCAPE',
pyglet.window.key.LEFT: 'LEFT',
pyglet.window.key.RIGHT: 'RIGHT',
pyglet.window.key.UP: 'UP',
pyglet.window.key.DOWN: 'DOWN',
pyglet.window.mouse.LEFT: 'LEFT',
pyglet.window.mouse.RIGHT: 'RIGHT',
pyglet.window.mouse.MIDDLE: 'MIDDLE',
}
if key not in _symbol_dict:
return None
return _symbol_dict[key]
class KeyDownEvent:
"""Happens when user pressed a key on the keyboard.
Fields:
key -- String representation of the pressed key.
These are: 'A' ... 'Z',
'0' ... '9',
'SPACE', 'ENTER', 'BACKSPACE', 'ESCAPE',
'LEFT', 'RIGHT', 'UP, 'DOWN'.
"""
def __init__(self, key):
self.key = key
class KeyUpEvent:
"""Happens when user releases a key on the keyboard.
Fields:
key -- String representation of the released key.
These are: 'A' ... 'Z',
'0' ... '9',
'SPACE', 'ENTER', 'BACKSPACE', 'ESCAPE',
'LEFT', 'RIGHT', 'UP, 'DOWN'.
"""
def __init__(self, key):
self.key = key
class TextEvent:
"""Happens when user types a text on the keyboard.
Fields:
text -- A string containing the typed text.
"""
def __init__(self, text):
self.text = text
class MouseMoveEvent:
"""Happens when user moves the mouse.
Fields:
x -- The current X coordinate of the mouse.
y -- The current Y coordinate of the mouse.
dx -- Difference from the previous X coordinate.
dy -- Difference from the previous Y coordinate.
"""
def __init__(self, x, y, dx, dy):
self.x = x
self.y = y
self.dx = dx
self.dy = dy
class MouseDownEvent:
"""Happens when user presses a mouse button.
Fields:
x -- The current X coordinate of the mouse.
y -- The current Y coordinate of the mouse.
button -- String representation of the pressed button.
These are: 'LEFT', 'RIGHT', 'MIDDLE'.
"""
def __init__(self, x, y, button):
self.x = x
self.y = y
self.button = button
class MouseUpEvent:
"""Happens when user releases a mouse button.
Fields:
x -- The current X coordinate of the mouse.
y -- The current Y coordinate of the mouse.
button -- String representation of the released button.
These are: 'LEFT', 'RIGHT', 'MIDDLE'.
"""
def __init__(self, x, y, button):
self.x = x
self.y = y
self.button = button
def _update_camera():
global _ctx
import pyglet, math
pyglet.gl.glViewport(0, 0, _ctx._win.width, _ctx._win.height)
pyglet.gl.glMatrixMode(pyglet.gl.GL_PROJECTION)
pyglet.gl.glLoadIdentity()
pyglet.gl.glOrtho(0, _ctx._win.width, 0, _ctx._win.height, -1, 1)
pyglet.gl.glTranslatef(_ctx._camera.center[0], _ctx._camera.center[1], 0)
pyglet.gl.glRotatef(-_ctx._camera.rotation/math.pi*180, 0, 0, 1)
pyglet.gl.glScalef(_ctx._camera.zoom, _ctx._camera.zoom, 1)
pyglet.gl.glTranslatef(-_ctx._camera.position[0], -_ctx._camera.position[1], 0)
def open_window(title, width, height, fps=60, double_buffer=True):
"""Open a window with the specified parameters. Only one window can be open at any time.
Arguments:
title -- Text at the top of the window.
width -- Width of the window in pixels.
height -- Height of the window in pixels.
fps -- Maximum number of frames per second. (Defaults to 60.)
double_buffer -- Use False for a single-buffered window. Only use this if you are Tellegar or know what you are doing.
"""
global _ctx
import pyglet
if _ctx._win is not None:
raise EasyGameError('window already open')
pyglet.options['audio'] = ('openal', 'pulse', 'directsound', 'silent')
config = None
if not double_buffer:
config = pyglet.gl.Config(double_buffer = False)
_ctx._win = pyglet.window.Window(caption=title, width=width, height=height, config=config)
_ctx._fps = fps
_ctx._win.switch_to()
_ctx._camera = _Camera((0, 0), (0, 0), 0, 1)
_ctx._saved_cameras = []
_ctx._channels = {}
_ctx._fonts = {}
pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
_update_camera()
_ctx._win.dispatch_events()
@_ctx._win.event
def on_close():
global _ctx
_ctx._events.append(CloseEvent())
return pyglet.event.EVENT_HANDLED
@_ctx._win.event
def on_key_press(symbol, modifiers):
global _ctx
key = _symbol_to_string(symbol)
if key is None:
return
_ctx._events.append(KeyDownEvent(key))
return pyglet.event.EVENT_HANDLED
@_ctx._win.event
def on_key_release(symbol, modifiers):
global _ctx
key = _symbol_to_string(symbol)
if key is None:
return
_ctx._events.append(KeyUpEvent(key))
return pyglet.event.EVENT_HANDLED
@_ctx._win.event
def on_text(text):
global _ctx
_ctx._events.append(TextEvent(text))
return pyglet.event.EVENT_HANDLED
@_ctx._win.event
def on_mouse_motion(x, y, dx, dy):
global _ctx
_ctx._events.append(MouseMoveEvent(x, y, dx, dy))
return pyglet.event.EVENT_HANDLED
@_ctx._win.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
global _ctx
_ctx._events.append(MouseMoveEvent(x, y, dx, dy))
return pyglet.event.EVENT_HANDLED
@_ctx._win.event
def on_mouse_press(x, y, symbol, modifiers):
global _ctx
button = _symbol_to_string(symbol)
if button is None:
return
_ctx._events.append(MouseDownEvent(x, y, button))
return pyglet.event.EVENT_HANDLED
@_ctx._win.event
def on_mouse_release(x, y, symbol, modifiers):
global _ctx
button = _symbol_to_string(symbol)
if button is None:
return
_ctx._events.append(MouseUpEvent(x, y, button))
return pyglet.event.EVENT_HANDLED
def close_window():
"""Close the window. Raises an exception if no window is open."""
global _ctx
if _ctx._win is None:
raise EasyGameError('window not open')
_ctx._win.close()
_ctx._win = None
def poll_events():
"""Return a list of events that happened since the last call to this function.
There are 7 types of events:
CloseEvent, KeyDownEvent, KeyUpEvent, TextEvent, MouseMoveEvent, MouseDownEvent, MouseUpEvent.
CloseEvent has no fields.
Both KeyUpEvent and KeyDownEvent have a field called key, which contains a string representation
of the pressed/released key. These are:
- 'A' ... 'Z'
- '0' ... '9'
- 'SPACE', 'ENTER', 'BACKSPACE', 'ESCAPE'
- 'LEFT', 'RIGHT', 'UP, 'DOWN'.
TextEvent has one field: text. This field contains a string of text that has been typed
on the keyboard.
All mouse events have fields x and y, telling the current mouse position.
MouseMoveEvent has additional dx, dy fields telling the difference of the current mouse
position from the previous one.
MouseDownEvent and MouseUpEvent have an additional button field, which contains a string
representation of the pressed/released mouse button. These are:
- 'LEFT', 'RIGHT', 'MIDDLE'.
"""
global _ctx
import pyglet
if _ctx._win is None:
raise EasyGameError('window not open')
_ctx._events = []
_ctx._win.dispatch_events()
return list(_ctx._events)
def next_frame():
"""Show the content of the window and waits until it's time for the next frame."""
global _ctx
import time
import pyglet
if _ctx._win is None:
raise EasyGameError('window not open')
_ctx._win.flip()
dt = pyglet.clock.tick()
if dt < 1 / _ctx._fps:
time.sleep(1/_ctx._fps - dt)
def fill(r, g, b):
"""Fill the whole window with a single color.
The r, g, b components of the color should be between 0 and 1.
"""
global _ctx
import pyglet
if _ctx._win is None:
raise EasyGameError('window not open')
pyglet.gl.glClearColor(r, g, b, 1)
_ctx._win.clear()
class _Image:
def __init__(self, img):
import pyglet
self._img = img
self._sprite = pyglet.sprite.Sprite(img)
@property
def width(self):
return self._img.width
@property
def height(self):
return self._img.height
@property
def center(self):
return (self._img.width//2, self._img.height//2)
def load_image(path):
"""Load an image from the specified path. PNG, JPEG, and many more formats are supported.
Returns the loaded image.
Arguments:
path -- Path to the image file. (For example 'images/crying_baby.png'.)
"""
import pyglet
return _Image(pyglet.resource.image(path))
def load_sheet(path, frame_width, frame_height):
"""Load a sprite sheet from the specified path and slices it into frames of the specified size.
Returns the list of images corresponding to the individual slices.
Arguments:
path -- Path to the sprite sheet.
frame_width -- Width of a single frame.
frame_height -- Height of a single frame.
"""
import pyglet
img = pyglet.resource.image(path)
frames = []
for x in map(lambda i: i * frame_width, range(img.width // frame_width)):
for y in map(lambda i: i * frame_height, range(img.height // frame_height)):
frames.append(img.get_region(x, y, frame_width, frame_height))
return frames
def image_data(image):
"""Returns a list of RGBA values of pixels of the image.
The pixels are listed row by row.
"""
raw = image._img.get_image_data()
pitch = raw.width * 4
data = raw.get_data('RGBA', pitch)
rows = []
for y in range(raw.height):
rows.append([])
for x in range(raw.width):
i = (y*raw.width + x) * 4
r, g, b, a = int(data[i+0])/255, int(data[i+1])/255, int(data[i+2])/255, int(data[i+3])/255
rows[y].append((r, g, b, a))
return rows
def draw_image(image, position=(0, 0), anchor=None, rotation=0, scale=1, scale_x=1, scale_y=1, opacity=1, pixelated=False):
"""Draw an image to the window, respecting the current camera settings.
Arguments:
image -- The image to draw. (Obtained from load_image or load_sheet)
position -- Anchor's position on the screen. (Defaults to 0, 0.)
anchor -- Anchor's position relative to the bottom-left corner of the image. (Defaults to the center.)
rotation -- Rotation of the image around the anchor in radians. (Defaults to 0.)
scale -- Scale of the image around the anchor. (Defaults to 1.)
scale_x -- Additional scale along X axis.
scale_y -- Additional scale along Y axis.
opacity -- Use 0 for completely transparent, 1 for completely opaque.
pixelated -- If True, image will be pixelated when scaled.
"""
global _ctx
import math, pyglet
if _ctx._win is None:
raise EasyGameError('window not open')
if anchor is None:
anchor = image.center
image._img.anchor_x, image._img.anchor_y = anchor
image._sprite.update(
x=position[0],
y=position[1],
rotation=-rotation/math.pi*180,
scale_x=scale*scale_x,
scale_y=scale*scale_y,
)
image._sprite.opacity = int(opacity * 255)
if pixelated:
tex = image._img.get_texture()
pyglet.gl.glBindTexture(pyglet.gl.GL_TEXTURE_2D, tex.id)
pyglet.gl.glTexParameteri(pyglet.gl.GL_TEXTURE_2D, pyglet.gl.GL_TEXTURE_MIN_FILTER, pyglet.gl.GL_NEAREST)
pyglet.gl.glTexParameteri(pyglet.gl.GL_TEXTURE_2D, pyglet.gl.GL_TEXTURE_MAG_FILTER, pyglet.gl.GL_NEAREST)
else:
tex = image._img.get_texture()
pyglet.gl.glBindTexture(pyglet.gl.GL_TEXTURE_2D, tex.id)
pyglet.gl.glTexParameteri(pyglet.gl.GL_TEXTURE_2D, pyglet.gl.GL_TEXTURE_MIN_FILTER, pyglet.gl.GL_LINEAR)
pyglet.gl.glTexParameteri(pyglet.gl.GL_TEXTURE_2D, pyglet.gl.GL_TEXTURE_MAG_FILTER, pyglet.gl.GL_LINEAR)
image._sprite.draw()
def draw_polygon(*points, color=(1, 1, 1, 1)):
"""Draw a convex polygon, respecting the current camera settings.
Example:
draw_polygon((0, 0), (100, 300), (200, 0), color=(0, 1, 1, 1))
Arguments:
points -- List of points of the polygon. (Is taken by variadic arguments.)
color -- Color of the polygon. Components are: red, green, blue, alpha.
"""
global _ctx
import pyglet
if _ctx._win is None:
raise EasyGameError('window not open')
vertices = []
for pt in points:
vertices.append(pt[0])
vertices.append(pt[1])
pyglet.graphics.draw(len(points), pyglet.gl.GL_POLYGON,
('v2f', vertices),
('c4f', color * len(points)),
)
def draw_line(*points, thickness=1, color=(1, 1, 1, 1)):
"""Draw a line between each two successive pair of points.
Example:
draw_line((0, 0), (100, 300), (200, 0), thickness=10 color=(0, 1, 1, 1))
Arguments:
points -- List of points of the line. (Is taken by variadic arguments.)
thickness -- Width of the line.
color -- Color of the line. Components are: red, green, blue, alpha.
"""
import math
for i in range(len(points)-1):
x0, y0 = points[i]
x1, y1 = points[i+1]
dx, dy = x1 - x0, y1 - y0
length = math.hypot(dx, dy)
dx, dy = dx/length*thickness/2, dy/length*thickness/2
draw_polygon(
(x0 - dy, y0 + dx),
(x0 + dy, y0 - dx),
(x1 + dy, y1 - dx),
(x1 - dy, y1 + dx),
color=color,
)
def draw_circle(center, radius, color=(1, 1, 1, 1)):
"""Draws a circle with the specified center and radius.
Example:
draw_circle((100, 100), 50, color=(1, 0, 0, 1))
Arguments:
center -- Coordinates of the center of the circle, in the form (x, y).
radius -- Radius of the circle.
color -- Color of the line.
"""
import math
x, y = center
pts = []
for i in range(32):
angle = i/32 * 2*math.pi
pts.append((x + math.cos(angle)*radius, y + math.sin(angle)*radius))
draw_polygon(*pts, color=color)
def draw_text(text, font, size, position=(0, 0), color=(1, 1, 1, 1), bold=False, italic=False):
"""Draw text using the selected font, respecting the current camera settings.
Arguments:
text -- String to draw.
font -- Name of the font to use. (For example: 'Times New Roman' or 'Courier New'.)
size -- Size of the font in pixels.
position -- Position of the bottom-left corner of the resulting text.
color -- Color of the text.
bold -- If True, the text will be bold.
italic -- If True, the text will be italic.
"""
global _ctx
import pyglet
if _ctx._win is None:
raise EasyGameError('window not open')
if (font, size) not in _ctx._fonts:
_ctx._fonts[(font, size)] = pyglet.text.Label(font_name=font, font_size=size)
label = _ctx._fonts[(font, size)]
label.text = text
label.x, label.y = position
label.color = tuple(map(lambda c: int(c*255), color))
label.bold = bold
label.italic = italic
label.draw()
def set_camera(center=None, position=None, rotation=None, zoom=None):
"""Set properties of the camera. Only properties you set will be changed.
Arguments:
center -- Position of the center of the camera on the screen.
position -- The world position that the camera is looking at.
rotation -- Rotation of the camera around its center.
zoom -- Zoom/scale of the camera. Value of 1 is no zoom, value of 2 is twice-scaled, etc.
"""
global _ctx
if _ctx._win is None:
raise EasyGameError('window not open')
if center is not None:
_ctx._camera.center = center
if position is not None:
_ctx._camera.position = position
if rotation is not None:
_ctx._camera.rotation = rotation
if zoom is not None:
_ctx._camera.zoom = zoom
_update_camera()
def move_camera(position=None, rotation=None, zoom=None):
"""Change properties of the camera relative to its current properties.
Arguments:
position -- Vector to add to the current position.
rotattion -- Angle to add to the current rotation.
zoom -- Number to multiply by the current zoom.
"""
global _ctx
import math
if _ctx._win is None:
raise EasyGameError('window not open')
if position is not None:
_ctx._camera.position = (
_ctx._camera.position[0] + position[0],
_ctx._camera.position[1] + position[1],
)
if rotation is not None:
_ctx._camera.rotation += rotation
while _ctx._camera.rotation >= 2*math.pi:
_ctx._camera.rotation -= 2*math.pi
while _ctx._camera.rotation < 0:
_ctx._camera.rotation += 2*math.pi
if zoom is not None:
_ctx._camera.zoom *= zoom
_update_camera()
def save_camera():
"""Save the current camera settings."""
global _ctx
if _ctx._win is None:
raise EasyGameError('window not open')
_ctx._saved_cameras.append(_Camera(
_ctx._camera.center,
_ctx._camera.position,
_ctx._camera.rotation,
_ctx._camera.zoom,
))
def restore_camera():
"""Restore the most recently saved and not yet restored camera settings."""
global _ctx
if _ctx._win is None:
raise EasyGameError('window not open')
if len(_ctx._saved_cameras) == 0:
raise EasyGameError('no saved camera')
_ctx._camera = _ctx._saved_cameras.pop(-1)
_update_camera()
def reset_camera():
"""Reset camera to the original settings."""
set_camera(center=(0, 0), position=(0, 0), rotation=0, zoom=1)
class _Audio:
def __init__(self, snd):
self._snd = snd
def load_audio(path, streaming=False):
"""Load an audio from the specified path.
Returns the loaded audio.
Arguments:
path -- Path to the audio file. (For example 'sounds/crying_baby.wav'.)
streaming -- Whether to stream the file directly from the disk, or load it to the memory instead.
"""
import pyglet
snd = pyglet.resource.media(path, streaming=streaming)
return _Audio(snd)
def play_audio(audio, channel=0, loop=False, volume=1, speed=1):
"""Play an audio on the specified channel.
There's infinite number of channels. Playing an audio on a channel stops previous playback
on this channel. Therefore, at most one audio can play on one channel at any time.
To stop playback on a channel, play a None audio:
play_audio(None, channel=0)
Arguments:
audio -- The audio to be played.
channel -- The channel index.
loop -- If True, playback will repeat forever, or until stopped.
volume -- 0 for mute, 1 for normal volume.
speed -- 1 for normal speed, 0.5 for 2x slowdown, 2 for 2x speed, etc.
"""
global _ctx
import pyglet
if channel in _ctx._channels:
_ctx._channels[channel].delete()
del _ctx._channels[channel]
if audio is None:
return
player = pyglet.media.Player()
if loop:
#looper = pyglet.media.SourceGroup() #audio._snd.audio_format
#looper.add(audio._snd)
#looper.loop = True
#player.queue(looper)
#player.loop = True
player.queue(audio._snd)
player.queue(audio._snd)
print('WAT')
else:
player.queue(audio._snd)
player.volume = volume
player.pitch = speed
_ctx._channels[channel] = player
player.play()
def playback_time(channel):
"""Returns the current time of the audio playing on the channel in
seconds or 0 if the channel isn't active."""
global _ctx
if channel not in _ctx._channels:
return 0
return _ctx._channels[channel].time
def fix_rectangle_overlap(rect1, rect2):
"""Calculate the minimum vector required to move rect1 to fix the overlap between
rect1 and rect2.
Arguments:
rect1 -- The first rectangle. Has form (x0, y0, x1, y1).
rect2 -- The second rectangle. Has the same form as rect1.
"""
ax0, ay0, ax1, ay1 = rect1
bx0, by0, bx1, by1 = rect2
left, right = max(0, ax1 - bx0), min(0, ax0 - bx1)
down, up = max(0, ay1 - by0), min(0, ay0 - by1)
move_x = min(left, right, key=abs)
move_y = min(down, up, key=abs)
if abs(move_x) < abs(move_y):
return (-move_x, 0)
else:
return (0, -move_y)