Pygame Zero: Creating games without boilerplate
Boilerplate is of course detestable, but PyGame is already pretty low in boilerplate.  <a href="https:&#x2F;&#x2F;github.com&#x2F;kragen&#x2F;pyconar-talk&#x2F;blob&#x2F;master&#x2F;helloneg1.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kragen&#x2F;pyconar-talk&#x2F;blob&#x2F;master&#x2F;helloneg1...</a> is five lines of code plus the shebang:<p><pre><code>    #!&#x2F;usr&#x2F;bin&#x2F;python
    from pygame import *
    pantalla = display.set_mode((0, 0), FULLSCREEN)
    draw.rect(pantalla, 128, (50, 100, 200, 400))
    display.flip()
    time.delay(2000)
</code></pre>
Admittedly this is rather daunting compared to<p><pre><code>    print(&quot;hello, world&quot;)
</code></pre>
but the benefits of the extra code are rather more immediate than those of public static void main: you can control whether your window is fullscreen or not, what size it is, when the screen updates, and how long the program runs before exiting.<p>When I see things like this,<p>&gt; <i>Your alien should now be appearing on screen! By passing the string &#x27;alien&#x27; to the Actor class, it automatically loads the sprite,</i><p>I shudder.  (That&#x27;s in <a href="https:&#x2F;&#x2F;pygame-zero.readthedocs.io&#x2F;en&#x2F;stable&#x2F;introduction.html#draw-a-sprite." rel="nofollow">https:&#x2F;&#x2F;pygame-zero.readthedocs.io&#x2F;en&#x2F;stable&#x2F;introduction.ht...</a>)  When you write a program and it <i>doesn&#x27;t</i> load your sprite, how do you figure out why not?  Maybe you called your sprite file images&#x2F;alien.PNG or Images&#x2F;alien.png or images&#x2F;alien.jpeg or .&#x2F;alien.png, and Pygame Zero doesn&#x27;t happen to look in those places?  I think you&#x27;re going to have to resort to either trying things at random or using strace.  Similarly, how hard is it to debug Pygame Zero displaying your window at the wrong size if you write HIEGHT = 300 instead of HEIGHT = 300?<p>In that talk I did eventually get around to drawing sprites, but it wasn&#x27;t until one of the later examples, <a href="https:&#x2F;&#x2F;github.com&#x2F;kragen&#x2F;pyconar-talk&#x2F;blob&#x2F;master&#x2F;reloj.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kragen&#x2F;pyconar-talk&#x2F;blob&#x2F;master&#x2F;reloj.py</a>.  The relevant code is<p><pre><code>    imagen = image.load(&#x27;trashcan_empty.png&#x27;)
    ...
        pantalla.blit(imagen, (xx, yy))
</code></pre>
(The talk was for a Spanish-speaking audience; &quot;imagen&quot; is Spanish for &quot;image&quot;, and &quot;pantalla&quot; for &quot;screen&quot;.)  So that&#x27;s what you&#x27;re saving yourself when you say<p><pre><code>    alien = Actor(&#x27;alien&#x27;)
    alien.pos = 100, 56
    ...
        alien.draw()
</code></pre>
But I don&#x27;t have a lot of experience teaching kids to code, so maybe my intuitions about what makes things harder and what makes them easier are not well-founded.  I&#x27;d be interested to hear other people&#x27;s experiences of the kinds of errors beginners make and what kinds of interface design work best for them.<p>With Yeso, things are slightly more complicated than with PyGame; <a href="https:&#x2F;&#x2F;gitlab.com&#x2F;kragen&#x2F;bubbleos&#x2F;blob&#x2F;master&#x2F;yeso&#x2F;yv.py" rel="nofollow">https:&#x2F;&#x2F;gitlab.com&#x2F;kragen&#x2F;bubbleos&#x2F;blob&#x2F;master&#x2F;yeso&#x2F;yv.py</a> similarly opens a window and displays a PNG in it:<p><pre><code>    import sys

    import yeso


    if __name__ == &#x27;__main__&#x27;:
        image = yeso.read_png(sys.argv[1])
        if image:
            with yeso.Window(sys.argv[1], image.size) as w:
                with w.frame() as f:
                    f.copy(image)
                while True:
                    w.wait()
</code></pre>
However, I&#x27;d argue that this interface is somewhat less error-prone than PyGame&#x27;s; a common error I&#x27;ve had when starting new PyGame programs is forgetting to call pygame.display.flip(), which has the delightful symptom that whatever I&#x27;ve drawn just doesn&#x27;t appear on the screen.  By using a context manager, the Yeso Python binding avoids that error.  As the comments point out, you can reduce yv.py to the following six lines of code if you depend on CPython&#x27;s prompt finalization to flip buffers:<p><pre><code>    #!&#x2F;usr&#x2F;bin&#x2F;python
    import yeso, sys
    _, n = sys.argv
    p = yeso.read_png(n)
    w = yeso.Window(n, p.size)
    w.frame().copy(p)
    while True: w.wait()
</code></pre>
Yeso is still in its early stages, and I might change this interface a little bit, but I don&#x27;t see why things need to be any more complicated than this for simple GUI programs.  And my intuition is that making the interface less explicit makes it worse, <i>for experienced programmers</i>.<p>Proce55ing and Arduino offer at least some evidence that at least the setup()&#x2F;draw()&#x2F;mousemove() scaffolding is a significant help to beginners; they can get a moving object on the screen or a flashing LED before they have to know what a loop is, and the default behavior you have in Python or C of closing the window (for GUI programs) or restarting the firmware (for embedded programs) is not a good default.