Browse Source

Fixed Pygame examples

pull/144/head
Jure Šorn 1 year ago
parent
commit
405b8eca3a
3 changed files with 20 additions and 20 deletions
  1. 16
      README.md
  2. 16
      index.html
  3. 8
      parse.js

16
README.md

@ -2969,10 +2969,10 @@ import pygame as pg
pg.init()
screen = pg.display.set_mode((500, 500))
rect = pg.Rect(240, 240, 20, 20)
while all(event.type != pg.QUIT for event in pg.event.get()):
deltas = {pg.K_UP: (0, -1), pg.K_RIGHT: (1, 0), pg.K_DOWN: (0, 1), pg.K_LEFT: (-1, 0)}
for ch, is_pressed in enumerate(pg.key.get_pressed()):
rect = rect.move(deltas[ch]) if ch in deltas and is_pressed else rect
while not pg.event.get(pg.QUIT):
deltas = {pg.K_UP: (0, -20), pg.K_RIGHT: (20, 0), pg.K_DOWN: (0, 20), pg.K_LEFT: (-20, 0)}
for event in pg.event.get(pg.KEYDOWN):
rect.move_ip(deltas.get(event.key, (0, 0))
screen.fill((0, 0, 0))
pg.draw.rect(screen, (255, 255, 255), rect)
pg.display.flip()
@ -3065,14 +3065,14 @@ def main():
run(get_screen(), get_images(), get_mario(), get_tiles())
def run(screen, images, mario, tiles):
clock = pg.time.Clock()
while all(event.type != pg.QUIT for event in pg.event.get()):
clock, pressed = pg.time.Clock(), set()
while not pg.event.get(pg.QUIT) and clock.tick(28):
keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}
pressed = {keys.get(ch) for ch, is_prsd in enumerate(pg.key.get_pressed()) if is_prsd}
pressed |= {keys.get(event.key) for event in pg.event.get(pg.KEYDOWN)}
pressed -= {keys.get(event.key) for event in pg.event.get(pg.KEYUP)}
update_speed(mario, tiles, pressed)
update_position(mario, tiles)
draw(screen, images, mario, tiles, pressed)
clock.tick(28)
def update_speed(mario, tiles, pressed):
x, y = mario.spd

16
index.html

@ -2424,10 +2424,10 @@ simpleaudio.play_buffer(samples_b, <span class="hljs-number">1</span>, <span cla
pg.init()
screen = pg.display.set_mode((<span class="hljs-number">500</span>, <span class="hljs-number">500</span>))
rect = pg.Rect(<span class="hljs-number">240</span>, <span class="hljs-number">240</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>)
<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()):
deltas = {pg.K_UP: (<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), pg.K_RIGHT: (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), pg.K_DOWN: (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), pg.K_LEFT: (<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}
<span class="hljs-keyword">for</span> ch, is_pressed <span class="hljs-keyword">in</span> enumerate(pg.key.get_pressed()):
rect = rect.move(deltas[ch]) <span class="hljs-keyword">if</span> ch <span class="hljs-keyword">in</span> deltas <span class="hljs-keyword">and</span> is_pressed <span class="hljs-keyword">else</span> rect
<span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> pg.event.get(pg.QUIT):
deltas = {pg.K_UP: (<span class="hljs-number">0</span>, <span class="hljs-number">-20</span>), pg.K_RIGHT: (<span class="hljs-number">20</span>, <span class="hljs-number">0</span>), pg.K_DOWN: (<span class="hljs-number">0</span>, <span class="hljs-number">20</span>), pg.K_LEFT: (<span class="hljs-number">-20</span>, <span class="hljs-number">0</span>)}
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN):
rect.move_ip(deltas.get(event.key, (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
screen.fill((<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
pg.draw.rect(screen, (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>), rect)
pg.display.flip()
@ -2502,14 +2502,14 @@ W, H, MAX_S = <span class="hljs-number">50</span>, <span class="hljs-number">50<
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 = 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()):
clock, pressed = pg.time.Clock(), set()
<span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> pg.event.get(pg.QUIT) <span class="hljs-keyword">and</span> clock.tick(<span class="hljs-number">28</span>):
keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}
pressed = {keys.get(ch) <span class="hljs-keyword">for</span> ch, is_prsd <span class="hljs-keyword">in</span> enumerate(pg.key.get_pressed()) <span class="hljs-keyword">if</span> is_prsd}
pressed |= {keys.get(event.key) <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN)}
pressed -= {keys.get(event.key) <span class="hljs-keyword">for</span> event <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)
clock.tick(<span class="hljs-number">28</span>)
<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

8
parse.js

@ -201,14 +201,14 @@ const MARIO =
' run(get_screen(), get_images(), get_mario(), get_tiles())\n' +
'\n' +
'<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>\n' +
' clock = pg.time.Clock()\n' +
' <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()):\n' +
' clock, pressed = pg.time.Clock(), set()\n' +
' <span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> pg.event.get(pg.QUIT) <span class="hljs-keyword">and</span> clock.tick(<span class="hljs-number">28</span>):\n' +
' keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}\n' +
' pressed = {keys.get(ch) <span class="hljs-keyword">for</span> ch, is_prsd <span class="hljs-keyword">in</span> enumerate(pg.key.get_pressed()) <span class="hljs-keyword">if</span> is_prsd}\n' +
' pressed |= {keys.get(event.key) <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN)}\n' +
' pressed -= {keys.get(event.key) <span class="hljs-keyword">for</span> event <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' +
' clock.tick(<span class="hljs-number">28</span>)\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' +

Loading…
Cancel
Save