Skip to content

Commit e6c56f2

Browse files
committed
PyGame
1 parent a3a023f commit e6c56f2

File tree

2 files changed

+59
-81
lines changed

2 files changed

+59
-81
lines changed

README.md

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,31 +2927,6 @@ while all(event.type != pg.QUIT for event in pg.event.get()):
29272927
pg.display.flip()
29282928
```
29292929

2930-
### Surface
2931-
**Object for representing images.**
2932-
```python
2933-
<Surface> = pg.display.set_mode((width, height)) # Retruns the display surface.
2934-
<Surface> = pg.Surface((width, height)) # Creates a new surface.
2935-
<Surface> = pg.image.load('<path>').convert() # Loads an image.
2936-
<Surface> = <Surface>.convert() # Converts to screen format.
2937-
<Surface> = <Surface>.convert_alpha() # Converts to screen format including alphas.
2938-
```
2939-
* **If no arguments are passed the new Surface will have the same pixel format as the display Surface. This is always the fastest format for blitting. It is a good idea to convert all Surfaces before they are blitted many times.**
2940-
2941-
```python
2942-
<Surface>.set_at((x, y), <color>) # Updates pixel.
2943-
<Surface>.fill(<color>) # Fills the whole surface.
2944-
<Surface>.blit(<Surface>, (x, y)/<Rect>) # Draws passed surface to the surface.
2945-
<Surface>.blit(<Surface>, (x, y)/<Rect>) # Draws one image onto another.
2946-
<Surface> = <Surface>.subsurface(<Rect>) # Returns subsurface.
2947-
```
2948-
2949-
```python
2950-
<Surface> = pg.transform.flip(<Surface>, xbool, ybool)
2951-
<Surface> = pg.transform.rotate(<Surface>, angle)
2952-
<Surface> = pg.transform.scale(<Surface>, (width, height))
2953-
```
2954-
29552930
### Rect
29562931
**Object for storing rectangular coordinates.**
29572932
```python
@@ -2978,7 +2953,28 @@ indices = <Rect>.collidelistall(<list_of_Rect>) # Returns indices of all colind
29782953
[(key, value), ...] = <Rect>.collidedictall(<dict_of_Rect>)
29792954
```
29802955

2981-
### Draw
2956+
### Surface
2957+
**Object for representing images.**
2958+
```python
2959+
<Surface> = pg.display.set_mode((width, height)) # Retruns the display surface.
2960+
<Surface> = pg.Surface((width, height)) # Creates a new surface.
2961+
<Surface> = pg.image.load('<path>').convert() # Loads an image.
2962+
```
2963+
2964+
```python
2965+
<Surface>.set_at((x, y), <color>) # Updates pixel.
2966+
<Surface>.fill(<color>) # Fills the whole surface.
2967+
<Surface>.blit(<Surface>, (x, y)/<Rect>) # Draws passed surface to the surface.
2968+
<Surface> = <Surface>.subsurface(<Rect>) # Returns subsurface.
2969+
```
2970+
2971+
```python
2972+
<Surface> = pg.transform.flip(<Surface>, xbool, ybool)
2973+
<Surface> = pg.transform.rotate(<Surface>, angle)
2974+
<Surface> = pg.transform.scale(<Surface>, (width, height))
2975+
```
2976+
2977+
#### Drawing:
29822978
```python
29832979
pg.draw.rect(<Surface>, color, <Rect>)
29842980
pg.draw.polygon(<Surface>, color, points)
@@ -2989,24 +2985,17 @@ pg.draw.line(<Surface>, color, start_pos, end_pos, width)
29892985
pg.draw.lines(<Surface>, color, points)
29902986
```
29912987

2992-
### Mixer
2988+
#### Fonts:
29932989
```python
2994-
pygame.mixer.init # initialize the mixer module
2995-
pygame.mixer.pre_init # preset the mixer init arguments
2996-
pygame.mixer.stop # stop playback of all sound channels
2997-
pygame.mixer.set_num_channels # set the total number of playback channels
2998-
pygame.mixer.set_reserved # reserve channels from being automatically used
2999-
pygame.mixer.find_channel # find an unused channel
3000-
pygame.mixer.Sound # Create a new Sound object from a file or buffer object
3001-
pygame.mixer.Channel # Create a Channel object for controlling playback
2990+
<Font> = pg.font.SysFont(name, size, bold=False, italic=False)
2991+
<Font> = pg.font.Font('<path>', size)
2992+
<Surface> = <Font>.render(text, antialias, color, background=None)
30022993
```
30032994

3004-
```python
3005-
pygame.mixer.music.load('test.wav')
3006-
pygame.mixer.music.play()
3007-
pygame.mixer.music.rewind()
3008-
pygame.mixer.music.stop()
3009-
pygame.mixer.music.set_volume(<float>)
2995+
### Sound
2996+
```
2997+
<Sound> = pg.mixer.Sound('<path>') # Loads a sound file.
2998+
<Sound>.play() # Starts playing sound.
30102999
```
30113000

30123001
### Basic Mario Brothers Example
@@ -3016,7 +3005,7 @@ from random import randint
30163005

