Skip to content

Commit

Permalink
Revert adding the command-line REPL.
Browse files Browse the repository at this point in the history
The REPL works OK and yet doesn't feel like part of the core value
proposition, and it isn't really battle-tested enough to ship.

This reverts commit c457125 and
5a20ed4.
  • Loading branch information
lordmauve committed Oct 30, 2021
1 parent bf48fc9 commit 8105d46
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 222 deletions.
Binary file removed doc/_static/repl.png
Binary file not shown.
1 change: 0 additions & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ User Guide
:maxdepth: 2

installation
repl
ide-mode
examples
resources
Expand Down
57 changes: 10 additions & 47 deletions doc/installation.rst
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
Installing Pygame Zero
======================

Included with Mu
----------------

The `Mu IDE <https://codewith.mu>`_, which is aimed at beginners, includes a
version of Pygame Zero.

You will need to `switch mode <https://codewith.mu/en/tutorials/1.0/modes>`_
into Pygame Zero mode to use it. Then type in a program and
`use the Play button <https://codewith.mu/en/tutorials/1.0/pgzero>`_ to run it
with Pygame Zero.

.. note::

The version of Mu included with Pygame Zero may not be the latest version!
You can find which version is installed by running this code in Mu::

import pgzero
print(pgzero.__version__)


Stand-alone installation
------------------------

First of all, you need **Python 3** installed! This is usually installed
already if you are using **Linux** or a **Raspberry Pi**. You can download it
from `python.org <https://www.python.org/>` on other systems.


Windows
'''''''

To install Pygame Zero, use **pip**. At a `command prompt`__, type

.. __: https://www.lifewire.com/how-to-open-command-prompt-2618089
On desktop systems
~~~~~~~~~~~~~~~~~~

::

pip install pgzero

This will also install Pygame. Pre-compiled Pygame packages are available to pip
for Windows & Linux (32-bit and 64-bit), and for Mac OS (64-bit only). If you
have a different system, you'll need to find a way to install pygame first. Make
sure you are using Python 3 not Python 2.

