|
2482 | 2482 | <span class="hljs-keyword">import</span> pygame <span class="hljs-keyword">as</span> pg
|
2483 | 2483 | pg.init()
|
2484 | 2484 | 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>) |
2486 | 2486 | <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}: |
2489 | 2489 | rect = rect.move(delta) <span class="hljs-keyword">if</span> delta <span class="hljs-keyword">else</span> rect
|
2490 | 2490 | screen.fill((<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
|
2491 | 2491 | pg.draw.rect(screen, (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>), rect)
|
2492 | 2492 | pg.display.flip()
|
2493 | 2493 | </code></pre></div></div>
|
2494 | 2494 |
|
2495 | 2495 |
|
2496 |
| -<div><h3 id="rect">Rect</h3><p><strong>Object for storing rectangular coordinates.</strong></p><pre><code class="python language-python hljs"><Rect> = pg.Rect(topleft_x, topleft_y, width, height) |
2497 |
| -<tuple> = <Rect>.topleft/topright/bottomright/bottomleft/center |
2498 |
| -<int> = <Rect>.x/y/centerx/centery |
2499 |
| -<Rect> = <Rect>.move(<tuple>) <span class="hljs-comment"># Or: <Rect>.move(<int>, <int>)</span> |
| 2496 | +<div><h3 id="rect">Rect</h3><p><strong>Object for storing rectangular coordinates.</strong></p><pre><code class="python language-python hljs"><Rect> = pg.Rect(topleft_x, topleft_y, width, height) |
| 2497 | +<int> = <Rect>.x/y/centerx/centery |
| 2498 | +<tup.> = <Rect>.topleft/topright/bottomright/bottomleft/center |
| 2499 | +<Rect> = <Rect>.move(<tuple>) |
2500 | 2500 | </code></pre></div>
|
2501 | 2501 |
|
2502 | 2502 |
|
2503 |
| -<pre><code class="python language-python hljs"><bool> = <Rect>.collidepoint(<tuple>) <span class="hljs-comment"># Or: <Rect>.collidepoint(<int>, <int>)</span> |
2504 |
| -<bool> = <Rect>.colliderect(<Rect>) |
2505 |
| -index = <Rect>.collidelist(<list_of_Rect>) <span class="hljs-comment"># Returns index of first coliding Rect or -1.</span> |
2506 |
| -indices = <Rect>.collidelistall(<list_of_Rect>) <span class="hljs-comment"># Returns indices of all colinding Rects.</span> |
| 2503 | +<pre><code class="python language-python hljs"><bool> = <Rect>.collidepoint(<tuple>) <span class="hljs-comment"># Tests if a point is inside a rectangle.</span> |
| 2504 | +<bool> = <Rect>.colliderect(<Rect>) <span class="hljs-comment"># Tests if two rectangles overlap.</span> |
| 2505 | +<int> = <Rect>.collidelist(<list_of_Rect>) <span class="hljs-comment"># Returns index of first colliding Rect or -1.</span> |
| 2506 | +<list> = <Rect>.collidelistall(<list_of_Rect>) <span class="hljs-comment"># Returns indices of all colliding Rects.</span> |
2507 | 2507 | </code></pre>
|
2508 |
| -<div><h3 id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><code class="python language-python hljs"><Surface> = pg.display.set_mode((width, height)) <span class="hljs-comment"># Retruns the display surface.</span> |
2509 |
| -<Surface> = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span> |
2510 |
| -<Surface> = pg.image.load(<span class="hljs-string">'<path>'</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"><Surf> = pg.display.set_mode((width, height)) <span class="hljs-comment"># Returns the display surface.</span> |
| 2509 | +<Surf> = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span> |
| 2510 | +<Surf> = pg.image.load(<span class="hljs-string">'<path>'</span>).convert() <span class="hljs-comment"># Loads an image.</span> |
2511 | 2511 | </code></pre></div>
|
2512 | 2512 |
|
2513 | 2513 |
|
2514 |
| -<pre><code class="python language-python hljs"><Surface>.set_at((x, y), <color>) <span class="hljs-comment"># Updates pixel.</span> |
2515 |
| -<Surface>.fill(<color>) <span class="hljs-comment"># Fills the whole surface.</span> |
2516 |
| -<Surface>.blit(<Surface>, (x, y)) <span class="hljs-comment"># Draws passed surface to the surface.</span> |
2517 |
| -<Surface> = <Surface>.subsurface(<Rect>) <span class="hljs-comment"># Returns subsurface.</span> |
| 2514 | +<pre><code class="python language-python hljs"><Surf>.set_at((x, y), color) <span class="hljs-comment"># Updates pixel.</span> |
| 2515 | +<Surf>.fill(color) <span class="hljs-comment"># Fills the whole surface.</span> |
| 2516 | +<Surf>.blit(<Surface>, (x, y)) <span class="hljs-comment"># Draws passed surface to the surface.</span> |
| 2517 | +<Surf> = <Surf>.subsurface(<Rect>) <span class="hljs-comment"># Returns subsurface.</span> |
2518 | 2518 | </code></pre>
|
2519 |
| -<pre><code class="python language-python hljs"><Surface> = pg.transform.flip(<Surface>, xbool, ybool) |
2520 |
| -<Surface> = pg.transform.rotate(<Surface>, angle) |
2521 |
| -<Surface> = pg.transform.scale(<Surface>, (width, height)) |
| 2519 | +<pre><code class="python language-python hljs"><Surf> = pg.transform.flip(<Surf>, xbool, ybool) |
| 2520 | +<Surf> = pg.transform.rotate(<Surf>, degrees) |
| 2521 | +<Surf> = pg.transform.scale(<Surf>, (width, height)) |
2522 | 2522 | </code></pre>
|
2523 |
| -<pre><code class="python language-python hljs">pg.draw.line(<Surface>, color, start_pos, end_pos, width) |
2524 |
| -pg.draw.arc(<Surface>, color, <Rect>, start_angle, stop_angle) |
2525 |
| -pg.draw.rect(<Surface>, color, <Rect>) |
2526 |
| -pg.draw.polygon(<Surface>, color, points) |
2527 |
| -pg.draw.ellipse(<Surface>, color, <Rect>) |
| 2523 | +<pre><code class="python language-python hljs">pg.draw.line(<Surf>, color, start_pos, end_pos, width) |
| 2524 | +pg.draw.arc(<Surf>, color, <Rect>, start_radians, stop_radians) |
| 2525 | +pg.draw.rect(<Surf>, color, <Rect>) |
| 2526 | +pg.draw.polygon(<Surf>, color, points) |
| 2527 | +pg.draw.ellipse(<Surf>, color, <Rect>) |
2528 | 2528 | </code></pre>
|
2529 |
| -<div><h3 id="font">Font</h3><pre><code class="python language-python hljs"><Font> = pg.font.SysFont(name, size, bold=<span class="hljs-keyword">False</span>, italic=<span class="hljs-keyword">False</span>) |
2530 |
| -<Font> = pg.font.Font(<span class="hljs-string">'<path>'</span>, size) |
2531 |
| -<Surface> = <Font>.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"><Font> = pg.font.SysFont(name, size, bold=<span class="hljs-keyword">False</span>, italic=<span class="hljs-keyword">False</span>) |
| 2530 | +<Font> = pg.font.Font(<span class="hljs-string">'<path>'</span>, size) |
| 2531 | +<Surf> = <Font>.render(text, antialias, color, background=<span class="hljs-keyword">None</span>) |
2532 | 2532 | </code></pre></div>
|
2533 | 2533 |
|
2534 |
| -<div><h3 id="sound">Sound</h3><pre><code class="python hljs"><Sound> = pg.mixer.Sound(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Loads a sound file.</span> |
2535 |
| -<Sound>.play() <span class="hljs-comment"># Starts playing sound.</span> |
| 2534 | +<div><h3 id="sound">Sound</h3><pre><code class="python hljs"><Sound> = pg.mixer.Sound(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Loads a sound file.</span> |
| 2535 | +<Sound>.play() <span class="hljs-comment"># Starts playing the sound.</span> |
2536 | 2536 | </code></pre></div>
|
2537 | 2537 |
|
2538 | 2538 | <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
|
2539 | 2539 | <span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
|
2540 | 2540 |
|
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> |
2544 | 2544 |
|
2545 | 2545 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
|
2546 | 2546 | <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