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
536 stars 189 forks source link

add helper functions and setup function #238

Closed yonghuming closed 3 years ago

yonghuming commented 3 years ago
import pgzrun
from random import choice

WIDTH = 300
HEIGHT = 400

def setup():
    screen.fill('red')
    print('setup: ',screen)
    rect(Rect(0, 0, 100, 100),'blue')

pgzrun.go()

i add get_setup_func to PGZeroGame class, if setup exist in main.py, call it once in mainloop method.

    def get_setup_func(self):
        """Get a setup function"""
        try:
            setup = self.mod.setup
        except AttributeError:
            return None
        else:
            if setup.__code__.co_argcount != 0:
                raise TypeError(
                    "draw() must not take any arguments."
                )
            return setup

and mainloop:

@asyncio.coroutine
    def mainloop(self):
        """Run the main loop of Pygame Zero."""
        clock = pygame.time.Clock()
        self.reinit_screen()
        # print(screen)
        setup =  self.get_setup_func()
        update = self.get_update_func()
        draw = self.get_draw_func()
        self.load_handlers()

        pgzclock = pgzero.clock.clock

        if setup:
            setup()

also i create a procesing.py file

import pgzero 
import builtins
from .screen import make_color
stroke_weight = 1
stroke_color = 'black'
fill_color = None

def background(color):
    color = make_color(color)
    screen.fill(color)

def rect(*args, **kwargs):
    screen.draw.rect(*args, **kwargs)

def line(*args, **kwargs):
    screen.draw.line(*args, **kwargs)

def circle(*args, **kwargs):
    screen.draw.circle(*args, **kwargs)

def text(*args, **kwargs):
    screen.draw.text(*args, **kwargs)

def filled_rect(*args, **kwargs):
    screen.draw.filled_rect(*args, **kwargs)

def filled_circle(*args, **kwargs):
    screen.draw.filled_circle(*args, **kwargs)

def textbox(*args, **kwargs):
    screen.draw.textbox(*args, **kwargs)

and import in builtins.py

from .processing import (rect, line, circle, 
                         filled_rect, filled_circle,
                         text, textbox)

__all__ = [
    'Actor', 'images',  # graphics
    'sounds', 'music', 'tone',  # sound
    'rect', 'circle', 'line', 'filled_rect',
    'filled_circle', 'text', 'textbox',
    'clock', 'animate',  # timing
    'Rect', 'ZRect',  # geometry
    'keyboard', 'mouse', 'keys', 'keymods',  # input
    'storage',  # persistence
    'exit',
    'rect'
]

so can draw shapes with shorter function rect instead of screen.draw.rect method, and all parameter are same

i want to do some work making pgzero like p5py or processing.py, or processing or p5.js , it's more friendly to newbee who have little programming expriences.

yonghuming commented 3 years ago

could i make a pr? and will this be merged?

fillwithjoy1 commented 3 years ago

You can do this add setup() at the top of your code and def setup(): - your function that is setup code. Now when you start your program, the setup() function is the first thing to be done

lordmauve commented 3 years ago

Yeah, I'm not sure either of these are a good candidate to be in Pygame Zero itself.

The whole module is run at start time, so as @fillwithjoy1 says, you can just call your setup() function at the bottom of the module.

The draw method shortcuts seem like too much pollution of the namespace. In particular something like rect() shouldn't be in builtins because Rect() is in the builtins and does something completely different.