|
2491 | 2491 | </code></pre></div></div>
|
2492 | 2492 |
|
2493 | 2493 |
|
2494 |
| -<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> |
2495 |
| -<Surface> = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span> |
2496 |
| -<Surface> = pg.image.load(<span class="hljs-string">'<path>'</span>).convert() <span class="hljs-comment"># Loads an image.</span> |
2497 |
| -<Surface> = <Surface>.convert() <span class="hljs-comment"># Converts to screen format.</span> |
2498 |
| -<Surface> = <Surface>.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"><Surface>.set_at((x, y), <color>) <span class="hljs-comment"># Updates pixel.</span> |
2506 |
| -<Surface>.fill(<color>) <span class="hljs-comment"># Fills the whole surface.</span> |
2507 |
| -<Surface>.blit(<Surface>, (x, y)/<Rect>) <span class="hljs-comment"># Draws passed surface to the surface. </span> |
2508 |
| -<Surface>.blit(<Surface>, (x, y)/<Rect>) <span class="hljs-comment"># Draws one image onto another.</span> |
2509 |
| -<Surface> = <Surface>.subsurface(<Rect>) <span class="hljs-comment"># Returns subsurface.</span> |
2510 |
| -</code></pre> |
2511 |
| -<pre><code class="python language-python hljs"><Surface> = pg.transform.flip(<Surface>, xbool, ybool) |
2512 |
| -<Surface> = pg.transform.rotate(<Surface>, angle) |
2513 |
| -<Surface> = pg.transform.scale(<Surface>, (width, height)) |
2514 |
| -</code></pre> |
2515 | 2494 | <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) <span class="hljs-comment"># x, y, w/width, h/height</span>
|
2516 | 2495 | <int> = <Rect>.x/y/centerx/centery/bottom/left/right/top
|
2517 | 2496 | <tuple> = <Rect>.topleft/center/topright/bottomright/bottomleft
|
|
2532 | 2511 | (key, value) = <Rect>.collidedict(<dict_of_Rect>)
|
2533 | 2512 | [(key, value), ...] = <Rect>.collidedictall(<dict_of_Rect>)
|
2534 | 2513 | </code></pre>
|
2535 |
| -<div><h3 id="draw">Draw</h3><pre><code class="python language-python hljs">pg.draw.rect(<Surface>, color, <Rect>) |
| 2514 | +<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> |
| 2515 | +<Surface> = pg.Surface((width, height)) <span class="hljs-comment"># Creates a new surface.</span> |
| 2516 | +<Surface> = pg.image.load(<span class="hljs-string">'<path>'</span>).convert() <span class="hljs-comment"># Loads an image.</span> |
| 2517 | +</code></pre></div> |
| 2518 | + |
| 2519 | + |
| 2520 | +<pre><code class="python language-python hljs"><Surface>.set_at((x, y), <color>) <span class="hljs-comment"># Updates pixel.</span> |
| 2521 | +<Surface>.fill(<color>) <span class="hljs-comment"># Fills the whole surface.</span> |
| 2522 | +<Surface>.blit(<Surface>, (x, y)/<Rect>) <span class="hljs-comment"># Draws passed surface to the surface.</span> |
| 2523 | +<Surface> = <Surface>.subsurface(<Rect>) <span class="hljs-comment"># Returns subsurface.</span> |
| 2524 | +</code></pre> |
| 2525 | +<pre><code class="python language-python hljs"><Surface> = pg.transform.flip(<Surface>, xbool, ybool) |
| 2526 | +<Surface> = pg.transform.rotate(<Surface>, angle) |
| 2527 | +<Surface> = pg.transform.scale(<Surface>, (width, height)) |
| 2528 | +</code></pre> |
| 2529 | +<div><h4 id="drawing-1">Drawing:</h4><pre><code class="python language-python hljs">pg.draw.rect(<Surface>, color, <Rect>) |
2536 | 2530 | pg.draw.polygon(<Surface>, color, points)
|
2537 | 2531 | pg.draw.circle(<Surface>, color, center, radius)
|
2538 | 2532 | pg.draw.ellipse(<Surface>, color, <Rect>)
|
|
2541 | 2535 | pg.draw.lines(<Surface>, color, points)
|
2542 | 2536 | </code></pre></div>
|
2543 | 2537 |
|
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"><Font> = pg.font.SysFont(name, size, bold=<span class="hljs-keyword">False</span>, italic=<span class="hljs-keyword">False</span>) |
| 2539 | +<Font> = pg.font.Font(<span class="hljs-string">'<path>'</span>, size) |
| 2540 | +<Surface> = <Font>.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"><Sound> = pg.mixer.Sound(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Loads a sound file.</span> |
| 2544 | +<Sound>.play() <span class="hljs-comment"># Starts playing sound.</span> |
2552 | 2545 | </code></pre></div>
|
2553 | 2546 |
|
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(<float>) |
2559 |
| -</code></pre> |
2560 | 2547 | <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
|
2561 | 2548 | <span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
|
2562 | 2549 |
|
2563 | 2550 | P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
|
2564 | 2551 | 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> |
2566 | 2553 |
|
2567 | 2554 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
|
2568 | 2555 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>
|
|
2584 | 2571 | run(get_screen(), get_images(), get_mario(), get_tiles())
|
2585 | 2572 |
|
2586 | 2573 | <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() |
2587 | 2575 | <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()):
|
2588 | 2576 | keys = {pygame.K_UP: D.n, pygame.K_RIGHT: D.e, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w}
|
2589 | 2577 | 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}
|
2590 | 2578 | update_speed(mario, tiles, pressed)
|
2591 | 2579 | update_position(mario, tiles)
|
2592 | 2580 | 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>) |
2594 | 2582 |
|
2595 | 2583 | <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>
|
2596 | 2584 | x, y = mario.spd
|
|
0 commit comments