Skip to content

Commit

Permalink
Move time docs to stubs, minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldalma999 committed Oct 21, 2024
1 parent 9258b19 commit ba4bc00
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 191 deletions.
146 changes: 137 additions & 9 deletions buildconfig/stubs/pygame/time.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,143 @@
"""Pygame module for monitoring time.
Times in pygame are represented in milliseconds (1/1000 seconds). Most
platforms have a limited time resolution of around 10 milliseconds. This
resolution, in milliseconds, is given in the ``TIMER_RESOLUTION`` constant.
"""

from typing import Union, final

from pygame.event import Event

def get_ticks() -> int: ...
def wait(milliseconds: int, /) -> int: ...
def delay(milliseconds: int, /) -> int: ...
def set_timer(event: Union[int, Event], millis: int, loops: int = 0) -> None: ...
def get_ticks() -> int:
"""Get the time in milliseconds.
Return the number of milliseconds since ``pygame.init()`` was called. Before
pygame is initialized this will always be 0.
"""

def wait(milliseconds: int, /) -> int:
"""Pause the program for an amount of time.
Will pause for a given number of milliseconds. This function sleeps the
process to share the processor with other programs. A program that waits for
even a few milliseconds will consume very little processor time. It is
slightly less accurate than the ``pygame.time.delay()`` function.
This returns the actual number of milliseconds used.
"""

def delay(milliseconds: int, /) -> int:
"""Pause the program for an amount of time.
Will pause for a given number of milliseconds. This function will use the
processor (rather than sleeping) in order to make the delay more accurate
than ``pygame.time.wait()``.
This returns the actual number of milliseconds used.
"""

def set_timer(event: Union[int, Event], millis: int, loops: int = 0) -> None:
"""Repeatedly create an event on the event queue.
Set an event to appear on the event queue every given number of milliseconds.
The first event will not appear until the amount of time has passed.
The ``event`` attribute can be a ``pygame.event.Event`` object or an integer
type that denotes an event.
``loops`` is an integer that denotes the number of events posted. If 0 (default)
then the events will keep getting posted, unless explicitly stopped.
To disable the timer for such an event, call the function again with the same
event argument with ``millis`` argument set to 0.
It is also worth mentioning that a particular event type can only be put on a
timer once. In other words, there cannot be two timers for the same event type.
Setting an event timer for a particular event discards the old one for that
event type.
When this function is called with an ``Event`` object, the event(s) received
on the event queue will be a shallow copy; the dict attribute of the event
object passed as an argument and the dict attributes of the event objects
received on timer will be references to the same dict object in memory.
Modifications on one dict can affect another, use deepcopy operations on the
dict object if you don't want this behaviour.
However, calling this function with an integer event type would place event objects
on the queue that don't have a common dict reference.
``loops`` replaces the ``once`` argument, and this does not break backward
compatibility.
.. versionaddedold:: 2.0.0.dev3 once argument added.
.. versionchangedold:: 2.0.1 event argument supports ``pygame.event.Event`` object
.. versionaddedold:: 2.0.1 added loops argument to replace once argument
"""

@final
class Clock:
def tick(self, framerate: float = 0, /) -> int: ...
def tick_busy_loop(self, framerate: float = 0, /) -> int: ...
def get_time(self) -> int: ...
def get_rawtime(self) -> int: ...
def get_fps(self) -> float: ...
"""Create an object to help track time.
Creates a new Clock object that can be used to track an amount of time. The
clock also provides several functions to help control a game's framerate.
.. versionchanged:: 2.1.4 This class is also available through the ``pygame.Clock``
alias.
"""

def __new__(cls) -> Clock: ...
def tick(self, framerate: float = 0, /) -> int:
"""Update the clock.
This method should be called once per frame. It will compute how many
milliseconds have passed since the previous call.
If you pass the optional framerate argument the function will delay to
keep the game running slower than the given ticks per second. This can be
used to help limit the runtime speed of a game. By calling
``Clock.tick(40)`` once per frame, the program will never run at more
than 40 frames per second.
Note that this function uses SDL_Delay function which is not accurate on
every platform, but does not use much CPU. Use tick_busy_loop if you want
an accurate timer, and don't mind chewing CPU.
"""

def tick_busy_loop(self, framerate: float = 0, /) -> int:
"""Update the clock.
This method should be called once per frame. It will compute how many
milliseconds have passed since the previous call.
If you pass the optional framerate argument the function will delay to
keep the game running slower than the given ticks per second. This can be
used to help limit the runtime speed of a game. By calling
``Clock.tick_busy_loop(40)`` once per frame, the program will never run at
more than 40 frames per second.
Note that this function uses :func:`pygame.time.delay`, which uses lots
of CPU in a busy loop to make sure that timing is more accurate.
.. versionaddedold:: 1.8
"""

