Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f39a9bd

Browse files
committed
Pygame
1 parent c88b949 commit f39a9bd

File tree

2 files changed

+68
-68
lines changed

2 files changed

+68
-68
lines changed

README.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,10 +2919,10 @@ Pygame
29192919
import pygame as pg
29202920
pg.init()
29212921
screen = pg.display.set_mode((500, 500))
2922-
rect = pg.Rect(235, 235, 30, 30)
2922+
rect = pg.Rect(240, 240, 20, 20)
29232923
while all(event.type != pg.QUIT for event in pg.event.get()):
2924-
keys = {pg.K_UP: (0, -3), pg.K_RIGHT: (3, 0), pg.K_DOWN: (0, 3), pg.K_LEFT: (-3, 0)}
2925-
for delta in {keys.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}:
2924+
deltas = {pg.K_UP: (0, -3), pg.K_RIGHT: (3, 0), pg.K_DOWN: (0, 3), pg.K_LEFT: (-3, 0)}
2925+
for delta in {deltas.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}:
29262926
rect = rect.move(delta) if delta else rect
29272927
screen.fill((0, 0, 0))
29282928
pg.draw.rect(screen, (255, 255, 255), rect)
@@ -2932,69 +2932,69 @@ while all(event.type != pg.QUIT for event in pg.event.get()):
29322932
### Rect
29332933
**Object for storing rectangular coordinates.**
29342934
```python
2935-
<Rect> = pg.Rect(topleft_x, topleft_y, width, height)
2936-
<tuple> = <Rect>.topleft/topright/bottomright/bottomleft/center
2937-
<int> = <Rect>.x/y/centerx/centery
2938-
<Rect> = <Rect>.move(<tuple>) # Or: <Rect>.move(<int>, <int>)
2935+
<Rect> = pg.Rect(topleft_x, topleft_y, width, height)
2936+
<int> = <Rect>.x/y/centerx/centery
2937+
<tup.> = <Rect>.topleft/topright/bottomright/bottomleft/center
2938+
<Rect> = <Rect>.move(<tuple>)
29392939
```
29402940

29412941
```python
2942-
<bool> = <Rect>.collidepoint(<tuple>) # Or: <Rect>.collidepoint(<int>, <int>)
2943-
<bool> = <Rect>.colliderect(<Rect>)
2944-
index = <Rect>.collidelist(<list_of_Rect>) # Returns index of first coliding Rect or -1.
2945-
indices = <Rect>.collidelistall(<list_of_Rect>) # Returns indices of all colinding Rects.
2942+
<bool> = <Rect>.collidepoint(<tuple>) # Tests if a point is inside a rectangle.
2943+
<bool> = <Rect>.colliderect(<Rect>) # Tests if two rectangles overlap.
2944+
<int> = <Rect>.collidelist(<list_of_Rect>) # Returns index of first colliding Rect or -1.
2945+
<list> = <Rect>.collidelistall(<list_of_Rect>) # Returns indices of all colliding Rects.
29462946
```
29472947

29482948
### Surface
29492949
**Object for representing images.**
29502950
```python
2951-
<Surface> = pg.display.set_mode((width, height)) # Retruns the display surface.
2952-
<Surface> = pg.Surface((width, height)) # Creates a new surface.
2953-
<Surface> = pg.image.load('<path>').convert() # Loads an image.
2951+
<Surf> = pg.display.set_mode((width, height)) # Returns the display surface.
2952+
<Surf> = pg.Surface((width, height)) # Creates a new surface.
2953+
<Surf> = pg.image.load('<path>').convert() # Loads an image.
29542954
```
29552955

29562956
```python
2957-
<Surface>.set_at((x, y), <color>) # Updates pixel.
2958-
<Surface>.fill(<color>) # Fills the whole surface.
2959-
<Surface>.blit(<Surface>, (x, y)) # Draws passed surface to the surface.
2960-
<Surface> = <Surface>.subsurface(<Rect>) # Returns subsurface.
2957+
<Surf>.set_at((x, y), color) # Updates pixel.
2958+
<Surf>.fill(color) # Fills the whole surface.
2959+
<Surf>.blit(<Surface>, (x, y)) # Draws passed surface to the surface.
2960+
<Surf> = <Surf>.subsurface(<Rect>) # Returns subsurface.
29612961
```
29622962

