Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added examples to events #34

Merged
merged 6 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions play/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
def when_program_starts(func):
"""
Call code right when the program starts.

Example:

text = play.new_text(words='hi there!')

@play.when_program_starts
def show():
text.words = 'started'

:param func: The function to call when the program starts.
:return: The decorator function.
"""
Expand Down Expand Up @@ -50,11 +59,13 @@ def repeat_forever(func):

Example:

text = play.new_text(words='hi there!', x=0, y=0, font='Arial.ttf', font_size=20, color='black')
text = play.new_text(words='hi there!')

@play.repeat_forever
async def do():
text.turn(degrees=15)
def turn():
print("this is printing every single frame")


:param func: The function to call repeatedly.
:return: The decorator function.
"""
Expand All @@ -77,6 +88,15 @@ async def repeat_wrapper():
# @decorator
def when_sprite_clicked(*sprites):
"""A decorator that runs a function when a sprite is clicked.

Example:

text = play.new_text(words='hi there!')

@play.when_sprite_clicked(text)
def show(sprite):
text.words = 'clicked!'

:param sprites: The sprites to run the function on.
:return: The function to run.
"""
Expand All @@ -93,6 +113,14 @@ def wrapper(func):
def when_any_key_pressed(func):
"""
Calls the given function when any key is pressed.

Example:

text = play.new_text(words='hi there!')

@play.when_any_key_pressed
def show(key):
text.words = key
"""
if not callable(func):
raise ValueError("""@play.when_any_key_pressed doesn't use a list of keys.""")
Expand All @@ -103,6 +131,14 @@ def when_any_key_pressed(func):
def when_key_pressed(*keys):
"""
Calls the given function when any of the specified keys are pressed.

Example:

text = play.new_text(words='hi there!')

@play.when_key_pressed('a')
def show(key):
text.words = key
"""
return _when_key(*keys, released=False)

Expand All @@ -111,6 +147,14 @@ def when_key_pressed(*keys):
def when_any_key_released(func):
"""
Calls the given function when any key is released.

Example:

text = play.new_text(words='hi there!')

@play.when_any_key_released
def show(key):
text.words = key
"""
if not callable(func):
raise ValueError("""@play.when_any_key_released doesn't use a list of keys.""")
Expand All @@ -121,6 +165,14 @@ def when_any_key_released(func):
def when_key_released(*keys):
"""
Calls the given function when any of the specified keys are released.

Example:

text = play.new_text(words='hi there!')

@play.when_key_released('a')
def show(key):
text.words = key
"""
return _when_key(*keys, released=True)

Expand All @@ -129,6 +181,13 @@ def when_key_released(*keys):
def when_mouse_clicked(func):
"""
Calls the given function when the mouse is clicked.

Example:
text = play.new_text(words='hi there!')

@play.when_mouse_clicked
def show():
text.words = 'mouse click'
"""
return mouse.when_clicked(func)

Expand All @@ -137,5 +196,12 @@ def when_mouse_clicked(func):
def when_click_released(func):
"""
Calls the given function when the mouse click is released.

Example:
text = play.new_text(words='hi there!')

@play.when_click_released
def show():
text.words = 'mouse click released'
"""
return mouse.when_click_released(func)
Loading