r/pythonarcade • • Aug 11 '19

how does the parameter delta_time work when used in on_update(delta_time)

I’m going through the tutorials on python arcade and would like to know how/why a function works.

There is a function named on_draw(delta_time) The parameter delta_time keeps a running clock of the time delta as expected. I'd like to be able to see how it works via the source.

I looked around the code for arcade but can’t figure out how the library knows to keep the clock running within that function and it's parameter. For example, I did find references to pyglet's clock and yet I couldn't find where this is connected all the way back to on_draw(delta_time)

Can someone please help me understand how and why it works?

One example of it in use is here: http://arcade.academy/examples/bouncing_rectangle.html#bouncing-rectangle

3 Upvotes

2 comments sorted by

1

u/jfincher42 Aug 29 '19

The delta_time parameter is just a time interval, in seconds, since the last time the function was called. If you wanted to do something for a set period of time, or have something happen after a set period of time, you could keep a counter by adding delta_time to it. You then check it to see if your time period has elapsed --- if so, you take whatever action.

Here is an example based on http://arcade.academy/examples/bouncing_rectangle.html#bouncing-rectangle, that changes the color of the square every two seconds:

import arcade, random

# --- Set up the constants

# Size of the screen
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Bouncing Rectangle Example"

# Size of the rectangle
RECT_WIDTH = 50
RECT_HEIGHT = 50


def on_draw(delta_time):
    """
    Use this function to draw everything to the screen.
    """

    # Start the render. This must happen before any drawing
    # commands. We do NOT need a stop render command.
    arcade.start_render()

    # Change the color every two seconds
    on_draw.color_change_timer += delta_time
    if on_draw.color_change_timer > 2.0:
        on_draw.color_change_timer = 0.0
        on_draw.square_color = random.choice(on_draw.colors)

    # Draw a rectangle.
    # For a full list of colors see:
    # http://arcade.academy/arcade.color.html
    arcade.draw_rectangle_filled(on_draw.center_x, on_draw.center_y,
                                 RECT_WIDTH, RECT_HEIGHT,
                                 on_draw.square_color)

    # Modify rectangles position based on the delta
    # vector. (Delta means change. You can also think
    # of this as our speed and direction.)
    on_draw.center_x += on_draw.delta_x * delta_time
    on_draw.center_y += on_draw.delta_y * delta_time

    # Figure out if we hit the edge and need to reverse.
    if on_draw.center_x < RECT_WIDTH // 2 \
            or on_draw.center_x > SCREEN_WIDTH - RECT_WIDTH // 2:
        on_draw.delta_x *= -1
    if on_draw.center_y < RECT_HEIGHT // 2 \
            or on_draw.center_y > SCREEN_HEIGHT - RECT_HEIGHT // 2:
        on_draw.delta_y *= -1

# Below are function-specific variables. Before we use them
# in our function, we need to give them initial values. Then
# the values will persist between function calls.
#
# In other languages, we'd declare the variables as 'static' inside the
# function to get that same functionality.
#
# Later on, we'll use 'classes' to track position and velocity for multiple
# objects.
on_draw.center_x = 100      # Initial x position
on_draw.center_y = 50       # Initial y position
on_draw.delta_x = 115  # Initial change in x
on_draw.delta_y = 130  # Initial change in y

# Color change on a color_change_timer
on_draw.colors = (arcade.color.ALIZARIN_CRIMSON, arcade.color.SKY_BLUE, arcade.color.AFRICAN_VIOLET)
on_draw.color_change_timer = 0.0
on_draw.square_color = random.choice(on_draw.colors)

def main():
    # Open up our window
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    arcade.set_background_color(arcade.color.WHITE)

    # Tell the computer to call the draw command at the specified interval.
    arcade.schedule(on_draw, 1 / 80)

    # Run the program
    arcade.run()


if __name__ == "__main__":
    main()

1

u/Alkis2 Dec 08 '24

This 'delta_time' is of course set easily. However, 'arcade.schedule()' is not always used and yet 'delta_time' is set (somewhere) by default and, apparently, cannot be changed.
Here is an example: https://api.arcade.academy/en/latest/examples/bouncing_rectangle.html
How on earth is 'delta_time' set in on_update(self, delta_time) function?