7 ms·
I'll be teaching a group of nine and ten years olds to code. I'm planning on using Jupyter. What I'd really like to do is make a multiplayer naval game, with e
by danielvf 9y ago
I'll be teaching a group of nine and ten years olds to code. I'm planning on using Jupyter.
What I'd really like to do is make a multiplayer naval game, with each player controlling their ship from their own notebook. Players would start out by running commands like fire(range=400, bearing=120) right from a cell, but would later be able automate their ship - for example, pick the nearest enemy, get the range, and plug that into firing automatically at it.
My server would be projecting a big map of the world up on the wall.
However, to do this nicely, I need the ability to make a cell (or a function defined in a cell) run every X milliseconds. I know I can do this for one cell, with a loop and sleep function, but I'd really rather have multiple cells/functions "running", so we can break the code into smaller chunks, and to let them build their own ship UIs.
Any advice on how to do this in Juptyer with Python? In an ideal world, I'd just "tag" a cell somehow so that it ran periodically.
- kd5bjo 9y agoI haven't looked a Jupyter at all; in plain Python, I'd start with something like this[1]: schedule = set() # (time, function) pairs while True: next=min(schedule) schedule.remove(next) sleep(next[0] - now()) next[1]() schedule.add((now() + interval, next[1])) [1] Pseudocode only; I don't have the API reference in front of me to verify method names and such. Several edge cases are unhandled.
- cat199 9y ago> multiplayer navel game I don't think you mean this, though it could be intriguing just as well.. https://en.wikipedia.org/wiki/Omphaloskepsis https://en.wikipedia.org/wiki/Omphaloskepsis
- danielvf 9y agoNope! Thanks for the catch!
- Tenoke 9y agoThe simplest (and still educational) way would be to have the stuff in the different cells as functions, and to collect all the functions that should be re-run in a single cell. To do it more properly, however, you can have a post_save_hook for the whole notebook, which runs the code from whichever cells however often you want outside of the notebooks. Of course, this way you wont see the output in the notebook.
- danielvf 9y agoYeah, output right in the notebook is important. I'm wanting them to be able to do something like this in a cell: for ship in myShip.radarScan(): if ship.is_enemy: display([ship.type, ship.range, ship.bearing]) And then have that cells results always showing the current data as they play the game.
- Tenoke 9y agoI haven't used it much, but I believe you can do this by using (or more likely slightly extending) the init_cell nbextension. https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/init_cell/README.html https://jupyter-contrib-nbextensions.readthedocs.io/en/lates...
- danielvf 9y agoOh, that's cool - it's just some javascript that's essentially clicking execute on the selected cells. That makes sense, and hacked up version of this could work fine! Thanks!
- Quenty 9y agoI know that matplotlib.pyplot has a sleep function used to animate graphs. May be worth investigating.
- pbhjpbhj 9y agoFWIW there's a Snoopy/Woodstock game on code.org that's a much simplified version of this where players control avatars who throw snowballs. It's turn based, each player programs their avatar with 6 different commands (IIRC, u d l r jump fire). Fun and very accessible for my 8yo and I. Can't find it on CODE.org any more, it's mentioned here http://www.gameinformer.com/b/news/archive/2017/11/10/little-coders-can-test-their-skills-in-snoopy-snow-brawl.aspx http://www.gameinformer.com/b/news/archive/2017/11/10/little....
- highesttide 9y agoRelated to that idea, there is a game called screeps [0] that has a lot of that general idea and executes it fairly well. The payment model discouraged me from sticking with it, sadly. [0] https://screeps.com https://screeps.com
- in9 9y agoTo be totally honest, even though I love Jupyter notebooks, I wouldn't teach someone to code using it. See, as someone who's not, originally a programmer, I believe that teaching someone to code is intertwined with teaching (gradually) workflows and environments. Now, Jupyter is great (I'm a statistician btw) precisely because you have a presentable literate programming tool. That is extremely valuable for data analysis and model building. Now, If you are teaching programming in it self, I think the terminal + text editor is important since that is a step towards how things are done irl. But if you think that the terminal is too much for your audience, just go for IDLE, which has many advantages for python development + teaching, and is already a step towards text editor + terminal or IDE level development.
- jasongrout 9y agoI'm teaching someone to code right now on the Raspberry Pi, and JupyterLab is working far better for us than IDLE or a separate text file and terminal. We use the workflow described in http://jupyterlab.readthedocs.io/en/stable/user/documents_kernels.html http://jupyterlab.readthedocs.io/en/stable/user/documents_ke.... We open up our python file and a console side-by-side, and pressing shift-enter in the file sends a line or selection to a console. We can easily investigate it in the console outside of the file too. You can just "pip install jupyterlab" on a Raspberry Pi and it works great!
- gldnspud 9y agoThese criticisms could be valid for Jupyter Notebook, but remember that JupyterLab is what's under discussion in this post. JuptyerLab is much more of an IDE-style than Notebook, and it includes very capable terminal and text editor components. Although I'm new to Jupyter Lab, I just tried it and it does feel like it could be a very approachable IDE for educational use.
- dragonwriter 9y ago> Now, If you are teaching programming in it self, I think the terminal + text editor is important since that is a step towards how things are done irl. Jupyter (and similar) Notebooks are a way things are done "in real life"; they aren't just for entertainment purposes; and JupyterLab blends that into an IDE and the IDE with notebooks approach is also a way things have been done "in real life"; both with third-party tools incorporating Jupyter (and earlier IPython) notebooks, and with other IDE's incorporating notebook-style interaction. Which isn't to say that there aren't arguments for teaching program outside of the notebook environment rather than within it (though the notebook is kind of a super REPL, and REPLs seem to me to be great tools for teaching), but that not using notebooks is better because it is how things are done "in real life" is just taking a highly selective view of real life.
- monkmartinez 9y ago> Any advice on how to do this in Juptyer with Python? In an ideal world, I'd just "tag" a cell somehow so that it ran periodically. Don't. Use something that will engage them visually and mentally. Kids are not impressed with text, that ship has sailed.
- icebraining 9y agoIsn't that what the map on the wall is for?
- coding123 9y agoThis is actually a good idea for this age range. If this was a college class I would recommend a more traditional programming environment, but this should be just about as cool as you can get if you can pull off the networking.
- psykotic 9y agoYour question relates more to IPython Kernel than Jupyter per se. Fortunately ipykernel has out of the box integration with event loops, e.g. https://github.com/ipython/ipykernel/blob/master/ipykernel/eventloops.py#L321 https://github.com/ipython/ipykernel/blob/master/ipykernel/e... for asyncio integration, and you can then schedule cell updates on the asyncio event loop as you would normally. To have events on the Python side generate visible updates in the Jupyter notebook you can use ipywidgets, for which the Python widget state in the kernel is automatically synchronized to the JavaScript widget state.
- danielvf 9y agoThanks! This looks like exactly what I was looking for.
- tcskeptic 9y agoWhat you describe sounds quite similar to the old Robocode project for Java programming, it may be worth taking a look at it for ideas/implementation: http://robowiki.net/wiki/Robocode http://robowiki.net/wiki/Robocode
- mrbarletta 9y agoHi Teacher - please teach them without war/combat games, there are so many fun things to do that don't involve guns, battles, fire, enemies :)