Skip to content

Commit 5c77a7d

Browse files
committed
Pygame
1 parent d332985 commit 5c77a7d

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,13 +2917,14 @@ Pygame
29172917
import collections, dataclasses, enum, io, math, pygame, urllib.request, itertools as it
29182918
from random import randint
29192919

2920-
P = collections.namedtuple('P', 'x y')
2921-
D = enum.Enum('D', 'n e s w')
2922-
SIZE, MAX_SPEED = 25, P(5, 10)
2920+
P = collections.namedtuple('P', 'x y') # Position
2921+
D = enum.Enum('D', 'n e s w') # Direction
2922+
SIZE, MAX_SPEED = 25, P(5, 10) # Screen size, Mario speed
29232923

29242924
def main():
2925-
def get_rect(x, y):
2926-
return pygame.Rect(x*16, y*16, 16, 16)
2925+
def get_screen():
2926+
pygame.init()
2927+
return pygame.display.set_mode(2 * [SIZE*16])
29272928
def get_images():
29282929
url = 'https://gto76.github.io/python-cheatsheet/web/mario_bros.png'
29292930
img = pygame.image.load(io.BytesIO(urllib.request.urlopen(url).read()))
@@ -2935,28 +2936,25 @@ def main():
29352936
positions = [p for p in it.product(range(SIZE), repeat=2) if {*p} & {0, SIZE-1}] + \
29362937
[(randint(1, SIZE-2), randint(2, SIZE-2)) for _ in range(SIZE**2 // 10)]
29372938
return [get_rect(*p) for p in positions]
2938-
def get_screen():
2939-
pygame.init()
2940-
return pygame.display.set_mode(2 * [SIZE*16])
2941-
run(get_images(), get_mario(), get_tiles(), get_screen())
2939+
def get_rect(x, y):
2940+
return pygame.Rect(x*16, y*16, 16, 16)
2941+
run(get_screen(), get_images(), get_mario(), get_tiles())
29422942

2943-
def run(images, mario, tiles, screen):
2943+
def run(screen, images, mario, tiles):
29442944
while all(event.type != pygame.QUIT for event in pygame.event.get()):
29452945
keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
29462946
pressed = {keys.get(i, None) for i, on in enumerate(pygame.key.get_pressed()) if on}
29472947
update_speed(mario, tiles, pressed)
29482948
update_position(mario, tiles)
2949-
draw(mario, tiles, screen, pressed, images)
2949+
draw(screen, images, mario, tiles, pressed)
29502950
pygame.time.wait(28)
29512951

29522952
def update_speed(mario, tiles, pressed):
2953-
bounds = get_boundaries(mario.rect, tiles)
29542953
x, y = mario.spd
29552954
x += 2 * ((D.e in pressed) - (D.w in pressed))
29562955
x = math.copysign(abs(x) - 1, x) if x else 0
2957-
y += 1 if D.s not in bounds else (-10 if D.n in pressed else 0)
2958-
speed = stop_on_collision(P(x, y), bounds)
2959-
mario.spd = P(*[max(-thresh, min(thresh, s)) for thresh, s in zip(MAX_SPEED, speed)])
2956+
y += 1 if D.s not in get_boundaries(mario.rect, tiles) else (-10 if D.n in pressed else 0)
2957+
mario.spd = P(*[max(-thresh, min(thresh, s)) for thresh, s in zip(MAX_SPEED, P(x, y))])
29602958

29612959
def update_position(mario, tiles):
29622960
old_p, delta = mario.rect.topleft, P(0, 0)
@@ -2974,7 +2972,7 @@ def stop_on_collision(spd, bounds):
29742972
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,
29752973
y=0 if (D.n in bounds and spd.y < 0) or (D.s in bounds and spd.y > 0) else spd.y)
29762974

2977-
def draw(mario, tiles, screen, pressed, images):
2975+
def draw(screen, images, mario, tiles, pressed):
29782976
def get_frame_index():
29792977
if D.s not in get_boundaries(mario.rect, tiles):
29802978
return 4

index.html

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,13 +2478,14 @@
24782478
<div><h2 id="pygame"><a href="#pygame" name="pygame">#</a>Pygame</h2><div><h3 id="example-3">Example</h3><div><h4 id="runsasimplesupermariogame">Runs a simple Super Mario game:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> collections, dataclasses, enum, io, math, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it
24792479
<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
24802480

2481-
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>)
2482-
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>)
2483-
SIZE, MAX_SPEED = <span class="hljs-number">25</span>, P(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>)
2481+
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
2482+
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
2483+
SIZE, MAX_SPEED = <span class="hljs-number">25</span>, P(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>) <span class="hljs-comment"># Screen size, Mario speed</span>
24842484

