3 ms·
Boilerplate is of course detestable, but PyGame is already pretty low in boilerplate. https://github.com/kragen/pyconar-talk/blob/master/helloneg1.py https://g
by kragen 7y ago
Boilerplate is of course detestable, but PyGame is already pretty low in boilerplate. https://github.com/kragen/pyconar-talk/blob/master/helloneg1.py https://github.com/kragen/pyconar-talk/blob/master/helloneg1... is five lines of code plus the shebang:
#!/usr/bin/python
from pygame import *
pantalla = display.set_mode((0, 0), FULLSCREEN)
draw.rect(pantalla, 128, (50, 100, 200, 400))
display.flip()
time.delay(2000)
Admittedly this is rather daunting compared to
print("hello, world")
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.
When I see things like this,
> Your alien should now be appearing on screen! By passing the string 'alien' to the Actor class, it automatically loads the sprite,
I shudder. (That's in https://pygame-zero.readthedocs.io/en/stable/introduction.html#draw-a-sprite. https://pygame-zero.readthedocs.io/en/stable/introduction.ht...) When you write a program and it doesn't load your sprite, how do you figure out why not? Maybe you called your sprite file images/alien.PNG or Images/alien.png or images/alien.jpeg or ./alien.png, and Pygame Zero doesn't happen to look in those places? I think you'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?
In that talk I did eventually get around to drawing sprites, but it wasn't until one of the later examples, https://github.com/kragen/pyconar-talk/blob/master/reloj.py https://github.com/kragen/pyconar-talk/blob/master/reloj.py. The relevant code is
imagen = image.load('trashcan_empty.png')
...
pantalla.blit(imagen, (xx, yy))
(The talk was for a Spanish-speaking audience; "imagen" is Spanish for "image", and "pantalla" for "screen".) So that's what you're saving yourself when you say
alien = Actor('alien')
alien.pos = 100, 56
...
alien.draw()
But I don'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'd be interested to hear other people's experiences of the kinds of errors beginners make and what kinds of interface design work best for them.
With Yeso, things are slightly more complicated than with PyGame; https://gitlab.com/kragen/bubbleos/blob/master/yeso/yv.py https://gitlab.com/kragen/bubbleos/blob/master/yeso/yv.py similarly opens a window and displays a PNG in it:
import sys
import yeso
if __name__ == '__main__':
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()
However, I'd argue that this interface is somewhat less error-prone than PyGame's; a common error I've had when starting new PyGame programs is forgetting to call pygame.display.flip(), which has the delightful symptom that whatever I've drawn just doesn'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's prompt finalization to flip buffers:
#!/usr/bin/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()
Yeso is still in its early stages, and I might change this interface a little bit, but I don'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, for experienced programmers.
Proce55ing and Arduino offer at least some evidence that at least the setup()/draw()/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.
- pierrebai 7y agoIn the example code for pygame you give, what happens if there is a single typo? It doesn't work either and you probably get an incomprehensible stack trace. There is no benefit to teh extra code because that extra code is magic gibberish. It's an incantation that somehow makes things work.
- deleted 7y ago[deleted]
- kragen 7y agoFrom my point of view, an incomprehensible stack trace is a big improvement over silently failing to work, because then you learn to comprehend the stack trace and you can fix the error. This is one of the central design principles of Python, rooted in experience with teaching beginners to program in the ABC teaching language. The extra code is not actually magic gibberish, although it does appear that way at first. In Python you can comment out individual lines and observe the effect, change the parameters, or type >>> from pygame import * >>> help(display.flip) Although an IDE may make that easier, and of course you don't start out knowing that. What is your experience teaching beginners to program? What tools did you have them use, and what did they have the most difficulty with?
- aoloe 7y agoin my experience, pygame zero works well with 10 - 14 year kids. i run a coderdojo and we have developed a system of cards that helps the kids creating a pong game with pygame zero. in two hours of our weekly meeting, they manage to - install the mu editor, - create the game window - download an image from the internet, - put it in their game - move the image around with the arrow keys mostly by themselves. loading the image is indeed one of the big issues they often face. but i'm convinced that it will often be an issue anyway: kids need some mentoring there. (really, you cannot rely on the fact that they will be typing the file name correctly or that they will put the image in a specific directory, with the right extension). this having been said, writing a full pong is much harder as in scratch. they do need some real mentoring for the further steps. concerning your suggestion with yeso: for our setup, providing an image through the command line is probably not an improvement. most of our kids are using windows or mac and we avoid forcing them to use the terminal during their first steps of "creating a game with real programming". managing files is hard...