30173006
P = collections.namedtuple('P', 'x y') # Position
30183007
D = enum.Enum('D', 'n e s w') # Direction
3019-
SIZE, MAX_SPEED = 25, P(5, 10) # Screen size, Speed limit
3008+
SIZE, MAX_SPEED = 50, P(5, 10) # Screen size, Speed limit
30203009

30213010
def main():
30223011
def get_screen():
@@ -3038,13 +3027,14 @@ def main():
30383027
run(get_screen(), get_images(), get_mario(), get_tiles())
30393028

30403029
def run(screen, images, mario, tiles):
3030+
clock = pygame.time.Clock()
30413031
while all(event.type != pygame.QUIT for event in pygame.event.get()):
30423032
keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
30433033
pressed = {keys.get(i) for i, on in enumerate(pygame.key.get_pressed()) if on}
30443034
update_speed(mario, tiles, pressed)
30453035
update_position(mario, tiles)
30463036
draw(screen, images, mario, tiles, pressed)
3047-
pygame.time.wait(28)
3037+
clock.tick(28)
30483038

30493039
def update_speed(mario, tiles, pressed):
30503040
x, y = mario.spd

index.html

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,27 +2491,6 @@
24912491
</code></pre></div></div>
24922492

24932493

2494-
<div><h3 id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><code class="python language-python hljs">&lt;Surface&gt; = pg.display.set_mode((width, height)) <span class="hljs-comment"># Retruns the display surface.</span>
2495-
&lt;Surface&gt; = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span>
2496-
&lt;Surface&gt; = pg.image.load(<span class="hljs-string">'&lt;path&gt;'</span>).convert() <span class="hljs-comment"># Loads an image.</span>
2497-
&lt;Surface&gt; = &lt;Surface&gt;.convert() <span class="hljs-comment"># Converts to screen format.</span>
2498-
&lt;Surface&gt; = &lt;Surface&gt;.convert_alpha() <span class="hljs-comment"># Converts to screen format including alphas.</span>
2499-
</code></pre></div>
2500-
2501-
2502-
<ul>
2503-
<li><strong>If no arguments are passed the new Surface will have the same pixel format as the display Surface. This is always the fastest format for blitting. It is a good idea to convert all Surfaces before they are blitted many times.</strong></li>
2504-
</ul>
2505-
<pre><code class="python language-python hljs">&lt;Surface&gt;.set_at((x, y), &lt;color&gt;) <span class="hljs-comment"># Updates pixel.</span>
2506-
&lt;Surface&gt;.fill(&lt;color&gt;) <span class="hljs-comment"># Fills the whole surface.</span>
2507-
&lt;Surface&gt;.blit(&lt;Surface&gt;, (x, y)/&lt;Rect&gt;) <span class="hljs-comment"># Draws passed surface to the surface. </span>
2508-
&lt;Surface&gt;.blit(&lt;Surface&gt;, (x, y)/&lt;Rect&gt;) <span class="hljs-comment"># Draws one image onto another.</span>
2509-
&lt;Surface&gt; = &lt;Surface&gt;.subsurface(&lt;Rect&gt;) <span class="hljs-comment"># Returns subsurface.</span>
2510-
</code></pre>
2511-
<pre><code class="python language-python hljs">&lt;Surface&gt; = pg.transform.flip(&lt;Surface&gt;, xbool, ybool)
2512-
&lt;Surface&gt; = pg.transform.rotate(&lt;Surface&gt;, angle)
2513-
&lt;Surface&gt; = pg.transform.scale(&lt;Surface&gt;, (width, height))
2514-
</code></pre>
25152494
<div><h3 id="rect">Rect</h3><p><strong>Object for storing rectangular coordinates.</strong></p><pre><code class="python language-python hljs">&lt;Rect&gt; = pg.Rect(topleft_x, topleft_y, width, height) <span class="hljs-comment"># x, y, w/width, h/height</span>
25162495
&lt;int&gt; = &lt;Rect&gt;.x/y/centerx/centery/bottom/left/right/top
25172496
&lt;tuple&gt; = &lt;Rect&gt;.topleft/center/topright/bottomright/bottomleft
@@ -2532,7 +2511,22 @@
25322511
(key, value) = &lt;Rect&gt;.collidedict(&lt;dict_of_Rect&gt;)
25332512
[(key, value), ...] = &lt;Rect&gt;.collidedictall(&lt;dict_of_Rect&gt;)
25342513
</code></pre>
2535-
<div><h3 id="draw">Draw</h3><pre><code class="python language-python hljs">pg.draw.rect(&lt;Surface&gt;, color, &lt;Rect&gt;)
2514+
<div><h3 id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><code class="python language-python hljs">&lt;Surface&gt; = pg.display.set_mode((width, height)) <span class="hljs-comment"># Retruns the display surface.</span>
2515+
&lt;Surface&gt; = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span>
2516+
&lt;Surface&gt; = pg.image.load(<span class="hljs-string">'&lt;path&gt;'</span>).convert() <span class="hljs-comment"># Loads an image.</span>
2517+
</code></pre></div>
2518+
2519+
2520+
<pre><code class="python language-python hljs">&lt;Surface&gt;.set_at((x, y), &lt;color&gt;) <span class="hljs-comment"># Updates pixel.</span>
2521+
&lt;Surface&gt;.fill(&lt;color&gt;) <span class="hljs-comment"># Fills the whole surface.</span>
2522+
&lt;Surface&gt;.blit(&lt;Surface&gt;, (x, y)/&lt;Rect&gt;) <span class="hljs-comment"># Draws passed surface to the surface.</span>
2523+
&lt;Surface&gt; = &lt;Surface&gt;.subsurface(&lt;Rect&gt;) <span class="hljs-comment"># Returns subsurface.</span>
2524+
</code></pre>
2525+
<pre><code class="python language-python hljs">&lt;Surface&gt; = pg.transform.flip(&lt;Surface&gt;, xbool, ybool)
2526+
&lt;Surface&gt; = pg.transform.rotate(&lt;Surface&gt;, angle)
2527+
&lt;Surface&gt; = pg.transform.scale(&lt;Surface&gt;, (width, height))
2528+
</code></pre>
2529+
<div><h4 id="drawing-1">Drawing:</h4><pre><code class="python language-python hljs">pg.draw.rect(&lt;Surface&gt;, color, &lt;Rect&gt;)
25362530
pg.draw.polygon(&lt;Surface&gt;, color, points)
25372531
pg.draw.circle(&lt;Surface&gt;, color, center, radius)
25382532
pg.draw.ellipse(&lt;Surface&gt;, color, &lt;Rect&gt;)
@@ -2541,28 +2535,21 @@
25412535
pg.draw.lines(&lt;Surface&gt;, color, points)
25422536
</code></pre></div>
25432537

