Particle Systems

Particle Systems
particle_systems.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
"""
Particle Systems

Demonstrate how to use the Emitter and Particle classes to create particle systems.

Demonstrate the different effects possible with Emitter's and Particle's by showing
a number of different emitters in sequence, with each example often varying just one
setting from the previous example.

If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.sprite_list_particle_systems
"""
import arcade
from arcade.examples.frametime_plotter import FrametimePlotter
import pyglet
import os
import random
import math

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Particle System Examples"
QUIET_BETWEEN_SPAWNS = 0.25 # time between spawning another particle system
EMITTER_TIMEOUT = 10*60
CENTER_POS = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
BURST_PARTICLE_COUNT = 500
TEXTURE = "images/pool_cue_ball.png"
TEXTURE2 = "images/playerShip3_orange.png"
TEXTURE3 = "images/bumper.png"
TEXTURE4 = "images/wormGreen.png"
TEXTURE5 = "images/meteorGrey_med1.png"
TEXTURE6 = "images/character.png"
TEXTURE7 = "images/boxCrate_double.png"
DEFAULT_SCALE = 0.3
DEFAULT_ALPHA = 32
DEFAULT_PARTICLE_LIFETIME = 3.0
PARTICLE_SPEED_FAST = 1.0
PARTICLE_SPEED_SLOW = 0.3
DEFAULT_EMIT_INTERVAL = 0.003
DEFAULT_EMIT_DURATION = 1.5


##### Utils
def sine_wave(t, min_x, max_x, wavelength):
    spread = max_x - min_x
    mid = (max_x + min_x) / 2
    return (spread / 2) * math.sin( 2 * math.pi * t / wavelength) + mid


##### Example emitters
def emitter_0():
    """Burst, emit from center, particle with lifetime"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_0.__doc__, e

def emitter_1():
    """Burst, emit from center, particle lifetime 1.0 seconds"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=1.0,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_1.__doc__, e

def emitter_2():
    """Burst, emit from center, particle lifetime random in range"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0),PARTICLE_SPEED_FAST),
            lifetime=random.uniform(DEFAULT_PARTICLE_LIFETIME - 1.0, DEFAULT_PARTICLE_LIFETIME),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_2.__doc__, e

def emitter_3():
    """Burst, emit in circle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_in_circle((0.0, 0.0), 100),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_3.__doc__, e

def emitter_4():
    """Burst, emit on circle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_on_circle((0.0, 0.0), 100),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_4.__doc__, e

def emitter_5():
    """Burst, emit in rectangle"""
    width, height = 200, 100
    centering_offset = (-width/2, -height/2)
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_in_rect(centering_offset, width, height),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_5.__doc__, e

def emitter_6():
    """Burst, emit on line"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_on_line((0.0, 0.0), (SCREEN_WIDTH, SCREEN_HEIGHT)),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_6.__doc__, e

def emitter_7():
    """Burst, emit from center, velocity fixed speed around 360 degrees"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT // 4),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_on_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_7.__doc__, e

def emitter_8():
    """Burst, emit from center, velocity in rectangle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_rect((-2.0, -2.0), 4.0, 4.0),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_8.__doc__, e

def emitter_9():
    """Burst, emit from center, velocity in fixed angle and random speed"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT // 4),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_vec_magnitude(45, 1.0, 4.0),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_9.__doc__, e

def emitter_10():
    """Burst, emit from center, velocity from angle with spread"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT // 4),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_vec_spread_deg(90, 45, 2.0),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_10.__doc__, e

def emitter_11():
    """Burst, emit from center, velocity along a line"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT // 4),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_on_line((-2, 1), (2, 1)),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_11.__doc__, e

def emitter_12():
    """Infinite emitting w/ eternal particle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitInterval(0.02),
        particle_factory=lambda emitter: arcade.EternalParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_12.__doc__, e

def emitter_13():
    """Interval, emit particle every 0.01 seconds for one second"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_13.__doc__, e

def emitter_14():
    """Interval, emit from center, particle lifetime 1.0 seconds"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=1.0,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_14.__doc__, e

def emitter_15():
    """Interval, emit from center, particle lifetime random in range"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=random.uniform( DEFAULT_PARTICLE_LIFETIME - 1.0, DEFAULT_PARTICLE_LIFETIME),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_15.__doc__, e

def emitter_16():
    """Interval, emit in circle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_in_circle((0.0, 0.0), 100),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_16.__doc__, e

