Skip to content

Commit d7a22a5

Browse files
committed
General fixes from CSV until Pygame
1 parent 1649a43 commit d7a22a5

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ Exit
14591459
import sys
14601460
sys.exit() # Exits with exit code 0 (success).
14611461
sys.exit(<el>) # Prints object to stderr and exits with 1.
1462-
sys.exit(<int>) # Exits with the passed exit code.
1462+
sys.exit(<int>) # Exits with passed exit code.
14631463
```
14641464

14651465

@@ -1476,19 +1476,19 @@ print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
14761476
from pprint import pprint
14771477
pprint(<collection>, width=80, depth=None, compact=False, sort_dicts=True)
14781478
```
1479-
* **Levels deeper than 'depth' get replaced by '...'.**
1479+
* **Levels deeper than 'depth' get replaced with '...'.**
14801480

14811481

14821482
Input
14831483
-----
1484-
**Reads a line from user input or pipe if present.**
1484+
**Reads a line from the user input or pipe if present.**
14851485

14861486
```python
14871487
<str> = input(prompt=None)
14881488
```
14891489
* **Trailing newline gets stripped.**
14901490
* **Prompt string is printed to the standard output before reading input.**
1491-
* **Raises EOFError when user hits EOF (ctrl-d) or input stream gets exhausted.**
1491+
* **Raises EOFError when user hits EOF (ctrl-d/z) or input stream gets exhausted.**
14921492

14931493

14941494
Command Line Arguments
@@ -2132,15 +2132,15 @@ Introspection
21322132

21332133
### Variables
21342134
```python
2135-
<list> = dir() # Returns names of local variables (incl. functions).
2136-
<dict> = vars() # Returns dict of local variables. Also locals().
2137-
<dict> = globals() # Returns dict of global variables.
2135+
<list> = dir() # Names of local variables (incl. functions).
2136+
<dict> = vars() # Dict of local variables. Also locals().
2137+
<dict> = globals() # Dict of global variables.
21382138
```
21392139

21402140
### Attributes
21412141
```python
2142-
<list> = dir(<object>) # Returns names of object's attributes (incl. methods).
2143-
<dict> = vars(<object>) # Returns dict of object's fields. Also <obj>.__dict__.
2142+
<list> = dir(<object>) # Names of object's attributes (incl. methods).
2143+
<dict> = vars(<object>) # Dict of object's fields. Also <obj>.__dict__.
21442144
```
21452145

