Browse Source

Open, Introspection, Plot, Pygame

pull/102/merge
Jure Šorn 4 weeks ago
parent
commit
b4d019e9ef
3 changed files with 46 additions and 46 deletions
  1. 34
      README.md
  2. 38
      index.html
  3. 20
      parse.js

34
README.md

@ -1571,14 +1571,14 @@ Open
* **`'newline=""'` means no conversions take place, but input is still broken into chunks by readline() and readlines() on every '\n', '\r' and '\r\n'.**
### Modes
* **`'r'` - Read (default).**
* **`'w'` - Write (truncate, i.e. delete existing contents).**
* **`'r'` - Read. Used by default.**
* **`'w'` - Write. Deletes existing contents.**
* **`'x'` - Write or fail if the file already exists.**
* **`'a'` - Append.**
* **`'w+'` - Read and write (truncate).**
* **`'a'` - Append. Creates new file if it doesn't exist.**
* **`'w+'` - Read and write. Deletes existing contents.**
* **`'r+'` - Read and write from the start.**
* **`'a+'` - Read and write from the end.**
* **`'b'` - Binary mode (`'br'`, `'bw'`, `'bx'`, …).**
* **`'b'` - Binary mode (`'br'`, `'bw'`, `'bx'`, …)**
### Exceptions
* **`'FileNotFoundError'` can be raised when reading with `'r'` or `'r+'`.**
@ -2304,7 +2304,7 @@ delattr(<obj>, '<attr_name>') # Same. Also `del <object>.<attr_name>`.
```python
<Sig> = inspect.signature(<function>) # Returns function's Signature object.
<dict> = <Sig>.parameters # Dict of Parameter objects. Also <Sig>.return_type.
<dict> = <Sig>.parameters # Dict of Parameters. Also <Sig>.return_annotation.
<memb> = <Param>.kind # Member of ParamKind enum (Parameter.KEYWORD_ONLY, …).
<obj> = <Param>.default # Returns parameter's default value or Parameter.empty.
<type> = <Param>.annotation # Returns parameter's type hint or Parameter.empty.
@ -2413,7 +2413,7 @@ import matplotlib.pyplot as plt
plt.plot/bar/scatter(x_data, y_data [, label=<str>]) # Or: plt.plot(y_data)
plt.legend() # Adds a legend.
plt.title/xlabel/ylabel(<str>) # Adds a title/labels.
plt.title/xlabel/ylabel(<str>) # Adds a title/label.
plt.savefig(<path>) # Saves the figure.
plt.show() # Displays the figure.
plt.clf() # Clears the figure.
@ -3100,7 +3100,7 @@ def run(screen, images, mario, tiles):
pressed -= {keys.get(e.key) for e in pg.event.get(pg.KEYUP)}
update_speed(mario, tiles, pressed)
update_position(mario, tiles)
draw(screen, images, mario, tiles, pressed)
draw(screen, images, mario, tiles)
def update_speed(mario, tiles, pressed):
x, y = mario.spd
@ -3114,7 +3114,8 @@ def update_position(mario, tiles):
n_steps = max(abs(s) for s in mario.spd)
for _ in range(n_steps):
mario.spd = stop_on_collision(mario.spd, get_boundaries(mario.rect, tiles))
mario.rect.topleft = x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)
x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)
mario.rect.topleft = x, y
def get_boundaries(rect, tiles):
deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)}
@ -3124,16 +3125,15 @@ def stop_on_collision(spd, bounds):
return P(x=0 if (D.w in bounds and spd.x < 0) or (D.e in bounds and spd.x > 0) else spd.x,
y=0 if (D.n in bounds and spd.y < 0) or (D.s in bounds and spd.y > 0) else spd.y)
def draw(screen, images, mario, tiles, pressed):
def get_marios_image_index():
if D.s not in get_boundaries(mario.rect, tiles):
return 4
return next(mario.frame_cycle) if {D.w, D.e} & pressed else 6
def draw(screen, images, mario, tiles):
screen.fill((85, 168, 255))
mario.facing_left = (D.w in pressed) if {D.w, D.e} & pressed else mario.facing_left
screen.blit(images[get_marios_image_index() + mario.facing_left * 9], mario.rect)
mario.facing_left = mario.spd.x < 0 if mario.spd.x else mario.facing_left
is_airborne = D.s not in get_boundaries(mario.rect, tiles)
image_index = 4 if is_airborne else (next(mario.frame_cycle) if mario.spd.x else 6)
screen.blit(images[image_index + mario.facing_left * 9], mario.rect)
for t in tiles:
screen.blit(images[18 if t.x in [0, (W-1)*16] or t.y in [0, (H-1)*16] else 19], t)
is_border = t.x in [0, (W-1)*16] or t.y in [0, (H-1)*16]
screen.blit(images[18 if is_border else 19], t)
pg.display.flip()
if __name__ == '__main__':

38
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>August 28, 2024</aside>
<aside>September 1, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1333,14 +1333,14 @@ p.add_argument(<span class="hljs-string">'&lt;name&gt;'</span>, type=&lt;type&gt
<li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on every '\n', '\r' and '\r\n'.</strong></li>
</ul>
<div><h3 id="modes">Modes</h3><ul>
<li><strong><code class="python hljs"><span class="hljs-string">'r'</span></code> - Read (default).</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'w'</span></code> - Write (truncate, i.e. delete existing contents).</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'r'</span></code> - Read. Used by default.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'w'</span></code> - Write. Deletes existing contents.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'x'</span></code> - Write or fail if the file already exists.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'a'</span></code> - Append.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'w+'</span></code> - Read and write (truncate).</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'a'</span></code> - Append. Creates new file if it doesn't exist.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'w+'</span></code> - Read and write. Deletes existing contents.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'r+'</span></code> - Read and write from the start.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode (<code class="python hljs"><span class="hljs-string">'br'</span></code>, <code class="python hljs"><span class="hljs-string">'bw'</span></code>, <code class="python hljs"><span class="hljs-string">'bx'</span></code>, …).</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode (<code class="python hljs"><span class="hljs-string">'br'</span></code>, <code class="python hljs"><span class="hljs-string">'bw'</span></code>, <code class="python hljs"><span class="hljs-string">'bx'</span></code>, …)</strong></li>
</ul><div><h3 id="exceptions-1">Exceptions</h3><ul>
<li><strong><code class="python hljs"><span class="hljs-string">'FileNotFoundError'</span></code> can be raised when reading with <code class="python hljs"><span class="hljs-string">'r'</span></code> or <code class="python hljs"><span class="hljs-string">'r+'</span></code>.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'FileExistsError'</span></code> can be raised when writing with <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li>
@ -1891,7 +1891,7 @@ setattr(&lt;obj&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>, value
delattr(&lt;obj&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Same. Also `del &lt;object&gt;.&lt;attr_name&gt;`.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;Sig&gt; = inspect.signature(&lt;function&gt;) <span class="hljs-comment"># Returns function's Signature object.</span>
&lt;dict&gt; = &lt;Sig&gt;.parameters <span class="hljs-comment"># Dict of Parameter objects. Also &lt;Sig&gt;.return_type.</span>
&lt;dict&gt; = &lt;Sig&gt;.parameters <span class="hljs-comment"># Dict of Parameters. Also &lt;Sig&gt;.return_annotation.</span>
&lt;memb&gt; = &lt;Param&gt;.kind <span class="hljs-comment"># Member of ParamKind enum (Parameter.KEYWORD_ONLY, …).</span>
&lt;obj&gt; = &lt;Param&gt;.default <span class="hljs-comment"># Returns parameter's default value or Parameter.empty.</span>
&lt;type&gt; = &lt;Param&gt;.annotation <span class="hljs-comment"># Returns parameter's type hint or Parameter.empty.</span>
@ -1980,7 +1980,7 @@ Processing: 100%|████████████████████| 3
plt.plot/bar/scatter(x_data, y_data [, label=&lt;str&gt;]) <span class="hljs-comment"># Or: plt.plot(y_data)</span>
plt.legend() <span class="hljs-comment"># Adds a legend.</span>
plt.title/xlabel/ylabel(&lt;str&gt;) <span class="hljs-comment"># Adds a title/labels.</span>
plt.title/xlabel/ylabel(&lt;str&gt;) <span class="hljs-comment"># Adds a title/label.</span>
plt.savefig(&lt;path&gt;) <span class="hljs-comment"># Saves the figure.</span>
plt.show() <span class="hljs-comment"># Displays the figure.</span>
plt.clf() <span class="hljs-comment"># Clears the figure.</span>
@ -2533,7 +2533,7 @@ W, H, MAX_S = <span class="hljs-number">50</span>, <span class="hljs-number">50<
pressed -= {keys.get(e.key) <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> pg.event.get(pg.KEYUP)}
update_speed(mario, tiles, pressed)
update_position(mario, tiles)
draw(screen, images, mario, tiles, pressed)
draw(screen, images, mario, tiles)
<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>
x, y = mario.spd
@ -2547,7 +2547,8 @@ W, H, MAX_S = <span class="hljs-number">50</span>, <span class="hljs-number">50<
n_steps = max(abs(s) <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> mario.spd)
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(n_steps):
mario.spd = stop_on_collision(mario.spd, get_boundaries(mario.rect, tiles))
mario.rect.topleft = x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)
x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)
mario.rect.topleft = x, y
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_boundaries</span><span class="hljs-params">(rect, tiles)</span>:</span>
deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}
@ -2557,16 +2558,15 @@ W, H, MAX_S = <span class="hljs-number">50</span>, <span class="hljs-number">50<
<span class="hljs-keyword">return</span> P(x=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.w <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.e <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x &gt; <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.x,
y=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.n <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.s <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y &gt; <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.y)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(screen, images, mario, tiles, pressed)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_marios_image_index</span><span class="hljs-params">()</span>:</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">return</span> <span class="hljs-number">4</span>
<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>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(screen, images, mario, tiles)</span>:</span>
screen.fill((<span class="hljs-number">85</span>, <span class="hljs-number">168</span>, <span class="hljs-number">255</span>))
mario.facing_left = (D.w <span class="hljs-keyword">in</span> pressed) <span class="hljs-keyword">if</span> {D.w, D.e} &amp; pressed <span class="hljs-keyword">else</span> mario.facing_left
screen.blit(images[get_marios_image_index() + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)
mario.facing_left = mario.spd.x &lt; <span class="hljs-number">0</span> <span class="hljs-keyword">if</span> mario.spd.x <span class="hljs-keyword">else</span> mario.facing_left
is_airborne = D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles)
image_index = <span class="hljs-number">4</span> <span class="hljs-keyword">if</span> is_airborne <span class="hljs-keyword">else</span> (next(mario.frame_cycle) <span class="hljs-keyword">if</span> mario.spd.x <span class="hljs-keyword">else</span> <span class="hljs-number">6</span>)
screen.blit(images[image_index + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)
<span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> tiles:
screen.blit(images[<span class="hljs-number">18</span> <span class="hljs-keyword">if</span> t.x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (W-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">or</span> t.y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (H-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">else</span> <span class="hljs-number">19</span>], t)
is_border = t.x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (W-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">or</span> t.y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (H-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>]
screen.blit(images[<span class="hljs-number">18</span> <span class="hljs-keyword">if</span> is_border <span class="hljs-keyword">else</span> <span class="hljs-number">19</span>], t)
pg.display.flip()
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
@ -2933,7 +2933,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer>
<aside>August 28, 2024</aside>
<aside>September 1, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

20
parse.js

@ -261,7 +261,7 @@ const MARIO =
' pressed -= {keys.get(e.key) <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> pg.event.get(pg.KEYUP)}\n' +
' update_speed(mario, tiles, pressed)\n' +
' update_position(mario, tiles)\n' +
' draw(screen, images, mario, tiles, pressed)\n' +
' draw(screen, images, mario, tiles)\n' +
'\n' +
'<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>\n' +
' x, y = mario.spd\n' +
@ -275,7 +275,8 @@ const MARIO =
' n_steps = max(abs(s) <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> mario.spd)\n' +
' <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(n_steps):\n' +
' mario.spd = stop_on_collision(mario.spd, get_boundaries(mario.rect, tiles))\n' +
' mario.rect.topleft = x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)\n' +
' x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)\n' +
' mario.rect.topleft = x, y\n' +
'\n' +
'<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_boundaries</span><span class="hljs-params">(rect, tiles)</span>:</span>\n' +
' deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}\n' +
@ -285,16 +286,15 @@ const MARIO =
' <span class="hljs-keyword">return</span> P(x=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.w <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.e <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x &gt; <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.x,\n' +
' y=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.n <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.s <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y &gt; <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.y)\n' +
'\n' +
'<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(screen, images, mario, tiles, pressed)</span>:</span>\n' +
' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_marios_image_index</span><span class="hljs-params">()</span>:</span>\n' +
' <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):\n' +
' <span class="hljs-keyword">return</span> <span class="hljs-number">4</span>\n' +
' <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>\n' +
'<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(screen, images, mario, tiles)</span>:</span>\n' +
' screen.fill((<span class="hljs-number">85</span>, <span class="hljs-number">168</span>, <span class="hljs-number">255</span>))\n' +
' mario.facing_left = (D.w <span class="hljs-keyword">in</span> pressed) <span class="hljs-keyword">if</span> {D.w, D.e} &amp; pressed <span class="hljs-keyword">else</span> mario.facing_left\n' +
' screen.blit(images[get_marios_image_index() + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)\n' +
' mario.facing_left = mario.spd.x &lt; <span class="hljs-number">0</span> <span class="hljs-keyword">if</span> mario.spd.x <span class="hljs-keyword">else</span> mario.facing_left\n' +
' is_airborne = D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles)\n' +
' image_index = <span class="hljs-number">4</span> <span class="hljs-keyword">if</span> is_airborne <span class="hljs-keyword">else</span> (next(mario.frame_cycle) <span class="hljs-keyword">if</span> mario.spd.x <span class="hljs-keyword">else</span> <span class="hljs-number">6</span>)\n' +
' screen.blit(images[image_index + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)\n' +
' <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> tiles:\n' +
' screen.blit(images[<span class="hljs-number">18</span> <span class="hljs-keyword">if</span> t.x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (W-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">or</span> t.y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (H-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">else</span> <span class="hljs-number">19</span>], t)\n' +
' is_border = t.x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (W-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">or</span> t.y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (H-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>]\n' +
' screen.blit(images[<span class="hljs-number">18</span> <span class="hljs-keyword">if</span> is_border <span class="hljs-keyword">else</span> <span class="hljs-number">19</span>], t)\n' +
' pg.display.flip()\n' +
'\n' +
'<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">\'__main__\'</span>:\n' +

Loading…
Cancel
Save