2544-
<div><h3 id="mixer">Mixer</h3><pre><code class="python language-python hljs">pygame.mixer.init <span class="hljs-comment"># initialize the mixer module</span>
2545-
pygame.mixer.pre_init <span class="hljs-comment"># preset the mixer init arguments</span>
2546-
pygame.mixer.stop <span class="hljs-comment"># stop playback of all sound channels</span>
2547-
pygame.mixer.set_num_channels <span class="hljs-comment"># set the total number of playback channels</span>
2548-
pygame.mixer.set_reserved <span class="hljs-comment"># reserve channels from being automatically used</span>
2549-
pygame.mixer.find_channel <span class="hljs-comment"># find an unused channel</span>
2550-
pygame.mixer.Sound <span class="hljs-comment"># Create a new Sound object from a file or buffer object</span>
2551-
pygame.mixer.Channel <span class="hljs-comment"># Create a Channel object for controlling playback</span>
2538+
<div><h4 id="fonts">Fonts:</h4><pre><code class="python language-python hljs">&lt;Font&gt; = pg.font.SysFont(name, size, bold=<span class="hljs-keyword">False</span>, italic=<span class="hljs-keyword">False</span>)
2539+
&lt;Font&gt; = pg.font.Font(<span class="hljs-string">'&lt;path&gt;'</span>, size)
2540+
&lt;Surface&gt; = &lt;Font&gt;.render(text, antialias, color, background=<span class="hljs-keyword">None</span>)
2541+
</code></pre></div>
2542+
2543+
<div><h3 id="sound">Sound</h3><pre><code class="python hljs">&lt;Sound&gt; = pg.mixer.Sound(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Loads a sound file.</span>
2544+
&lt;Sound&gt;.play() <span class="hljs-comment"># Starts playing sound.</span>
25522545
</code></pre></div>
25532546

2554-
<pre><code class="python language-python hljs">pygame.mixer.music.load(<span class="hljs-string">'test.wav'</span>)
2555-
pygame.mixer.music.play()
2556-
pygame.mixer.music.rewind()
2557-
pygame.mixer.music.stop()
2558-
pygame.mixer.music.set_volume(&lt;float&gt;)
2559-
</code></pre>
25602547
<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, math, pygame, urllib.request, itertools <span class="hljs-keyword">as</span> it
25612548
<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
25622549

25632550
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
25642551
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
2565-
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, Speed limit</span>
2552+
SIZE, MAX_SPEED = <span class="hljs-number">50</span>, P(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>) <span class="hljs-comment"># Screen size, Speed limit</span>
25662553

25672554
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
25682555
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>
@@ -2584,13 +2571,14 @@
25842571
run(get_screen(), get_images(), get_mario(), get_tiles())
25852572

25862573
<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>
2574+
clock = pygame.time.Clock()
25872575
<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()):
25882576
keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
25892577
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}
25902578
update_speed(mario, tiles, pressed)
25912579
update_position(mario, tiles)
25922580
draw(screen, images, mario, tiles, pressed)
2593-
pygame.time.wait(<span class="hljs-number">28</span>)
2581+
clock.tick(<span class="hljs-number">28</span>)
25942582

25952583
<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>
25962584
x, y = mario.spd

0 commit comments

Comments
 (0)