def get_time(self) -> int:
"""Time used in the previous tick.
The number of milliseconds that passed between the previous two calls to
``Clock.tick()``.
"""

def get_rawtime(self) -> int:
"""Actual time used in the previous tick.
Similar to ``Clock.get_time()``, but does not include any time used
while ``Clock.tick()`` was delaying to limit the framerate.
"""

def get_fps(self) -> float:
"""Compute the clock framerate.
Compute your game's framerate (in frames per second). It is computed by
averaging the last ten calls to ``Clock.tick()``.
"""
2 changes: 1 addition & 1 deletion docs/reST/ext/documenters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def build_signatures(object):
object = child
break

if object is None:
if object is None or object.obj.get("args", None) is None:
return

sigs = [(object.obj["args"], object.obj["return_annotation"])]
Expand Down
174 changes: 4 additions & 170 deletions docs/reST/ref/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,175 +3,9 @@
:mod:`pygame.time`
==================

.. module:: pygame.time
:synopsis: pygame module for monitoring time
.. autopgmodule:: pygame.time
:members: get_ticks, wait, delay, set_timer

| :sl:`pygame module for monitoring time`
.. autopgclass:: Clock
:members: tick, tick_busy_loop, get_time, get_rawtime, get_fps

Times in pygame are represented in milliseconds (1/1000 seconds). Most
platforms have a limited time resolution of around 10 milliseconds. This
resolution, in milliseconds, is given in the ``TIMER_RESOLUTION`` constant.

.. function:: get_ticks

| :sl:`get the time in milliseconds`
| :sg:`get_ticks() -> milliseconds`
Return the number of milliseconds since ``pygame.init()`` was called. Before
pygame is initialized this will always be 0.

.. ## pygame.time.get_ticks ##
.. function:: wait

| :sl:`pause the program for an amount of time`
| :sg:`wait(milliseconds, /) -> time`
Will pause for a given number of milliseconds. This function sleeps the
process to share the processor with other programs. A program that waits for
even a few milliseconds will consume very little processor time. It is
slightly less accurate than the ``pygame.time.delay()`` function.

This returns the actual number of milliseconds used.

.. ## pygame.time.wait ##
.. function:: delay

| :sl:`pause the program for an amount of time`
| :sg:`delay(milliseconds, /) -> time`
Will pause for a given number of milliseconds. This function will use the
processor (rather than sleeping) in order to make the delay more accurate
than ``pygame.time.wait()``.

This returns the actual number of milliseconds used.

.. ## pygame.time.delay ##
.. function:: set_timer

| :sl:`repeatedly create an event on the event queue`
| :sg:`set_timer(event, millis) -> None`
| :sg:`set_timer(event, millis, loops=0) -> None`
Set an event to appear on the event queue every given number of milliseconds.
The first event will not appear until the amount of time has passed.

The ``event`` attribute can be a ``pygame.event.Event`` object or an integer
type that denotes an event.

``loops`` is an integer that denotes the number of events posted. If 0 (default)
then the events will keep getting posted, unless explicitly stopped.

To disable the timer for such an event, call the function again with the same
event argument with ``millis`` argument set to 0.

It is also worth mentioning that a particular event type can only be put on a
timer once. In other words, there cannot be two timers for the same event type.
Setting an event timer for a particular event discards the old one for that
event type.

When this function is called with an ``Event`` object, the event(s) received
on the event queue will be a shallow copy; the dict attribute of the event
object passed as an argument and the dict attributes of the event objects
received on timer will be references to the same dict object in memory.
Modifications on one dict can affect another, use deepcopy operations on the
dict object if you don't want this behaviour.
However, calling this function with an integer event type would place event objects
on the queue that don't have a common dict reference.

``loops`` replaces the ``once`` argument, and this does not break backward
compatibility.

.. versionaddedold:: 2.0.0.dev3 once argument added.
.. versionchangedold:: 2.0.1 event argument supports ``pygame.event.Event`` object
.. versionaddedold:: 2.0.1 added loops argument to replace once argument

.. ## pygame.time.set_timer ##
.. class:: Clock

| :sl:`create an object to help track time`
| :sg:`Clock() -> Clock`
Creates a new Clock object that can be used to track an amount of time. The
clock also provides several functions to help control a game's framerate.

.. versionchanged:: 2.1.4 This class is also available through the ``pygame.Clock``
alias.

.. method:: tick

| :sl:`update the clock`
| :sg:`tick(framerate=0, /) -> milliseconds`
This method should be called once per frame. It will compute how many
milliseconds have passed since the previous call.