Mac
'''
Expand Down Expand Up @@ -80,17 +52,8 @@ running the above commands again::
sudo python3 -m ensurepip


.. _install-repl:

Installing the REPL
-------------------

:doc:`Pygame Zero's REPL <repl>` is an optional feature. This can be enabled
when installing with ``pip`` by adding ``pgzero[repl]`` to the pip command
line::

pip install pgzero[repl]

If you aren't sure if you have the REPL installed, you can still run this
command (it won't break anything if it is installed!).
On Raspberry Pi
~~~~~~~~~~~~~~~

pgzero has been installed by default since the release of Raspbian Jessie in
September 2015!
54 changes: 0 additions & 54 deletions doc/repl.rst

This file was deleted.

17 changes: 1 addition & 16 deletions pgzero/game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys
import operator
import time
import asyncio

import pygame
import pgzero.clock
Expand Down Expand Up @@ -47,7 +46,6 @@ def __init__(self, mod):
self.height = None
self.title = None
self.icon = None
self.running = False
self.keyboard = pgzero.keyboard.keyboard
self.handlers = {}

Expand Down Expand Up @@ -220,23 +218,12 @@ def get_draw_func(self):

def run(self):
"""Invoke the main loop, and then clean up."""
loop = asyncio.SelectorEventLoop()
try:
loop.run_until_complete(self.run_as_coroutine())
finally:
loop.close()

@asyncio.coroutine
def run_as_coroutine(self):
self.running = True
try:
yield from self.mainloop()
self.mainloop()
finally:
pygame.display.quit()
pygame.mixer.quit()
self.running = False

@asyncio.coroutine
def mainloop(self):
"""Run the main loop of Pygame Zero."""
clock = pygame.time.Clock()
Expand All @@ -250,8 +237,6 @@ def mainloop(self):

self.need_redraw = True
while True:
# TODO: Use asyncio.sleep() for frame delay if accurate enough
yield from asyncio.sleep(0)
dt = clock.tick(60) / 1000.0

for event in pygame.event.get():
Expand Down
109 changes: 14 additions & 95 deletions pgzero/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from . import loaders
from .game import PGZeroGame, DISPLAY_FLAGS
from types import ModuleType
import argparse
from optparse import OptionParser
import warnings
import sys
import os
Expand All @@ -13,10 +13,6 @@
pygame.init()


# The base URL for Pygame Zero documentation
DOCS_URL = 'http://pygame-zero.readthedocs.io/en/stable'


def _check_python_ok_for_pygame():
"""If we're on a Mac, is this a full Framework python?
Expand Down Expand Up @@ -61,45 +57,24 @@ def _substitute_full_framework_python():


def main():

# 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()

parser = argparse.ArgumentParser()
try:
import ptpython # noqa: checking if this is importable
except ImportError:
replhelp = argparse.SUPPRESS
have_repl = False
else:
replhelp = "Show a REPL for interacting with the game while it is running."
have_repl = True
parser.add_argument(
'--repl',
action='store_true',
help=replhelp
)
parser.add_argument(
'script',
help='The name of the Pygame Zero game to run'
)
args = parser.parse_args()
if args.repl and not have_repl:
sys.exit(
"Error: Pygame Zero was not installed with REPL support.\n"
"\n"
"Please read\n"
"{}/installation.html#install-repl\n"
"for instructions on how to install this feature.".format(DOCS_URL)
)
parser = OptionParser()
options, args = parser.parse_args()

if len(args) != 1:
parser.error("You must specify which module to run.")

if __debug__:
warnings.simplefilter('default', DeprecationWarning)
path = args.script
load_and_run(path, repl=args.repl)
path = path = args[0]
load_and_run(path)


def load_and_run(path, repl=False):
def load_and_run(path):
"""Load and run the given Python file as the main PGZero game module.
Note that the 'import pgzrun' IDE mode doesn't pass through this entry
Expand Down Expand Up @@ -128,7 +103,7 @@ def load_and_run(path, repl=False):
pygame.display.init()
PGZeroGame.show_default_icon()
try:
run_mod(mod, repl=repl)
run_mod(mod)
finally:
# Clean some of the state we created, useful in testing
pygame.display.quit()
Expand Down Expand Up @@ -194,62 +169,6 @@ def prepare_mod(mod):
python_builtins.__dict__.setdefault(k, v)


def configure_repl(repl):
"""Configure the ptpython REPL."""
from . import __version__ as pgzero_version
try:
import pkg_resources
except ImportError:
ptpython_version = '???'
else:
try:
dist = pkg_resources.working_set.require('ptpython')[0]
except (pkg_resources.DistributionNotFound, IndexError):
ptpython_version = '???'
else:
ptpython_version = dist.version

print(
'Pygame Zero {} REPL (ptpython {})'.format(
pgzero_version, ptpython_version
)
)
repl.show_status_bar = False
repl.confirm_exit = False


def run_mod(mod, repl=False):
"""Run the module.
If `repl` is True, also run a REPL to interact with the module.
"""
try:
game = PGZeroGame(mod)
if repl:
import asyncio
from ptpython.repl import embed
loop = asyncio.get_event_loop()

# Make sure the game runs
# NB. if the game exits, the REPL will keep running, which allows
# inspecting final state
game_task = loop.create_task(game.run_as_coroutine())

# Wait for the REPL to exit
loop.run_until_complete(embed(
globals=vars(mod),
return_asyncio_coroutine=True,
patch_stdout=True,
title="Pygame Zero REPL",
configure=configure_repl,
))

# Ask game loop to shut down (if it has not) and wait for it
if game.running:
pygame.event.post(pygame.event.Event(pygame.QUIT))
loop.run_until_complete(game_task)
else:
game.run()
finally:
storage.Storage.save_all()
def run_mod(mod):
"""Run the module."""
PGZeroGame(mod).run()
9 changes: 0 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
'pyfxr',
]

extras_require = {
'repl': ["ptpython==0.41"],
}

setup(
name='pgzero',
version=pgzero.__version__,
Expand All @@ -34,17 +30,12 @@
]
},
install_requires=install_requires,
extras_require=extras_require,
python_requires='>=3.4',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Education',
'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: Education',
'Topic :: Games/Entertainment',
],
Expand Down

0 comments on commit 8105d46

Please sign in to comment.