def emitter_17():
    """Interval, emit on circle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_on_circle((0.0, 0.0), 100),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_17.__doc__, e

def emitter_18():
    """Interval, emit in rectangle"""
    width, height = 200, 100
    centering_offset = (-width/2, -height/2)
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_in_rect(centering_offset, width, height),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_18.__doc__, e



#### left off formatting here.
## do auto-remove of angle and change_angle
def emitter_19():
    """Interval, emit on line"""
    e = arcade.Emitter(
        center_xy=(0.0, 0.0),
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_SLOW),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            center_xy=arcade.rand_on_line((0.0, 0.0), (SCREEN_WIDTH, SCREEN_HEIGHT)),
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_19.__doc__, e

def emitter_20():
    """Interval, emit from center, velocity fixed speed around 360 degrees"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_on_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_20.__doc__, e

def emitter_21():
    """Interval, emit from center, velocity in rectangle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_rect((-2.0, -2.0), 4.0, 4.0),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_21.__doc__, e

def emitter_22():
    """Interval, emit from center, velocity in fixed angle and speed"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(0.2, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=(1.0, 1.0),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=128
        )
    )
    return emitter_22.__doc__, e

def emitter_23():
    """Interval, emit from center, velocity in fixed angle and random speed"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL * 8, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_vec_magnitude(45, 1.0, 4.0),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_23.__doc__, e

def emitter_24():
    """Interval, emit from center, velocity from angle with spread"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_vec_spread_deg(90, 45, 2.0),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_24.__doc__, e

def emitter_25():
    """Interval, emit from center, velocity along a line"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_on_line((-2, 1), (2, 1)),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_25.__doc__, e

def emitter_26():
    """Interval, emit particles every 0.4 seconds and stop after emitting 5"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithCount(0.4, 5),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=0.6,
            alpha=128
        )
    )
    return emitter_26.__doc__, e

def emitter_27():
    """Maintain a steady count of particles"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitMaintainCount(3),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_on_circle((0.0, 0.0), 2.0),
            lifetime=random.uniform(1.0, 3.0),
        )
    )
    return emitter_27.__doc__, e

def emitter_28():
    """random particle textures"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL * 5, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=random.choice((TEXTURE, TEXTURE2, TEXTURE3)),
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE
        )
    )
    return emitter_28.__doc__, e

def emitter_29():
    """random particle scale"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL * 5, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=random.uniform(0.1, 0.8),
            alpha=DEFAULT_ALPHA
        )
    )
    return emitter_29.__doc__, e

def emitter_30():
    """random particle alpha"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL * 5, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE,
            alpha=random.uniform(32, 128)
        )
    )
    return emitter_30.__doc__, e

def emitter_31():
    """Constant particle angle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL * 5, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE2,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            angle=45,
            scale=DEFAULT_SCALE
        )
    )
    return emitter_31.__doc__, e

def emitter_32():
    """animate particle angle"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL * 5, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE2,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            change_angle=2,
            scale=DEFAULT_SCALE
        )
    )
    return emitter_32.__doc__, e

def emitter_33():
    """Particles that fade over time"""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.FadeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE
        )
    )
    return emitter_33.__doc__, e

def emitter_34():
    """Dynamically generated textures, burst emitting, fading particles"""
    textures = [arcade.make_soft_circle_texture(48, p) for p in (arcade.color.GREEN, arcade.color.BLUE_GREEN)]
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitBurst(BURST_PARTICLE_COUNT),
        particle_factory=lambda emitter: arcade.FadeParticle(
            filename_or_texture=random.choice(textures),
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST),
            lifetime=DEFAULT_PARTICLE_LIFETIME,
            scale=DEFAULT_SCALE
        )
    )
    return emitter_34.__doc__, e

def emitter_35():
    """Use most features"""
    soft_circle = arcade.make_soft_circle_texture(80, (255, 64, 64))
    textures = (TEXTURE, TEXTURE2, TEXTURE3, TEXTURE4, TEXTURE5, TEXTURE6, TEXTURE7, soft_circle)
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(0.01, 1.0),
        particle_factory=lambda emitter: arcade.FadeParticle(
            filename_or_texture=random.choice(textures),
            change_xy=arcade.rand_in_circle((0.0, 0.0), PARTICLE_SPEED_FAST * 2),
            lifetime=random.uniform(1.0, 3.5),
            angle=random.uniform(0, 360),
            change_angle=random.uniform(-3, 3),
            scale=random.uniform(0.1, 0.8)
        )
    )
    return emitter_35.__doc__, e