29632963
```python
2964-
<Surface> = pg.transform.flip(<Surface>, xbool, ybool)
2965-
<Surface> = pg.transform.rotate(<Surface>, angle)
2966-
<Surface> = pg.transform.scale(<Surface>, (width, height))
2964+
<Surf> = pg.transform.flip(<Surf>, xbool, ybool)
2965+
<Surf> = pg.transform.rotate(<Surf>, degrees)
2966+
<Surf> = pg.transform.scale(<Surf>, (width, height))
29672967
```
29682968

29692969
```python
2970-
pg.draw.line(<Surface>, color, start_pos, end_pos, width)
2971-
pg.draw.arc(<Surface>, color, <Rect>, start_angle, stop_angle)
2972-
pg.draw.rect(<Surface>, color, <Rect>)
2973-
pg.draw.polygon(<Surface>, color, points)
2974-
pg.draw.ellipse(<Surface>, color, <Rect>)
2970+
pg.draw.line(<Surf>, color, start_pos, end_pos, width)
2971+
pg.draw.arc(<Surf>, color, <Rect>, start_radians, stop_radians)
2972+
pg.draw.rect(<Surf>, color, <Rect>)
2973+
pg.draw.polygon(<Surf>, color, points)
2974+
pg.draw.ellipse(<Surf>, color, <Rect>)
29752975
```
29762976

29772977
### Font
29782978
```python
2979-
<Font> = pg.font.SysFont(name, size, bold=False, italic=False)
2980-
<Font> = pg.font.Font('<path>', size)
2981-
<Surface> = <Font>.render(text, antialias, color, background=None)
2979+
<Font> = pg.font.SysFont(name, size, bold=False, italic=False)
2980+
<Font> = pg.font.Font('<path>', size)
2981+
<Surf> = <Font>.render(text, antialias, color, background=None)
29822982
```
29832983

29842984
### Sound
29852985
```
2986-
<Sound> = pg.mixer.Sound('<path>') # Loads a sound file.
2987-
<Sound>.play() # Starts playing sound.
2986+
<Sound> = pg.mixer.Sound('<path>') # Loads a sound file.
2987+
<Sound>.play() # Starts playing the sound.
29882988
```
29892989

