-
Notifications
You must be signed in to change notification settings - Fork 0
/
A02.1_string_rendering_v2.html
739 lines (640 loc) · 90.3 KB
/
A02.1_string_rendering_v2.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<!--
generated by Pygments <https://pygments.org/>
Copyright 2006-2024 by the Pygments team.
Licensed under the BSD license, see LICENSE for details.
-->
<html>
<head>
<link rel='canonical' href='https://pet.triquence.org/A02.1_string_rendering_v2.html' />
<title>A02.1_string_rendering_v2</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
/*
generated by Pygments <https://pygments.org/>
Copyright 2006-2024 by the Pygments team.
Licensed under the BSD license, see LICENSE for details.
*/
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
body .hll { background-color: #49483e }
body { background: #272822; color: #f8f8f2 }
body .c { color: #959077 } /* Comment */
body .err { color: #ed007e; background-color: #1e0010 } /* Error */
body .esc { color: #f8f8f2 } /* Escape */
body .g { color: #f8f8f2 } /* Generic */
body .k { color: #66d9ef } /* Keyword */
body .l { color: #ae81ff } /* Literal */
body .n { color: #f8f8f2 } /* Name */
body .o { color: #ff4689 } /* Operator */
body .x { color: #f8f8f2 } /* Other */
body .p { color: #f8f8f2 } /* Punctuation */
body .ch { color: #959077 } /* Comment.Hashbang */
body .cm { color: #959077 } /* Comment.Multiline */
body .cp { color: #959077 } /* Comment.Preproc */
body .cpf { color: #959077 } /* Comment.PreprocFile */
body .c1 { color: #959077 } /* Comment.Single */
body .cs { color: #959077 } /* Comment.Special */
body .gd { color: #ff4689 } /* Generic.Deleted */
body .ge { color: #f8f8f2; font-style: italic } /* Generic.Emph */
body .ges { color: #f8f8f2; font-weight: bold; font-style: italic } /* Generic.EmphStrong */
body .gr { color: #f8f8f2 } /* Generic.Error */
body .gh { color: #f8f8f2 } /* Generic.Heading */
body .gi { color: #a6e22e } /* Generic.Inserted */
body .go { color: #66d9ef } /* Generic.Output */
body .gp { color: #ff4689; font-weight: bold } /* Generic.Prompt */
body .gs { color: #f8f8f2; font-weight: bold } /* Generic.Strong */
body .gu { color: #959077 } /* Generic.Subheading */
body .gt { color: #f8f8f2 } /* Generic.Traceback */
body .kc { color: #66d9ef } /* Keyword.Constant */
body .kd { color: #66d9ef } /* Keyword.Declaration */
body .kn { color: #ff4689 } /* Keyword.Namespace */
body .kp { color: #66d9ef } /* Keyword.Pseudo */
body .kr { color: #66d9ef } /* Keyword.Reserved */
body .kt { color: #66d9ef } /* Keyword.Type */
body .ld { color: #e6db74 } /* Literal.Date */
body .m { color: #ae81ff } /* Literal.Number */
body .s { color: #e6db74 } /* Literal.String */
body .na { color: #a6e22e } /* Name.Attribute */
body .nb { color: #f8f8f2 } /* Name.Builtin */
body .nc { color: #a6e22e } /* Name.Class */
body .no { color: #66d9ef } /* Name.Constant */
body .nd { color: #a6e22e } /* Name.Decorator */
body .ni { color: #f8f8f2 } /* Name.Entity */
body .ne { color: #a6e22e } /* Name.Exception */
body .nf { color: #a6e22e } /* Name.Function */
body .nl { color: #f8f8f2 } /* Name.Label */
body .nn { color: #f8f8f2 } /* Name.Namespace */
body .nx { color: #a6e22e } /* Name.Other */
body .py { color: #f8f8f2 } /* Name.Property */
body .nt { color: #ff4689 } /* Name.Tag */
body .nv { color: #f8f8f2 } /* Name.Variable */
body .ow { color: #ff4689 } /* Operator.Word */
body .pm { color: #f8f8f2 } /* Punctuation.Marker */
body .w { color: #f8f8f2 } /* Text.Whitespace */
body .mb { color: #ae81ff } /* Literal.Number.Bin */
body .mf { color: #ae81ff } /* Literal.Number.Float */
body .mh { color: #ae81ff } /* Literal.Number.Hex */
body .mi { color: #ae81ff } /* Literal.Number.Integer */
body .mo { color: #ae81ff } /* Literal.Number.Oct */
body .sa { color: #e6db74 } /* Literal.String.Affix */
body .sb { color: #e6db74 } /* Literal.String.Backtick */
body .sc { color: #e6db74 } /* Literal.String.Char */
body .dl { color: #e6db74 } /* Literal.String.Delimiter */
body .sd { color: #e6db74 } /* Literal.String.Doc */
body .s2 { color: #e6db74 } /* Literal.String.Double */
body .se { color: #ae81ff } /* Literal.String.Escape */
body .sh { color: #e6db74 } /* Literal.String.Heredoc */
body .si { color: #e6db74 } /* Literal.String.Interpol */
body .sx { color: #e6db74 } /* Literal.String.Other */
body .sr { color: #e6db74 } /* Literal.String.Regex */
body .s1 { color: #e6db74 } /* Literal.String.Single */
body .ss { color: #e6db74 } /* Literal.String.Symbol */
body .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
body .fm { color: #a6e22e } /* Name.Function.Magic */
body .vc { color: #f8f8f2 } /* Name.Variable.Class */
body .vg { color: #f8f8f2 } /* Name.Variable.Global */
body .vi { color: #f8f8f2 } /* Name.Variable.Instance */
body .vm { color: #f8f8f2 } /* Name.Variable.Magic */
body .il { color: #ae81ff } /* Literal.Number.Integer.Long */
</style>
</head>
<body>
<h2></h2>
<div class="highlight"><pre><span></span><span class="c1"># Filename: A02.1_string_rendering_v2.py</span>
<span class="c1"># Written by: James D. Miller</span>
<span class="sd">'''</span>
<span class="sd">10:05 PM Thu September 20, 2018</span>
<span class="sd">Noticed recently that control-s wasn't pausing execution in Windows. So, </span>
<span class="sd">updated this script to include the pygame (keyboard) event handlers. </span>
<span class="sd">This requires a little pygame window be displayed. This window is only </span>
<span class="sd">used to enable these handlers. Use the p key to pause and resume. Use </span>
<span class="sd">the x key to quit.</span>
<span class="sd">see check_pause_orDone.</span>
<span class="sd">'''</span>
<span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">textwrap</span>
<span class="kn">from</span> <span class="nn">timeit</span> <span class="kn">import</span> <span class="n">default_timer</span>
<span class="kn">import</span> <span class="nn">pygame</span>
<span class="kn">from</span> <span class="nn">pygame.locals</span> <span class="kn">import</span> <span class="o">*</span>
<span class="k">def</span> <span class="nf">px_from_m</span><span class="p">(</span> <span class="n">x_m</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span> <span class="n">x_m</span> <span class="o">*</span> <span class="n">env</span><span class="p">[</span><span class="s1">'m_to_px'</span><span class="p">]))</span>
<span class="k">def</span> <span class="nf">move</span><span class="p">(</span> <span class="n">car</span><span class="p">):</span>
<span class="n">v_i</span> <span class="o">=</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">+=</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">*</span> <span class="n">dt_s</span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'exact_solution'</span><span class="p">]:</span>
<span class="w"> </span><span class="sd">'''</span>
<span class="sd"> Note that the following formulation is equivalent to the one being used below.</span>
<span class="sd"> car['x_m'] += ((v_i + car['v_mps'])/2.0) * dt_s</span>
<span class="sd"> '''</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">+=</span> <span class="n">v_i</span> <span class="o">*</span> <span class="n">dt_s</span> <span class="o">+</span> <span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">dt_s</span> <span class="o">**</span> <span class="mf">2.0</span><span class="p">))</span><span class="o">/</span><span class="mf">2.0</span>
<span class="k">else</span><span class="p">:</span>
<span class="w"> </span><span class="sd">'''</span>
<span class="sd"> Normal Euler's method (using v at the beginning of the frame).</span>
<span class="sd"> car['x_m'] += v_i * dt_s</span>
<span class="sd"> '''</span>
<span class="c1"># Backward Euler's method (using v at the end of the frame).</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">+=</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">*</span> <span class="n">dt_s</span>
<span class="k">def</span> <span class="nf">debug_print</span><span class="p">(</span><span class="n">name_string</span><span class="p">):</span>
<span class="w"> </span><span class="sd">'''</span>
<span class="sd"> Print out the values for a set of global names contained in a string </span>
<span class="sd"> and separated by commas. This isn't actually used, but cute enough to keep.</span>
<span class="sd"> '''</span>
<span class="n">names</span> <span class="o">=</span> <span class="n">name_string</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">","</span><span class="p">)</span>
<span class="n">print_string</span> <span class="o">=</span> <span class="s1">''</span>
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
<span class="n">print_string</span> <span class="o">+=</span> <span class="n">name</span> <span class="o">+</span> <span class="s2">":"</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="nb">eval</span><span class="p">(</span><span class="n">name</span><span class="p">))</span> <span class="o">+</span> <span class="s2">", "</span>
<span class="nb">print</span> <span class="n">print_string</span>
<span class="k">def</span> <span class="nf">dp</span><span class="p">(</span><span class="n">variable</span><span class="p">,</span> <span class="n">variable_name</span><span class="p">):</span>
<span class="c1"># dp is short for debug print.</span>
<span class="nb">print</span> <span class="n">variable_name</span> <span class="o">+</span> <span class="s2">"="</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">variable</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">x_corrected_exact</span><span class="p">(</span> <span class="n">car</span><span class="p">,</span> <span class="n">x_overlap</span><span class="p">):</span>
<span class="c1"># Inputs are conditions at the time of collision detection. These</span>
<span class="c1"># values have signs. For example x_overlap is positive for penetration</span>
<span class="c1"># on the right end of the track. </span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'stickiness_correction'</span><span class="p">]:</span>
<span class="n">x_coll_m</span> <span class="o">=</span> <span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span>
<span class="n">v_coll_mps</span> <span class="o">=</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span>
<span class="w"> </span><span class="sd">'''</span>
<span class="sd"> Determine the car state as it passes through the wall.</span>
<span class="sd"> Solve the following equation for v_wall.</span>
<span class="sd"> v_coll_mps**2 = v_wall_mps**2 + 2*a*x_overlap</span>
<span class="sd"> '''</span>
<span class="n">v_wall_mps</span> <span class="o">=</span> <span class="p">(</span><span class="n">v_coll_mps</span><span class="o">**</span><span class="mf">2.0</span> <span class="o">-</span> <span class="mf">2.0</span> <span class="o">*</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">*</span> <span class="n">x_overlap</span><span class="p">)</span><span class="o">**</span><span class="mf">0.5</span>
<span class="k">if</span> <span class="n">track</span><span class="p">[</span><span class="s1">'collision_state'</span><span class="p">]</span> <span class="o">==</span> <span class="s1">'left'</span><span class="p">:</span>
<span class="n">v_wall_mps</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.0</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="n">v_wall_mps</span><span class="p">)</span>
<span class="w"> </span><span class="sd">'''</span>
<span class="sd"> The time expended penetrating the wall.</span>
<span class="sd"> Solve the following equation for t_pen.</span>
<span class="sd"> x_coll_m = x_wall_m + ((v_wall_mps + v_coll_mps)/2.0) * t_pen </span>
<span class="sd"> '''</span>
<span class="n">t_pen</span> <span class="o">=</span> <span class="mf">2.0</span> <span class="o">*</span> <span class="n">x_overlap</span><span class="o">/</span><span class="p">(</span><span class="n">v_wall_mps</span> <span class="o">+</span> <span class="n">v_coll_mps</span><span class="p">)</span>
<span class="c1"># The distance covered bouncing back from the wall in time t_pen. Change</span>
<span class="c1"># the sign (direction) of v_wall.</span>
<span class="n">v_wall_afterbounce_mps</span> <span class="o">=</span> <span class="n">v_wall_mps</span> <span class="o">*</span> <span class="o">-</span><span class="mf">1.0</span> <span class="o">*</span> <span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span>
<span class="n">x_bounce_pen</span> <span class="o">=</span> <span class="n">v_wall_afterbounce_mps</span> <span class="o">*</span> <span class="n">t_pen</span> <span class="o">+</span> <span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">*</span> <span class="n">t_pen</span><span class="o">**</span><span class="mf">2.0</span><span class="p">)</span><span class="o">/</span><span class="mf">2.0</span>
<span class="k">if</span> <span class="n">track</span><span class="p">[</span><span class="s1">'collision_state'</span><span class="p">]</span> <span class="o">==</span> <span class="s1">'left'</span><span class="p">:</span>
<span class="k">if</span> <span class="n">x_bounce_pen</span> <span class="o"><</span> <span class="mf">0.0</span><span class="p">:</span> <span class="n">x_bounce_pen</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="n">x_bounce_pen</span> <span class="o">></span> <span class="mf">0.0</span><span class="p">:</span> <span class="n">x_bounce_pen</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="c1"># The corrected position, that is, where it would be if it had bounced off the wall.</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">x_coll_m</span> <span class="o">-</span> <span class="n">x_overlap</span><span class="p">)</span> <span class="o">+</span> <span class="n">x_bounce_pen</span>
<span class="c1"># Also need to determine the velocity at the corrected position.</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="n">v_wall_afterbounce_mps</span> <span class="o">+</span> <span class="n">t_pen</span> <span class="o">*</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span>
<span class="c1"># Change this to True to print these variables for debugging.</span>
<span class="k">if</span> <span class="kc">False</span><span class="p">:</span>
<span class="n">dp</span><span class="p">(</span><span class="n">x_coll_m</span><span class="p">,</span> <span class="s2">"x_coll_m"</span><span class="p">)</span>
<span class="n">dp</span><span class="p">(</span><span class="n">v_coll_mps</span><span class="p">,</span> <span class="s2">"v_coll_mps"</span><span class="p">)</span>
<span class="n">dp</span><span class="p">(</span><span class="n">t_pen</span><span class="p">,</span> <span class="s2">"t_pen"</span><span class="p">)</span>
<span class="n">dp</span><span class="p">(</span><span class="n">v_wall_mps</span><span class="p">,</span> <span class="s2">"v_wall_mps"</span><span class="p">)</span>
<span class="n">dp</span><span class="p">(</span><span class="n">v_wall_afterbounce_mps</span><span class="p">,</span> <span class="s2">"v_wall_afterbounce_mps"</span><span class="p">)</span>
<span class="n">dp</span><span class="p">(</span><span class="n">x_bounce_pen</span><span class="p">,</span> <span class="s2">"x_bounce_pen"</span><span class="p">)</span>
<span class="n">dp</span><span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">],</span> <span class="s2">"car['x_m']"</span><span class="p">)</span>
<span class="n">dp</span><span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">],</span> <span class="s2">"car['v_mps']"</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># If no position correction, simply reverse the direction of the car.</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">*=</span> <span class="o">-</span><span class="mf">1.0</span> <span class="o">*</span> <span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">check_for_wall_collisions</span><span class="p">(</span> <span class="n">car</span><span class="p">):</span>
<span class="c1"># Check for a collision.</span>
<span class="k">if</span> <span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o"><</span> <span class="n">track</span><span class="p">[</span><span class="s1">'left_edge_m'</span><span class="p">]):</span>
<span class="n">x_overlap</span> <span class="o">=</span> <span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">-</span> <span class="n">track</span><span class="p">[</span><span class="s1">'left_edge_m'</span><span class="p">]</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'collision_state'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'left'</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">></span> <span class="n">track</span><span class="p">[</span><span class="s1">'right_edge_m'</span><span class="p">]):</span>
<span class="n">x_overlap</span> <span class="o">=</span> <span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">-</span> <span class="n">track</span><span class="p">[</span><span class="s1">'right_edge_m'</span><span class="p">]</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'collision_state'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'right'</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'collision_state'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'none'</span>
<span class="c1"># Resolve the collision.</span>
<span class="k">if</span> <span class="n">track</span><span class="p">[</span><span class="s1">'collision_state'</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">'none'</span><span class="p">:</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'collision_mark_px'</span><span class="p">]</span> <span class="o">=</span> <span class="n">px_from_m</span><span class="p">(</span> <span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">])</span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'exact_solution'</span><span class="p">]:</span>
<span class="n">x_corrected_exact</span><span class="p">(</span> <span class="n">car</span><span class="p">,</span> <span class="n">x_overlap</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'stickiness_correction'</span><span class="p">]:</span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'correction_version_2'</span><span class="p">]:</span>
<span class="c1"># Move the car back to the surface and then an additional</span>
<span class="c1"># equal amount but reduced by the CR coefficient.</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">-=</span> <span class="n">x_overlap</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># Simple stickiness correction. Move it back by the amount of the overlap.</span>
<span class="c1"># This puts the car at the surface.</span>
<span class="k">if</span> <span class="n">track</span><span class="p">[</span><span class="s1">'collision_state'</span><span class="p">]</span> <span class="o">==</span> <span class="s1">'left'</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="n">track</span><span class="p">[</span><span class="s1">'left_edge_m'</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="n">track</span><span class="p">[</span><span class="s1">'right_edge_m'</span><span class="p">]</span>
<span class="c1"># Loss of (1-CR)*100% on each bounce.</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'collision_mark_px'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">999</span>
<span class="k">def</span> <span class="nf">build_airtrack_string</span><span class="p">(</span> <span class="n">car</span><span class="p">):</span>
<span class="n">left_edge_px</span> <span class="o">=</span> <span class="n">px_from_m</span><span class="p">(</span> <span class="n">track</span><span class="p">[</span><span class="s1">'left_edge_m'</span><span class="p">])</span>
<span class="n">right_edge_px</span> <span class="o">=</span> <span class="n">px_from_m</span><span class="p">(</span> <span class="n">track</span><span class="p">[</span><span class="s1">'right_edge_m'</span><span class="p">])</span>
<span class="n">display_width_px</span> <span class="o">=</span> <span class="mi">135</span>
<span class="n">car_location_px</span> <span class="o">=</span> <span class="n">px_from_m</span><span class="p">(</span> <span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">])</span>
<span class="n">string</span> <span class="o">=</span> <span class="s1">''</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">display_width_px</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
<span class="k">if</span> <span class="p">(</span><span class="n">j</span> <span class="o">==</span> <span class="n">car_location_px</span><span class="p">):</span>
<span class="n">string</span> <span class="o">+=</span> <span class="s1">'*'</span>
<span class="k">elif</span> <span class="p">((</span><span class="n">j</span> <span class="o">==</span> <span class="n">left_edge_px</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">j</span> <span class="o">==</span> <span class="n">right_edge_px</span><span class="p">)):</span>
<span class="n">string</span> <span class="o">+=</span> <span class="s1">'|'</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">]</span> <span class="ow">and</span> <span class="p">(</span><span class="n">j</span> <span class="o">==</span> <span class="n">track</span><span class="p">[</span><span class="s1">'track_mark_px'</span><span class="p">])):</span>
<span class="n">string</span> <span class="o">+=</span> <span class="s2">"."</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">track</span><span class="p">[</span><span class="s1">'show_collision_mark'</span><span class="p">]</span> <span class="ow">and</span> <span class="p">(</span><span class="n">j</span> <span class="o">==</span> <span class="n">track</span><span class="p">[</span><span class="s1">'collision_mark_px'</span><span class="p">])):</span>
<span class="n">string</span> <span class="o">+=</span> <span class="s2">"0"</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">string</span> <span class="o">+=</span> <span class="s1">' '</span>
<span class="k">return</span> <span class="n">string</span>
<span class="k">def</span> <span class="nf">render</span><span class="p">(</span> <span class="n">car</span><span class="p">):</span>
<span class="n">display_string</span> <span class="o">=</span> <span class="n">build_airtrack_string</span><span class="p">(</span> <span class="n">car</span><span class="p">)</span>
<span class="k">if</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'details'</span><span class="p">]:</span>
<span class="nb">print</span> <span class="n">display_string</span> <span class="o">+</span> <span class="s1">'x='</span> <span class="o">+</span> <span class="s2">"</span><span class="si">%6.3f</span><span class="s2">"</span> <span class="o">%</span> <span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">+</span> <span class="s1">', v='</span> <span class="o">+</span> <span class="s2">"</span><span class="si">% .2f</span><span class="s2">"</span> <span class="o">%</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">+</span> <span class="s2">", F="</span> <span class="o">+</span> <span class="s2">"</span><span class="si">%3.0f</span><span class="s2">"</span> <span class="o">%</span> <span class="n">fps_observed</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span> <span class="n">display_string</span>
<span class="k">def</span> <span class="nf">pos_avg_10</span><span class="p">(</span> <span class="n">car</span><span class="p">):</span>
<span class="n">x_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">])</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">x_list</span><span class="p">)</span> <span class="o">></span> <span class="mi">10</span><span class="p">:</span> <span class="n">x_list</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x_list</span><span class="p">)</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">x_list</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">pretty_paragraphs</span><span class="p">(</span> <span class="n">text_string</span><span class="p">,</span> <span class="n">n_blanklines</span><span class="p">):</span>
<span class="n">paragraph_list</span> <span class="o">=</span> <span class="n">text_string</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'||'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">paragraph</span> <span class="ow">in</span> <span class="n">paragraph_list</span><span class="p">:</span>
<span class="n">dedented_text</span> <span class="o">=</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">dedent</span><span class="p">(</span> <span class="n">paragraph</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="nb">print</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">dedented_text</span><span class="p">,</span> <span class="n">initial_indent</span><span class="o">=</span><span class="s1">' '</span><span class="p">,</span> <span class="n">subsequent_indent</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="nb">print</span> <span class="s2">""</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span> <span class="n">n_blanklines</span><span class="p">):</span> <span class="nb">print</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span>
<span class="k">def</span> <span class="nf">print_delay</span><span class="p">(</span> <span class="n">string</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">string</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span> <span class="mf">0.10</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">try_sleep</span><span class="p">(</span> <span class="n">seconds</span><span class="p">):</span>
<span class="c1"># If you don't want to wait. Press control-c to break out of the sleep.</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span> <span class="n">seconds</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">KeyboardInterrupt</span><span class="p">:</span>
<span class="n">print_delay</span><span class="p">(</span><span class="s2">" * "</span><span class="p">)</span>
<span class="n">print_delay</span><span class="p">(</span><span class="s2">" * * "</span><span class="p">)</span>
<span class="n">print_delay</span><span class="p">(</span><span class="s2">" * * "</span><span class="p">)</span>
<span class="n">print_delay</span><span class="p">(</span><span class="s2">" * * "</span><span class="p">)</span>
<span class="n">print_delay</span><span class="p">(</span><span class="s2">" * "</span><span class="p">)</span>
<span class="nb">print</span> <span class="s2">"</span><span class="se">\n\n\n</span><span class="s2">"</span>
<span class="k">def</span> <span class="nf">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">):</span>
<span class="nb">print</span> <span class="s2">"</span><span class="se">\n\n\n\n</span><span class="s2">"</span>
<span class="nb">print</span> <span class="s2">" Example #"</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" ---------------------"</span>
<span class="nb">print</span> <span class="s2">" Initial x = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Initial v = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" a = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Coefficient of Restitution = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">""</span>
<span class="nb">print</span> <span class="s2">" Stickiness correction = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'stickiness_correction'</span><span class="p">])</span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'stickiness_correction'</span><span class="p">]</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">env</span><span class="p">[</span><span class="s1">'exact_solution'</span><span class="p">]:</span>
<span class="nb">print</span> <span class="s2">" Correction (version 2) = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'correction_version_2'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Exact solution = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'exact_solution'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Use observed dt in next frame = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'use_observed_dt'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">""</span>
<span class="nb">print</span> <span class="s2">" Show starting mark (</span><span class="se">\"</span><span class="s2">.</span><span class="se">\"</span><span class="s2">) = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Show collision mark (</span><span class="se">\"</span><span class="s2">0</span><span class="se">\"</span><span class="s2">) = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">track</span><span class="p">[</span><span class="s1">'show_collision_mark'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Show physics-engine output = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cl</span><span class="p">[</span><span class="s1">'details'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">""</span>
<span class="nb">print</span> <span class="s2">" FPS target = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Auto-Off = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'auto_off'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" Zoom (meters to px factor) = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s1">'m_to_px'</span><span class="p">])</span>
<span class="nb">print</span> <span class="s2">" "</span>
<span class="k">def</span> <span class="nf">modify</span><span class="p">(</span> <span class="n">car</span><span class="p">,</span> <span class="n">env</span><span class="p">):</span>
<span class="k">if</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.5</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_collision_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.7</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">''' </span>
<span class="s1"> This first example has the car (represented by a "*") starting from</span>
<span class="s1"> rest and accelerating to the left. Stickiness correction is ON. There is</span>
<span class="s1"> energy loss (fractional reduction in v) after each wall collision.</span>
<span class="s1"> ||</span>
<span class="s1"> The "p" key pauses (and restarts) the run. This can be used to give</span>
<span class="s1"> additional time for reading the descriptions at the beginning.</span>
<span class="s1"> The "x" stops the run.</span>
<span class="s1"> ||</span>
<span class="s1"> First, remember that this is 1-D motion! The history of this</span>
<span class="s1"> motion moves vertically, one step at a time, as the program renders each</span>
<span class="s1"> new single-line snapshot. An effective way to view this 1-D motion</span>
<span class="s1"> (animation) is to focus your attention at the bottom row. The YouTube</span>
<span class="s1"> video provides a visual aid (an annotation rectangle) to</span>
<span class="s1"> help you do this. Another approach is to place a sheet of paper over</span>
<span class="s1"> everything on the screen except the bottom row.</span>
<span class="s1"> ||</span>
<span class="s1"> If the "d" option is given at the command line, the details of the</span>
<span class="s1"> physics calculation are printed with each frame. This outputs position,</span>
<span class="s1"> velocity, and frame rate.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop</span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">5.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.65</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.7</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'stickiness_correction'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_collision_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">''' </span>
<span class="s1"> Stickiness correction is turned off which allows the overlap</span>
<span class="s1"> (penetration) to be seen. These are elastic collisions (CR=1), meaning</span>
<span class="s1"> this will run until a keyboard stop or the loop counter hits its limit.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop</span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.5</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'stickiness_correction'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_collision_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'auto_off'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">30</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.6</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">''' </span>
<span class="s1"> All parameters are identical to example 1 except that stickiness</span>
<span class="s1"> correction is OFF.</span>
<span class="s1"> ||</span>
<span class="s1"> Watch the wall collision. With stickiness correction turned off, the</span>
<span class="s1"> car will be allowed to render in the state of collision (on the other</span>
<span class="s1"> side of the wall). But with the first bounce, due to gravity and the</span>
<span class="s1"> collision-related energy losses, the car does not recover from the state</span>
<span class="s1"> of penetration (the car sticks inside the wall), that is, the ball does not</span>
<span class="s1"> bounce back far enough to get back to the other side. This leads to a</span>
<span class="s1"> state of perpetual collisions, with gravity dragging the car to the</span>
<span class="s1"> left.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop </span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">4.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.2</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">10.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">10.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.8</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">300</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_collision_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">''' </span>
<span class="s1"> The target frame rate is set high to give an interesting display of the</span>
<span class="s1"> time-series tail. The car loses speed from each wall collision.</span>
<span class="s1"> Acceleration (set high) to the left (negative).</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop</span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">5</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">3.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.7</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">''' </span>
<span class="s1"> Acceleration is to the right (opposite direction from other examples).</span>
<span class="s1"> ||</span>
<span class="s1"> A collision mark (0) is displayed at the original collision</span>
<span class="s1"> position (before stickiness correction).</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop </span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">6</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">3.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.7</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'m_to_px'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">30.0</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">''' </span>
<span class="s1"> The scaling factor between the physics engine and the renderer has</span>
<span class="s1"> decreased from the level used in example 5. This effectively zooms out</span>
<span class="s1"> the view of the track and the car on it. The output from the physics</span>
<span class="s1"> engine is unchanged by this; all characteristics of the movement are</span>
<span class="s1"> identical to those in example 5.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop</span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">7</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">240</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">'''</span>
<span class="s1"> The CR value of unity yields elastic collisions. The frame rate is set</span>
<span class="s1"> high to give the highest precision in the physics predictions. Note</span>
<span class="s1"> the car consistently returns to the initial starting point as marked by</span>
<span class="s1"> the period symbol on the track.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop </span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">8</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">'''</span>
<span class="s1"> This is like the previous example (7), except the target frame rate is</span>
<span class="s1"> reduced to give the lower precision in the physics predictions.</span>
<span class="s1"> Note the car does NOT return to the initial starting point (as marked</span>
<span class="s1"> on the track by column of period symbols). </span>
<span class="s1"> ||</span>
<span class="s1"> The "0"s are easy to see in this example. As mentioned before, these</span>
<span class="s1"> are marks to indicate the position of the car at the time of collision</span>
<span class="s1"> detection (before stickiness correction is applied to resolve the state</span>
<span class="s1"> of overlap).</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop</span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">9</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'use_observed_dt'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">'''</span>
<span class="s1"> The observed dt is used in the subsequent frame to calculate the</span>
<span class="s1"> physics engine motions. All other settings are identical to those in</span>
<span class="s1"> example 8. Note the car, again, does NOT return to the initial starting</span>
<span class="s1"> point (as marked on the track by column of period symbols). But here the</span>
<span class="s1"> return behavior is more variable.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop </span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">10</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'exact_solution'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">'''</span>
<span class="s1"> The Euler-method calculation has been replaced with an exact</span>
<span class="s1"> calculation method in this example. The calculation uses physics</span>
<span class="s1"> kinematics equations to model the motion between the ends of the track</span>
<span class="s1"> and also the motion in the collision frames. The frame rate is set low</span>
<span class="s1"> to give the most severe test for this exact method. Note the car</span>
<span class="s1"> consistently returns to the initial starting point.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop </span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">11</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'exact_solution'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'use_observed_dt'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">'''</span>
<span class="s1"> The observed dt is used in the calculations of subsequent frames. Note</span>
<span class="s1"> that in contrast to example 9, the car consistently returns to the</span>
<span class="s1"> initial starting point.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop</span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">12</span><span class="p">:</span>
<span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'v_mps'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">;</span> <span class="n">car</span><span class="p">[</span><span class="s1">'a_mps2'</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.0</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'CR'</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.8</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'show_start_mark'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'exact_solution'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">env</span><span class="p">[</span><span class="s1">'use_observed_dt'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">'''</span>
<span class="s1"> Same as example 11 but with a CR of less than 1.0.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop </span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"--> Defaults"</span>
<span class="n">print_header</span><span class="p">(</span><span class="n">car</span><span class="p">)</span>
<span class="n">explaination</span> <span class="o">=</span> <span class="s1">''' </span>
<span class="s1"> No command line arguments were supplied or there was no</span>
<span class="s1"> match for the mode value. Default parameters will be used.</span>
<span class="s1"> ||</span>
<span class="s1"> p key: pause or resume / x key: stop</span>
<span class="s1"> '''</span>
<span class="n">pretty_paragraphs</span><span class="p">(</span> <span class="n">explaination</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">try_sleep</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">at_rest</span><span class="p">(</span> <span class="n">car</span><span class="p">):</span>
<span class="n">avg_car_position</span> <span class="o">=</span> <span class="n">pos_avg_10</span><span class="p">(</span> <span class="n">car</span><span class="p">)</span>
<span class="n">rest_tolerance</span> <span class="o">=</span> <span class="mf">0.001</span>
<span class="k">if</span> <span class="p">(</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">avg_car_position</span> <span class="o">-</span> <span class="n">track</span><span class="p">[</span><span class="s1">'left_edge_m'</span><span class="p">])</span> <span class="o"><</span> <span class="n">rest_tolerance</span><span class="p">)</span> <span class="ow">or</span>
<span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">avg_car_position</span> <span class="o">-</span> <span class="n">track</span><span class="p">[</span><span class="s1">'right_edge_m'</span><span class="p">])</span> <span class="o"><</span> <span class="n">rest_tolerance</span><span class="p">)</span> <span class="p">):</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">def</span> <span class="nf">cl_args_init</span><span class="p">():</span>
<span class="n">cl</span><span class="p">[</span><span class="s1">'n_args'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<span class="k">if</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'n_args'</span><span class="p">]</span> <span class="o">>=</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">cl</span><span class="p">[</span><span class="s1">'example_index'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="n">cl</span><span class="p">[</span><span class="s1">'details'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">if</span> <span class="n">cl</span><span class="p">[</span><span class="s1">'n_args'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"d"</span><span class="p">:</span>
<span class="n">cl</span><span class="p">[</span><span class="s1">'details'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">check_pause_orDone</span><span class="p">():</span>
<span class="k">for</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">pygame</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">get</span><span class="p">():</span>
<span class="k">if</span> <span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">pygame</span><span class="o">.</span><span class="n">QUIT</span><span class="p">):</span>
<span class="nb">print</span> <span class="s1">'Stopped by closing pygame window.'</span>
<span class="n">user</span><span class="p">[</span><span class="s1">'done'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">pygame</span><span class="o">.</span><span class="n">KEYDOWN</span><span class="p">):</span>
<span class="k">if</span> <span class="p">((</span><span class="n">event</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="n">K_ESCAPE</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="n">K_x</span><span class="p">)):</span>
<span class="nb">print</span> <span class="s1">'Stopped with escape key or x key.'</span>
<span class="n">user</span><span class="p">[</span><span class="s1">'done'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="n">K_p</span><span class="p">):</span>
<span class="n">user</span><span class="p">[</span><span class="s1">'paused'</span><span class="p">]</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">user</span><span class="p">[</span><span class="s1">'paused'</span><span class="p">]</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="n">K_s</span><span class="p">):</span>
<span class="n">keys</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">key</span><span class="o">.</span><span class="n">get_pressed</span><span class="p">()</span>
<span class="k">if</span> <span class="p">(</span><span class="n">keys</span><span class="p">[</span><span class="n">pygame</span><span class="o">.</span><span class="n">K_LCTRL</span><span class="p">]</span> <span class="ow">or</span> <span class="n">keys</span><span class="p">[</span><span class="n">pygame</span><span class="o">.</span><span class="n">K_LCTRL</span><span class="p">]):</span>
<span class="n">user</span><span class="p">[</span><span class="s1">'paused'</span><span class="p">]</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">user</span><span class="p">[</span><span class="s1">'paused'</span><span class="p">]</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="n">K_c</span><span class="p">):</span>
<span class="n">keys</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">key</span><span class="o">.</span><span class="n">get_pressed</span><span class="p">()</span>
<span class="k">if</span> <span class="p">(</span><span class="n">keys</span><span class="p">[</span><span class="n">pygame</span><span class="o">.</span><span class="n">K_LCTRL</span><span class="p">]</span> <span class="ow">or</span> <span class="n">keys</span><span class="p">[</span><span class="n">pygame</span><span class="o">.</span><span class="n">K_LCTRL</span><span class="p">]):</span>
<span class="nb">print</span> <span class="s1">'Stopped with ctrl-c.'</span>
<span class="n">user</span><span class="p">[</span><span class="s1">'done'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="k">global</span> <span class="n">env</span><span class="p">,</span> <span class="n">dt_s</span><span class="p">,</span> <span class="n">track</span><span class="p">,</span> <span class="n">fps_observed</span><span class="p">,</span> <span class="n">x_list</span><span class="p">,</span> <span class="n">cl</span><span class="p">,</span> <span class="n">user</span>
<span class="c1"># This pygame window is only used here to facilitate the event handling features of pygame.</span>
<span class="n">pygame</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>
<span class="n">display_surface</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">set_mode</span><span class="p">((</span><span class="mi">100</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="c1"># A list to support calculating a running average of the position.</span>
<span class="n">x_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># Initialize the general parameters that control the environment.</span>
<span class="n">env</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'stickiness_correction'</span><span class="p">:</span><span class="kc">True</span><span class="p">,</span> <span class="s1">'correction_version_2'</span><span class="p">:</span><span class="kc">True</span> <span class="p">,</span>
<span class="s1">'m_to_px'</span><span class="p">:</span><span class="mf">55.0</span><span class="p">,</span> <span class="s1">'CR'</span><span class="p">:</span><span class="mf">0.80</span><span class="p">,</span> <span class="s1">'auto_off'</span><span class="p">:</span><span class="kc">True</span><span class="p">,</span> <span class="s1">'fps_target'</span><span class="p">:</span><span class="mi">30</span><span class="p">,</span>
<span class="s1">'exact_solution'</span><span class="p">:</span><span class="kc">False</span><span class="p">,</span> <span class="s1">'use_observed_dt'</span><span class="p">:</span><span class="kc">False</span><span class="p">}</span>
<span class="c1"># Characteristics of the air track (the 1-D range of space that the car moves along).</span>
<span class="n">track</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'left_edge_m'</span><span class="p">:</span><span class="mf">0.25</span> <span class="p">,</span> <span class="s1">'right_edge_m'</span><span class="p">:</span><span class="mf">2.2</span><span class="p">,</span>
<span class="s1">'show_start_mark'</span><span class="p">:</span><span class="kc">False</span><span class="p">,</span>
<span class="s1">'collision_state'</span><span class="p">:</span><span class="s1">'none'</span><span class="p">,</span>
<span class="s1">'collision_mark_px'</span><span class="p">:</span><span class="o">-</span><span class="mi">999</span><span class="p">,</span> <span class="s1">'show_collision_mark'</span><span class="p">:</span><span class="kc">True</span><span class="p">}</span>
<span class="n">car</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'x_m'</span><span class="p">:</span><span class="mf">1.1</span><span class="p">,</span> <span class="s1">'v_mps'</span><span class="p">:</span><span class="mf">0.0</span><span class="p">,</span> <span class="s1">'a_mps2'</span><span class="p">:</span><span class="mf">0.0</span><span class="p">}</span>
<span class="c1"># Use the command-line arguments if provided. Put them in a dictionary.</span>
<span class="n">cl</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'details'</span><span class="p">:</span><span class="kc">False</span><span class="p">,</span> <span class="s1">'n_args'</span><span class="p">:</span><span class="mi">0</span><span class="p">}</span>
<span class="n">cl_args_init</span><span class="p">()</span>
<span class="c1"># Modify the initial conditions for the car and environment if command line parameters </span>
<span class="c1"># were provided.</span>
<span class="k">if</span> <span class="p">(</span><span class="n">cl</span><span class="p">[</span><span class="s1">'n_args'</span><span class="p">]</span> <span class="o">></span> <span class="mi">0</span><span class="p">):</span>
<span class="n">modify</span><span class="p">(</span> <span class="n">car</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
<span class="n">fps_observed</span> <span class="o">=</span> <span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span>
<span class="n">dt_target_s</span> <span class="o">=</span> <span class="mf">1.0</span><span class="o">/</span><span class="n">env</span><span class="p">[</span><span class="s1">'fps_target'</span><span class="p">]</span>
<span class="n">dt_s</span> <span class="o">=</span> <span class="n">dt_target_s</span>
<span class="n">dt_observed_s</span> <span class="o">=</span> <span class="n">dt_target_s</span>
<span class="c1"># A mark on the track where the car started at (optionally displayed)</span>
<span class="n">track</span><span class="p">[</span><span class="s1">'track_mark_px'</span><span class="p">]</span> <span class="o">=</span> <span class="n">px_from_m</span><span class="p">(</span><span class="n">car</span><span class="p">[</span><span class="s1">'x_m'</span><span class="p">])</span>
<span class="n">t_now_s</span> <span class="o">=</span> <span class="n">default_timer</span><span class="p">()</span>
<span class="n">user</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'paused'</span><span class="p">:</span><span class="kc">False</span><span class="p">,</span> <span class="s1">'done'</span><span class="p">:</span><span class="kc">False</span><span class="p">}</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span> <span class="mi">50000</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">t_previous_s</span> <span class="o">=</span> <span class="n">t_now_s</span>
<span class="n">check_pause_orDone</span><span class="p">()</span>
<span class="k">if</span> <span class="n">user</span><span class="p">[</span><span class="s1">'done'</span><span class="p">]:</span>
<span class="k">break</span>
<span class="c1"># This check stops the physics calculations if paused by</span>
<span class="c1"># by the p key or if the little pygame window is clicked.</span>
<span class="k">if</span> <span class="p">((</span><span class="n">dt_observed_s</span> <span class="o"><</span> <span class="mf">0.15</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">user</span><span class="p">[</span><span class="s1">'paused'</span><span class="p">])):</span>
<span class="n">move</span><span class="p">(</span> <span class="n">car</span><span class="p">)</span>
<span class="n">check_for_wall_collisions</span><span class="p">(</span> <span class="n">car</span><span class="p">)</span>
<span class="n">render</span><span class="p">(</span> <span class="n">car</span><span class="p">)</span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'auto_off'</span><span class="p">]:</span>
<span class="k">if</span> <span class="n">at_rest</span><span class="p">(</span> <span class="n">car</span><span class="p">):</span>
<span class="k">break</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span> <span class="n">dt_target_s</span><span class="p">)</span>
<span class="n">t_now_s</span> <span class="o">=</span> <span class="n">default_timer</span><span class="p">()</span>
<span class="n">dt_observed_s</span> <span class="o">=</span> <span class="n">t_now_s</span> <span class="o">-</span> <span class="n">t_previous_s</span>
<span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'use_observed_dt'</span><span class="p">]:</span>
<span class="n">dt_s</span> <span class="o">=</span> <span class="n">dt_observed_s</span>
<span class="n">fps_observed</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="n">dt_observed_s</span>
<span class="k">except</span> <span class="ne">KeyboardInterrupt</span><span class="p">:</span>
<span class="c1"># Note: this exception will only fire if the main command window has the focus. So if using</span>
<span class="c1"># the pygame events (and it's window) you must click in the main command window before</span>
<span class="c1"># issuing the ctrl-c.</span>
<span class="nb">print</span> <span class="s2">"Stopped by keyboard (ctrl-c)."</span>
<span class="k">break</span>
<span class="k">except</span><span class="p">:</span>
<span class="nb">print</span> <span class="s2">"There is a problem in the TRY block above: </span><span class="se">\n</span><span class="s2">"</span>
<span class="c1"># The following "raise" will print out the traceback.</span>
<span class="k">raise</span>
<span class="k">break</span>
<span class="n">main</span><span class="p">()</span>
</pre></div>
</body>
</html>