lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
538 stars 188 forks source link

Add a way of invoking pgzrun on the current file from within Idle #23

Closed lordmauve closed 7 years ago

lordmauve commented 9 years ago

Originally reported by: David Ames (Bitbucket: amos1969, GitHub: amos1969)


Hi

I think I mentioned this before. Most of the schools I have been in restrict access to the command line for "security" reasons, they also often prevent the running of .bat files, and other similar scripts for the same reason.

In addition the vast majority of schools use Idle as their Python IDE (anything more advanced tends to be overkill). In order to use pgzrun within a school environment it is therefore going to be necessary to create a way of running the current python file through the runner from within Idle. Is it possible to create a function of some kind which will invoke the runner on the current file, when it is run as a Python script (ie when F5 is pressed in Idle).

Something as simple as putting: pgzrun() as the first function call in the current file perhaps.

I hope that makes sense. As a relatively technically minded teacher I was unable to convince the technicians in school to allow me to use the command line, as they said it was a potential security issue.

Cheers


lordmauve commented 7 years ago

Original comment by David Ames (Bitbucket: amos1969, GitHub: amos1969):


Yay! Time to have another play.

Dave

lordmauve commented 7 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


This has been implemented in the default branch and will be available in Pgzero 1.2. Documentation is here:

http://pygame-zero.readthedocs.io/en/latest/ide-mode.html

lordmauve commented 7 years ago

Original comment by Terry Reedy (Bitbucket: terryjreedy, GitHub: terryjreedy):


After reading this thread, I just rejected what I understand to be Daniel's IDLE request, at https://bugs.python.org/issue24718, with this message: "From your post, it was unclear to me what you are actually asking for. From reading the bitbucket thread, it appears that you want something I cannot give you -- a built-in privileged position in IDLE. However, as I said before, you can write a pgz extension. An extension can add a menu entry and get a reference to the editor's tk Text widget. It would be easy to write one that added needed boilerplate at the top and bottom of a file so that it is ready to run with F5. The top part could end with

Ignore everything above this line.

and the bottom part start with

Ignore everything below this line.

With more effort, you could do probably do more to hide the boilerplate, but as I explained before, I have no motivation for that."

I want to be clear that I support teaching kids python with IDLE. That is why I am volunteering my unpaid time to improve it. I taught my daughter Python with IDLE, and she now uses both for her graduate research.

lordmauve commented 7 years ago

Original comment by David Ames (Bitbucket: amos1969, GitHub: amos1969):


Essentially I just wanted a single line import statement, like: import pgzrun

Which would invoke the pgzrun tool on the current script in the background without the need for the command line. Allowing pgzero to be run from any ide without reconfiguring, just by running the Python script normally.

Dave

lordmauve commented 7 years ago

Original comment by Aivar Annamaa (Bitbucket: aivarannamaa, GitHub: aivarannamaa):


Could you give some heuristics for deciding whether a script is pgzero script or not?

I'm considering writing a plug-in for Thonny (http://thonny.org) which would intercept F5 command and run current script via pgzero if it's confident that current script is really a pgzero script.

lordmauve commented 7 years ago

Original comment by fizban (Bitbucket: fizban, GitHub: fizban):


That seems to be some leftover from the original run_game.py . Since I do not have a Mac I did not need the workaround and I am not able to even test it. Anyway, this was meant to be a quick fix to allow using pgzero with an import statement under Windows... I was hoping an official update would come at some point to address this need, but it seems that the project has not been updated for more than one year...

lordmauve commented 7 years ago

Original comment by Steve Clement (Bitbucket: SteveClement, GitHub: SteveClement):


@fizban is this a place holder:

#!python

if not check_python_ok_for_pygame():
    substitute_full_framework_python()

What is your workaround to get pgzero/pygame running in an MacOS virtualenv?

lordmauve commented 8 years ago

Original comment by fizban (Bitbucket: fizban, GitHub: fizban):


I have not started the Python lessons with my students, but I am considering either using the Turtle library or the pgzero library. I have slightly modified the Turtle library to allow rotation of images, emulating the functionality of Trinket. I have also modified the pgzero library to be able to use it with:

#!python

pgzero.rungame import *

# some code here

run_game()

Ideally, I would like to use standard libraries, but I did not find any that would fully fit my idea of teaching python through the creation of simple games, with students that already know some programming basics using Scratch.

This is how my rungame.py looks like:

#!python

# Expose pgzero objects as a builtins
from pgzero.builtins import *
from pgzero.runner import *
import pgzero.screen

def run_game():
    pygame.init()
    PGZeroGame(sys.modules['__main__']).run()
    pygame.quit()

# Pygame won't run from a normal virtualenv copy of Python on a Mac
if not check_python_ok_for_pygame():
    substitute_full_framework_python()
if __debug__:
    warnings.simplefilter('default', DeprecationWarning)
path = sys.argv[0]
loaders.set_root(path)

screen= pgzero.screen.Screen(pygame.display.set_mode((1, 1), DISPLAY_FLAGS))

I did this some time ago, so I am not sure whether I actually modified something else... The code autocompletion functionality in PyZo and PyScripter seems to work well with this...

lordmauve commented 8 years ago

Original comment by Nigel Morse (Bitbucket: musmuris, GitHub: musmuris):