21462146
```python
@@ -2198,7 +2198,7 @@ class MyMetaClass(type):
21982198
* **The only difference between the examples above is that my\_meta\_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.**
21992199

22002200
### Metaclass Attribute
2201-
**Right before a class is created it checks if it has a 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
2201+
**Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
22022202

22032203
```python
22042204
class MyClass(metaclass=MyMetaClass):
@@ -2223,8 +2223,8 @@ type(MyMetaClass) == type # MyMetaClass is an instance of type.
22232223
| MyClass --> MyMetaClass |
22242224
| | v |
22252225
| object -----> type <+ |
2226-
| | ^ +---+ |
2227-
| str ---------+ |
2226+
| | ^ +--+ |
2227+
| str ----------+ |
22282228
+-------------+-------------+
22292229
```
22302230

@@ -2388,7 +2388,7 @@ def get_border(screen):
23882388
from collections import namedtuple
23892389
P = namedtuple('P', 'x y')
23902390
height, width = screen.getmaxyx()
2391-
return P(width - 1, height - 1)
2391+
return P(width-1, height-1)
23922392

23932393
if __name__ == '__main__':
23942394
main()
@@ -2775,7 +2775,7 @@ for velocity in range(15):
27752775
y = sum(range(velocity+1))
27762776
frame = Image.new('L', (WIDTH, WIDTH))
27772777
draw = ImageDraw.Draw(frame)
2778-
draw.ellipse((WIDTH/2-R, y, WIDTH/2+R, y+2*R), fill='white')
2778+
draw.ellipse((WIDTH/2-R, y, WIDTH/2+R, y+R*2), fill='white')
27792779
frames.append(frame)
27802780
frames += reversed(frames[1:-1])
27812781
imageio.mimsave('test.gif', frames, duration=0.03)
@@ -2804,7 +2804,7 @@ nframes = <Wave_read>.getnframes() # Number of frames.
28042804
<Wave_write>.setnchannels(<int>) # 1 for mono, 2 for stereo.
28052805
<Wave_write>.setsampwidth(<int>) # 2 for CD quality sound.
28062806
<Wave_write>.setparams(<params>) # Sets all parameters.
2807-
<Wave_write>.writeframes(<bytes>) # Appends frames to file.
2807+
<Wave_write>.writeframes(<bytes>) # Appends frames to the file.
28082808
```
28092809
* **Bytes object contains a sequence of frames, each consisting of one or more samples.**
28102810
* **In a stereo signal, the first sample of a frame belongs to the left channel.**
@@ -2933,8 +2933,8 @@ while all(event.type != pg.QUIT for event in pg.event.get()):
29332933
**Object for storing rectangular coordinates.**
29342934
```python
29352935
<Rect> = pg.Rect(x, y, width, height)
2936-
<int> = <Rect>.x/y/centerx/centery
2937-
<tup.> = <Rect>.topleft/center
2936+
<int> = <Rect>.x/y/centerx/centery/
2937+
<tup.> = <Rect>.topleft/center/
29382938
<Rect> = <Rect>.move((x, y))
29392939
```
29402940

@@ -2987,7 +2987,7 @@ pg.draw.ellipse(<Surf>, color, <Rect>)
29872987
<Sound>.play() # Starts playing the sound.
29882988
```
29892989

2990-
### Basic Mario Brothers Example
2990+
### Super Mario Bros. Example
29912991
```python
29922992
import collections, dataclasses, enum, io, math, pygame, urllib.request, itertools as it
29932993
from random import randint
@@ -3028,9 +3028,9 @@ def run(screen, images, mario, tiles):
30283028
def update_speed(mario, tiles, pressed):
30293029
x, y = mario.spd
30303030
x += 2 * ((D.e in pressed) - (D.w in pressed))
3031-
x = math.copysign(abs(x) - 1, x) if x else 0
3031+
x -= x / abs(x) if x else 0
30323032
y += 1 if D.s not in get_boundaries(mario.rect, tiles) else (-10 if D.n in pressed else 0)
3033-
mario.spd = P(*[max(-thresh, min(thresh, s)) for thresh, s in zip(MAX_SPEED, P(x, y))])
3033+
mario.spd = P(*[max(-limit, min(limit, s)) for limit, s in zip(MAX_SPEED, P(x, y))])
30343034

30353035
def update_position(mario, tiles):
30363036
old_p, delta = mario.rect.topleft, P(0, 0)
@@ -3055,7 +3055,7 @@ def draw(screen, images, mario, tiles, pressed):
30553055
return next(mario.frame_cycle) if {D.w, D.e} & pressed else 6
30563056
screen.fill((85, 168, 255))
30573057
mario.facing_left = (D.w in pressed) if {D.e, D.w} & pressed else mario.facing_left
3058-
screen.blit(images[get_frame_index() + mario.facing_left*9], mario.rect)
3058+
screen.blit(images[get_frame_index() + mario.facing_left * 9], mario.rect)
30593059
for rect in tiles:
30603060
screen.blit(images[19 if {*rect.topleft} & {0, (SIZE-1)*16} else 18], rect)
30613061
pygame.display.flip()

