Introduction to animation and scheduling with a drawing function.
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()
- 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
- Change the
schedulefunction to run much more frequently.
draw_textto “accidentally” draw most of the text outside of the window.
- Use Python’s
rangeto randomly choose an X position between 100 and 400.
- What is
delta_timeand what is it used for?
- In this step, is
- In this step, is it useful to have window height/width as constants at the top?