Browse Source

Progress bar and Pygame

pull/83/head
Jure Šorn 4 years ago
parent
commit
7df46ab443
3 changed files with 37 additions and 28 deletions
  1. 28
      README.md
  2. 28
      index.html
  3. 9
      parse.js

28
README.md

@ -2332,10 +2332,10 @@ Progress Bar
------------
```python
# $ pip3 install tqdm
from tqdm import tqdm
from time import sleep
for el in tqdm([1, 2, 3]):
sleep(0.2)
>>> from tqdm import tqdm
>>> for el in tqdm([1, 2, 3], desc='Processing'):
... pass
Processing: 100%|██████████████████| 3/3 [00:00<00:00, 11516.49it/s]
```
@ -2989,7 +2989,7 @@ pg.draw.ellipse(<Surf>, color, <Rect>)
### Basic Mario Brothers Example
```python
import collections, dataclasses, enum, io, pygame, urllib.request, itertools as it
import collections, dataclasses, enum, io, itertools as it, pygame as pg, urllib.request
from random import randint
P = collections.namedtuple('P', 'x y') # Position
@ -2998,11 +2998,11 @@ SIZE, MAX_SPEED = 50, P(5, 10) # Screen size, Speed limit
def main():
def get_screen():
pygame.init()
return pygame.display.set_mode(2 * [SIZE*16])
pg.init()
return pg.display.set_mode(2 * [SIZE*16])
def get_images():
url = 'https://gto76.github.io/python-cheatsheet/web/mario_bros.png'
img = pygame.image.load(io.BytesIO(urllib.request.urlopen(url).read()))
img = pg.image.load(io.BytesIO(urllib.request.urlopen(url).read()))
return [img.subsurface(get_rect(x, 0)) for x in range(img.get_width() // 16)]
def get_mario():
Mario = dataclasses.make_dataclass('Mario', 'rect spd facing_left frame_cycle'.split())
@ -3012,14 +3012,14 @@ def main():
[(randint(1, SIZE-2), randint(2, SIZE-2)) for _ in range(SIZE**2 // 10)]
return [get_rect(*p) for p in positions]
def get_rect(x, y):
return pygame.Rect(x*16, y*16, 16, 16)
return pg.Rect(x*16, y*16, 16, 16)
run(get_screen(), get_images(), get_mario(), get_tiles())
def run(screen, images, mario, tiles):
clock = pygame.time.Clock()
while all(event.type != pygame.QUIT for event in pygame.event.get()):
keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
pressed = {keys.get(i) for i, on in enumerate(pygame.key.get_pressed()) if on}
clock = pg.time.Clock()
while all(event.type != pg.QUIT for event in pg.event.get()):
keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}
pressed = {keys.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}
update_speed(mario, tiles, pressed)
update_position(mario, tiles)
draw(screen, images, mario, tiles, pressed)
@ -3058,7 +3058,7 @@ def draw(screen, images, mario, tiles, pressed):
screen.blit(images[get_frame_index() + mario.facing_left * 9], mario.rect)
for rect in tiles:
screen.blit(images[18 if {*rect.topleft} & {0, (SIZE-1)*16} else 19], rect)
pygame.display.flip()
pg.display.flip()
if __name__ == '__main__':
main()

28
index.html

@ -2037,10 +2037,10 @@ curses.wrapper(main)
<p><br></p>
<div><h1 id="libraries">Libraries</h1><div><h2 id="progressbar"><a href="#progressbar" name="progressbar">#</a>Progress Bar</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install tqdm</span>
<span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm
<span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep
<span class="hljs-keyword">for</span> el <span class="hljs-keyword">inn class="p"></span> tqdm([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]):
sleep(<span class="hljs-number">0.2</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> tqdm([<span class="hljs-number">1</span>, pan class="p"><span class="hljs-number">2</span>, <span class="hljs-number">3</span>], desc=<span class="hljs-string">'Processing'</span>):
<span class="hljs-meta">... </span> <span class="hljs-keyword">pass</span>
Processing: 100%|██████████████████| 3/3 [00:00&lt;00:00, 11516.49it/s]
</code></pre></div></div>
@ -2550,7 +2550,7 @@ pg.draw.ellipse(&lt;Surf&gt;, color, &lt;Rect&gt;)
&lt;Sound&gt;.play() <span class="hljs-comment"># Starts playing the sound.</span>
</code></pre></div>
<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, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it
<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, itertools <span class="hljs-keyword">as</span> it, pygame <span class="hljs-keyword">as</span> pg, urllib.request
<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
@ -2559,11 +2559,11 @@ SIZE, MAX_SPEED = <span class="hljs-number">50</span>, P(<span class="hljs-numbe
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>
pygame.init()
<span class="hljs-keyword">return</span> pygame.display.set_mode(<span class="hljs-number">2</span> * [SIZE*<span class="hljs-number">16</span>])
pg.init()
<span class="hljs-keyword">return</span> pg.display.set_mode(<span class="hljs-number">2</span> * [SIZE*<span class="hljs-number">16</span>])
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_images</span><span class="hljs-params">()</span>:</span>
url = <span class="hljs-string">'https://gto76.github.io/python-cheatsheet/web/mario_bros.png'</span>
img = pygame.image.load(io.BytesIO(urllib.request.urlopen(url).read()))
img = pg.image.load(io.BytesIO(urllib.request.urlopen(url).read()))
<span class="hljs-keyword">return</span> [img.subsurface(get_rect(x, <span class="hljs-number">0</span>)) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(img.get_width() // <span class="hljs-number">16</span>)]
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_mario</span><span class="hljs-params">()</span>:</span>
Mario = dataclasses.make_dataclass(<span class="hljs-string">'Mario'</span>, <span class="hljs-string">'rect spd facing_left frame_cycle'</span>.split())
@ -2573,14 +2573,14 @@ SIZE, MAX_SPEED = <span class="hljs-number">50</span>, P(<span class="hljs-numbe
[(randint(<span class="hljs-number">1</span>, SIZE<span class="hljs-number">-2</span>), randint(<span class="hljs-number">2</span>, SIZE<span class="hljs-number">-2</span>)) <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(SIZE**<span class="hljs-number">2</span> // <span class="hljs-number">10</span>)]
<span class="hljs-keyword">return</span> [get_rect(*p) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> positions]
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_rect</span><span class="hljs-params">(x, y)</span>:</span>
<span class="hljs-keyword">return</span> pygame.Rect(x*<span class="hljs-number">16</span>, y*<span class="hljs-number">16</span>, <span class="hljs-number">16</span>, <span class="hljs-number">16</span>)
<span class="hljs-keyword">return</span> pg.Rect(x*<span class="hljs-number">16</span>, y*<span class="hljs-number">16</span>, <span class="hljs-number">16</span>, <span class="hljs-number">16</span>)
run(get_screen(), get_images(), get_mario(), get_tiles())
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span><span class="hljs-params">(screen, images, mario, tiles)</span>:</span>
clock = pygame.time.Clock()
<span class="hljs-keyword">while</span> all(event.type != pygame.QUIT <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pygame.event.get()):
keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
pressed = {keys.get(i) <span class="hljs-keyword">for</span> i, on <span class="hljs-keyword">in</span> enumerate(pygame.key.get_pressed()) <span class="hljs-keyword">if</span> on}
clock = pg.time.Clock()
<span class="hljs-keyword">while</span> all(event.type != pg.QUIT <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pg.event.get()):
keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}
pressed = {keys.get(i) <span class="hljs-keyword">for</span> i, on <span class="hljs-keyword">in</span> enumerate(pg.key.get_pressed()) <span class="hljs-keyword">if</span> on}
update_speed(mario, tiles, pressed)
update_position(mario, tiles)
draw(screen, images, mario, tiles, pressed)
@ -2619,7 +2619,7 @@ SIZE, MAX_SPEED = <span class="hljs-number">50</span>, P(<span class="hljs-numbe
screen.blit(images[get_frame_index() + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)
<span class="hljs-keyword">for</span> rect <span class="hljs-keyword">in</span> tiles:
screen.blit(images[<span class="hljs-number">18</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">19</span>], rect)
pygame.display.flip()
pg.display.flip()
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
main()

9
parse.js

@ -75,6 +75,14 @@ const INDEX =
'<li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>\n' +
'<li><strong>Searching <code class="python hljs"><span class="hljs-string">\'#&lt;title&gt;\'</span></code> will limit the search to the titles.</strong></li>\n';
const PROGRESS_BAR =
'<span class="hljs-comment"># $ pip3 install tqdm</span>\n' +
'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm\n' +
'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> tqdm([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], desc=<span class="hljs-string">\'Processing\'</span>):\n' +
'<span class="hljs-meta">... </span> <span class="hljs-keyword">pass</span>\n' +
'Processing: 100%|██████████████████| 3/3 [00:00&lt;00:00, 11516.49it/s]\n';
const DIAGRAM_1_A =
'+------------------+------------+------------+------------+\n' +
'| | Sequence | Collection | Iterable |\n' +
@ -504,6 +512,7 @@ function fixHighlights() {
$(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
$(`code:contains(pip3 install pyinstaller)`).html(PYINSTALLER);
$(`ul:contains(Only available in)`).html(INDEX);
$(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
}
function preventPageBreaks() {

Loading…
Cancel
Save