9 ms·
Pygame Zero: Creating games without boilerplate
- archgoon 7y agoThere isn't that much boilerplate with pygame (I've taught middleschool students pygame in the past); I'm not sure if it makes sense to move things into a framework, and have a lot of configuration by convention, when the goal is education.
- ggggtez 7y agoI agree. Simply setting a magic global variable WIDTH and HEIGHT doesn't provide any insight, and confuses things more. There is no logical reasoning why those two variables are globals, and what other globals might have special behavior.
- jibe 7y agoI don't think there is much downside in starting with the good stuff (placing and moving pixels). You can, and will eventually expose more of the cruft as needed. But if you lose a students on day 1 because you spending the day configuring instead of creating, you may never get it back.
- ggggtez 7y agoYou have to take a step father to argue that this framework is better than just using boilerplate. If you need magic variables, you haven't actually fixed the boilerplate problem.
- kragen 7y agoPossibly you've made it worse: defining the wrong magic variable will pass silently, as I argue in https://news.ycombinator.com/item?id=22156525 https://news.ycombinator.com/item?id=22156525, while misspelling the name of display.set_mode will give you a traceback that tells you where the problem is. Of course most people didn't get into programming because they got excited about reading error messages...
- ducaale 7y agoActually pygame zero warns you if it detects that you have mispelled. For example of you use width, it will suggest that you use WIDTH. If you write onmousedown instead of on_mouse_down() it will also catch that and warn you.
- kragen 7y agoCool! So you have to get really far off before it doesn't notice?
- albertzeyer 7y agoYou can just give the student an already existing example. Then there is no time wasted in configuring etc. Just tell him, leave this cruft aside (put a comment in the code to make it simpler), and only touch this function, and that function. This is not really too hard. And when the student becomes more curious, at some point, he might want to try to understand parts of the cruft.
- dTal 7y agoHow so? Unless you plan to have multiple windows, of different resolutions (exceedingly rare in a video game), then WIDTH and HEIGHT simply are pieces of global state. Whether you like it or not, global state exists, and if you need access to it in many places, then passing it up and down the call stack becomes an exercise in pointless book-keeping.
- _carl_jung 7y agoGame programming is an area in which I feel benefits greatly from the use of global state.
- electronstudio 7y agoThe goal isn't education in how to make games or use a particular library. The goal is education in coding, and games are just the hook to maintain interest. Pygame Zero hides the bits we don't care about so they don't distract from the bits we do. Middleschool students might cope with full Pygame, but when you're teaching 8-year-olds who can't type quickly, getting a sprite on the screen without any boilerplate is an advantage in maintaining their attention. Another advantage of Pygame Zero is that the author was involved in the creation of Mu, and so Mu comes with Pygame Zero pre-installed. Having to install Pygame via pip would be a challenge for many kids. Anyway, Pygame Zero includes all of Pygame so you're free to introduce as much of it as you like.
- dang 7y agoA small thread from 2015: https://news.ycombinator.com/item?id=9634296 https://news.ycombinator.com/item?id=9634296
- electronstudio 7y agoI have been using this to teach kids to code. I have compiled some of the material I created into a book: https://github.com/electronstudio/pygame-zero-book https://github.com/electronstudio/pygame-zero-book I have also been working on a similiar but 3d library: https://github.com/electronstudio/richlib https://github.com/electronstudio/richlib
- aoloe 7y agoi've skimmed through it: very nice book! one thought: after having read your introduction for the teachers, i wonder why you did not "sort it" the other way round: first the hands on and the tutorials and then the theory... for our coderdojo we went for a "cards" approach: https://github.com/CoderDojoZH/resources/tree/master/cards-python/mu-pgzero https://github.com/CoderDojoZH/resources/tree/master/cards-p... we suggest the kids to first do the pong game in scratch (we have a booklet with step by step instructions) and then use the set of cards (in order) to do the same. we try to avoid that they just copy the snippets line by line, without thinking at what they are doing... i will now give them a link to your book when they want to learn about python!
- kragen 7y agoBoilerplate 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.
- ErotemeObelus 7y agoThis is as good a place as any to ask. How do you do concurrency in video games, and how does concurrent code map onto parallel execution?
- blondin 7y agothe problem with teaching programming is bigger than boiler plate code to be honest. but what the author is doing is really appreciated! i feel like the abstraction is leaky though. why the person you are shielding from the boiler plate has to write on_mouse_down, on_mouse_up, clock.schedule_unique?! the problem with teaching programming is that people are given user interfaces that hide the fact that you "actually make your computer or phone do stuff". at least that's what i think the bigger issue is. back then when i discovered i could make the computer do things, i'd spent hours in the QBasic online manual, discovering new things i can show people.
- zeta0134 7y agoWhile I can certainly appreciate a desire to get down in the nuts and bolts of a programming problem, and really understand what you're doing, I don't think that's required to spark the joy of learning in a new student. No, for video games in particular, what seems to work most consistently is rapid iteration. How fast can the student go from idea in their head to their character doing that behavior on screen? That core loop is fascinating to a youngster, who immediately begins to piece together how Mario might run and jump, or a race car might move along a track, almost entirely on their own. To that end, there's a certain value in declaring that, "You know what, PyGame Zero's event loop is probably fine. Let it sweat the details, and we'll worry about writing the character.jump() function next." Another framework I love that works this way is Love2D, which targets Lua but has a similar approach: https://love2d.org/ https://love2d.org/ Sure it's not suitable for any project of decent complexity, but there's a reason we keep reaching for it during Ludum Dares, and any time we want to rapidly prototype an idea. It takes the least amount of effort of any tool I know to go from concept to prototype, and get the idea in playtesters hands so we know if it's worth doing properly. Getting to do that in Python sounds awesome, so kudos all around; this is neat stuff.
- Kwantuum 7y agoDepends on what you mean by decent complexity: https://lk-games.itch.io/citizen-of-nowhere https://lk-games.itch.io/citizen-of-nowhere This game is made using love2d
- tony 7y agoIncredible! The sphinx docs look great. Also, the logo! Not to mention, the translations: - https://pygame-zero.readthedocs.io/zh_CN/latest/ https://pygame-zero.readthedocs.io/zh_CN/latest/ - https://pygame-zero.readthedocs.io/ja/latest/ https://pygame-zero.readthedocs.io/ja/latest/ - https://pygame-zero.readthedocs.io/sk/latest/ https://pygame-zero.readthedocs.io/sk/latest/ One reason of why I wouldn't use it yet: LGPL. This is the issue pygame had when I tried it. The license made it very difficult to incorporate and use in practice, and I ended up playing with kivy instead (https://github.com/kivy/kivy https://github.com/kivy/kivy). The reasons why: 1. Writing anything commercial would be a gamble. I've wrote at length on why this license isn't meant for scripting languages, but even if it's a compiled language, SDL moved away from LGPL here: https://youtu.be/MeMPCSqQ-34?t=430 https://youtu.be/MeMPCSqQ-34?t=430. 2. LGPL and scripting languages just don't work from an open source perspective. It's not copy-paste and vendorize friendly. Functions can't be copied a la carte. I'd rather write something from scratch than link an LGPL library in one of my MIT licensed projects. I'm just not willing to open downstream users to the risk. What ends up happening is 5/10 years on, someone ends up writing a new MIT/BSD library to get around a viral one (e.g. GCC -> Clang/LLVM).
- mattkrause 7y agoI'm not sure this is the sort of library you'd use for a commercial project; it seems like it's aimed at teaching beginning programming instead.
- _carl_jung 7y agoDo you have any links for the implications for commercial use? If I released a game made with PyGame, what's the gamble? I've seen examples of people using it commercially [0]. [0] https://www.reddit.com/r/gamedev/comments/16d5ak/is_pygame_or_any_other_python_gamedev_library/c7uz2y6?utm_source=share&utm_medium=web2x https://www.reddit.com/r/gamedev/comments/16d5ak/is_pygame_o...
- tony 7y agopygame has successful deployments onto steam, but it doesn't show us developers that would have used for pygame if it wasn't for licensing concerns. LGPL in scripting languages is a gray area. There are no previous legal cases to cite. Python projects have transferred away from LGPL due to its ambiguity. Example dating back to 2004: https://twistedmatrix.com/pipermail/twisted-python/2004-May/007946.html https://twistedmatrix.com/pipermail/twisted-python/2004-May/... Namely around the language of what creates a derivative / combined work: LGPL was intended for languages that use header files, like C and C++. So, if a project was ever accused of creating a combined work, they'd be on the hook to defend the the case, whether or not a combined work is created. In v2, the license even shifts the question onto licensee/licensor: "The threshold for this to be true is not precisely defined by law." In licenses such as BSD/MIT/ISC/etc. these issues do not arise, and a breeze through top python projects show reciprocal contributions and access to source are commonplace without viral clauses in licenses. I write about LGPL/python at length before here (2013): https://github.com/ScottDuckworth/python-anyvcs/issues/32#issuecomment-28528142 https://github.com/ScottDuckworth/python-anyvcs/issues/32#is... and (2016) https://github.com/PyGithub/PyGithub/issues/468#issuecomment-252437234 https://github.com/PyGithub/PyGithub/issues/468#issuecomment... For gaming, it's even more relevant, because if the deployment is to steam or a mobile phone (like what kivy does with https://github.com/kivy/python-for-android https://github.com/kivy/python-for-android), that could easily be interpreted as a combined work. They're not like C/C++ application linking to .so / shared libraries.
- acbart 7y agoI'm very interested in this project, because I have a student researching what good options there are for Python Game Dev at the CS1 level. We used Arcade[1] this past Fall, and things didn't go very well (I'm not sold on Pyglet, and there was a lot of small "What??" issues with Arcade's design itself). Pygame Zero is a very appealing choice, but suffers from its design around global state - which is something I spend most of the semester railing against. The authors gave me a good explanation for why[2], but it still is a deal-breaker for an introductory-level university course for CS majors. At this point, we're leaning towards making our own library to satisfy our purposes. We're still looking at other options though - hey, if you have one, feel free to suggest it :) [1] http://arcade.academy/ http://arcade.academy/ [2] https://github.com/lordmauve/pgzero/issues/174 https://github.com/lordmauve/pgzero/issues/174
- WoodenChair 7y agoCan you elaborate on what you don’t like about working with Arcade?
- nnq 7y agoThere was/is Kevy for Python game dev, focused a lot on touch and mobile, dunno if it is well maintained but it seemed to have a nicer architecture/API, maybe it's what you need.
- stuaxo 7y agoKivy ?
- nnq 7y agoYes. Sorry: https://kivy.org/#home https://kivy.org/#home , https://kivy.org/doc/stable/ https://kivy.org/doc/stable/ (docs) & https://github.com/kivy/kivy https://github.com/kivy/kivy . It's not branded as an actual game framework, but it's based on OpenGL ES and that's pretty much what it is - I mean, even their first tutorial is a mini game https://kivy.org/doc/stable/tutorials/pong.html https://kivy.org/doc/stable/tutorials/pong.html .
- 7y ago
- nnq 7y agoCan this target mobile platforms, eg. Android or iOS? Otherwise it's a non starter, the kind of games a beginer could easily make mostly only make sense as mobile games...
- dvfjsdhgfv 7y agohave a look at Love2D. Although it's Lua rather than Python, it's quite similar, and you have a bit more distribution options: https://news.ycombinator.com/item?id=11633278 https://news.ycombinator.com/item?id=11633278 The docs are a mess though.
- anonymoushn 7y agoDid you mean to link to "A word about why Pinboard was not included?"
- dvfjsdhgfv 7y agoAh sorry, yes, wrong thread, can't edit it now. I meant the following link: https://love2d.org/wiki/Game_Distribution https://love2d.org/wiki/Game_Distribution
- sails 7y agoThis looks really great! A somewhat related video that your docs reminded me of, "Bret Victor - Inventing on Principle" [], which I think is a really interesting take on inventing and learning. Perhaps some useful thoughts for your principles section. []https://vimeo.com/36579366 https://vimeo.com/36579366