29902990
### Basic Mario Brothers Example
29912991
```python
29922992
import collections, dataclasses, enum, io, math, pygame, urllib.request, itertools as it
29932993
from random import randint
29942994

2995-
P = collections.namedtuple('P', 'x y') # Position
2996-
D = enum.Enum('D', 'n e s w') # Direction
2997-
SIZE, MAX_SPEED = 50, P(5, 10) # Screen size, Speed limit
2995+
P = collections.namedtuple('P', 'x y') # Position
2996+
D = enum.Enum('D', 'n e s w') # Direction
2997+
SIZE, MAX_SPEED = 50, P(5, 10) # Screen size, Speed limit
29982998

29992999
def main():
30003000
def get_screen():

index.html

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,65 +2482,65 @@
24822482
<span class="hljs-keyword">import</span> pygame <span class="hljs-keyword">as</span> pg
24832483
pg.init()
24842484
screen = pg.display.set_mode((<span class="hljs-number">500</span>, <span class="hljs-number">500</span>))
2485-
rect = pg.Rect(<span class="hljs-number">235</span>, <span class="hljs-number">235</span>, <span class="hljs-number">30</span>, <span class="hljs-number">30</span>)
2485+
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>)
24862486
<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()):
2487-
keys = {pg.K_UP: (<span class="hljs-number">0</span>, <span class="hljs-number">-3</span>), pg.K_RIGHT: (<span class="hljs-number">3</span>, <span class="hljs-number">0</span>), pg.K_DOWN: (<span class="hljs-number">0</span>, <span class="hljs-number">3</span>), pg.K_LEFT: (<span class="hljs-number">-3</span>, <span class="hljs-number">0</span>)}
2488-
<span class="hljs-keyword">for</span> delta <span class="hljs-keyword">in</span> {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}:
2487+
deltas = {pg.K_UP: (<span class="hljs-number">0</span>, <span class="hljs-number">-3</span>), pg.K_RIGHT: (<span class="hljs-number">3</span>, <span class="hljs-number">0</span>), pg.K_DOWN: (<span class="hljs-number">0</span>, <span class="hljs-number">3</span>), pg.K_LEFT: (<span class="hljs-number">-3</span>, <span class="hljs-number">0</span>)}
2488+
<span class="hljs-keyword">for</span> delta <span class="hljs-keyword">in</span> {deltas.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}:
24892489
rect = rect.move(delta) <span class="hljs-keyword">if</span> delta <span class="hljs-keyword">else</span> rect
24902490
screen.fill((<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
24912491
pg.draw.rect(screen, (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>), rect)
24922492
pg.display.flip()
24932493
</code></pre></div></div>
24942494

24952495

2496-
<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)
2497-
&lt;tuple&gt; = &lt;Rect&gt;.topleft/topright/bottomright/bottomleft/center
2498-
&lt;int&gt; = &lt;Rect&gt;.x/y/centerx/centery
2499-
&lt;Rect&gt; = &lt;Rect&gt;.move(&lt;tuple&gt;) <span class="hljs-comment"># Or: &lt;Rect&gt;.move(&lt;int&gt;, &lt;int&gt;)</span>
2496+
<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)
2497+
&lt;int&gt; = &lt;Rect&gt;.x/y/centerx/centery
2498+
&lt;tup.&gt; = &lt;Rect&gt;.topleft/topright/bottomright/bottomleft/center
2499+
&lt;Rect&gt; = &lt;Rect&gt;.move(&lt;tuple&gt;)
25002500
</code></pre></div>
25012501

25022502

2503-
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Rect&gt;.collidepoint(&lt;tuple&gt;) <span class="hljs-comment"># Or: &lt;Rect&gt;.collidepoint(&lt;int&gt;, &lt;int&gt;)</span>
2504-
&lt;bool&gt; = &lt;Rect&gt;.colliderect(&lt;Rect&gt;)
2505-
index = &lt;Rect&gt;.collidelist(&lt;list_of_Rect&gt;) <span class="hljs-comment"># Returns index of first coliding Rect or -1.</span>
2506-
indices = &lt;Rect&gt;.collidelistall(&lt;list_of_Rect&gt;) <span class="hljs-comment"># Returns indices of all colinding Rects.</span>
2503+
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Rect&gt;.collidepoint(&lt;tuple&gt;) <span class="hljs-comment"># Tests if a point is inside a rectangle.</span>
2504+
&lt;bool&gt; = &lt;Rect&gt;.colliderect(&lt;Rect&gt;) <span class="hljs-comment"># Tests if two rectangles overlap.</span>
2505+
&lt;int&gt; = &lt;Rect&gt;.collidelist(&lt;list_of_Rect&gt;) <span class="hljs-comment"># Returns index of first colliding Rect or -1.</span>
2506+
&lt;list&gt; = &lt;Rect&gt;.collidelistall(&lt;list_of_Rect&gt;) <span class="hljs-comment"># Returns indices of all colliding Rects.</span>
25072507
</code></pre>
2508-
<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>
2509-
&lt;Surface&gt; = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span>
2510-
&lt;Surface&gt; = pg.image.load(<span class="hljs-string">'&lt;path&gt;'</span>).convert() <span class="hljs-comment"># Loads an image.</span>
2508+
<div><h3 id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><code class="python language-python hljs">&lt;Surf&gt; = pg.display.set_mode((width, height)) <span class="hljs-comment"># Returns the display surface.</span>
2509+
&lt;Surf&gt; = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span>
2510+
&lt;Surf&gt; = pg.image.load(<span class="hljs-string">'&lt;path&gt;'</span>).convert() <span class="hljs-comment"># Loads an image.</span>
25112511
</code></pre></div>
25122512

25132513

2514-
<pre><code class="python language-python hljs">&lt;Surface&gt;.set_at((x, y), &lt;color&gt;) <span class="hljs-comment"># Updates pixel.</span>
2515-
&lt;Surface&gt;.fill(&lt;color&gt;) <span class="hljs-comment"># Fills the whole surface.</span>
2516-
&lt;Surface&gt;.blit(&lt;Surface&gt;, (x, y)) <span class="hljs-comment"># Draws passed surface to the surface.</span>
2517-
&lt;Surface&gt; = &lt;Surface&gt;.subsurface(&lt;Rect&gt;) <span class="hljs-comment"># Returns subsurface.</span>
2514+
<pre><code class="python language-python hljs">&lt;Surf&gt;.set_at((x, y), color) <span class="hljs-comment"># Updates pixel.</span>
2515+
&lt;Surf&gt;.fill(color) <span class="hljs-comment"># Fills the whole surface.</span>
2516+
&lt;Surf&gt;.blit(&lt;Surface&gt;, (x, y)) <span class="hljs-comment"># Draws passed surface to the surface.</span>
2517+
&lt;Surf&gt; = &lt;Surf&gt;.subsurface(&lt;Rect&gt;) <span class="hljs-comment"># Returns subsurface.</span>
25182518
</code></pre>
2519-
<pre><code class="python language-python hljs">&lt;Surface&gt; = pg.transform.flip(&lt;Surface&gt;, xbool, ybool)
2520-
&lt;Surface&gt; = pg.transform.rotate(&lt;Surface&gt;, angle)
2521-
&lt;Surface&gt; = pg.transform.scale(&lt;Surface&gt;, (width, height))
2519+
<pre><code class="python language-python hljs">&lt;Surf&gt; = pg.transform.flip(&lt;Surf&gt;, xbool, ybool)
2520+
&lt;Surf&gt; = pg.transform.rotate(&lt;Surf&gt;, degrees)
2521+
&lt;Surf&gt; = pg.transform.scale(&lt;Surf&gt;, (width, height))
25222522
</code></pre>
2523-
<pre><code class="python language-python hljs">pg.draw.line(&lt;Surface&gt;, color, start_pos, end_pos, width)
2524-
pg.draw.arc(&lt;Surface&gt;, color, &lt;Rect&gt;, start_angle, stop_angle)
2525-
pg.draw.rect(&lt;Surface&gt;, color, &lt;Rect&gt;)
2526-
pg.draw.polygon(&lt;Surface&gt;, color, points)
2527-
pg.draw.ellipse(&lt;Surface&gt;, color, &lt;Rect&gt;)
2523+
<pre><code class="python language-python hljs">pg.draw.line(&lt;Surf&gt;, color, start_pos, end_pos, width)
2524+
pg.draw.arc(&lt;Surf&gt;, color, &lt;Rect&gt;, start_radians, stop_radians)
2525+
pg.draw.rect(&lt;Surf&gt;, color, &lt;Rect&gt;)
2526+
pg.draw.polygon(&lt;Surf&gt;, color, points)
2527+
pg.draw.ellipse(&lt;Surf&gt;, color, &lt;Rect&gt;)
25282528
</code></pre>
2529-
<div><h3 id="font">Font</h3><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>)
2530-
&lt;Font&gt; = pg.font.Font(<span class="hljs-string">'&lt;path&gt;'</span>, size)
2531-
&lt;Surface&gt; = &lt;Font&gt;.render(text, antialias, color, background=<span class="hljs-keyword">None</span>)
2529+
<div><h3 id="font">Font</h3><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>)
2530+
&lt;Font&gt; = pg.font.Font(<span class="hljs-string">'&lt;path&gt;'</span>, size)
2531+
&lt;Surf&gt; = &lt;Font&gt;.render(text, antialias, color, background=<span class="hljs-keyword">None</span>)
25322532
</code></pre></div>
25332533

2534-
<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>
2535-
&lt;Sound&gt;.play() <span class="hljs-comment"># Starts playing sound.</span>
2534+
<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>
2535+
&lt;Sound&gt;.play() <span class="hljs-comment"># Starts playing the sound.</span>
25362536
</code></pre></div>
25372537

25382538
<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
25392539
<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
25402540

2541-
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
2542-
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
2543-
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>
2541+
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
2542+
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
2543+
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>
25442544

25452545
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
25462546
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>

0 commit comments

Comments
 (0)