Browse Source

Coroutine, Audio, Pygame

pull/121/head
Jure Šorn 3 years ago
parent
commit
82c90eaf59
2 changed files with 8 additions and 8 deletions
  1. 8
      README.md
  2. 8
      index.html

8
README.md

@ -2340,7 +2340,7 @@ async def model(moves, state):
state[id_] = P((x + deltas[d].x) % W, (y + deltas[d].y) % H)
async def view(state, screen):
offset = P(x=curses.COLS//2 - W//2, y=curses.LINES//2 - H//2)
offset = P(curses.COLS//2 - W//2, curses.LINES//2 - H//2)
while True:
screen.erase()
curses.textpad.rectangle(screen, offset.y-1, offset.x-1, offset.y+H, offset.x+W)
@ -2856,7 +2856,7 @@ nframes = <Wave_read>.getnframes() # Number of frames.
```python
def read_wav_file(filename):
def get_int(bytes_obj):
an_int = int.from_bytes(bytes_obj, 'little', signed=sampwidth!=1)
an_int = int.from_bytes(bytes_obj, 'little', signed=(sampwidth != 1))
return an_int - 128 * (sampwidth == 1)
with wave.open(filename, 'rb') as file:
sampwidth = file.getsampwidth()
@ -2872,7 +2872,7 @@ def write_to_wav_file(filename, float_samples, nchannels=1, sampwidth=2, framera
a_float = max(-1, min(1 - 2e-16, a_float))
a_float += sampwidth == 1
a_float *= pow(2, sampwidth * 8 - 1)
return int(a_float).to_bytes(sampwidth, 'little', signed=sampwidth!=1)
return int(a_float).to_bytes(sampwidth, 'little', signed=(sampwidth != 1))
with wave.open(filename, 'wb') as file:
file.setnchannels(nchannels)
file.setsampwidth(sampwidth)
@ -3064,7 +3064,7 @@ 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))
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):

8
index.html

@ -1916,7 +1916,7 @@ W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span>
state[id_] = P((x + deltas[d].x) % W, (y + deltas[d].y) % H)
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view</span><span class="hljs-params">(state, screen)</span>:</span>
offset = P(x=curses.COLS//<span class="hljs-number">2</span> - W//<span class="hljs-number">2</span>, y=curses.LINES//<span class="hljs-number">2</span> - H//<span class="hljs-number">2</span>)
offset = P(curses.COLS//<span class="hljs-number">2</span> - W//<span class="hljs-number">2</span>, curses.LINES//<span class="hljs-number">2</span> - H//<span class="hljs-number">2</span>)
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
screen.erase()
curses.textpad.rectangle(screen, offset.y<span class="hljs-number">-1</span>, offset.x<span class="hljs-number">-1</span>, offset.y+H, offset.x+W)
@ -2318,7 +2318,7 @@ nframes = &lt;Wave_read&gt;.getnframes() <span class="hljs-comment"
<div><h3 id="readfloatsamplesfromwavfile">Read Float Samples from WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_wav_file</span><span class="hljs-params">(filename)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_int</span><span class="hljs-params">(bytes_obj)</span>:</span>
an_int = int.from_bytes(bytes_obj, <span class="hljs-string">'little'</span>, signed=sampwidth!=<span class="hljs-number">1</span>)
an_int = int.from_bytes(bytes_obj, <span class="hljs-string">'little'</span>, signed=(sampwidth != <span class="hljs-number">1</span>))
<span class="hljs-keyword">return</span> an_int - <span class="hljs-number">128</span> * (sampwidth == <span class="hljs-number">1</span>)
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
sampwidth = file.getsampwidth()
@ -2332,7 +2332,7 @@ nframes = &lt;Wave_read&gt;.getnframes() <span class="hljs-comment"
a_float = max(<span class="hljs-number">-1</span>, min(<span class="hljs-number">1</span> - <span class="hljs-number">2e-16</span>, a_float))
a_float += sampwidth == <span class="hljs-number">1</span>
a_float *= pow(<span class="hljs-number">2</span>, sampwidth * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>)
<span class="hljs-keyword">return</span> int(a_float).to_bytes(sampwidth, <span class="hljs-string">'little'</span>, signed=sampwidth!=<span class="hljs-number">1</span>)
<span class="hljs-keyword">return</span> int(a_float).to_bytes(sampwidth, <span class="hljs-string">'little'</span>, signed=(sampwidth != <span class="hljs-number">1</span>))
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
file.setnchannels(nchannels)
file.setsampwidth(sampwidth)
@ -2490,7 +2490,7 @@ SIZE, MAX_SPEED = <span class="hljs-number">50</span>, P(<span class="hljs-numbe
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))
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>

Loading…
Cancel
Save