Animation

Introduction to animation and scheduling with a drawing function.

Code

import arcade

GAME_TITLE = 'Drawing Example'
WINDOW_HEIGHT = 600
WINDOW_WIDTH = 600


def on_draw(delta_time):
    arcade.start_render()
    arcade.draw_text(GAME_TITLE, 200, 300, arcade.color.BLACK, 12)


def main():
    arcade.open_window(WINDOW_WIDTH, WINDOW_HEIGHT, GAME_TITLE)
    arcade.set_background_color(arcade.color.WHEAT)
    arcade.schedule(on_draw, 1 / 2)
    arcade.finish_render()
    arcade.run()


if __name__ == '__main__':
    main()

Analysis

  • We moved our drawing into a second function, named ‘on_draw’
  • Our ‘main’ function now does the basics (make a window, set up drawing, and run)
  • However, we introduce a BIG new idea: arcade.schedule
  • This is a function in the arcade package
  • Schedule runs a unit of work (the first argument) every X seconds (the second argument)
  • In our case, we tell arcade to run our ‘on_draw’ function every 1/2 second
  • This is the basis of animation
  • Because of how arcade does animation, with arcade.schedule, you need start_render but we no longer need finish_render

Exercises

  1. Change the schedule function to run much more frequently.
  2. Change draw_text to “accidentally” draw most of the text outside of the window.
  3. Use Python’s random.choice and range to randomly choose an X position between 100 and 400.

Quiz

  1. What is delta_time and what is it used for?
  2. In this step, is delta_time used?
  3. In this step, is it useful to have window height/width as constants at the top?