def emitter_36():
    """Moving emitter. Particles spawn relative to emitter."""
    class MovingEmitter(arcade.Emitter):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.elapsed = 0.0

        def update(self):
            super().update()
            self.elapsed += 1 / 60
            self.center_x = sine_wave(self.elapsed, 0, SCREEN_WIDTH, SCREEN_WIDTH / 100)
            self.center_y = sine_wave(self.elapsed, 0, SCREEN_HEIGHT, SCREEN_HEIGHT / 100)

    e = MovingEmitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitInterval(0.005),
        particle_factory=lambda emitter: arcade.FadeParticle(
            filename_or_texture=TEXTURE,
            change_xy=arcade.rand_in_circle((0.0, 0.0), 0.1),
            lifetime=random.uniform(1.5, 5.5),
            scale=random.uniform(0.05, 0.2)
        )
    )
    return emitter_36.__doc__, e

def emitter_37():
    """Rotating emitter. Particles initial velocity is relative to emitter's angle."""
    e = arcade.Emitter(
        center_xy=CENTER_POS,
        emit_controller=arcade.EmitterIntervalWithTime(DEFAULT_EMIT_INTERVAL, DEFAULT_EMIT_DURATION),
        particle_factory=lambda emitter: arcade.LifetimeParticle(
            filename_or_texture=TEXTURE,
            change_xy=(0.0, 2.0),
            lifetime=2.0,
            scale=DEFAULT_SCALE
        )
    )
    e.change_angle = 10.0
    return emitter_37.__doc__, e

def emitter_38():
    """Use simple emitter interface to create a burst emitter"""
    e = arcade.make_burst_emitter(
        center_xy=CENTER_POS,
        filenames_and_textures=(TEXTURE, TEXTURE3, TEXTURE4),
        particle_count=50,
        particle_speed=2.5,
        particle_lifetime_min=1.0,
        particle_lifetime_max=2.5,
        particle_scale=0.3,
        fade_particles=True
    )
    return emitter_38.__doc__, e

def emitter_39():
    """Use simple emitter interface to create an interval emitter"""
    e = arcade.make_interval_emitter(
        center_xy=CENTER_POS,
        filenames_and_textures=(TEXTURE, TEXTURE3, TEXTURE4),
        emit_interval=0.01,
        emit_duration=2.0,
        particle_speed=1.5,
        particle_lifetime_min=1.0,
        particle_lifetime_max=3.0,
        particle_scale=0.2,
        fade_particles=True
    )
    return emitter_39.__doc__, e


class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        arcade.set_background_color(arcade.color.BLACK)

        # collect particle factory functions
        self.factories = [v for k, v in globals().items() if k.startswith("emitter_")]

        self.emitter_factory_id = -1
        self.label = None
        self.emitter = None
        self.obj = arcade.Sprite("images/bumper.png", 0.2, center_x=0, center_y=15)
        self.obj.change_x = 3
        self.frametime_plotter = FrametimePlotter()
        pyglet.clock.schedule_once(self.next_emitter, QUIET_BETWEEN_SPAWNS)

    def next_emitter(self, time_delta):
        self.emitter_factory_id = (self.emitter_factory_id + 1) % len(self.factories)
        print("Changing emitter to {}".format(self.emitter_factory_id))
        self.emitter_timeout = 0
        self.label, self.emitter = self.factories[self.emitter_factory_id]()
        self.frametime_plotter.add_event("spawn {}".format(self.emitter_factory_id))

    def update(self, delta_time):
        if self.emitter:
            self.emitter_timeout += 1
            self.emitter.update()
            if self.emitter.can_reap() or self.emitter_timeout > EMITTER_TIMEOUT:
                self.frametime_plotter.add_event("reap")
                pyglet.clock.schedule_once(self.next_emitter, QUIET_BETWEEN_SPAWNS)
                self.emitter = None
        self.obj.update()
        if self.obj.center_x > SCREEN_WIDTH:
            self.obj.center_x = 0
        self.frametime_plotter.end_frame(delta_time)

    def on_draw(self):
        arcade.start_render()
        self.obj.draw()
        if self.label:
            arcade.draw_text("#{} {}".format(self.emitter_factory_id, self.label),
                             SCREEN_WIDTH / 2, SCREEN_HEIGHT - 20,
                             arcade.color.PALE_GOLD, 20, width=SCREEN_WIDTH, align="center",
                             anchor_x="center", anchor_y="center")
        if self.emitter:
            self.emitter.draw()
            arcade.draw_text("Particles: " + str(len(self.emitter._particles)), 10, 30, arcade.color.PALE_GOLD, 12)

    def on_key_press(self, key, modifiers):
        if key == arcade.key.ESCAPE:
            arcade.close_window()


if __name__ == "__main__":
    game = MyGame()
    arcade.run()
    game.frametime_plotter.show()