index.html

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@
13621362
<div><h2 id="exit"><a href="#exit" name="exit">#</a>Exit</h2><p><strong>Exits the interpreter by raising SystemExit exception.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
13631363
sys.exit() <span class="hljs-comment"># Exits with exit code 0 (success).</span>
13641364
sys.exit(&lt;el&gt;) <span class="hljs-comment"># Prints object to stderr and exits with 1.</span>
1365-
sys.exit(&lt;int&gt;) <span class="hljs-comment"># Exits with the passed exit code.</span>
1365+
sys.exit(&lt;int&gt;) <span class="hljs-comment"># Exits with passed exit code.</span>
13661366
</code></pre></div>
13671367

13681368

@@ -1378,16 +1378,16 @@
13781378
</code></pre></div>
13791379

13801380
<ul>
1381-
<li><strong>Levels deeper than 'depth' get replaced by '…'.</strong></li>
1381+
<li><strong>Levels deeper than 'depth' get replaced with '…'.</strong></li>
13821382
</ul>
1383-
<div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><p><strong>Reads a line from user input or pipe if present.</strong></p><pre><code class="python language-python hljs">&lt;str&gt; = input(prompt=<span class="hljs-keyword">None</span>)
1383+
<div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><p><strong>Reads a line from the user input or pipe if present.</strong></p><pre><code class="python language-python hljs">&lt;str&gt; = input(prompt=<span class="hljs-keyword">None</span>)
13841384
</code></pre></div>
13851385

13861386

13871387
<ul>
13881388
<li><strong>Trailing newline gets stripped.</strong></li>
13891389
<li><strong>Prompt string is printed to the standard output before reading input.</strong></li>
1390-
<li><strong>Raises EOFError when user hits EOF (ctrl-d) or input stream gets exhausted.</strong></li>
1390+
<li><strong>Raises EOFError when user hits EOF (ctrl-d/z) or input stream gets exhausted.</strong></li>
13911391
</ul>
13921392
<div><h2 id="commandlinearguments"><a href="#commandlinearguments" name="commandlinearguments">#</a>Command Line Arguments</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
13931393
script_name = sys.argv[<span class="hljs-number">0</span>]
@@ -1867,15 +1867,15 @@
18671867
LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span> : op.or_})
18681868
last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(&lt;list&gt;)
18691869
</code></pre>
1870-
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Returns names of local variables (incl. functions).</span>
1871-
&lt;dict&gt; = vars() <span class="hljs-comment"># Returns dict of local variables. Also locals().</span>
1872-
&lt;dict&gt; = globals() <span class="hljs-comment"># Returns dict of global variables.</span>
1870+
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Names of local variables (incl. functions).</span>
1871+
&lt;dict&gt; = vars() <span class="hljs-comment"># Dict of local variables. Also locals().</span>
1872+
&lt;dict&gt; = globals() <span class="hljs-comment"># Dict of global variables.</span>
18731873
</code></pre></div></div>
18741874

18751875

18761876

1877-
<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir(&lt;object&gt;) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span>
1878-
&lt;dict&gt; = vars(&lt;object&gt;) <span class="hljs-comment"># Returns dict of object's fields. Also &lt;obj&gt;.__dict__.</span>
1877+
<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir(&lt;object&gt;) <span class="hljs-comment"># Names of object's attributes (incl. methods).</span>
1878+
&lt;dict&gt; = vars(&lt;object&gt;) <span class="hljs-comment"># Dict of object's fields. Also &lt;obj&gt;.__dict__.</span>
18791879
</code></pre></div>
18801880