24852485
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
2486-
<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>
2487-
<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>)
2486+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>
2487+
pygame.init()
2488+
<span class="hljs-keyword">return</span> pygame.display.set_mode(<span class="hljs-number">2</span> * [SIZE*<span class="hljs-number">16</span>])
24882489
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_images</span><span class="hljs-params">()</span>:</span>
24892490
url = <span class="hljs-string">'https://gto76.github.io/python-cheatsheet/web/mario_bros.png'</span>
24902491
img = pygame.image.load(io.BytesIO(urllib.request.urlopen(url).read()))
@@ -2496,28 +2497,25 @@
24962497
positions = [p <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> it.product(range(SIZE), repeat=<span class="hljs-number">2</span>) <span class="hljs-keyword">if</span> {*p} &amp; {<span class="hljs-number">0</span>, SIZE<span class="hljs-number">-1</span>}] + \
24972498
[(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>)]
24982499
<span class="hljs-keyword">return</span> [get_rect(*p) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> positions]
2499-
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>
2500-
pygame.init()
2501-
<span class="hljs-keyword">return</span> pygame.display.set_mode(<span class="hljs-number">2</span> * [SIZE*<span class="hljs-number">16</span>])
2502-
run(get_images(), get_mario(), get_tiles(), get_screen())
2500+
<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>
2501+
<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>)
2502+
run(get_screen(), get_images(), get_mario(), get_tiles())
25032503

2504-
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span><span class="hljs-params">(images, mario, tiles, screen)</span>:</span>
2504+
<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>
25052505
<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()):
25062506
keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
25072507
pressed = {keys.get(i, <span class="hljs-keyword">None</span>) <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}
25082508
update_speed(mario, tiles, pressed)
25092509
update_position(mario, tiles)
2510-
draw(mario, tiles, screen, pressed, images)
2510+
draw(screen, images, mario, tiles, pressed)
25112511
pygame.time.wait(<span class="hljs-number">28</span>)
25122512

25132513
<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>
2514-
bounds = get_boundaries(mario.rect, tiles)
25152514
x, y = mario.spd
25162515
x += <span class="hljs-number">2</span> * ((D.e <span class="hljs-keyword">in</span> pressed) - (D.w <span class="hljs-keyword">in</span> pressed))
25172516
x = math.copysign(abs(x) - <span class="hljs-number">1</span>, x) <span class="hljs-keyword">if</span> x <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>
2518-
y += <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">else</span> (<span class="hljs-number">-10</span> <span class="hljs-keyword">if</span> D.n <span class="hljs-keyword">in</span> pressed <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>)
2519-
speed = stop_on_collision(P(x, y), bounds)
2520-
mario.spd = P(*[max(-thresh, min(thresh, s)) <span class="hljs-keyword">for</span> thresh, s <span class="hljs-keyword">in</span> zip(MAX_SPEED, speed)])
2517+
y += <span class="hljs-number">1</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">else</span> (<span class="hljs-number">-10</span> <span class="hljs-keyword">if</span> D.n <span class="hljs-keyword">in</span> pressed <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>)
2518+
mario.spd = P(*[max(-thresh, min(thresh, s)) <span class="hljs-keyword">for</span> thresh, s <span class="hljs-keyword">in</span> zip(MAX_SPEED, P(x, y))])
25212519

25222520
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_position</span><span class="hljs-params">(mario, tiles)</span>:</span>
25232521
old_p, delta = mario.rect.topleft, P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
@@ -2535,7 +2533,7 @@
25352533
<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,
25362534
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)
25372535

2538-
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(mario, tiles, screen, pressed, images)</span>:</span>
2536+
<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>
25392537
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_frame_index</span><span class="hljs-params">()</span>:</span>
25402538
<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):
25412539
<span class="hljs-keyword">return</span> <span class="hljs-number">4</span>

0 commit comments

Comments
 (0)