@ -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 > tim e < 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" > > > > < / span > < span class = "hljs-keyword" > from< / span > tqdm < span class = "hljs-keyword" > import< / span > tqdm
< span class = "hljs-meta" > > > > < / span > < span class = "hljs-keyword" > for < / span > el < span class = "hljs-keyword" > in< / span > tqd m([< span class = "hljs-numbe r" > 1< / span > , pan class="p"><span class = "hljs-number" > 2< / span > , < span class = "hljs-numb er" > 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< 00:00, 11516.49it/s]
< / code > < / pre > < / div > < / div >
@ -2550,7 +2550,7 @@ pg.draw.ellipse(<Surf>, color, <Rect>)
< Sound> .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 >
py game .init()
< span class = "hljs-keyword" > return< / span > py game .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 = py game .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 > py game .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 = py game .time.Clock()
< span class = "hljs-keyword" > while< / span > all(event.type != py game .QUIT < span class = "hljs-keyword" > for< / span > event < span class = "hljs-keyword" > in< / span > py game .event.get()):
keys = {py game .K_UP: D.n, py game .K_RIGHT: D.e, py game .K_DOWN: D.s, py game .K_LEFT: D.w}
pressed = {keys.get(i) < span class = "hljs-keyword" > for< / span > i, on < span class = "hljs-keyword" > in< / span > enumerate(py game .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} & {< 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)
py game .display.flip()
pg.display.flip()
< span class = "hljs-keyword" > if< / span > __name__ == < span class = "hljs-string" > '__main__'< / span > :
main()