18811881
<pre><code class="python language-python hljs">&lt;bool&gt; = hasattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>)
@@ -1916,7 +1916,7 @@
19161916
<li><strong>Like in our case, new() can also be called directly, usually from a new() method of a child class (</strong><code class="python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__new__</span><span class="hljs-params">(cls)</span>:</span> <span class="hljs-keyword">return</span> super().__new__(cls)</code><strong>).</strong></li>
19171917
<li><strong>The only difference between the examples above is that my_meta_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.</strong></li>
19181918
</ul>
1919-
<div><h3 id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has a 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span><span class="hljs-params">(metaclass=MyMetaClass)</span>:</span>
1919+
<div><h3 id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span><span class="hljs-params">(metaclass=MyMetaClass)</span>:</span>
19201920
b = <span class="hljs-number">12345</span>
19211921
</code></pre></div>
19221922

@@ -1934,8 +1934,8 @@
19341934
| MyClass --&gt; MyMetaClass |
19351935
| | v |
19361936
| object -----&gt; type &lt;+ |
1937-
| | ^ +---+ |
1938-
| str ---------+ |
1937+
| | ^ +--+ |
1938+
| str ----------+ |
19391939
+-------------+-------------+
19401940
</code></pre>
19411941
<div><h3 id="inheritancediagram">Inheritance Diagram</h3><pre><code class="python language-python hljs">MyClass.__base__ == object <span class="hljs-comment"># MyClass is a subclass of object.</span>
@@ -2067,7 +2067,7 @@
20672067
<span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
20682068
P = namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>)
20692069
height, width = screen.getmaxyx()
2070-
<span class="hljs-keyword">return</span> P(width - <span class="hljs-number">1</span>, height - <span class="hljs-number">1</span>)
2070+
<span class="hljs-keyword">return</span> P(width<span class="hljs-number">-1</span>, height<span class="hljs-number">-1</span>)
20712071

20722072
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
20732073
main()
@@ -2367,7 +2367,7 @@
23672367
y = sum(range(velocity+<span class="hljs-number">1</span>))
23682368
frame = Image.new(<span class="hljs-string">'L'</span>, (WIDTH, WIDTH))
23692369
draw = ImageDraw.Draw(frame)
2370-
draw.ellipse((WIDTH/<span class="hljs-number">2</span>-R, y, WIDTH/<span class="hljs-number">2</span>+R, y+<span class="hljs-number">2</span>*R), fill=<span class="hljs-string">'white'</span>)
2370+
draw.ellipse((WIDTH/<span class="hljs-number">2</span>-R, y, WIDTH/<span class="hljs-number">2</span>+R, y+R*<span class="hljs-number">2</span>), fill=<span class="hljs-string">'white'</span>)
23712371
frames.append(frame)
23722372
frames += reversed(frames[<span class="hljs-number">1</span>:<span class="hljs-number">-1</span>])
23732373
imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<span class="hljs-number">0.03</span>)
@@ -2390,7 +2390,7 @@
23902390
&lt;Wave_write&gt;.setnchannels(&lt;int&gt;) <span class="hljs-comment"># 1 for mono, 2 for stereo.</span>
23912391
&lt;Wave_write&gt;.setsampwidth(&lt;int&gt;) <span class="hljs-comment"># 2 for CD quality sound.</span>
23922392
&lt;Wave_write&gt;.setparams(&lt;params&gt;) <span class="hljs-comment"># Sets all parameters.</span>
2393-
&lt;Wave_write&gt;.writeframes(&lt;bytes&gt;) <span class="hljs-comment"># Appends frames to file.</span>
2393+
&lt;Wave_write&gt;.writeframes(&lt;bytes&gt;) <span class="hljs-comment"># Appends frames to the file.</span>
23942394
</code></pre>
23952395
<ul>
23962396
<li><strong>Bytes object contains a sequence of frames, each consisting of one or more samples.</strong></li>
@@ -2494,8 +2494,8 @@
24942494

24952495

24962496
<div><h3 id="rect">Rect</h3><p><strong>Object for storing rectangular coordinates.</strong></p><pre><code class="python language-python hljs">&lt;Rect&gt; = pg.Rect(x, y, width, height)
2497-
&lt;int&gt; = &lt;Rect&gt;.x/y/centerx/centery
2498-
&lt;tup.&gt; = &lt;Rect&gt;.topleft/center
2497+
&lt;int&gt; = &lt;Rect&gt;.x/y/centerx/centery/…
2498+
&lt;tup.&gt; = &lt;Rect&gt;.topleft/center/…
24992499
&lt;Rect&gt; = &lt;Rect&gt;.move((x, y))
25002500
</code></pre></div>
25012501