If you pass the optional framerate argument the function will delay to
keep the game running slower than the given ticks per second. This can be
used to help limit the runtime speed of a game. By calling
``Clock.tick(40)`` once per frame, the program will never run at more
than 40 frames per second.

Note that this function uses SDL_Delay function which is not accurate on
every platform, but does not use much CPU. Use tick_busy_loop if you want
an accurate timer, and don't mind chewing CPU.

.. ## Clock.tick ##
.. method:: tick_busy_loop

| :sl:`update the clock`
| :sg:`tick_busy_loop(framerate=0, /) -> milliseconds`
This method should be called once per frame. It will compute how many
milliseconds have passed since the previous call.

If you pass the optional framerate argument the function will delay to
keep the game running slower than the given ticks per second. This can be
used to help limit the runtime speed of a game. By calling
``Clock.tick_busy_loop(40)`` once per frame, the program will never run at
more than 40 frames per second.

Note that this function uses :func:`pygame.time.delay`, which uses lots
of CPU in a busy loop to make sure that timing is more accurate.

.. versionaddedold:: 1.8

.. ## Clock.tick_busy_loop ##
.. method:: get_time

| :sl:`time used in the previous tick`
| :sg:`get_time() -> milliseconds`
The number of milliseconds that passed between the previous two calls to
``Clock.tick()``.

.. ## Clock.get_time ##
.. method:: get_rawtime

| :sl:`actual time used in the previous tick`
| :sg:`get_rawtime() -> milliseconds`
Similar to ``Clock.get_time()``, but does not include any time used
while ``Clock.tick()`` was delaying to limit the framerate.

.. ## Clock.get_rawtime ##
.. method:: get_fps

| :sl:`compute the clock framerate`
| :sg:`get_fps() -> float`
Compute your game's framerate (in frames per second). It is computed by
averaging the last ten calls to ``Clock.tick()``.

.. ## Clock.get_fps ##
.. ## pygame.time.Clock ##
.. ## pygame.time ##
22 changes: 11 additions & 11 deletions src_c/doc/time_doc.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* Auto generated file: with make_docs.py . Docs go in docs/reST/ref/ . */
#define DOC_TIME "pygame module for monitoring time"
#define DOC_TIME_GETTICKS "get_ticks() -> milliseconds\nget the time in milliseconds"
#define DOC_TIME_WAIT "wait(milliseconds, /) -> time\npause the program for an amount of time"
#define DOC_TIME_DELAY "delay(milliseconds, /) -> time\npause the program for an amount of time"
#define DOC_TIME_SETTIMER "set_timer(event, millis) -> None\nset_timer(event, millis, loops=0) -> None\nrepeatedly create an event on the event queue"
#define DOC_TIME_CLOCK "Clock() -> Clock\ncreate an object to help track time"
#define DOC_TIME_CLOCK_TICK "tick(framerate=0, /) -> milliseconds\nupdate the clock"
#define DOC_TIME_CLOCK_TICKBUSYLOOP "tick_busy_loop(framerate=0, /) -> milliseconds\nupdate the clock"
#define DOC_TIME_CLOCK_GETTIME "get_time() -> milliseconds\ntime used in the previous tick"
#define DOC_TIME_CLOCK_GETRAWTIME "get_rawtime() -> milliseconds\nactual time used in the previous tick"
#define DOC_TIME_CLOCK_GETFPS "get_fps() -> float\ncompute the clock framerate"
#define DOC_TIME "Pygame module for monitoring time."
#define DOC_TIME_GETTICKS "get_ticks() -> int\nGet the time in milliseconds."
#define DOC_TIME_WAIT "wait(milliseconds, /) -> int\nPause the program for an amount of time."
#define DOC_TIME_DELAY "delay(milliseconds, /) -> int\nPause the program for an amount of time."
#define DOC_TIME_SETTIMER "set_timer(event, millis, loops=0) -> None\nRepeatedly create an event on the event queue."
#define DOC_TIME_CLOCK "Clock() -> Clock\nCreate an object to help track time."
#define DOC_TIME_CLOCK_TICK "tick(framerate=0, /) -> int\nUpdate the clock."
#define DOC_TIME_CLOCK_TICKBUSYLOOP "tick_busy_loop(framerate=0, /) -> int\nUpdate the clock."
#define DOC_TIME_CLOCK_GETTIME "get_time() -> int\nTime used in the previous tick."
#define DOC_TIME_CLOCK_GETRAWTIME "get_rawtime() -> int\nActual time used in the previous tick."
#define DOC_TIME_CLOCK_GETFPS "get_fps() -> float\nCompute the clock framerate."

0 comments on commit ba4bc00

Please sign in to comment.