I'm sure the kids will be fine with it. However most people work in an IDE these days and being able to set a breakpoint and hit "Debug" is far nicer than having to put "prints" over all the code like I had to 20+ years ago! I did look at the source, but my python-fu isn't yet strong enough to look at how it might be possible (I'm mainly a C++/C# coder myself). In fact the lack of an import is what has put me off using this so far, but i plan to write some projects with it now. I still wanted to add my +1 to the issue and my reasoning.

lordmauve commented 8 years ago

Original comment by David Ames (Bitbucket: amos1969, GitHub: amos1969):


We used PGZero quite successfully with a group of kids last weekend at PyconUK on the Raspberry Pi. They had no problems coping with using the command line to launch the game. In fact they coped with it better than the teachers the day before.

I also got a chance to talk to Dan and ask again for the import option, which doesn't look like it will materialise. There's the possibility of a button on a version of Mu (the MicroBit MicroPython editor) which will launch a PGZero file, correctly.

lordmauve commented 8 years ago

Original comment by Nigel Morse (Bitbucket: musmuris, GitHub: musmuris):


I was about to open a very similar issue. I want to try and use PyGame zero in CodeClub, but the projects in there have them using IDLE and getting used to running stuff from there. I've also been able to use the (albeit limited) debugger to solve bugs.
The pygame zero framework looks great for making pygame it a lot easier for kids, and I can understand the zero-boilerplate idea. However having the the option to allow a minimal boilerplate, of an (e.g)

#!python

import pygamezero

# some code here

pygamezero.run() 

would enable the use in any python IDE and might even make it more accessible, and actually more like a normal python program where you import a library and then use it (for example the turtle library that's a great way to start with kids)

lordmauve commented 8 years ago

Original comment by fizban (Bitbucket: fizban, GitHub: fizban):


One thing I think would help my students is allowing the code completion functionality to properly guess the type of the different variables that use pgzero's builtins. Updating the dictionary of the main module at runtime prevents PyScripter from guessing the type at design time. So an alternate solution (although I guess uglier) might be using from "pgzero.rungame import *" at the start of the script instead of "from pgzero import rungame" , and add also the content of the builtins module to the rungame module. If then I throw in the rungame module a reference to the screen variable as well, I get a nice autocompletion functionality for the main objects of pgzero, together with debugging functionality and ability to run the script directly from PyScripter as a normal Python script.

lordmauve commented 8 years ago

Original comment by fizban (Bitbucket: fizban, GitHub: fizban):


I personally like PyScripter so the IDLE patch would not really work for me. I think that having an import statement at the beginning ("from pgzero import run_game") and a run_game() at the end would not make things that complex and would allow students to execute and debug their code normally through the IDE. We typically work with a portable edition of Python, so this solution would also be easier to use in a portable environment. I am no Python expert, but I gave it a try creating a run_game module based on the runner module, where I updated the main dictionary and it seemed to do the trick, although I am not sure if that's the best way to do it.

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


@JustAMan - I rejected your pull request for reasons I explained in the PR. But there's definitely some merit in the approach and I appreciate the effort you've put into it.

The idea of a file extension for Pygame Zero game modules has various downsides, such as losing IDE support like syntax highlighting and indentation defaults. I would like to discuss this further before deciding whether it's a good option for Pygame Zero.

lordmauve commented 9 years ago

Original comment by first last (Bitbucket: JustAMan, GitHub: JustAMan):


Here you go - I've filed a pull request https://bitbucket.org/lordmauve/pgzero/pull-requests/22/adding-ability-for-idle-to-run-pgzero

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


I guess that could work - installing Pygame Zero could install an idle package that shadows (and monkey patches) the one in the standard library. I'd be interested to see a proof-of-concept of that.

lordmauve commented 9 years ago

Original comment by first last (Bitbucket: JustAMan, GitHub: JustAMan):


We can provide some code in the PyGame Zero package that, when package is installed, monkey-patches IDLE to, say, recognize files with .pygz extension as PyGame Zero files.

I'm talking about making a package that inserts something more magical in setuptools.pth (this file is processed on each interpreter invocation, so it will be processed when IDLE starts up).

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


These are my ideas so far:

I think some research/feedback is still needed:

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


This was discussed much over the weekend.

We could do something as an interim measure, but like all of pgzero, spending time thinking up more elegant solutions may pay dividends in terms of the classroom experience.

I have a few ideas. The "just one line of boilerplate" idea and the monkey patch of Idle are two.

lordmauve commented 9 years ago

Original comment by David Ames (Bitbucket: amos1969, GitHub: amos1969):


Whilst I understand that the ultimate goal is zero boilerplate code, this will present a major stumbling block to use in schools. It would be nice for it to be added as a feature to a new/updated Idle, but how long will that take to come around?

Would it be possible to add it as an optional import statement that can be used for now? With the proviso that the feature will be deprecated once either Idle is updated or better solution is devised?

lordmauve commented 9 years ago

Original comment by first last (Bitbucket: JustAMan, GitHub: JustAMan):


Making it be as simple as using a single import statement is pretty easy.

Another easy option is to provide an alternative entry point to launch IDLE itself, like "IDLE-pgzero", which would launch modules as "pgzero" modules instead of normal Python modules.

This would require people to launch alternative IDLE, though, but is pretty easy to get it working.

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


Filed as issue 24718 in the bugs.python.org tracker.

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


This would definitely need at least an import statement. Maybe nothing more than an import statement. But that really spoils the whole idea of "zero boilerplate".

It may be better to pursue getting this feature into IDLE itself.