@@ -2535,7 +2535,7 @@
25352535
&lt;Sound&gt;.play() <span class="hljs-comment"># Starts playing the sound.</span>
25362536
</code></pre></div>
25372537

2538-
<div><h3 id="basicmariobrothersexample">Basic Mario Brothers Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> collections, dataclasses, enum, io, math, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it
2538+
<div><h3 id="supermariobrosexample">Super Mario Bros. Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> collections, dataclasses, enum, io, math, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it
25392539
<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
25402540

25412541
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
@@ -2574,9 +2574,9 @@
25742574
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_speed</span><span class="hljs-params">(mario, tiles, pressed)</span>:</span>
25752575
x, y = mario.spd
25762576
x += <span class="hljs-number">2</span> * ((D.e <span class="hljs-keyword">in</span> pressed) - (D.w <span class="hljs-keyword">in</span> pressed))
2577-
x = math.copysign(abs(x) - <span class="hljs-number">1</span>, x) <span class="hljs-keyword">if</span> x <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>
2577+
x -= x / abs(x) <span class="hljs-keyword">if</span> x <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>
25782578
y += <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles) <span class="hljs-keyword">else</span> (<span class="hljs-number">-10</span> <span class="hljs-keyword">if</span> D.n <span class="hljs-keyword">in</span> pressed <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>)
2579-
mario.spd = P(*[max(-thresh, min(thresh, s)) <span class="hljs-keyword">for</span> thresh, s <span class="hljs-keyword">in</span> zip(MAX_SPEED, P(x, y))])
2579+
mario.spd = P(*[max(-limit, min(limit, s)) <span class="hljs-keyword">for</span> limit, s <span class="hljs-keyword">in</span> zip(MAX_SPEED, P(x, y))])
25802580

25812581
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_position</span><span class="hljs-params">(mario, tiles)</span>:</span>
25822582
old_p, delta = mario.rect.topleft, P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
@@ -2601,7 +2601,7 @@
26012601
<span class="hljs-keyword">return</span> next(mario.frame_cycle) <span class="hljs-keyword">if</span> {D.w, D.e} &amp; pressed <span class="hljs-keyword">else</span> <span class="hljs-number">6</span>
26022602
screen.fill((<span class="hljs-number">85</span>, <span class="hljs-number">168</span>, <span class="hljs-number">255</span>))
26032603
mario.facing_left = (D.w <span class="hljs-keyword">in</span> pressed) <span class="hljs-keyword">if</span> {D.e, D.w} &amp; pressed <span class="hljs-keyword">else</span> mario.facing_left
2604-
screen.blit(images[get_frame_index() + mario.facing_left*<span class="hljs-number">9</span>], mario.rect)
2604+
screen.blit(images[get_frame_index() + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)
26052605
<span class="hljs-keyword">for</span> rect <span class="hljs-keyword">in</span> tiles:
26062606
screen.blit(images[<span class="hljs-number">19</span> <span class="hljs-keyword">if</span> {*rect.topleft} &amp; {<span class="hljs-number">0</span>, (SIZE<span class="hljs-number">-1</span>)*<span class="hljs-number">16</span>} <span class="hljs-keyword">else</span> <span class="hljs-number">18</span>], rect)
26072607
pygame.display.flip()

web/script_2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ const DIAGRAM_1_B =
5151
'┃ MyClass ──→ MyMetaClass ┃\n' +
5252
'┃ │ ↓ ┃\n' +
5353
'┃ object ─────→ type ←╮ ┃\n' +
54-
'┃ │ ↑ ╰──╯ ┃\n' +
55-
'┃ str ───────── ┃\n' +
54+
'┃ │ ↑ ╰──╯ ┃\n' +
55+
'┃ str ──────────╯ ┃\n' +
5656
'┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
5757

5858
const DIAGRAM_2_A =

0 commit comments

Comments
 (0)